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
haEntity.h
Go to the documentation of this file.
1 
11 #ifndef _HA_ENTITY_h
12 #define _HA_ENTITY_h
13 
14 #include <Arduino.h>
15 #include "EnigmaIoTconfig.h"
16 
17 #if SUPPORT_HA_DISCOVERY
18 
19 #include <ArduinoJson.h>
20 #include <EnigmaIOTdebug.h>
21 #include <helperFunctions.h>
22 
23 constexpr auto ha_device_type = "type";
24 constexpr auto ha_device_class = "dev_cla";
25 constexpr auto ha_payload_on = "pl_on";
26 constexpr auto ha_payload_off = "pl_off";
27 constexpr auto ha_value_key = "val";
28 constexpr auto ha_value_template = "val_tpl";
29 constexpr auto ha_expiration = "exp_aft";
30 constexpr auto ha_payload_open = "pl_open";
31 constexpr auto ha_payload_close = "pl_cls";
32 constexpr auto ha_payload_stop = "pl_stop";
33 constexpr auto ha_set_position_template = "set_pos_tpl";
34 constexpr auto ha_position_open = "pos_open";
35 constexpr auto ha_position_closed = "pos_clsd";
36 constexpr auto ha_payload_goto = "pl_goto"; // custom
37 constexpr auto ha_payload = "pl";
38 constexpr auto ha_state_open = "stat_open";
39 constexpr auto ha_state_opening = "stat_opening";
40 constexpr auto ha_state_closed = "stat_clsd";
41 constexpr auto ha_state_closing = "stat_closing";
42 constexpr auto ha_state_stopped = "stat_stopped";
43 constexpr auto ha_state_on = "stat_on";
44 constexpr auto ha_state_off = "stat_off";
45 constexpr auto ha_off_delay = "off_dly";
46 constexpr auto ha_unit_of_measurement = "unit_of_meas";
47 constexpr auto ha_name_sufix = "nmsfx";
48 constexpr auto ha_allow_attrib = "attr";
49 constexpr auto ha_type = "ttype";
50 constexpr auto ha_subtype = "tstype";
51 
52 
53 typedef enum {
61  FAN,
69  VACUUM
71 
72 
73 typedef enum {
74  bs_none, // Generic on / off.This is the defaultand doesn’t need to be set.
75  bs_battery, // on means low, off means normal
76  bs_battery_charging, // on means charging, off means not charging
77  bs_cold, // on means cold, off means normal
78  bs_connectivity, // on means connected, off means disconnected
79  bs_door, // on means open, off means closed
80  bs_garage_door, // on means open, off means closed
81  bs_gas, // on means gas detected, off means no gas (clear)
82  bs_heat, // on means hot, off means normal
83  bs_light, // on means light detected, off means no light
84  bs_lock, // on means open (unlocked), off means closed (locked)
85  bs_moisture, // on means moisture detected (wet), off means no moisture (dry)
86  bs_motion, // on means motion detected, off means no motion (clear)
87  bs_moving, // on means moving, off means not moving (stopped)
88  bs_occupancy, // on means occupied, off means not occupied (clear)
89  bs_opening, // on means open, off means closed
90  bs_plug, // on means device is plugged in, off means device is unplugged
91  bs_power, // on means power detected, off means no power
92  bs_presence, // on means home, off means away
93  bs_problem, // on means problem detected, off means no problem (OK)
94  bs_safety, // on means unsafe, off means safe
95  bs_smoke, // on means smoke detected, off means no smoke (clear)
96  bs_sound, // on means sound detected, off means no sound (clear)
97  bs_vibration, // on means vibration detected, off means no vibration (clear)
98  bs_window // on means open, off means closed
100 
101 
102 typedef enum {
103  cover_none, // Generic cover. This is the default and doesn’t need to be set
104  cover_awning, // Control of an awning, such as an exterior retractable window, door, or patio cover
105  cover_blind, // Control of blinds, which are linked slats that expand or collapse to cover an opening or may be tilted to partially covering an opening, such as window blinds
106  cover_curtain, // Control of curtains or drapes, which is often fabric hung above a window or door that can be drawn open
107  cover_damper, // Control of a mechanical damper that reduces airflow, sound, or light
108  cover_door, // Control of a door or gate that provides access to an area
109  cover_garage, // Control of a garage door that provides access to a garage
110  cover_gate, // Control of a gate. Gates are found outside of a structure and are typically part of a fence
111  cover_shade, // Control of shades, which are a continuous plane of material or connected cells that expanded or collapsed over an opening, such as window shades
112  cover_shutter, // Control of shutters, which are linked slats that swing out / in to covering an opening or may be tilted to partially cover an opening, such as indoor or exterior window shutters
113  cover_window // Control of a physical window that opens and closes or may tilt
115 
116 typedef enum {
117  sensor_none, // Generic sensor. This is the default and doesn’t need to be set.
118  sensor_battery, // Percentage of battery that is left.
119  sensor_current, // Current in A.
120  sensor_energy, // Energy in Wh or kWh.
121  sensor_humidity, // Percentage of humidity in the air.
122  sensor_illuminance, // The current light level in lx or lm.
123  sensor_signal_strength, // Signal strength in dB or dBm.
124  sensor_temperature, // Temperature in °C or °F.
125  sensor_power, // Power in W or kW.
126  sensor_power_factor, // Power factor in % .
127  sensor_pressure, // Pressure in hPa or mbar.
128  sensor_timestamp, // Datetime object or timestamp string (ISO 8601).
129  sensor_voltage, // Voltage in V.
130 } haSensorClass_t;
131 
132 class HAEntity {
133 protected:
134  size_t capacity;
136  // uint expiration = 0; ///< @brief Entity expiration parameter
137  //char name[20]; ///< @brief Entity name
138  DynamicJsonDocument* entityConfig;
139 
143  HAEntity () {}
144 
145 public:
146 
153  size_t getAnounceMessage (int bufferlen, uint8_t* buffer) {
154 #if DEBUG_LEVEL >= WARN
155  char* output;
156  size_t json_len = measureJsonPretty (*entityConfig) + 1;
157  output = (char*)malloc (json_len);
158  serializeJsonPretty (*entityConfig, output, json_len);
159 
160  DEBUG_DBG ("JSON message\n%s", output);
161  free (output);
162 #endif
163 
164 
165  if (!buffer) {
166  DEBUG_WARN ("Buffer is null");
167  return 0;
168  }
169  //message = buffer;
170  int len = measureMsgPack (*entityConfig);
171 
172  if (len > MAX_DATA_PAYLOAD_LENGTH) {
173  DEBUG_WARN ("Too long message. Reduce HA anounce options");
174  return 0;
175  }
176 
177  if (bufferlen < len) {
178  DEBUG_WARN ("Buffer is not big enough");
179  return 0;
180  }
181 
182  len = serializeMsgPack (*entityConfig, buffer, bufferlen);
183  DEBUG_DBG ("Msg Pack size: %u", len);
184 
185  DEBUG_VERBOSE ("%s", printHexBuffer (buffer, len));
186 
187  if (entityConfig) {
188  delete (entityConfig);
189  entityConfig = NULL;
190  DEBUG_DBG ("Deleted JSON");
191  }
192 
193  return len;
194  }
195 
200  void setNameSufix (const char* payload) {
201  if (payload) {
202  (*entityConfig)[ha_name_sufix] = payload;
203  }
204  }
205 
210  (*entityConfig)[ha_allow_attrib] = true;
211  }
212 
217  size_t measureMessage () {
218  return measureMsgPack (*entityConfig) + 1;
219  }
220 
227  static String deviceTypeStr (haDeviceType_t entityType) {
228  switch (entityType) {
229  case ALARM_PANEL:
230  return "alarm_control_panel";
231  case BINARY_SENSOR:
232  return "binary_sensor";
233  case CAMERA:
234  return "camera";
235  case COVER:
236  return "cover";
237  case DEVICE_TRACKER:
238  return "device_tracker";
239  case DEVICE_TRIGGER:
240  return "device_automation";
241  case FAN:
242  return "fan";
243  case HVAC:
244  return "climate";
245  case LIGHT:
246  return "light";
247  case LOCK:
248  return "lock";
249  case SCENE:
250  return "scene";
251  case SENSOR:
252  return "sensor";
253  case SWITCH:
254  return "switch";
255  case TAG_SCANNER:
256  return "tag";
257  case VACUUM:
258  return "vacuum";
259  default:
260  return "";
261  }
262  }
263 
264  /*
265  Discovery configuration topic template
266  <hass_prefix>/<device_type>/<node_name>/config
267  */
277  static String getDiscoveryTopic (const char* hassPrefix, const char* nodeName, haDeviceType_t entityType, const char* nameSuffix = NULL) {
278  String output;
279 
280  if (!hassPrefix) {
281  DEBUG_WARN ("Empty prefix");
282  return "";
283  }
284  if (!nodeName) {
285  DEBUG_WARN ("Empty node name");
286  return "";
287  }
288 
289  if (nameSuffix) {
290  output = String (hassPrefix) + "/" + deviceTypeStr (entityType) + "/" + String (nodeName) + "_" + String(nameSuffix) + "/config";
291  } else {
292  output = String (hassPrefix) + "/" + deviceTypeStr (entityType) + "/" + String (nodeName) + "/config";
293  }
294 
295  return output;
296  }
297 
298 
299 };
300 
301 #endif // SUPPORT_HA_DISCOVERY
302 
303 #endif // _HA_ENTITY_h
haCoverClass_t
haCoverClass_t
Definition: haEntity.h:102
CAMERA
@ CAMERA
Definition: haEntity.h:57
cover_awning
@ cover_awning
Definition: haEntity.h:104
sensor_power_factor
@ sensor_power_factor
Definition: haEntity.h:126
bs_lock
@ bs_lock
Definition: haEntity.h:84
cover_window
@ cover_window
Definition: haEntity.h:113
COVER
@ COVER
Definition: haEntity.h:58
bs_power
@ bs_power
Definition: haEntity.h:91
BINARY_SENSOR
@ BINARY_SENSOR
Definition: haEntity.h:56
ha_payload_open
constexpr auto ha_payload_open
Definition: haEntity.h:30
bs_garage_door
@ bs_garage_door
Definition: haEntity.h:80
HAEntity::measureMessage
size_t measureMessage()
Gets needed buffer size for discovery message.
Definition: haEntity.h:217
ha_position_closed
constexpr auto ha_position_closed
Definition: haEntity.h:35
HAEntity::entityConfig
DynamicJsonDocument * entityConfig
JSON object to be sent to gateway.
Definition: haEntity.h:138
cover_door
@ cover_door
Definition: haEntity.h:108
SWITCH
@ SWITCH
Definition: haEntity.h:67
ha_device_class
constexpr auto ha_device_class
Definition: haEntity.h:24
ha_payload_stop
constexpr auto ha_payload_stop
Definition: haEntity.h:32
bs_moisture
@ bs_moisture
Definition: haEntity.h:85
HAEntity::getAnounceMessage
size_t getAnounceMessage(int bufferlen, uint8_t *buffer)
Gets entity anounce message to be sent over EnigmaIOT message.
Definition: haEntity.h:153
LOCK
@ LOCK
Definition: haEntity.h:64
cover_shade
@ cover_shade
Definition: haEntity.h:111
bs_light
@ bs_light
Definition: haEntity.h:83
LIGHT
@ LIGHT
Definition: haEntity.h:63
haBinarySensorClass_t
haBinarySensorClass_t
Definition: haEntity.h:73
ha_state_open
constexpr auto ha_state_open
Definition: haEntity.h:38
DEVICE_TRACKER
@ DEVICE_TRACKER
Definition: haEntity.h:59
bs_cold
@ bs_cold
Definition: haEntity.h:77
EnigmaIoTconfig.h
Parameter configuration.
bs_presence
@ bs_presence
Definition: haEntity.h:92
ha_state_closing
constexpr auto ha_state_closing
Definition: haEntity.h:41
haDeviceType_t
haDeviceType_t
Definition: haEntity.h:53
sensor_none
@ sensor_none
Definition: haEntity.h:117
HVAC
@ HVAC
Definition: haEntity.h:62
MAX_DATA_PAYLOAD_LENGTH
static const uint8_t MAX_DATA_PAYLOAD_LENGTH
Maximum EnigmaIOT user data payload size.
Definition: EnigmaIoTconfigAdvanced.h:22
bs_smoke
@ bs_smoke
Definition: haEntity.h:95
printHexBuffer
char * printHexBuffer(const uint8_t *buffer, uint16_t len)
Debug helper function that generates a string that represent a buffer hexadecimal values.
Definition: helperFunctions.cpp:16
HAEntity::capacity
size_t capacity
JSON object memory reservation length.
Definition: haEntity.h:134
TAG_SCANNER
@ TAG_SCANNER
Definition: haEntity.h:68
cover_curtain
@ cover_curtain
Definition: haEntity.h:106
UNDEFINED
@ UNDEFINED
Definition: haEntity.h:54
ha_state_on
constexpr auto ha_state_on
Definition: haEntity.h:43
bs_battery
@ bs_battery
Definition: haEntity.h:75
bs_safety
@ bs_safety
Definition: haEntity.h:94
bs_occupancy
@ bs_occupancy
Definition: haEntity.h:88
HAEntity::getDiscoveryTopic
static String getDiscoveryTopic(const char *hassPrefix, const char *nodeName, haDeviceType_t entityType, const char *nameSuffix=NULL)
Allows Gateway to get discovery message MQTT topic.
Definition: haEntity.h:277
cover_damper
@ cover_damper
Definition: haEntity.h:107
ha_state_closed
constexpr auto ha_state_closed
Definition: haEntity.h:40
bs_plug
@ bs_plug
Definition: haEntity.h:90
HAEntity::HAEntity
HAEntity()
Default constructor. Needed for inheritance.
Definition: haEntity.h:143
ha_allow_attrib
constexpr auto ha_allow_attrib
Definition: haEntity.h:48
SCENE
@ SCENE
Definition: haEntity.h:65
ha_name_sufix
constexpr auto ha_name_sufix
Definition: haEntity.h:47
bs_motion
@ bs_motion
Definition: haEntity.h:86
cover_shutter
@ cover_shutter
Definition: haEntity.h:112
cover_blind
@ cover_blind
Definition: haEntity.h:105
bs_vibration
@ bs_vibration
Definition: haEntity.h:97
bs_none
@ bs_none
Definition: haEntity.h:74
ha_payload
constexpr auto ha_payload
Definition: haEntity.h:37
ha_payload_off
constexpr auto ha_payload_off
Definition: haEntity.h:26
sensor_temperature
@ sensor_temperature
Definition: haEntity.h:124
cover_none
@ cover_none
Definition: haEntity.h:103
ha_expiration
constexpr auto ha_expiration
Definition: haEntity.h:29
bs_connectivity
@ bs_connectivity
Definition: haEntity.h:78
ha_subtype
constexpr auto ha_subtype
Definition: haEntity.h:50
sensor_pressure
@ sensor_pressure
Definition: haEntity.h:127
sensor_battery
@ sensor_battery
Definition: haEntity.h:118
ha_payload_close
constexpr auto ha_payload_close
Definition: haEntity.h:31
sensor_signal_strength
@ sensor_signal_strength
Definition: haEntity.h:123
bs_sound
@ bs_sound
Definition: haEntity.h:96
sensor_illuminance
@ sensor_illuminance
Definition: haEntity.h:122
HAEntity::deviceTypeStr
static String deviceTypeStr(haDeviceType_t entityType)
Gets entity type string from haDeviceType_t value https://www.home-assistant.io/docs/mqtt/discovery/.
Definition: haEntity.h:227
ha_value_template
constexpr auto ha_value_template
Definition: haEntity.h:28
ha_payload_goto
constexpr auto ha_payload_goto
Definition: haEntity.h:36
sensor_voltage
@ sensor_voltage
Definition: haEntity.h:129
ha_position_open
constexpr auto ha_position_open
Definition: haEntity.h:34
ha_state_off
constexpr auto ha_state_off
Definition: haEntity.h:44
ha_state_stopped
constexpr auto ha_state_stopped
Definition: haEntity.h:42
ha_unit_of_measurement
constexpr auto ha_unit_of_measurement
Definition: haEntity.h:46
ha_device_type
constexpr auto ha_device_type
Definition: haEntity.h:23
HAEntity::deviceType
haDeviceType_t deviceType
HomeAssistant entity device type.
Definition: haEntity.h:135
bs_problem
@ bs_problem
Definition: haEntity.h:93
sensor_energy
@ sensor_energy
Definition: haEntity.h:120
SENSOR
@ SENSOR
Definition: haEntity.h:66
ha_payload_on
constexpr auto ha_payload_on
Definition: haEntity.h:25
ha_type
constexpr auto ha_type
Definition: haEntity.h:49
haSensorClass_t
haSensorClass_t
Definition: haEntity.h:116
VACUUM
@ VACUUM
Definition: haEntity.h:69
sensor_humidity
@ sensor_humidity
Definition: haEntity.h:121
sensor_current
@ sensor_current
Definition: haEntity.h:119
sensor_power
@ sensor_power
Definition: haEntity.h:125
helperFunctions.h
Auxiliary function definition.
HAEntity::setNameSufix
void setNameSufix(const char *payload)
Sets name suffix. Used for multi entity nodes.
Definition: haEntity.h:200
bs_door
@ bs_door
Definition: haEntity.h:79
bs_battery_charging
@ bs_battery_charging
Definition: haEntity.h:76
EnigmaIOTdebug.h
Auxiliary functions for debugging over Serial.
ha_state_opening
constexpr auto ha_state_opening
Definition: haEntity.h:39
DEVICE_TRIGGER
@ DEVICE_TRIGGER
Definition: haEntity.h:60
sensor_timestamp
@ sensor_timestamp
Definition: haEntity.h:128
ALARM_PANEL
@ ALARM_PANEL
Definition: haEntity.h:55
bs_gas
@ bs_gas
Definition: haEntity.h:81
HAEntity::allowSendAttributes
void allowSendAttributes()
Enables registering entity attributes as a json object.
Definition: haEntity.h:209
bs_window
@ bs_window
Definition: haEntity.h:98
ha_set_position_template
constexpr auto ha_set_position_template
Definition: haEntity.h:33
HAEntity
Definition: haEntity.h:132
bs_opening
@ bs_opening
Definition: haEntity.h:89
ha_off_delay
constexpr auto ha_off_delay
Definition: haEntity.h:45
cover_garage
@ cover_garage
Definition: haEntity.h:109
bs_heat
@ bs_heat
Definition: haEntity.h:82
bs_moving
@ bs_moving
Definition: haEntity.h:87
FAN
@ FAN
Definition: haEntity.h:61
cover_gate
@ cover_gate
Definition: haEntity.h:110
ha_value_key
constexpr auto ha_value_key
Definition: haEntity.h:27