Add restore last state for EnOcean Sensors (#34375)
This commit is contained in:
parent
06d32baea4
commit
d98bd418b1
1 changed files with 13 additions and 1 deletions
|
@ -19,6 +19,7 @@ from homeassistant.const import (
|
|||
UNIT_PERCENTAGE,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_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)])
|
||||
|
||||
|
||||
class EnOceanSensor(enocean.EnOceanDevice):
|
||||
class EnOceanSensor(enocean.EnOceanDevice, RestoreEntity):
|
||||
"""Representation of an EnOcean sensor device such as a power meter."""
|
||||
|
||||
def __init__(self, dev_id, dev_name, sensor_type):
|
||||
|
@ -142,6 +143,17 @@ class EnOceanSensor(enocean.EnOceanDevice):
|
|||
"""Return the 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):
|
||||
"""Update the internal state of the sensor."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue