Skip to content

Commit

Permalink
meter reading updates failed after some time, call login before fetch…
Browse files Browse the repository at this point in the history
…ing meter_readings to make sure bearer token remains valid
  • Loading branch information
myTselection committed Mar 2, 2023
1 parent af81de2 commit 2b7eb9c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions custom_components/pixometer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def __init__(self, username, password, hass):
self._hass = hass
self._meter_readings = dict()

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def _initiate(self):
async def initiate(self):
_LOGGER.info("Fetching stuff for " + NAME)
if not(self._session):
self._session = ComponentSession()
Expand All @@ -97,9 +96,6 @@ async def _initiate(self):
self._meter_list = await self._hass.async_add_executor_job(lambda: self._session.meterlist())
_LOGGER.debug("meter list retrieved " + NAME)
assert self._meter_list is not None

async def initiate(self):
await self._initiate()
return self._meter_list

async def init_meter_readings(self, meter_id):
Expand All @@ -111,10 +107,15 @@ async def init_meter_readings(self, meter_id):

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def _update(self, meter_id):
meter_readings = await self._hass.async_add_executor_job(lambda: self._session.meter_readings(meter_id))
_LOGGER.debug(f"updated meter readings for {NAME} - meter id: {meter_id}")
assert meter_readings is not None
self._meter_readings[meter_id] = meter_readings.get("results")[0]
if not(self._session):
self._session = ComponentSession()

if self._session:
await self._hass.async_add_executor_job(lambda: self._session.login(self._username, self._password))
meter_readings = await self._hass.async_add_executor_job(lambda: self._session.meter_readings(meter_id))
_LOGGER.debug(f"updated meter readings for {NAME} - meter id: {meter_id}")
assert meter_readings is not None
self._meter_readings[meter_id] = meter_readings.get("results")[0]

async def update(self, meter_id):
await self._update(meter_id)
Expand Down

0 comments on commit 2b7eb9c

Please sign in to comment.