From 6640772ba7194a012208525866680dec8bfa7d28 Mon Sep 17 00:00:00 2001 From: Werner Pieterson Date: Thu, 26 Oct 2023 22:48:11 +0200 Subject: [PATCH] Add persistent notification for SePushErrors --- custom_components/load_shedding/__init__.py | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/custom_components/load_shedding/__init__.py b/custom_components/load_shedding/__init__.py index 82649c6..7ab8ace 100644 --- a/custom_components/load_shedding/__init__.py +++ b/custom_components/load_shedding/__init__.py @@ -132,7 +132,7 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> boo async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Migrate old entry.""" - _LOGGER.debug("Migrating from version %s", config_entry.version) + _LOGGER.info("Migrating from version %s", config_entry.version) if config_entry.version == 3: old_data = {**config_entry.data} @@ -168,6 +168,8 @@ async def _async_update_data(self) -> dict: diff = 0 if self.last_update is not None: diff = (now - self.last_update).seconds + _LOGGER.debug("Stage now: %s, last_update: %s, diff: %s, ", now, self.last_update, diff) + if 0 < diff < STAGE_UPDATE_INTERVAL: return self.data @@ -189,9 +191,13 @@ async def async_update_stage(self) -> dict: try: esp = await self.hass.async_add_executor_job(self.sepush.status) except SePushError as err: + self.hass.components.persistent_notification.async_create( + f"SePush Error: {err}", + title='Load Shedding', + notification_id='SePushError' + ) raise UpdateFailed(err) from err else: - data = {} statuses = esp.get("status", {}) for idx, area in statuses.items(): stage = Stage(int(area.get("stage", "0"))) @@ -240,7 +246,7 @@ async def async_update_stage(self) -> dict: ATTR_PLANNED: filtered, } - return data + return data class LoadSheddingAreaCoordinator(DataUpdateCoordinator[dict[str, Any]]): @@ -272,6 +278,7 @@ async def _async_update_data(self) -> dict: if self.last_update is not None: diff = (now - self.last_update).seconds + _LOGGER.info("Area now: %s, last_update: %s, diff: %s, ", now, self.last_update, diff) if 0 < diff < AREA_UPDATE_INTERVAL: await self.async_area_forecast() return self.data @@ -281,9 +288,13 @@ async def _async_update_data(self) -> dict: except UpdateFailed as err: _LOGGER.error("Unable to get area schedule: %s", err, exc_info=True) self.data = {} + except Exception as err: + _LOGGER.error("Unable to get area schedule: %s", err, exc_info=True) + self.data = {} else: self.data = area self.last_update = now + _LOGGER.info("Area updated: last_update: %s, ", self.last_update) await self.async_area_forecast() return self.data @@ -296,6 +307,11 @@ async def async_update_area(self) -> dict: try: esp = await self.hass.async_add_executor_job(self.sepush.area, area.id) except SePushError as err: + self.hass.components.persistent_notification.async_create( + f"SePush Error: {err}", + title='Load Shedding', + notification_id='SePushError' + ) raise UpdateFailed(err) from err # Get events for area