Feature: add nice Icons for HA autoconfiguration (#413)

* add Icons for Battery and Victron device sensors in Home Assistant
  overriding the boring default icon for many sensors

Signed-off-by: Martin Dummer <martin.dummer@gmx.net>
This commit is contained in:
Martin 2023-09-04 14:07:24 +02:00 committed by GitHub
parent 8c36532cea
commit a7a38e74a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 51 deletions

View File

@ -12,8 +12,8 @@ public:
private:
void publish(const String& subtopic, const String& payload);
void publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off);
void publishSensor(const char* caption, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL);
void publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off);
void publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL);
void createDeviceInfo(JsonObject& object);
bool _wasConnected = false;

View File

@ -13,8 +13,8 @@ public:
private:
void publish(const String& subtopic, const String& payload);
void publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off);
void publishSensor(const char* caption, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL);
void publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off);
void publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass = NULL, const char* stateClass = NULL, const char* unitOfMeasurement = NULL);
void createDeviceInfo(JsonObject& object);
bool _wasConnected = false;

View File

@ -55,32 +55,32 @@ void MqttHandleVedirectHassClass::publishConfig()
}
// device info
publishBinarySensor("MPPT load output state", "LOAD", "ON", "OFF");
publishSensor("MPPT serial number", "SER");
publishSensor("MPPT firmware number", "FW");
publishSensor("MPPT state of operation", "CS");
publishSensor("MPPT error code", "ERR");
publishSensor("MPPT off reason", "OR");
publishSensor("MPPT tracker operation mode", "MPPT");
publishSensor("MPPT Day sequence number (0...364)", "HSDS", NULL, "total", "d");
publishBinarySensor("MPPT load output state", "mdi:export", "LOAD", "ON", "OFF");
publishSensor("MPPT serial number", "mdi:counter", "SER");
publishSensor("MPPT firmware number", "mdi:counter", "FW");
publishSensor("MPPT state of operation", "mdi:wrench", "CS");
publishSensor("MPPT error code", "mdi:bell", "ERR");
publishSensor("MPPT off reason", "mdi:wrench", "OR");
publishSensor("MPPT tracker operation mode", "mdi:wrench", "MPPT");
publishSensor("MPPT Day sequence number (0...364)", "mdi:calendar-month-outline", "HSDS", NULL, "total", "d");
// battery info
publishSensor("Battery voltage", "V", "voltage", "measurement", "V");
publishSensor("Battery current", "I", "current", "measurement", "A");
publishSensor("Battery voltage", NULL, "V", "voltage", "measurement", "V");
publishSensor("Battery current", NULL, "I", "current", "measurement", "A");
// panel info
publishSensor("Panel voltage", "VPV", "voltage", "measurement", "V");
publishSensor("Panel power", "PPV", "power", "measurement", "W");
publishSensor("Panel yield total", "H19", "energy", "total_increasing", "kWh");
publishSensor("Panel yield today", "H20", "energy", "total", "kWh");
publishSensor("Panel maximum power today", "H21", "power", "measurement", "W");
publishSensor("Panel yield yesterday", "H22", "energy", "total", "kWh");
publishSensor("Panel maximum power yesterday", "H23", "power", "measurement", "W");
publishSensor("Panel voltage", NULL, "VPV", "voltage", "measurement", "V");
publishSensor("Panel power", NULL, "PPV", "power", "measurement", "W");
publishSensor("Panel yield total", NULL, "H19", "energy", "total_increasing", "kWh");
publishSensor("Panel yield today", NULL, "H20", "energy", "total", "kWh");
publishSensor("Panel maximum power today", NULL, "H21", "power", "measurement", "W");
publishSensor("Panel yield yesterday", NULL, "H22", "energy", "total", "kWh");
publishSensor("Panel maximum power yesterday", NULL, "H23", "power", "measurement", "W");
yield();
}
void MqttHandleVedirectHassClass::publishSensor(const char* caption, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement )
void MqttHandleVedirectHassClass::publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement )
{
String serial = VeDirect.veFrame.SER;
@ -105,6 +105,10 @@ void MqttHandleVedirectHassClass::publishSensor(const char* caption, const char*
root[F("stat_t")] = statTopic;
root[F("uniq_id")] = serial + "_" + sensorId;
if (icon != NULL) {
root[F("icon")] = icon;
}
if (unitOfMeasurement != NULL) {
root[F("unit_of_meas")] = unitOfMeasurement;
}
@ -127,7 +131,7 @@ void MqttHandleVedirectHassClass::publishSensor(const char* caption, const char*
publish(configTopic, buffer);
}
void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off)
void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off)
{
String serial = VeDirect.veFrame.SER;
@ -154,6 +158,10 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char* caption, const
root[F("pl_on")] = payload_on;
root[F("pl_off")] = payload_off;
if (icon != NULL) {
root[F("icon")] = icon;
}
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);

