Set device_class to duration in HA sensor definitions #822

This commit is contained in:
Proddy
2022-12-23 13:42:59 +01:00
parent 9802581301
commit 3c91ac27dc
7 changed files with 133 additions and 130 deletions

View File

@@ -48,28 +48,28 @@ class DeviceValue {
// also used with HA as uom
enum DeviceValueUOM : uint8_t {
NONE = 0, // 0
DEGREES, // 1
DEGREES_R, // 2 - relative temperature
PERCENT, // 3
DEGREES, // 1 - °C
DEGREES_R, // 2 - °C (relative temperature)
PERCENT, // 3 - %
LMIN, // 4 - l/min
KWH, // 5 - kWh
WH, // 6 - Wh
HOURS, // 7
MINUTES, // 8
HOURS, // 7 - h
MINUTES, // 8 - m
UA, // 9 - µA
BAR, // 10
BAR, // 10 - bar
KW, // 11 - kW
W, // 12
KB, // 13
SECONDS, // 14
W, // 12 - W
KB, // 13 - kB
SECONDS, // 14 - s
DBM, // 15 - dBm
FAHRENHEIT, // 16
FAHRENHEIT, // 16 - °F
MV, // 17 - mV
SQM, // 18 - square meter
M3, // 19 - cubic meter
L, // 20 - liter
KMIN, // 21 - Kelvin * minutes
K, // 22 - Kelvin
SQM, // 18 -
M3, // 19 -
L, // 20 - L
KMIN, // 21 - K*min
K, // 22 - K
CONNECTIVITY // 23 - used in HA
};

View File

@@ -197,7 +197,7 @@ MAKE_PSTR_LIST(tpl_input, "Format: <inv>[<evu1><evu2><evu3><comp><aux><cool><hea
MAKE_PSTR_LIST(tpl_input4, "Format: <inv>[<comp><aux><cool><heat><dhw><pv>]")
// Unit Of Measurement mapping - maps to DeviceValueUOM_s in emsdevice.cpp
// Translating hours/minute/seconds in emsdevice.cpp
// Translating hours/minute/seconds are done in emsdevice.cpp (uom_to_string())
MAKE_PSTR(uom_blank, " ")
MAKE_PSTR(uom_percent, "%")
MAKE_PSTR(uom_degrees, "°C")
@@ -208,13 +208,13 @@ MAKE_PSTR(uom_ua, "µA")
MAKE_PSTR(uom_lmin, "l/min")
MAKE_PSTR(uom_kw, "kW")
MAKE_PSTR(uom_w, "W")
MAKE_PSTR(uom_kb, "KB")
MAKE_PSTR(uom_kb, "kB")
MAKE_PSTR(uom_dbm, "dBm")
MAKE_PSTR(uom_fahrenheit, "°F")
MAKE_PSTR(uom_mv, "mV")
MAKE_PSTR(uom_sqm, "")
MAKE_PSTR(uom_m3, "")
MAKE_PSTR(uom_l, "l")
MAKE_PSTR(uom_l, "L")
MAKE_PSTR(uom_kmin, "K*min")
MAKE_PSTR(uom_k, "K")

View File

@@ -66,16 +66,10 @@ MAKE_PSTR_WORD(measurement)
MAKE_PSTR_WORD(total_increasing)
MAKE_PSTR(icondegrees, "mdi:coolant-temperature") // DeviceValueUOM::DEGREES
MAKE_PSTR(iconpercent, "mdi:percent-outline") // DeviceValueUOM::PERCENT
MAKE_PSTR(icontime, "mdi:clock-outline") // DeviceValueUOM::SECONDS MINUTES & HOURS
MAKE_PSTR(iconkb, "mdi:memory") // DeviceValueUOM::KB
MAKE_PSTR(iconlmin, "mdi:water-boiler") // DeviceValueUOM::LMIN
// MAKE_PSTR(iconkwh, "mdi:transmission-tower") // DeviceValueUOM::KWH & WH
MAKE_PSTR(iconua, "mdi:lightning-bolt-circle") // DeviceValueUOM::UA
// MAKE_PSTR(iconbar, "mdi:gauge") // DeviceValueUOM::BAR
// MAKE_PSTR(iconkw, "mdi:omega") // DeviceValueUOM::KW & W
// MAKE_PSTR(icondbm, "mdi:wifi-strength-2") // DeviceValueUOM::DBM
MAKE_PSTR(iconnum, "mdi:counter") // DeviceValueUOM::NONE
// MAKE_PSTR(icondevice, "mdi:home-automation") // for devices in HA
MAKE_PSTR(iconua, "mdi:lightning-bolt-circle") // DeviceValueUOM::UA
MAKE_PSTR(iconnum, "mdi:counter") // DeviceValueUOM::NONE
uuid::log::Logger Mqtt::logger_{F_(mqtt), uuid::log::Facility::DAEMON};
@@ -1171,8 +1165,16 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
doc["payload_off"] = Helpers::render_boolean(result, false);
doc[sc_ha] = F_(measurement);
} else {
// always set the uom
if (uom != DeviceValueUOM::NONE) {
// always set the uom, using the standards except for hours/minutes/seconds
// using HA specific codes from https://github.com/home-assistant/core/blob/dev/homeassistant/const.py
if (uom == DeviceValueUOM::HOURS) {
doc[uom_ha] = "h";
} else if (uom == DeviceValueUOM::MINUTES) {
doc[uom_ha] = "min";
} else if (uom == DeviceValueUOM::SECONDS) {
doc[uom_ha] = "s";
} else if (uom != DeviceValueUOM::NONE) {
// default
doc[uom_ha] = EMSdevice::uom_to_string(uom);
}
}
@@ -1196,12 +1198,12 @@ void Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
case DeviceValueUOM::SECONDS:
case DeviceValueUOM::MINUTES:
case DeviceValueUOM::HOURS:
doc[ic_ha] = F_(icontime);
if (type == DeviceValueType::TIME) {
doc[sc_ha] = F_(total_increasing);
} else {
doc[sc_ha] = F_(measurement);
}
doc[dc_ha] = "duration"; // https://github.com/emsesp/EMS-ESP32/issues/822
break;
case DeviceValueUOM::KB:
doc[ic_ha] = F_(iconkb);