Skip to content

Commit 5f6b662

Browse files
author
lbbrhzn
authored
Bugfix/preassign sensor unit of measurement (#207)
* derive sensor unit_of_measurement from device_class * remove unused _state variable * improve sensor device classification * fix frequency state_class * bugfix Co-authored-by: lbbrhzn <@lbbrhzn>
1 parent 4e61080 commit 5f6b662

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

custom_components/ocpp/sensor.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,13 @@ def unit_of_measurement(self):
9797
unit_of_measurement = ha.POWER_KILO_WATT
9898
elif self.device_class is DEVICE_CLASS_TEMPERATURE:
9999
unit_of_measurement = ha.TEMP_CELSIUS
100-
elif self.device_class is DEVICE_CLASS_FREQUENCY:
101-
if self.metric is Measurand.rpm:
102-
unit_of_measurement = FREQUENCY_RPM
103-
else:
104-
unit_of_measurement = FREQUENCY_HERTZ
105100
elif self.device_class is DEVICE_CLASS_TIMESTAMP:
106101
# Home assistant does not define a unit, must be a Datetime object or timestamp string (ISO 8601).
107102
unit_of_measurement = None
108103
elif self.device_class is DEVICE_CLASS_VOLTAGE:
109104
unit_of_measurement = ha.ELECTRIC_POTENTIAL_VOLT
105+
elif self.metric in [Measurand.rpm, Measurand.frequency]:
106+
unit_of_measurement = FREQUENCY_RPM
110107
return unit_of_measurement
111108

112109
@property
@@ -141,12 +138,17 @@ def state_class(self):
141138
state_class = None
142139
if self.device_class is DEVICE_CLASS_ENERGY:
143140
state_class = STATE_CLASS_TOTAL_INCREASING
144-
elif self.device_class in [
145-
DEVICE_CLASS_CURRENT,
146-
DEVICE_CLASS_POWER,
147-
DEVICE_CLASS_TEMPERATURE,
148-
DEVICE_CLASS_BATTERY,
149-
]:
141+
elif (
142+
self.device_class
143+
in [
144+
DEVICE_CLASS_CURRENT,
145+
DEVICE_CLASS_VOLTAGE,
146+
DEVICE_CLASS_POWER,
147+
DEVICE_CLASS_TEMPERATURE,
148+
DEVICE_CLASS_BATTERY,
149+
]
150+
or self.metric in [Measurand.rpm, Measurand.frequency]
151+
):
150152
state_class = STATE_CLASS_MEASUREMENT
151153
return state_class
152154

@@ -166,7 +168,7 @@ def device_class(self):
166168
Measurand.frequency,
167169
Measurand.rpm,
168170
]
169-
or self.metric.lower().startwith("frequency")
171+
or self.metric.lower().startswith("frequency")
170172
):
171173
device_class = DEVICE_CLASS_FREQUENCY
172174
elif self.metric.lower().startswith("power."):

0 commit comments

Comments
 (0)