Skip to content

Commit

Permalink
empty stat_t fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 27, 2023
1 parent 7fae04a commit 9c1ecec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/httpserver/hass.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,13 @@ HassDeviceInfo* hass_init_binary_sensor_device_info(int index, bool bInverse) {
/// @param index Index corresponding to sensor_mqttNames.
/// @return
HassDeviceInfo* hass_init_power_sensor_device_info(int index) {
HassDeviceInfo* info = hass_init_device_info(POWER_SENSOR, index, NULL, NULL);
HassDeviceInfo* info = 0;

//https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
//device_class automatically assigns unit,icon
if ((index >= OBK_VOLTAGE) && (index <= OBK_POWER))
{
info = hass_init_device_info(POWER_SENSOR, index, NULL, NULL);
cJSON_AddStringToObject(info->root, "dev_cla", sensor_mqtt_device_classes[index]); //device_class=voltage,current,power
cJSON_AddStringToObject(info->root, "unit_of_meas", sensor_mqtt_device_units[index]); //unit_of_measurement

Expand All @@ -406,6 +407,7 @@ HassDeviceInfo* hass_init_power_sensor_device_info(int index) {
}
else if ((index >= OBK_CONSUMPTION_TOTAL) && (index <= OBK_CONSUMPTION_STATS))
{
info = hass_init_device_info(POWER_SENSOR, index, NULL, NULL);
const char* device_class_value = counter_devClasses[index - OBK_CONSUMPTION_TOTAL];
if (strlen(device_class_value) > 0) {
cJSON_AddStringToObject(info->root, "dev_cla", device_class_value); //device_class=energy
Expand Down
8 changes: 5 additions & 3 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -1802,9 +1802,11 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) {
for (i = 0; i < OBK_NUM_SENSOR_COUNT; i++)
{
dev_info = hass_init_power_sensor_device_info(i);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
discoveryQueued = true;
if (dev_info) {
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
discoveryQueued = true;
}
}
}
#endif
Expand Down

0 comments on commit 9c1ecec

Please sign in to comment.