Skip to content

Commit

Permalink
use STATE_CLASS_TOTAL_INCREASING (#145)
Browse files Browse the repository at this point in the history
* use STATE_CLASS_TOTAL_INCREASING

* update pytest-homeassistant-custom-component

* change homeassistnat to 2021.9.0

* change homeassistnat to 2021.9.0b2

* fix constraints

* fix constraints

Co-authored-by: lbbrhzn <@lbbrhzn>
  • Loading branch information
lbbrhzn authored Sep 6, 2021
1 parent 2a3a3ef commit 32860b3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ black==21.7b0
flake8==3.9.2
reorder-python-imports==2.6.0
websockets==9.1
sqlalchemy==1.4.17
sqlalchemy==1.4.23
ocpp>=0.8.3
30 changes: 11 additions & 19 deletions custom_components/ocpp/sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Sensor platform for ocpp."""

import datetime

import homeassistant
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
CONF_MONITORED_VARIABLES,
DEVICE_CLASS_CURRENT,
Expand Down Expand Up @@ -78,16 +80,7 @@ def unique_id(self):
@property
def state(self):
"""Return the state of the sensor."""
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()
self._state = self.central_system.get_metric(self.cp_id, self.metric)
return self._state

@property
Expand Down Expand Up @@ -129,7 +122,11 @@ def extra_state_attributes(self):
@property
def state_class(self):
"""Return the state class of the sensor."""
return STATE_CLASS_MEASUREMENT
if self.device_class is DEVICE_CLASS_ENERGY:
state_class = STATE_CLASS_TOTAL_INCREASING
else:
state_class = STATE_CLASS_MEASUREMENT
return state_class

@property
def device_class(self):
Expand All @@ -156,11 +153,6 @@ def device_class(self):
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
3 changes: 1 addition & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

pre-commit
homeassistant>=2021.6.6
homeassistant>=2021.8.0
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r requirements_dev.txt
pytest-homeassistant-custom-component==0.4.3
pytest-homeassistant-custom-component==0.4.4
websockets
ocpp

0 comments on commit 32860b3

Please sign in to comment.