Skip to content

Commit

Permalink
Fix to sensor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
megakid committed Jan 1, 2023
1 parent d7123cf commit d214ab4
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions custom_components/octopus_intelligent/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ def __init__(self, hass, octopus_system) -> None:
hass, self.timer_update, minute=range(0, 60, 30), second=1)

self._attributes = {}
self._set_native_value()
self._native_value = None
self._set_native_value(log_on_error=False)

def _set_native_value(self):
self._native_value = self._octopus_system.next_offpeak_start_utc()
return True
def _set_native_value(self, log_on_error = True):
try:
self._native_value = self._octopus_system.next_offpeak_start_utc()
return True
except:
if log_on_error:
_LOGGER.exception("Could not set native_value")
return False

@callback
def _handle_coordinator_update(self) -> None:
Expand Down Expand Up @@ -111,15 +117,20 @@ def __init__(self, hass, octopus_system) -> None:
hass, self.timer_update, minute=range(0, 60, 30), second=1)

self._attributes = {}
self._set_native_value()
self._native_value = None
self._set_native_value(log_on_error=False)

def _set_native_value(self):
def _set_native_value(self, log_on_error = True):
utcnow = dt_util.utcnow()
offpeak_range = self._octopus_system.next_offpeak_range_utc()
# Only update if we're in an offpeak window now
if offpeak_range["start"] <= utcnow:
self._native_value = offpeak_range["end"]
return True
try:
self._native_value = offpeak_range["end"]
return True
except:
if log_on_error:
_LOGGER.exception("Could not set native_value")
return False

@callback
Expand Down

0 comments on commit d214ab4

Please sign in to comment.