Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
use coordinator's built in last_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
rlippmann committed Jan 31, 2024
1 parent 19c9d46 commit 297d4fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
19 changes: 8 additions & 11 deletions custom_components/adtpulse/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ def __init__(self, hass: HomeAssistant, pulse_service: PyADTPulseAsync):
LOG.debug("%s: creating update coordinator", ADTPULSE_DOMAIN)
self._adt_pulse = pulse_service
self._push_wait_task: Task[None] | None = None
self._exception: Exception | None = None
super().__init__(hass, LOG, name=ADTPULSE_DOMAIN)

@property
def adtpulse(self) -> PyADTPulseAsync:
"""Return the ADT Pulse service object."""
return self._adt_pulse

@property
def last_update_exception(self) -> Exception | None:
"""Return the last exception."""
return self._exception

async def stop(self, _: Any) -> None:
"""Stop the update coordinator."""
if self._push_wait_task:
Expand All @@ -66,6 +60,7 @@ async def _pulse_push_task(self) -> None:
while True:
LOG.debug("%s: coordinator waiting for updates", ADTPULSE_DOMAIN)
next_check = 0
update_exception: Exception | None = None
try:
await self._adt_pulse.wait_for_update()
except PulseLoginException as ex:
Expand All @@ -79,7 +74,7 @@ async def _pulse_push_task(self) -> None:
LOG.debug(
"%s: coordinator received retry exception: %s", ADTPULSE_DOMAIN, ex
)
self._exception = ex
update_exception = ex
if ex.retry_time:
next_check = max(ex.retry_time - now().timestamp(), 0)
except PulseExceptionWithBackoff as ex:
Expand All @@ -88,7 +83,7 @@ async def _pulse_push_task(self) -> None:
ADTPULSE_DOMAIN,
ex,
)
self._exception = ex
update_exception = ex
next_check = ex.backoff.get_current_backoff_interval()
except CancelledError:
LOG.debug(
Expand All @@ -102,10 +97,12 @@ async def _pulse_push_task(self) -> None:
ADTPULSE_DOMAIN,
ex,
)
else:
self._exception = None
update_exception = ex
LOG.debug("%s: coordinator received update notification", ADTPULSE_DOMAIN)
self.async_set_updated_data(None)
if update_exception:
self.async_set_update_error(update_exception)
else:
self.async_set_updated_data(None)
if next_check != 0:
LOG.debug(
"%s: coordinator scheduling next update in %f seconds",
Expand Down
8 changes: 4 additions & 4 deletions custom_components/adtpulse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def options(self) -> list[str]:
@property
def native_value(self) -> str:
"""Return the state of the sensor."""
if not self.coordinator.last_update_exception:
if not self.coordinator.last_exception:
return CONNECTION_STATUS_OK[0]
if self.coordinator.last_exception in COORDINATOR_EXCEPTION_MAP:
return COORDINATOR_EXCEPTION_MAP[self.coordinator.last_exception][0]
Expand All @@ -118,8 +118,8 @@ def icon(self) -> str:
"""Return the icon of this sensor."""
if self.native_value == CONNECTION_STATUS_OK[0]:
return CONNECTION_STATUS_OK[1]
if self.coordinator.last_update_exception in COORDINATOR_EXCEPTION_MAP:
return COORDINATOR_EXCEPTION_MAP[self.coordinator.last_update_exception][1]
if self.coordinator.last_exception in COORDINATOR_EXCEPTION_MAP:
return COORDINATOR_EXCEPTION_MAP[self.coordinator.last_exception][1]
return "mdi:alert-octogram"

@property
Expand Down Expand Up @@ -176,7 +176,7 @@ def native_value(self) -> datetime | None:
"""Return the state of the sensor."""
timediff = 0
curr_time = now()
last_ex = self.coordinator.last_update_exception
last_ex = self.coordinator.last_exception
if not last_ex:
return None
if isinstance(last_ex, PulseExceptionWithRetry):
Expand Down

0 comments on commit 297d4fe

Please sign in to comment.