Skip to content

Commit b859e15

Browse files
author
lbbrhzn
authored
derive sensor unit_of_measurement from device_class (#200)
* derive sensor unit_of_measurement from device_class * remove unused _state variable Co-authored-by: lbbrhzn <@lbbrhzn>
1 parent d21576d commit b859e15

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

custom_components/ocpp/sensor.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
STATE_CLASS_TOTAL_INCREASING,
77
SensorEntity,
88
)
9+
import homeassistant.const as ha
910
from homeassistant.const import (
1011
CONF_MONITORED_VARIABLES,
1112
DEVICE_CLASS_BATTERY,
@@ -59,7 +60,6 @@ def __init__(
5960
self.central_system = central_system
6061
self.cp_id = cp_id
6162
self.metric = metric
62-
self._state = None
6363
self._extra_attr = {}
6464
self._last_reset = homeassistant.util.dt.utc_from_timestamp(0)
6565

@@ -73,12 +73,6 @@ def unique_id(self):
7373
"""Return the unique id of this sensor."""
7474
return ".".join([DOMAIN, self.cp_id, self.metric, "sensor"])
7575

76-
@property
77-
def state(self):
78-
"""Return the state of the sensor."""
79-
self._state = self.central_system.get_metric(self.cp_id, self.metric)
80-
return self._state
81-
8276
@property
8377
def available(self) -> bool:
8478
"""Return if sensor is available."""
@@ -87,7 +81,22 @@ def available(self) -> bool:
8781
@property
8882
def unit_of_measurement(self):
8983
"""Return the unit the value is expressed in."""
90-
return self.central_system.get_ha_unit(self.cp_id, self.metric)
84+
unit_of_measurement = None
85+
if self.device_class is DEVICE_CLASS_BATTERY:
86+
unit_of_measurement = ha.PERCENTAGE
87+
elif self.device_class is DEVICE_CLASS_CURRENT:
88+
unit_of_measurement = ha.ELECTRIC_CURRENT_AMPERE
89+
elif self.device_class is DEVICE_CLASS_ENERGY:
90+
unit_of_measurement = ha.ENERGY_KILO_WATT_HOUR
91+
elif self.device_class is DEVICE_CLASS_POWER:
92+
unit_of_measurement = ha.POWER_KILO_WATT
93+
elif self.device_class is DEVICE_CLASS_TEMPERATURE:
94+
unit_of_measurement = ha.TEMP_CELSIUS
95+
elif self.device_class is DEVICE_CLASS_TIMESTAMP:
96+
unit_of_measurement = None
97+
elif self.device_class is DEVICE_CLASS_VOLTAGE:
98+
unit_of_measurement = ha.ELECTRIC_POTENTIAL_VOLT
99+
return unit_of_measurement
91100

92101
@property
93102
def should_poll(self):

0 commit comments

Comments
 (0)