-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sensor properties for long term statistics (#121)
* add sensor properties for long term statistics * fix typo: isnumeric * fix typo: isnumeric Co-authored-by: lbbrhzn <@lbbrhzn>
- Loading branch information
lbbrhzn
authored
Aug 22, 2021
1 parent
47519b5
commit 0aa64dc
Showing
3 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
"""Sensor platform for ocpp.""" | ||
|
||
from homeassistant.const import CONF_MONITORED_VARIABLES | ||
from homeassistant.helpers.entity import Entity | ||
import datetime | ||
|
||
import homeassistant | ||
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity | ||
from homeassistant.const import ( | ||
CONF_MONITORED_VARIABLES, | ||
DEVICE_CLASS_CURRENT, | ||
DEVICE_CLASS_ENERGY, | ||
DEVICE_CLASS_POWER, | ||
DEVICE_CLASS_TEMPERATURE, | ||
DEVICE_CLASS_VOLTAGE, | ||
) | ||
|
||
from ocpp.v16.enums import UnitOfMeasure | ||
|
||
from .api import CentralSystem | ||
from .const import CONF_CPID, DOMAIN, ICON | ||
from .const import CONF_CPID, DEFAULT_CPID, DOMAIN, ICON | ||
from .enums import HAChargerDetails, HAChargerSession, HAChargerStatuses | ||
|
||
|
||
async def async_setup_entry(hass, entry, async_add_devices): | ||
"""Configure the sensor platform.""" | ||
central_system = hass.data[DOMAIN][entry.entry_id] | ||
cp_id = entry.data[CONF_CPID] | ||
cp_id = entry.data.get(CONF_CPID, DEFAULT_CPID) | ||
|
||
entities = [] | ||
|
||
|
@@ -36,7 +48,7 @@ async def async_setup_entry(hass, entry, async_add_devices): | |
async_add_devices(entities, False) | ||
|
||
|
||
class ChargePointMetric(Entity): | ||
class ChargePointMetric(SensorEntity): | ||
"""Individual sensor for charge point metrics.""" | ||
|
||
def __init__( | ||
|
@@ -51,6 +63,7 @@ def __init__( | |
self.metric = metric | ||
self._state = None | ||
self._extra_attr = {} | ||
self._last_reset = homeassistant.util.dt.utc_from_timestamp(0) | ||
|
||
@property | ||
def name(self): | ||
|
@@ -65,7 +78,17 @@ def unique_id(self): | |
@property | ||
def state(self): | ||
"""Return the state of the sensor.""" | ||
return self.central_system.get_metric(self.cp_id, self.metric) | ||
old_state = self._state | ||
new_state = self.central_system.get_metric(self.cp_id, self.metric) | ||
self._state = new_state | ||
if ( | ||
(self.device_class is DEVICE_CLASS_ENERGY) | ||
and (new_state is not None) | ||
and (old_state is not None) | ||
and (new_state < old_state) | ||
): | ||
self._last_reset = datetime.datetime.now() | ||
return self._state | ||
|
||
@property | ||
def available(self) -> bool: | ||
|
@@ -103,6 +126,41 @@ def extra_state_attributes(self): | |
"""Return the state attributes.""" | ||
return self.central_system.get_extra_attr(self.cp_id, self.metric) | ||
|
||
@property | ||
def state_class(self): | ||
"""Return the state class of the sensor.""" | ||
return STATE_CLASS_MEASUREMENT | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lbbrhzn
Owner
|
||
|
||
@property | ||
def device_class(self): | ||
"""Return the device class of the sensor.""" | ||
if self.unit_of_measurement in [ | ||
UnitOfMeasure.wh.value, | ||
UnitOfMeasure.kwh.value, | ||
]: | ||
return DEVICE_CLASS_ENERGY | ||
elif self.unit_of_measurement in [ | ||
UnitOfMeasure.w.value, | ||
UnitOfMeasure.kw.value, | ||
]: | ||
return DEVICE_CLASS_POWER | ||
elif self.unit_of_measurement in [ | ||
UnitOfMeasure.celsius.value, | ||
UnitOfMeasure.fahrenheit.value, | ||
]: | ||
return DEVICE_CLASS_TEMPERATURE | ||
elif self.unit_of_measurement in [UnitOfMeasure.a.value]: | ||
return DEVICE_CLASS_CURRENT | ||
elif self.unit_of_measurement in [UnitOfMeasure.v.value]: | ||
return DEVICE_CLASS_VOLTAGE | ||
else: | ||
return None | ||
|
||
@property | ||
def last_reset(self): | ||
"""Return the time when a metered value wwas ;ast reset.""" | ||
return self._last_reset | ||
|
||
async def async_update(self): | ||
"""Get the latest data and update the states.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@lbbrhzn , should we use
STATE_CLASS_TOTAL_INCREASING
for energy metrics which are monotonically increasing? The HA documentation is somewhat confusing showing energy in both classes