Skip to content

Commit

Permalink
feat(sensor): restore sensor state after HomeAssistant restart
Browse files Browse the repository at this point in the history
  • Loading branch information
IATkachenko committed Nov 14, 2021
1 parent 97bc8dd commit 49b46ce
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions custom_components/sleep_as_android/sensor.py
Expand Up @@ -5,8 +5,9 @@
import logging
import json

from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.core import HomeAssistant
from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers import device_registry as dr
Expand Down Expand Up @@ -49,7 +50,7 @@ async def add_configured_entities():
return True


class SleepAsAndroidSensor(Entity):
class SleepAsAndroidSensor(SensorEntity, RestoreEntity):
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, name: str):
self._instance: SleepAsAndroidInstance = hass.data[DOMAIN][config_entry.entry_id]

Expand All @@ -70,6 +71,14 @@ async def async_added_to_hass(self):
device = device_registry.async_get_device(identifiers=self.device_info['identifiers'], connections=set())
_LOGGER.debug("My device id is %s", device.id)
self._device_id = device.id

if (old_state := await self.async_get_last_state()) is not None:
self._state = old_state.state
_LOGGER.debug(f"async_added_to_hass: restored previous state for {self.name}: {self.state}")
else:
# No previous state. It is fine, but it would be nice to report
_LOGGER.debug(f"async_added_to_hass: no previously saved state for {self.name}")

self.async_write_ha_state()

async def async_will_remove_from_hass(self):
Expand Down

0 comments on commit 49b46ce

Please sign in to comment.