Skip to content

Commit

Permalink
Fix invalid "None" device class
Browse files Browse the repository at this point in the history
https://www.home-assistant.io/integrations/sensor/#device-class

> None: Generic sensor. This is the default and doesn’t need to be set.
  • Loading branch information
infertux committed Oct 12, 2024
1 parent a2f9da3 commit 9518342
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/growatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9518342

Please sign in to comment.