EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
haCover.cpp
Go to the documentation of this file.
1 
9 #include "haCover.h"
10 
11 #if SUPPORT_HA_DISCOVERY
12 
14  if (devClass > cover_none) {
15  // DEBUG_WARN ("Set device class to %d", devClass);
16  (*entityConfig)[ha_device_class] = devClass;
17  }
18 }
19 
20 void HACover::setPayloadOpen (const char* payload) {
21  if (payload) {
22  (*entityConfig)[ha_payload_open] = payload;
23  }
24 }
25 
26 void HACover::setPayloadClose (const char* payload) {
27  if (payload) {
28  (*entityConfig)[ha_payload_close] = payload;
29  }
30 }
31 
32 void HACover::setPayloadStop (const char* payload) {
33  if (payload) {
34  (*entityConfig)[ha_payload_stop] = payload;
35  }
36 }
37 
38 void HACover::setPayloadGoto (const char* payload) {
39  if (payload) {
40  (*entityConfig)[ha_payload_goto] = payload;
41  }
42 }
43 
44 void HACover::setStateOpen (const char* payload) {
45  if (payload) {
46  (*entityConfig)[ha_state_open] = payload;
47  }
48 }
49 
50 void HACover::setStateOpening (const char* payload) {
51  if (payload) {
52  (*entityConfig)[ha_state_opening] = payload;
53  }
54 }
55 
56 void HACover::setStateClosed (const char* payload) {
57  if (payload) {
58  (*entityConfig)[ha_state_closed] = payload;
59  }
60 }
61 
62 void HACover::setStateClosing (const char* payload) {
63  if (payload) {
64  (*entityConfig)[ha_state_closing] = payload;
65  }
66 }
67 
68 void HACover::setStateStopped (const char* payload) {
69  if (payload) {
70  (*entityConfig)[ha_state_stopped] = payload;
71  }
72 }
73 
74 /* Discovery JSON template for Cover
75 {
76  "dev_cla":<device_class>,
77  "pl_cls":<pl_cls>>,
78  "pl_open":<pl_open>,
79  "pl_stop":<pl_stop>,
80  "pl_goto":<pl_goto>,
81  "pos_open":<pos_open>,
82  "pos_clsd":<pos_clsd>,
83  "set_pos_tpl":<pl_goto>,
84  "stat_clsd":<stat_clsd>,
85  "stat_closing":<stat_closing>,
86  "stat_open":<stat_open>,
87  "stat_opening":<stat_opening>,
88  "stat_stopped":<stat_stopped>,
89 }
90 {
91  "name":<node_name>,
92  "unique_id":<node_name>,
93  "command_topic":<network_name>/<node_name>/set/data,
94  "device_class":<device_class>,
95  "json_attributes_topic":"<network_name>/<node_name>/data",
96  "json_attributes_template":"{{value_json}}",
97  "payload_close":"{\"cmd\":<pl_cls>}",
98  "payload_open":"{\"cmd\":<pl_open>}",
99  "payload_stop":"{\"cmd\":<pl_stop>}",
100  "position_topic":"<network_name>/<node_name>/data",
101  "position_template":"{{value_json.pos}}",
102  "set_position_topic":"<network_name>/<node_name>/set/data,
103  "set_position_template":"{\"cmd\":<pl_goto>,\"pos\":{{position|int}}}",
104  "state_closed":<stat_clsd>,
105  "state_closing":<stat_closing>,
106  "state_open":<stat_open>,
107  "state_opening":<stat_opening>,
108  "state_stopped":<stat_stopped>,
109  "state_topic":"<network_name>/<node_name>/data",
110  "value_template":"{{value_json.state}}",
111 }
112 */
113 
114 size_t HACover::getDiscoveryJson (char* buffer, size_t buflen, const char* nodeName, const char* networkName, DynamicJsonDocument* inputJSON) {
115  //DynamicJsonDocument inputJSON (1024);
116  DynamicJsonDocument outputJSON (1300);
117 
118  //deserializeMsgPack (inputJSON, msgPack, len);
119 
120  if (!nodeName || !networkName || !inputJSON ) {
121  DEBUG_WARN ("Whrong parameters");
122  return 0;
123  }
124 
125  if (inputJSON->containsKey (ha_name_sufix)) {
126  outputJSON["name"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
127  } else {
128  outputJSON["name"] = nodeName;
129  }
130  if (inputJSON->containsKey (ha_name_sufix)) {
131  outputJSON["unique_id"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
132  } else {
133  outputJSON["unique_id"] = nodeName;
134  }
135  outputJSON["command_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
136  if (inputJSON->containsKey (ha_device_class)) {
137  outputJSON["device_class"] = deviceClassStr ((*inputJSON)[ha_device_class]);
138  }
139  if (inputJSON->containsKey (ha_payload_close)) {
140  outputJSON["payload_close"] = (*inputJSON)[ha_payload_close];
141  }
142  if (inputJSON->containsKey (ha_payload_open)) {
143  outputJSON["payload_open"] = (*inputJSON)[ha_payload_open];
144  }
145  if (inputJSON->containsKey (ha_payload_stop)) {
146  outputJSON["payload_stop"] = (*inputJSON)[ha_payload_stop];
147  }
148  outputJSON["position_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
149  outputJSON["position_template"] = "{{value_json.pos}}";
150  outputJSON["set_position_topic"] = String (networkName) + "/" + String (nodeName) + "/set/data";
151  String pl_goto = "pos";
152  if (inputJSON->containsKey (ha_payload_goto)) {
153  pl_goto = (*inputJSON)[ha_payload_goto].as<String> ();
154  }
155  outputJSON["set_position_template"]= String("{\"cmd\":") + pl_goto + String("\"pos\":{{position|int}}}");
156  if (inputJSON->containsKey (ha_state_closed)) {
157  outputJSON["state_closed"] = (*inputJSON)[ha_state_closed];
158  }
159  if (inputJSON->containsKey (ha_state_closing)) {
160  outputJSON["state_closing"] = (*inputJSON)[ha_state_closing];
161  }
162  if (inputJSON->containsKey (ha_state_open)) {
163  outputJSON["state_open"] = (*inputJSON)[ha_state_open];
164  }
165  if (inputJSON->containsKey (ha_state_opening)) {
166  outputJSON["state_opening"] = (*inputJSON)[ha_state_opening];
167  }
168  if (inputJSON->containsKey (ha_state_stopped)) {
169  outputJSON["state_stopped"] = (*inputJSON)[ha_state_stopped];
170  }
171  outputJSON["state_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
172  outputJSON["value_template"] = "{{value_json.state}}";
173 
174  if (inputJSON->containsKey (ha_allow_attrib) && (*inputJSON)[ha_allow_attrib].as<bool> ()) {
175  outputJSON["json_attributes_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
176  outputJSON["json_attributes_template"] = "{{value_json | tojson}}";
177  }
178 
179  size_t jsonLen = measureJson (outputJSON);
180 
181  if (jsonLen > buflen) {
182  DEBUG_WARN ("Too small buffer. Required %u Has %u", jsonLen, buflen);
183  return 0;
184  }
185 
186  //buffer[jsonLen - 1] = '\0';
187  serializeJson (outputJSON, buffer, 1024);
188 
189  return jsonLen;
190 }
191 
193  switch (sensorClass) {
194  case cover_awning:
195  return "awning";
196  case cover_blind:
197  return "blind";
198  case cover_curtain:
199  return "curtain";
200  case cover_damper:
201  return "damper";
202  case cover_door:
203  return "door";
204  case cover_garage:
205  return "garage";
206  case cover_gate:
207  return "gate";
208  case cover_shade:
209  return "shade";
210  case cover_shutter:
211  return "shutter";
212  case cover_window:
213  return "window";
214  default:
215  return "";
216  }
217 }
218 #endif // SUPPORT_HA_DISCOVERY
haCoverClass_t
haCoverClass_t
Definition: haEntity.h:102
cover_awning
@ cover_awning
Definition: haEntity.h:104
HACover::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 Cover template.
Definition: haCover.cpp:114
cover_window
@ cover_window
Definition: haEntity.h:113
HACover::setStateOpening
void setStateOpening(const char *payload)
The payload that represents the opening state https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:50
ha_payload_open
constexpr auto ha_payload_open
Definition: haEntity.h:30
cover_door
@ cover_door
Definition: haEntity.h:108
ha_device_class
constexpr auto ha_device_class
Definition: haEntity.h:24
ha_payload_stop
constexpr auto ha_payload_stop
Definition: haEntity.h:32
HACover::setStateClosing
void setStateClosing(const char *payload)
The payload that represents the closing state https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:62
cover_shade
@ cover_shade
Definition: haEntity.h:111
ha_state_open
constexpr auto ha_state_open
Definition: haEntity.h:38
HACover::setPayloadClose
void setPayloadClose(const char *payload)
The command payload that closes the cover https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:26
ha_state_closing
constexpr auto ha_state_closing
Definition: haEntity.h:41
cover_curtain
@ cover_curtain
Definition: haEntity.h:106
cover_damper
@ cover_damper
Definition: haEntity.h:107
ha_state_closed
constexpr auto ha_state_closed
Definition: haEntity.h:40
HACover::setPayloadOpen
void setPayloadOpen(const char *payload)
The command payload that opens the cover https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:20
ha_allow_attrib
constexpr auto ha_allow_attrib
Definition: haEntity.h:48
ha_name_sufix
constexpr auto ha_name_sufix
Definition: haEntity.h:47
cover_shutter
@ cover_shutter
Definition: haEntity.h:112
cover_blind
@ cover_blind
Definition: haEntity.h:105
HACover::deviceClassStr
static String deviceClassStr(haCoverClass_t sensorClass)
Gets binary sensor class name from haCoverClass_t https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:192
cover_none
@ cover_none
Definition: haEntity.h:103
haCover.h
Home Assistant cover and shade integration.
ha_payload_close
constexpr auto ha_payload_close
Definition: haEntity.h:31
ha_payload_goto
constexpr auto ha_payload_goto
Definition: haEntity.h:36
HACover::setDeviceClass
void setDeviceClass(haCoverClass_t devClass)
Define cover class as haCoverClass_t
Definition: haCover.cpp:13
HACover::setPayloadStop
void setPayloadStop(const char *payload)
The command payload that stops the cover https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:32
ha_state_stopped
constexpr auto ha_state_stopped
Definition: haEntity.h:42
HACover::setPayloadGoto
void setPayloadGoto(const char *payload)
The command that moves the cover to specific position.
Definition: haCover.cpp:38
HACover::setStateStopped
void setStateStopped(const char *payload)
The payload that represents the stopped state https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:68
ha_state_opening
constexpr auto ha_state_opening
Definition: haEntity.h:39
HACover::setStateOpen
void setStateOpen(const char *payload)
The payload that represents the open state https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:44
cover_garage
@ cover_garage
Definition: haEntity.h:109
HACover::setStateClosed
void setStateClosed(const char *payload)
The payload that represents the closed state https://www.home-assistant.io/integrations/cover....
Definition: haCover.cpp:56
cover_gate
@ cover_gate
Definition: haEntity.h:110