EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
haSensor.cpp
Go to the documentation of this file.
1 
9 #include "haSensor.h"
10 
11 #if SUPPORT_HA_DISCOVERY
12 
14  if (devClass > sensor_none) {
15  // DEBUG_WARN ("Set device class to %d", devClass);
16  (*entityConfig)[ha_device_class] = devClass;
17  }
18 }
19 
20 void HASensor::setExpireTime (uint payload) {
21  if (payload) {
22  (*entityConfig)[ha_expiration] = payload;
23  }
24 }
25 
26 void HASensor::setUnitOfMeasurement (const char* payload) {
27  if (payload) {
28  (*entityConfig)[ha_unit_of_measurement] = payload;
29  }
30 }
31 
32 void HASensor::setValueField (const char* payload) {
33  if (payload) {
34  (*entityConfig)[ha_value_key] = payload;
35  }
36 }
37 
38 void HASensor::setValueTemplate (const char* payload) {
39  if (payload) {
40  (*entityConfig)[ha_value_template] = payload;
41  }
42 }
43 
44 /* Discovery JSON template for binary sensor
45 {
46  "dev_cla":<device_class>,
47  "exp_aft":<expire_time>,
48  "unit_of_meas":<unit_of_measurement>,
49  "val":<value_field>,
50  "nmsfx":<name_suffix>
51 }
52 {
53  "name":<node_name>_<name_suffix>,
54  "unique_id":<node_name>_<name_suffix>,
55  "device_class": <device_class>,
56  "expire_after":<expire_time>,
57  "json_attributes_template":"{{value_json | tojson}}",
58  "json_attributes_topic":"<network_name>/<node_name>/data",
59  "state_topic":"<network_name>/<node_name>/data",
60  "unit_of_measurement":<unit_of_measurement>,
61  "value_template":"{{value_json.<value_field>}}"
62 }
63 */
64 
65 size_t HASensor::getDiscoveryJson (char* buffer, size_t buflen, const char* nodeName, const char* networkName, DynamicJsonDocument* inputJSON) {
66  //DynamicJsonDocument inputJSON (1024);
67  DynamicJsonDocument outputJSON (1300);
68 
69  //deserializeMsgPack (inputJSON, msgPack, len);
70 
71  if (!nodeName || !networkName || !inputJSON ) {
72  DEBUG_WARN ("Whrong parameters");
73  return 0;
74  }
75 
76  if (inputJSON->containsKey (ha_name_sufix)) {
77  outputJSON["name"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
78  } else {
79  outputJSON["name"] = nodeName;
80  }
81  if (inputJSON->containsKey (ha_name_sufix)) {
82  outputJSON["unique_id"] = String (nodeName) + "_" + (*inputJSON)[ha_name_sufix].as<String> ();
83  } else {
84  outputJSON["unique_id"] = nodeName;
85  }
86  if (inputJSON->containsKey (ha_device_class)) {
87  outputJSON["device_class"] = deviceClassStr ((*inputJSON)[ha_device_class]);
88  }
89  if (inputJSON->containsKey (ha_expiration) && (*inputJSON)[ha_expiration].is<int> ()) {
90  outputJSON["expire_after"] = (*inputJSON)[ha_expiration].as<int>();
91  }
92  outputJSON["state_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
93  if (inputJSON->containsKey (ha_unit_of_measurement)) {
94  outputJSON["unit_of_measurement"] = (*inputJSON)[ha_unit_of_measurement];
95  }
96  if (inputJSON->containsKey (ha_value_template)) {
97  String templ = ((*inputJSON)[ha_value_template]).as<String> ();
98  templ.replace ("***", nodeName);
99  outputJSON["value_template"].set<String> (templ);
100  } else if (inputJSON->containsKey (ha_value_key)) {
101  outputJSON["value_template"] = String ("{{value_json.") + (*inputJSON)[ha_value_key].as<String> () + String ("}}");
102  } else {
103  outputJSON["value_template"] = "{{value_json.value}}";
104  }
105  if (inputJSON->containsKey (ha_allow_attrib) && (*inputJSON)[ha_allow_attrib].as<bool> ()) {
106  outputJSON["json_attributes_topic"] = String (networkName) + "/" + String (nodeName) + "/data";
107  outputJSON["json_attributes_template"] = "{{value_json | tojson}}";
108  }
109 
110  size_t jsonLen = measureJson (outputJSON);
111 
112  if (jsonLen > buflen) {
113  DEBUG_WARN ("Too small buffer. Required %u Has %u", jsonLen, buflen);
114  return 0;
115  }
116 
117  //buffer[jsonLen - 1] = '\0';
118  serializeJson (outputJSON, buffer, 1024);
119 
120  return jsonLen;
121 }
122 
124  switch (sensorClass) {
125  case sensor_battery:
126  return "battery";
127  case sensor_current:
128  return "current";
129  case sensor_energy:
130  return "energy";
131  case sensor_humidity:
132  return "humidity";
133  case sensor_illuminance:
134  return "illuminance";
136  return "signal_strength";
137  case sensor_temperature:
138  return "temperature";
139  case sensor_power:
140  return "power";
141  case sensor_power_factor:
142  return "power_factor";
143  case sensor_pressure:
144  return "pressure";
145  case sensor_timestamp:
146  return "timestamp";
147  case sensor_voltage:
148  return "voltage";
149  default:
150  return "";
151  }
152 }
153 
154 #endif // SUPPORT_HA_DISCOVERY
sensor_power_factor
@ sensor_power_factor
Definition: haEntity.h:126
ha_device_class
constexpr auto ha_device_class
Definition: haEntity.h:24
HASensor::setValueField
void setValueField(const char *payload)
Defines a json key that defines sensor value. When setting this, setValueTemplate () should not be ca...
Definition: haSensor.cpp:32
HASensor::setDeviceClass
void setDeviceClass(haSensorClass_t devClass)
Define sensor class as haSensorClass_t https://www.home-assistant.io/integrations/sensor....
Definition: haSensor.cpp:13
sensor_none
@ sensor_none
Definition: haEntity.h:117
HASensor::deviceClassStr
static String deviceClassStr(haSensorClass_t sensorClass)
Gets sensor class name from haSensorClass_t https://www.home-assistant.io/integrations/sensor....
Definition: haSensor.cpp:123
ha_allow_attrib
constexpr auto ha_allow_attrib
Definition: haEntity.h:48
ha_name_sufix
constexpr auto ha_name_sufix
Definition: haEntity.h:47
haSensor.h
Home Assistant sensor integration.
sensor_temperature
@ sensor_temperature
Definition: haEntity.h:124
ha_expiration
constexpr auto ha_expiration
Definition: haEntity.h:29
sensor_pressure
@ sensor_pressure
Definition: haEntity.h:127
sensor_battery
@ sensor_battery
Definition: haEntity.h:118
sensor_signal_strength
@ sensor_signal_strength
Definition: haEntity.h:123
sensor_illuminance
@ sensor_illuminance
Definition: haEntity.h:122
ha_value_template
constexpr auto ha_value_template
Definition: haEntity.h:28
sensor_voltage
@ sensor_voltage
Definition: haEntity.h:129
HASensor::setUnitOfMeasurement
void setUnitOfMeasurement(const char *payload)
Set unit of measure https://www.home-assistant.io/integrations/sensor.mqtt/#unit_of_measurement.
Definition: haSensor.cpp:26
ha_unit_of_measurement
constexpr auto ha_unit_of_measurement
Definition: haEntity.h:46
sensor_energy
@ sensor_energy
Definition: haEntity.h:120
HASensor::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 Sensor template.
Definition: haSensor.cpp:65
haSensorClass_t
haSensorClass_t
Definition: haEntity.h:116
sensor_humidity
@ sensor_humidity
Definition: haEntity.h:121
sensor_current
@ sensor_current
Definition: haEntity.h:119
sensor_power
@ sensor_power
Definition: haEntity.h:125
sensor_timestamp
@ sensor_timestamp
Definition: haEntity.h:128
HASensor::setExpireTime
void setExpireTime(uint payload)
Defines the number of seconds after the sensor’s state expires, if it’s not updated....
Definition: haSensor.cpp:20
HASensor::setValueTemplate
void setValueTemplate(const char *payload)
Defines a template that defines sensor value. When setting this you should not call setValueField () ...
Definition: haSensor.cpp:38
ha_value_key
constexpr auto ha_value_key
Definition: haEntity.h:27