EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
haBinarySensor.cpp
Go to the documentation of this file.
1 
9 #include "haBinarySensor.h"
10 
11 #if SUPPORT_HA_DISCOVERY
12 
14  if (devClass > bs_none) {
15  // DEBUG_WARN ("Set device class to %d", devClass);
16  (*entityConfig)[ha_device_class] = devClass;
17  }
18 }
19 
20 void HABinarySensor::setPayloadOn (const char* payload) {
21  if (payload) {
22  (*entityConfig)[ha_payload_on] = payload;
23  }
24 }
25 
26 void HABinarySensor::setPayloadOff (const char* payload) {
27  if (payload) {
28  (*entityConfig)[ha_payload_off] = payload;
29  }
30 }
31 
32 void HABinarySensor::setPayloadOn (int payload) {
33  (*entityConfig)[ha_payload_on] = payload;
34 }
35 
36 void HABinarySensor::setPayloadOff (int payload) {
37  (*entityConfig)[ha_payload_off] = payload;
38 }
39 
40 void HABinarySensor::setValueField (const char* payload) {
41  if (payload) {
42  (*entityConfig)[ha_value_key] = payload;
43  }
44 }
45 
46 void HABinarySensor::setValueTemplate (const char* payload) {
47  if (payload) {
48  (*entityConfig)[ha_value_template] = payload;
49  }
50 }
51 
52 void HABinarySensor::setOffDelay (uint payload) {
53  (*entityConfig)[ha_off_delay] = payload;
54 }
55 
56 /* Discovery JSON template for binary sensor
57 {
58  "pl_on":<cmd_payload_on>,
59  "pl_off":<cmd_payload_off>,
60  "val":<value_field>,
61  "exp_aft":<expire_time>,
62  "dev_cla":<device_class>
63  "nmsfx":<name_suffix>
64 }
65 
66 {
67  "name":<node_name>_<name_suffix>,
68  "unique_id":<node_name>_<name_suffix>,
69  "device_class":<device_class>,
70  "state_topic":"<network_name>/<node_name>/data",
71  "payload_on":<cmd_payload_on>,
72  "payload_off":<cmd_payload_off>,
73  "value_template":"{{value_json.<value_field>}}",
74  "expire_after":<expire_time>
75 }
76 */
77 
78 size_t HABinarySensor::getDiscoveryJson (char* buffer, size_t buflen, const char* nodeName, const char* networkName, DynamicJsonDocument* inputJSON) {
79  //DynamicJsonDocument inputJSON (1024);
80  DynamicJsonDocument outputJSON (1024);
81 
82  //deserializeMsgPack (inputJSON, msgPack, len);
83 
84  if (!nodeName || !networkName || !inputJSON ) {
85  DEBUG_WARN ("Whrong parameters");
86  return 0;
87  }
88 
89  if (inputJSON->containsKey (ha_name_sufix)) {
90  outputJSON["name"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
91  } else {
92  outputJSON["name"] = nodeName;
93  }
94  if (inputJSON->containsKey (ha_name_sufix)) {
95  outputJSON["unique_id"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
96  } else {
97  outputJSON["unique_id"] = nodeName;
98  }
99  outputJSON["state_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
100  if (inputJSON->containsKey (ha_device_class)) {
101  outputJSON["device_class"] = deviceClassStr ((*inputJSON)[ha_device_class]);
102  }
103  if (inputJSON->containsKey (ha_payload_on)) {
104  outputJSON["payload_on"] = (*inputJSON)[ha_payload_on];
105  }
106  if (inputJSON->containsKey (ha_payload_off)) {
107  outputJSON["payload_off"] = (*inputJSON)[ha_payload_off];
108  }
109  if (inputJSON->containsKey (ha_value_template)) {
110  String templ = ((*inputJSON)[ha_value_template]).as<String> ();
111  templ.replace ("***", nodeName);
112  outputJSON["value_template"].set<String> (templ);
113  } else if (inputJSON->containsKey (ha_value_key) && (*inputJSON)[ha_value_key].is<String> ()) {
114  outputJSON["value_template"] = String ("{{value_json.") + (*inputJSON)[ha_value_key].as<String> () + String ("}}");
115  } else {
116  outputJSON["value_template"] = "{{value_json.value}}";
117  }
118  if (inputJSON->containsKey (ha_expiration) && (*inputJSON)[ha_expiration].is<int> ()) {
119  outputJSON["expire_after"] = (*inputJSON)[ha_expiration].as<int> ();
120  }
121  if (inputJSON->containsKey (ha_off_delay) && (*inputJSON)[ha_off_delay].is<int> ()) {
122  outputJSON["off_delay"] = (*inputJSON)[ha_off_delay].as<int> ();
123  }
124  if (inputJSON->containsKey (ha_allow_attrib) && (*inputJSON)[ha_allow_attrib].as<bool> ()) {
125  outputJSON["json_attributes_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
126  outputJSON["json_attributes_template"] = "{{value_json | tojson}}";
127  }
128 
129  size_t jsonLen = measureJson (outputJSON);
130 
131  if (jsonLen > buflen) {
132  DEBUG_WARN ("Too small buffer. Required %u Has %u", jsonLen, buflen);
133  return 0;
134  }
135 
136  //buffer[jsonLen - 1] = '\0';
137  serializeJson (outputJSON, buffer, 1024);
138 
139  return jsonLen;
140 }
141 
143  switch (sensorClass) {
144  case bs_battery:
145  return "battery";
146  case bs_battery_charging:
147  return "battery_charging";
148  case bs_cold:
149  return "cold";
150  case bs_connectivity:
151  return "connectivity";
152  case bs_door:
153  return "door";
154  case bs_garage_door:
155  return "garage_door";
156  case bs_gas:
157  return "gas";
158  case bs_heat:
159  return "heat";
160  case bs_light:
161  return "light";
162  case bs_lock:
163  return "lock";
164  case bs_moisture:
165  return "moisture";
166  case bs_motion:
167  return "motion";
168  case bs_moving:
169  return "moving";
170  case bs_occupancy:
171  return "occupancy";
172  case bs_opening:
173  return "opening";
174  case bs_plug:
175  return "plug";
176  case bs_power:
177  return "power";
178  case bs_presence:
179  return "presence";
180  case bs_problem:
181  return "problem";
182  case bs_safety:
183  return "safety";
184  case bs_smoke:
185  return "smoke";
186  case bs_sound:
187  return "sound";
188  case bs_vibration:
189  return "vibration";
190  case bs_window:
191  return "window";
192  default:
193  return "";
194  }
195 }
196 #endif
bs_lock
@ bs_lock
Definition: haEntity.h:84
bs_power
@ bs_power
Definition: haEntity.h:91
bs_garage_door
@ bs_garage_door
Definition: haEntity.h:80
ha_device_class
constexpr auto ha_device_class
Definition: haEntity.h:24
bs_moisture
@ bs_moisture
Definition: haEntity.h:85
bs_light
@ bs_light
Definition: haEntity.h:83
haBinarySensorClass_t
haBinarySensorClass_t
Definition: haEntity.h:73
bs_cold
@ bs_cold
Definition: haEntity.h:77
HABinarySensor::deviceClassStr
static String deviceClassStr(haBinarySensorClass_t sensorClass)
Gets binary sensor class name from haBinarySensorClass_t https://www.home-assistant....
Definition: haBinarySensor.cpp:142
bs_presence
@ bs_presence
Definition: haEntity.h:92
bs_smoke
@ bs_smoke
Definition: haEntity.h:95
bs_battery
@ bs_battery
Definition: haEntity.h:75
bs_safety
@ bs_safety
Definition: haEntity.h:94
bs_occupancy
@ bs_occupancy
Definition: haEntity.h:88
HABinarySensor::setValueField
void setValueField(const char *payload)
Defines a template that returns a string to be compared to payload_on/payload_off or an empty string,...
Definition: haBinarySensor.cpp:40
bs_plug
@ bs_plug
Definition: haEntity.h:90
HABinarySensor::setValueTemplate
void setValueTemplate(const char *payload)
Defines a template that defines binary sensor value. When setting this you should not call setValueFi...
Definition: haBinarySensor.cpp:46
ha_allow_attrib
constexpr auto ha_allow_attrib
Definition: haEntity.h:48
ha_name_sufix
constexpr auto ha_name_sufix
Definition: haEntity.h:47
bs_motion
@ bs_motion
Definition: haEntity.h:86
bs_vibration
@ bs_vibration
Definition: haEntity.h:97
bs_none
@ bs_none
Definition: haEntity.h:74
ha_payload_off
constexpr auto ha_payload_off
Definition: haEntity.h:26
ha_expiration
constexpr auto ha_expiration
Definition: haEntity.h:29
bs_connectivity
@ bs_connectivity
Definition: haEntity.h:78
bs_sound
@ bs_sound
Definition: haEntity.h:96
HABinarySensor::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 Binary Sensor template.
Definition: haBinarySensor.cpp:78
ha_value_template
constexpr auto ha_value_template
Definition: haEntity.h:28
HABinarySensor::setPayloadOff
void setPayloadOff(const char *payload)
Defines the string that represents the off state. It will be compared to the message in the state_top...
Definition: haBinarySensor.cpp:26
bs_problem
@ bs_problem
Definition: haEntity.h:93
HABinarySensor::setPayloadOn
void setPayloadOn(const char *payload)
Defines the string that represents the on state. It will be compared to the message in the state_topi...
Definition: haBinarySensor.cpp:20
ha_payload_on
constexpr auto ha_payload_on
Definition: haEntity.h:25
haBinarySensor.h
Home Assistant binary sensor integration.
HABinarySensor::setOffDelay
void setOffDelay(uint payload)
For sensors that only send on state updates (like PIRs), this sets a delay in seconds after which the...
Definition: haBinarySensor.cpp:52
HABinarySensor::setDeviceClass
void setDeviceClass(haBinarySensorClass_t devClass)
Define binary sensor class as haBinarySensorClass_t
Definition: haBinarySensor.cpp:13
bs_door
@ bs_door
Definition: haEntity.h:79
bs_battery_charging
@ bs_battery_charging
Definition: haEntity.h:76
bs_gas
@ bs_gas
Definition: haEntity.h:81
bs_window
@ bs_window
Definition: haEntity.h:98
bs_opening
@ bs_opening
Definition: haEntity.h:89
ha_off_delay
constexpr auto ha_off_delay
Definition: haEntity.h:45
bs_heat
@ bs_heat
Definition: haEntity.h:82
bs_moving
@ bs_moving
Definition: haEntity.h:87
ha_value_key
constexpr auto ha_value_key
Definition: haEntity.h:27