View File

@ -51,47 +51,47 @@ void MqttHandlePylontechHassClass::publishConfig()
}
// device info
publishSensor("Manufacturer", "manufacturer");
publishSensor("Manufacturer", "mdi:factory", "manufacturer");
// battery info
publishSensor("Battery voltage", "voltage", "voltage", "measurement", "V");
publishSensor("Battery current", "current", "current", "measurement", "A");
publishSensor("Temperature", "temperature", "temperature", "measurement", "°C");
publishSensor("State of Charge (SOC)", "stateOfCharge", "battery", "measurement", "%");
publishSensor("State of Health (SOH)", "stateOfHealth", NULL, "measurement", "%");
publishSensor("Charge voltage (BMS)", "settings/chargeVoltage", "voltage", "measurement", "V");
publishSensor("Charge current limit", "settings/chargeCurrentLimitation", "current", "measurement", "A");
publishSensor("Discharge current limit", "settings/dischargeCurrentLimitation", "current", "measurement", "A");
publishSensor("Battery voltage", NULL, "voltage", "voltage", "measurement", "V");
publishSensor("Battery current", NULL, "current", "current", "measurement", "A");
publishSensor("Temperature", NULL, "temperature", "temperature", "measurement", "°C");
publishSensor("State of Charge (SOC)", NULL, "stateOfCharge", "battery", "measurement", "%");
publishSensor("State of Health (SOH)", "mdi:heart-plus", "stateOfHealth", NULL, "measurement", "%");
publishSensor("Charge voltage (BMS)", NULL, "settings/chargeVoltage", "voltage", "measurement", "V");
publishSensor("Charge current limit", NULL, "settings/chargeCurrentLimitation", "current", "measurement", "A");
publishSensor("Discharge current limit", NULL, "settings/dischargeCurrentLimitation", "current", "measurement", "A");
publishBinarySensor("Alarm Discharge current", "alarm/overCurrentDischarge", "1", "0");
publishBinarySensor("Warning Discharge current", "warning/highCurrentDischarge", "1", "0");
publishBinarySensor("Alarm Discharge current", "mdi:alert", "alarm/overCurrentDischarge", "1", "0");
publishBinarySensor("Warning Discharge current", "mdi:alert-outline", "warning/highCurrentDischarge", "1", "0");
publishBinarySensor("Alarm Temperature low", "alarm/underTemperature", "1", "0");
publishBinarySensor("Warning Temperature low", "warning/lowTemperature", "1", "0");
publishBinarySensor("Alarm Temperature low", "mdi:thermometer-low", "alarm/underTemperature", "1", "0");
publishBinarySensor("Warning Temperature low", "mdi:thermometer-low", "warning/lowTemperature", "1", "0");
publishBinarySensor("Alarm Temperature high", "alarm/overTemperature", "1", "0");
publishBinarySensor("Warning Temperature high", "warning/highTemperature", "1", "0");
publishBinarySensor("Alarm Temperature high", "mdi:thermometer-high", "alarm/overTemperature", "1", "0");
publishBinarySensor("Warning Temperature high", "mdi:thermometer-high", "warning/highTemperature", "1", "0");
publishBinarySensor("Alarm Voltage low", "alarm/underVoltage", "1", "0");
publishBinarySensor("Warning Voltage low", "warning/lowVoltage", "1", "0");
publishBinarySensor("Alarm Voltage low", "mdi:alert", "alarm/underVoltage", "1", "0");
publishBinarySensor("Warning Voltage low", "mdi:alert-outline", "warning/lowVoltage", "1", "0");
publishBinarySensor("Alarm Voltage high", "alarm/overVoltage", "1", "0");
publishBinarySensor("Warning Voltage high", "warning/highVoltage", "1", "0");
publishBinarySensor("Alarm Voltage high", "mdi:alert", "alarm/overVoltage", "1", "0");
publishBinarySensor("Warning Voltage high", "mdi:alert-outline", "warning/highVoltage", "1", "0");
publishBinarySensor("Alarm BMS internal", "alarm/bmsInternal", "1", "0");
publishBinarySensor("Warning BMS internal", "warning/bmsInternal", "1", "0");
publishBinarySensor("Alarm BMS internal", "mdi:alert", "alarm/bmsInternal", "1", "0");
publishBinarySensor("Warning BMS internal", "mdi:alert-outline", "warning/bmsInternal", "1", "0");
publishBinarySensor("Alarm High charge current", "alarm/overCurrentCharge", "1", "0");
publishBinarySensor("Warning High charge current", "warning/highCurrentCharge", "1", "0");
publishBinarySensor("Alarm High charge current", "mdi:alert", "alarm/overCurrentCharge", "1", "0");
publishBinarySensor("Warning High charge current", "mdi:alert-outline", "warning/highCurrentCharge", "1", "0");
publishBinarySensor("Charge enabled", "charging/chargeEnabled", "1", "0");
publishBinarySensor("Discharge enabled", "charging/dischargeEnabled", "1", "0");
publishBinarySensor("Charge immediately", "charging/chargeImmediately", "1", "0");
publishBinarySensor("Charge enabled", "mdi:battery-arrow-up", "charging/chargeEnabled", "1", "0");
publishBinarySensor("Discharge enabled", "mdi:battery-arrow-down", "charging/dischargeEnabled", "1", "0");
publishBinarySensor("Charge immediately", "mdi:alert", "charging/chargeImmediately", "1", "0");
yield();
}
void MqttHandlePylontechHassClass::publishSensor(const char* caption, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement )
void MqttHandlePylontechHassClass::publishSensor(const char* caption, const char* icon, const char* subTopic, const char* deviceClass, const char* stateClass, const char* unitOfMeasurement )
{
String sensorId = caption;
sensorId.replace(" ", "_");
@ -115,6 +115,10 @@ void MqttHandlePylontechHassClass::publishSensor(const char* caption, const char
root[F("stat_t")] = statTopic;
root[F("uniq_id")] = serial + "_" + sensorId;
if (icon != NULL) {
root[F("icon")] = icon;
}
if (unitOfMeasurement != NULL) {
root[F("unit_of_meas")] = unitOfMeasurement;
}
@ -137,7 +141,8 @@ void MqttHandlePylontechHassClass::publishSensor(const char* caption, const char
publish(configTopic, buffer);
}
void MqttHandlePylontechHassClass::publishBinarySensor(const char* caption, const char* subTopic, const char* payload_on, const char* payload_off)
void MqttHandlePylontechHassClass::publishBinarySensor(const char* caption, const char* icon, const char* subTopic, const char* payload_on, const char* payload_off)
{
String sensorId = caption;
sensorId.replace(" ", "_");
@ -163,6 +168,10 @@ void MqttHandlePylontechHassClass::publishBinarySensor(const char* caption, cons
root[F("pl_on")] = payload_on;
root[F("pl_off")] = payload_off;
if (icon != NULL) {
root[F("icon")] = icon;
}
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);