EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
haSwitch.cpp
Go to the documentation of this file.
1 
10 #include "haSwitch.h"
11 
12 #if SUPPORT_HA_DISCOVERY
13 
14 void HASwitch::setPayloadOn (const char* payload) {
15  if (payload) {
16  (*entityConfig)[ha_payload_on] = payload;
17  }
18 }
19 
20 void HASwitch::setPayloadOff (const char* payload) {
21  if (payload) {
22  (*entityConfig)[ha_payload_off] = payload;
23  }
24 }
25 
26 void HASwitch::setPayloadOn (int payload) {
27  (*entityConfig)[ha_payload_on].set<int> (payload);
28 }
29 
30 void HASwitch::setPayloadOff (int payload) {
31  (*entityConfig)[ha_payload_off].set<int> (payload);
32 }
33 
34 void HASwitch::setStateOn (const char* payload) {
35  if (payload) {
36  (*entityConfig)[ha_state_on] = payload;
37  }
38 }
39 
40 void HASwitch::setStateOff (const char* payload) {
41  if (payload) {
42  (*entityConfig)[ha_state_off] = payload;
43  }
44 }
45 
46 void HASwitch::setStateOn (int payload) {
47  (*entityConfig)[ha_state_on].set<int> (payload);
48 }
49 
50 void HASwitch::setStateOff (int payload) {
51  (*entityConfig)[ha_state_off].set<int>(payload);
52 }
53 
54 void HASwitch::setValueField (const char* payload) {
55  if (payload) {
56  (*entityConfig)[ha_value_key] = payload;
57  }
58 }
59 
60 /* Discovery JSON template for binary sensor
61 {
62  "nmsfx":<name_suffix>,
63  "pl_on":<cmd_payload_on>,
64  "pl_off":<cmd_payload_off>,
65  "stat_on":<state_on>,
66  "stat_off":<state_off>,
67  "val":<value_field>
68 }
69 
70 {
71  "name":"<node_name>_<name_suffix>",
72  "unique_id":"<node_name>_<name_suffix>",
73  "command_topic":"<network_name>/<node_name>/set/data"
74  "payload_off":"<cmd_payload_on>",
75  "payload_on":"<cmd_payload_off>"
76  "state_topic":"<network_name>/<node_name>/data",
77  "state_off":"<state_off>",
78  "state_on":"<state_on>",
79  "value_template":"{{value_json.<value_field>}}"
80  "json_attributes_template":"{{value_json | tojson}}",
81  "json_attributes_topic":"<network_name>/<node_name>/data",
82 }
83 */
84 
85 size_t HASwitch::getDiscoveryJson (char* buffer, size_t buflen, const char* nodeName, const char* networkName, DynamicJsonDocument* inputJSON) {
86  //DynamicJsonDocument inputJSON (1024);
87  DynamicJsonDocument outputJSON (1024);
88 
89  //deserializeMsgPack (inputJSON, msgPack, len);
90 
91  if (!nodeName || !networkName || !inputJSON ) {
92  DEBUG_WARN ("Whrong parameters");
93  return 0;
94  }
95 
96  if (inputJSON->containsKey (ha_name_sufix)) {
97  outputJSON["name"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
98  } else {
99  outputJSON["name"] = nodeName;
100  }
101  if (inputJSON->containsKey (ha_name_sufix)) {
102  outputJSON["unique_id"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
103  } else {
104  outputJSON["unique_id"] = nodeName;
105  }
106  outputJSON["command_topic"] = String (networkName) + "/" + String (nodeName) + "/set/data";
107  if (inputJSON->containsKey (ha_payload_on)) {
108  outputJSON["payload_on"] = (*inputJSON)[ha_payload_on];
109  }
110  if (inputJSON->containsKey (ha_payload_off)) {
111  outputJSON["payload_off"] = (*inputJSON)[ha_payload_off];
112  }
113  outputJSON["state_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
114  if (inputJSON->containsKey (ha_state_on)) {
115  outputJSON["state_on"] = (*inputJSON)[ha_state_on];
116  }
117  if (inputJSON->containsKey (ha_state_off)) {
118  outputJSON["state_off"] = (*inputJSON)[ha_state_off];
119  }
120  if (inputJSON->containsKey (ha_value_key) && (*inputJSON)[ha_value_key].is<String> ()) {
121  outputJSON["value_template"] = String ("{{value_json.") + (*inputJSON)[ha_value_key].as<String> () + String ("}}");
122  }
123  if (inputJSON->containsKey (ha_allow_attrib) && (*inputJSON)[ha_allow_attrib].as<bool> ()) {
124  outputJSON["json_attributes_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
125  outputJSON["json_attributes_template"] = "{{value_json | tojson}}";
126  }
127 
128  size_t jsonLen = measureJson (outputJSON);
129 
130  if (jsonLen > buflen) {
131  DEBUG_WARN ("Too small buffer. Required %u Has %u", jsonLen, buflen);
132  return 0;
133  }
134 
135  //buffer[jsonLen - 1] = '\0';
136  serializeJson (outputJSON, buffer, 1024);
137 
138  return jsonLen;
139 }
140 
141 #endif // SUPPORT_HA_DISCOVERY
haSwitch.h
Home Assistant switch integration.
ha_state_on
constexpr auto ha_state_on
Definition: haEntity.h:43
HASwitch::setStateOn
void setStateOn(const char *payload)
The payload that represents the on state. Used when value that represents on state in the state_topic...
Definition: haSwitch.cpp:34
ha_allow_attrib
constexpr auto ha_allow_attrib
Definition: haEntity.h:48
ha_name_sufix
constexpr auto ha_name_sufix
Definition: haEntity.h:47
ha_payload_off
constexpr auto ha_payload_off
Definition: haEntity.h:26
HASwitch::setStateOff
void setStateOff(const char *payload)
The payload that represents the off state. Used when value that represents off state in the state_top...
Definition: haSwitch.cpp:40
HASwitch::setPayloadOff
void setPayloadOff(const char *payload)
The payload that represents off state. If specified, will be used for both comparing to the value in ...
Definition: haSwitch.cpp:20
HASwitch::getDiscoveryJson
static size_t getDiscoveryJson(char *buffer, size_t buflen, const char *nodeName, const char *networkName, DynamicJsonDocument *inputJSON)
Allows Gateway to get Home Assistant discovery message using Switch template.
Definition: haSwitch.cpp:85
ha_state_off
constexpr auto ha_state_off
Definition: haEntity.h:44
ha_payload_on
constexpr auto ha_payload_on
Definition: haEntity.h:25
HASwitch::setPayloadOn
void setPayloadOn(const char *payload)
The payload that represents on state. If specified, will be used for both comparing to the value in t...
Definition: haSwitch.cpp:14
HASwitch::setValueField
void setValueField(const char *payload)
Defines a json key to extract device’s state from the state_topic. To determine the switches’s state ...
Definition: haSwitch.cpp:54
ha_value_key
constexpr auto ha_value_key
Definition: haEntity.h:27