Skip to content

Commit

Permalink
Bugfix/preassign sensor unit of measurement (#207)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
lbbrhzn authored Nov 22, 2021
1 parent 4e61080 commit 5f6b662
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions custom_components/ocpp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,13 @@ def unit_of_measurement(self):
unit_of_measurement = ha.POWER_KILO_WATT
elif self.device_class is DEVICE_CLASS_TEMPERATURE:
unit_of_measurement = ha.TEMP_CELSIUS
elif self.device_class is DEVICE_CLASS_FREQUENCY:
if self.metric is Measurand.rpm:
unit_of_measurement = FREQUENCY_RPM
else:
unit_of_measurement = FREQUENCY_HERTZ
elif self.device_class is DEVICE_CLASS_TIMESTAMP:
# Home assistant does not define a unit, must be a Datetime object or timestamp string (ISO 8601).
unit_of_measurement = None
elif self.device_class is DEVICE_CLASS_VOLTAGE:
unit_of_measurement = ha.ELECTRIC_POTENTIAL_VOLT
elif self.metric in [Measurand.rpm, Measurand.frequency]:
unit_of_measurement = FREQUENCY_RPM
return unit_of_measurement

@property
Expand Down Expand Up @@ -141,12 +138,17 @@ def state_class(self):
state_class = None
if self.device_class is DEVICE_CLASS_ENERGY:
state_class = STATE_CLASS_TOTAL_INCREASING
elif self.device_class in [
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_BATTERY,
]:
elif (
self.device_class
in [
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_BATTERY,
]
or self.metric in [Measurand.rpm, Measurand.frequency]
):
state_class = STATE_CLASS_MEASUREMENT
return state_class

Expand All @@ -166,7 +168,7 @@ def device_class(self):
Measurand.frequency,
Measurand.rpm,
]
or self.metric.lower().startwith("frequency")
or self.metric.lower().startswith("frequency")
):
device_class = DEVICE_CLASS_FREQUENCY
elif self.metric.lower().startswith("power."):
Expand Down

0 comments on commit 5f6b662

Please sign in to comment.