Skip to content

Commit

Permalink
Move graph timeslot calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 27, 2023
1 parent b829fc1 commit 4aa76c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.const import UnitOfPower, UnitOfEnergy
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util.dt import (utcnow)
from .const import DOMAIN, DATA_CLIENT, DATA_COORDINATOR, DATA_STATISTICS_COORDINATOR
from .coordinator import OhmeUpdateCoordinator, OhmeStatisticsUpdateCoordinator
from .utils import charge_graph_next_slot
Expand Down Expand Up @@ -148,8 +149,17 @@ def icon(self):

@property
def native_value(self):
"""Get value from data returned from API by coordinator"""
if self.coordinator.data and self.coordinator.data['mode'] != "DISCONNECTED":
return charge_graph_next_slot(self.coordinator.data['startTime'], self.coordinator.data['chargeGraph']['points'])
"""Return pre-calculated state."""
return self._state

return None
@callback
def _handle_coordinator_update(self) -> None:
"""Calculate next timeslot. This is a bit slow so we only update on coordinator data update."""
if self.coordinator.data is None:
self._state = None
else:
self._state = charge_graph_next_slot(self.coordinator.data['startTime'], self.coordinator.data['chargeGraph']['points'])

self._last_updated = utcnow()

self.async_write_ha_state()
2 changes: 1 addition & 1 deletion custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def charge_graph_next_slot(charge_start, points):

# Give up if we have less than 3 points
if len(data) < 3:
return False
return None

next_ts = None

Expand Down

0 comments on commit 4aa76c7

Please sign in to comment.