Skip to content

Commit

Permalink
Add persistent notification for SePushErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerhp authored Oct 26, 2023
1 parent 4d7f656 commit 6640772
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions custom_components/load_shedding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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")))
Expand Down Expand Up @@ -240,7 +246,7 @@ async def async_update_stage(self) -> dict:
ATTR_PLANNED: filtered,
}

return data
return data


class LoadSheddingAreaCoordinator(DataUpdateCoordinator[dict[str, Any]]):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6640772

Please sign in to comment.