Skip to content

Commit

Permalink
feat(sensor): Updated previous day consumption to only be valid if en…
Browse files Browse the repository at this point in the history
…ough data points are available
  • Loading branch information
BottlecapDave committed Dec 15, 2021
1 parent 4f09269 commit 9e71ef2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async def async_update_data():
previous_data = None

current_datetime = now()
if (previous_data == None or current_datetime.minute % 30 == 0):
_LOGGER.info('Updating consumption')
if (previous_data == None or (previous_data[-1]["interval_end"] < utcnow() and current_datetime.minute % 30 == 0)):
_LOGGER.debug('Updating electricity consumption...')

period_from = as_utc((current_datetime - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))
period_to = as_utc(current_datetime.replace(hour=0, minute=0, second=0, microsecond=0))
Expand All @@ -61,7 +61,7 @@ async def async_update_data():
else:
data = await client.async_gas_consumption(identifier, serial_number, period_from, period_to)

if data != None:
if data != None and len(data) == 48:
hass.data[DOMAIN][previous_consumption_key] = data
return data

Expand Down Expand Up @@ -320,6 +320,8 @@ def state(self):
if (self.coordinator.data != None and len(self.coordinator.data) == 48):

if (self._latest_date != self.coordinator.data[-1]["interval_end"]):
_LOGGER.info(f"Calculating previous electricity consumption for '{self._mprn}/{self._serial_number}'...")

total = 0
for consumption in self.coordinator.data:
total = total + consumption["consumption"]
Expand Down Expand Up @@ -398,6 +400,7 @@ async def async_update(self):

# Only calculate our consumption if our data has changed
if (self._latest_date != self.coordinator.data[-1]["interval_end"]):
_LOGGER.debug(f"Calculating previous electricity cost for '{self._mprn}/{self._serial_number}'...")

current_datetime = now()
period_from = as_utc((current_datetime - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))
Expand All @@ -417,7 +420,7 @@ async def async_update(self):

total_cost_in_pence = total_cost_in_pence + (rate["value_inc_vat"] * value)

self._state = total_cost_in_pence / 100
self._state = round(total_cost_in_pence / 100, 2)
self._latest_date = self.coordinator.data[-1]["interval_end"]

class OctopusEnergyPreviousAccumulativeGasReading(SensorEntity):
Expand Down Expand Up @@ -487,7 +490,7 @@ async def async_update(self):
# We only need to do this once a day, unless we don't have enough data for the day therefore we want to retrieve it
# every hour until we have enough data for the day
if (current_datetime.hour == 0 and current_datetime.minute == 0) or self._state == None or (current_datetime.minute % 60 == 0 and len(self._data) != 48):
_LOGGER.info('Updating OctopusEnergyPreviousAccumulativeGasReading')
_LOGGER.debug('Updating OctopusEnergyPreviousAccumulativeGasReading')

period_from = as_utc((current_datetime - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))
period_to = as_utc(current_datetime.replace(hour=0, minute=0, second=0, microsecond=0))
Expand Down

0 comments on commit 9e71ef2

Please sign in to comment.