Skip to content

Commit

Permalink
fixup! fixup! [WIP] Historical sensor LeighCurran#6
Browse files Browse the repository at this point in the history
  • Loading branch information
shtrom committed Jul 27, 2023
1 parent c536617 commit 3036d80
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions custom_components/auroraplus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,39 @@ def extra_state_attributes(self):
attributes['Bill Overdue Amount'] = self._session.BillOverDueAmount
return attributes

def async_update_historical(self):
# XXX
self.update()
async def async_update_historical(self):
await self._session.getday()
if self._sensor.startswith(SENSOR_KILOWATTHOURUSAGETARIFF):
tariff = self._sensor.removeprefix(SENSOR_KILOWATTHOURUSAGETARIFF)

try:
_LOGGER.debug("Getting historical states for: %s", self._sensor)
await self._session.getday()
except OSError as err:
_LOGGER.error("Updating Aurora+ failed: %s", err)
metered_records = self._session.day.get(
'MeteredUsageRecords'
)
if not metered_records:
_LOGGER.warning(f"No daily records for {self._sensor}")
return

self._historical_state = [
HistoricalState(
state=r.get('KilowattHourUsage').get(tariff),
dt=datetime.datetime.fromisoformat(r['EndTime'])
)
for r in metered_records
if r.get('KilowattHourUsage')
]
_LOGGER.debug("Done with historical states for: %s", self._sensor)

def update(self):
"""Collect updated data from Aurora+ API."""
try:
_LOGGER.debug("Updating sensor: %s", self._sensor)
# _LOGGER.debug("Updating sensor: %s", self._sensor)
self._session.getcurrent()
self._session.getsummary()
self._data = self._session
except OSError as err:
_LOGGER.error("Updating Aurora+ failed: %s", err)
self._old_state = self._state
Expand All @@ -201,26 +223,7 @@ def update(self):
self._state = round(
self._session.KilowattHourUsage['Total'], self._rounding)
elif self._sensor.startswith(SENSOR_KILOWATTHOURUSAGETARIFF):
tariff = self._sensor.removeprefix(SENSOR_KILOWATTHOURUSAGETARIFF)
self._state = self._session.KilowattHourUsage.get(tariff)
if self._state:
self._state = round(self._state, self._rounding)

self._session.getday()
metered_records = self._session.day.get(
'MeteredUsageRecords'
)
if not metered_records:
_LOGGER.warning(f"No daily records for {self._sensor}")
return

self._historical_state = [
HistoricalState(
state=(r.get('KilowattHourUsageAEST') or {}).get(tariff),
dt=datetime.datetime.fromisoformat(r['EndTime'])
)
for r in metered_records
]
pass

else:
_LOGGER.error("Unknown sensor type found")
Expand Down

0 comments on commit 3036d80

Please sign in to comment.