Skip to content

Commit

Permalink
Adding RestoreEntity to Challenge Sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
valleedelisle committed Jan 17, 2022
1 parent e1b07bf commit e1ff851
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion custom_components/hilo/sensor.py
Expand Up @@ -532,7 +532,7 @@ async def _async_update(self):
self._history.append(season)


class HiloChallengeSensor(HiloEntity, SensorEntity):
class HiloChallengeSensor(HiloEntity, RestoreEntity, SensorEntity):
"""Hilo challenge sensor.
Its state will be either:
- off: no ongoing or scheduled challenge
Expand Down Expand Up @@ -587,6 +587,15 @@ def should_poll(self):
def extra_state_attributes(self):
return {"next_events": self._next_events}

async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
self._last_update = dt_util.utcnow()
self._state = last_state.state
self._next_events = last_state.attributes.get("next_events", [])

async def _async_update(self):
self._next_events = []
events = await self._hilo._api.get_gd_events(self._hilo.devices.location_id)
Expand Down

0 comments on commit e1ff851

Please sign in to comment.