Add restore last state for EnOcean Sensors (#34375)

This commit is contained in:
rhadamantys 2020-05-17 15:12:49 +02:00 committed by GitHub
parent 06d32baea4
commit d98bd418b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,6 +19,7 @@ from homeassistant.const import (
UNIT_PERCENTAGE, UNIT_PERCENTAGE,
) )
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -104,7 +105,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
add_entities([EnOceanWindowHandle(dev_id, dev_name)]) add_entities([EnOceanWindowHandle(dev_id, dev_name)])
class EnOceanSensor(enocean.EnOceanDevice): class EnOceanSensor(enocean.EnOceanDevice, RestoreEntity):
"""Representation of an EnOcean sensor device such as a power meter.""" """Representation of an EnOcean sensor device such as a power meter."""
def __init__(self, dev_id, dev_name, sensor_type): def __init__(self, dev_id, dev_name, sensor_type):
@ -142,6 +143,17 @@ class EnOceanSensor(enocean.EnOceanDevice):
"""Return the unit of measurement.""" """Return the unit of measurement."""
return self._unit_of_measurement return self._unit_of_measurement
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
# If not None, we got an initial value.
await super().async_added_to_hass()
if self._state is not None:
return
state = await self.async_get_last_state()
if state is not None:
self._state = state.state
def value_changed(self, packet): def value_changed(self, packet):
"""Update the internal state of the sensor.""" """Update the internal state of the sensor."""