From 95183422df77aeb76175f13ccd3982094cea3d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20F=C3=A9lizard?= Date: Sat, 12 Oct 2024 12:04:15 +0000 Subject: [PATCH] Fix invalid "None" device class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.home-assistant.io/integrations/sensor/#device-class > None: Generic sensor. This is the default and doesn’t need to be set. --- src/growatt.h | 8 ++++---- src/mqtt.h | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/growatt.h b/src/growatt.h index 5f57210..ef342e3 100644 --- a/src/growatt.h +++ b/src/growatt.h @@ -45,7 +45,7 @@ const REGISTER holding_registers[] = { const REGISTER input_registers[] = { // NOLINTBEGIN(readability-magic-numbers) - {0, "system status", "system_status", "None", "", "measurement", REGISTER_SINGLE, 1}, + {0, "system status", "system_status", "", "", "measurement", REGISTER_SINGLE, 1}, {1, "PV1 voltage", "pv1_volts", "voltage", "V", "measurement", REGISTER_SINGLE, 0.1}, {3, "PV1 power", "pv1_watts", "power", "W", "measurement", REGISTER_DOUBLE, 0.1}, {7, "buck1 current", "buck1_amps", "current", "A", "measurement", REGISTER_SINGLE, 0.1}, @@ -61,14 +61,14 @@ const REGISTER input_registers[] = { // {24, "output DC voltage", "output_dc_volts", "voltage", "V", "measurement", REGISTER_SINGLE, 0.1}, // XXX: always zero {25, "inverter temperature", "temperature_inverter_celsius", "temperature", "°C", "measurement", REGISTER_SINGLE, 0.1}, {26, "DC-DC temperature", "temperature_dcdc_celsius", "temperature", "°C", "measurement", REGISTER_SINGLE, 0.1}, - {27, "inverter load percent", "inverter_load_percent", "None", "%", "measurement", REGISTER_SINGLE, 0.1}, + {27, "inverter load percent", "inverter_load_percent", "", "%", "measurement", REGISTER_SINGLE, 0.1}, // {30, "work time total", "work_time_total_seconds", REGISTER_DOUBLE, 0.5}, // XXX: always zero {32, "buck1 temperature", "temperature_buck1_celsius", "temperature", "°C", "measurement", REGISTER_SINGLE, 0.1}, // {33, "buck2 temperature", "temperature_buck2_celsius", REGISTER_SINGLE, 0.1}, // irrelevant {34, "output current", "output_amps", "current", "A", "measurement", REGISTER_SINGLE, 0.1}, {35, "inverter current", "inverter_amps", "current", "A", "measurement", REGISTER_SINGLE, 0.1}, - {40, "fault bit", "fault_bit", "None", "", "measurement", REGISTER_SINGLE, 1}, - {41, "warning bit", "warning_bit", "None", "", "measurement", REGISTER_SINGLE, 1}, + {40, "fault bit", "fault_bit", "", "", "measurement", REGISTER_SINGLE, 1}, + {41, "warning bit", "warning_bit", "", "", "measurement", REGISTER_SINGLE, 1}, // {42, "fault value", "fault_value", REGISTER_SINGLE, 1}, // XXX: always zero // {43, "warning value", "warning_value", REGISTER_SINGLE, 1}, // XXX: always zero // {45, "product check step", "product_check_step", REGISTER_SINGLE, 1}, // irrelevant diff --git a/src/mqtt.h b/src/mqtt.h index a96be02..af29b87 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -91,11 +91,21 @@ int start_mqtt_thread(void *config_ptr) { const REGISTER reg = input_registers[index]; sprintf(unique_id, "growatt_%s", reg.metric_name); - sprintf(payload, - "{\"device_class\":\"%s\",\"state_class\":\"%s\",\"state_topic\":\"%s\",\"unit_of_measurement\":\"%s\"," - "\"value_template\":\"{{value_json.%s}}\",\"name\":\"%s\",\"unique_id\":\"%s\"," - "\"device\":{\"identifiers\":[\"1\"],\"name\":\"Growatt\",\"manufacturer\":\"Growatt\"}}", - reg.device_class, reg.state_class, TOPIC_STATE, reg.unit, reg.metric_name, reg.human_name, unique_id); + + // don't include empty device_class otherwise https://www.home-assistant.io/integrations/mqtt will throw errors in the logs + if (strlen(reg.device_class) > 0) { + sprintf(payload, + "{\"device_class\":\"%s\",\"state_class\":\"%s\",\"state_topic\":\"%s\",\"unit_of_measurement\":\"%s\"," + "\"value_template\":\"{{value_json.%s}}\",\"name\":\"%s\",\"unique_id\":\"%s\"," + "\"device\":{\"identifiers\":[\"1\"],\"name\":\"Growatt\",\"manufacturer\":\"Growatt\"}}", + reg.device_class, reg.state_class, TOPIC_STATE, reg.unit, reg.metric_name, reg.human_name, unique_id); + } else { + sprintf(payload, + "{\"state_class\":\"%s\",\"state_topic\":\"%s\",\"unit_of_measurement\":\"%s\"," + "\"value_template\":\"{{value_json.%s}}\",\"name\":\"%s\",\"unique_id\":\"%s\"," + "\"device\":{\"identifiers\":[\"1\"],\"name\":\"Growatt\",\"manufacturer\":\"Growatt\"}}", + reg.state_class, TOPIC_STATE, reg.unit, reg.metric_name, reg.human_name, unique_id); + } sprintf(topic, "homeassistant/sensor/%s/config", unique_id); mosquitto_publish(client, NULL, topic, (int)strlen(payload), payload, 0, true);