Make sure command entities restore from state (#38038)

This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-22 00:01:31 +02:00
1 parent ad5d7ee615
commit 945acb4e29
11 files changed
+182 -11

No files matched your search

@@ -398,12 +398,6 @@ class RfxtrxEntity(RestoreEntity):
"""Restore RFXtrx device state (ON/OFF)."""
if self._event:
self._apply_event(self._event)
else:
old_state = await self.async_get_last_state()
if old_state is not None:
event = old_state.attributes.get(ATTR_EVENT)
if event:
self._apply_event(get_rfx_object(event))
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
@@ -24,7 +24,12 @@ from . import (
get_pt2262_cmd,
get_rfx_object,
)
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTING4
from .const import (
ATTR_EVENT,
COMMAND_OFF_LIST,
COMMAND_ON_LIST,
DEVICE_PACKET_TYPE_LIGHTING4,
)
_LOGGER = logging.getLogger(__name__)
@@ -124,6 +129,17 @@ class RfxtrxBinarySensor(RfxtrxEntity, BinarySensorEntity):
self._cmd_on = cmd_on
self._cmd_off = cmd_off
async def async_added_to_hass(self):
"""Restore device state."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
event = old_state.attributes.get(ATTR_EVENT)
if event:
self._apply_event(get_rfx_object(event))
@property
def force_update(self) -> bool:
"""We should force updates. Repeated states have meaning."""
+10 -1
View File
@@ -2,7 +2,7 @@
import logging
from homeassistant.components.cover import CoverEntity
from homeassistant.const import CONF_DEVICES
from homeassistant.const import CONF_DEVICES, STATE_OPEN
from homeassistant.core import callback
from . import (
@@ -81,6 +81,15 @@ async def async_setup_entry(
class RfxtrxCover(RfxtrxCommandEntity, CoverEntity):
"""Representation of a RFXtrx cover."""
async def async_added_to_hass(self):
"""Restore device state."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
self._state = old_state.state == STATE_OPEN
@property
def is_closed(self):
"""Return if the cover is closed."""
+11 -1
View File
@@ -8,7 +8,7 @@ from homeassistant.components.light import (
SUPPORT_BRIGHTNESS,
LightEntity,
)
from homeassistant.const import CONF_DEVICES
from homeassistant.const import CONF_DEVICES, STATE_ON
from homeassistant.core import callback
from . import (
@@ -97,6 +97,16 @@ class RfxtrxLight(RfxtrxCommandEntity, LightEntity):
_brightness = 0
async def async_added_to_hass(self):
"""Restore RFXtrx device state (ON/OFF)."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
self._state = old_state.state == STATE_ON
self._brightness = old_state.attributes.get(ATTR_BRIGHTNESS)
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
+12
View File
@@ -20,6 +20,7 @@ from . import (
get_device_id,
get_rfx_object,
)
from .const import ATTR_EVENT
_LOGGER = logging.getLogger(__name__)
@@ -125,6 +126,17 @@ class RfxtrxSensor(RfxtrxEntity):
self._device_class = DEVICE_CLASSES.get(data_type)
self._convert_fun = CONVERT_FUNCTIONS.get(data_type, lambda x: x)
async def async_added_to_hass(self):
"""Restore device state."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
event = old_state.attributes.get(ATTR_EVENT)
if event:
self._apply_event(get_rfx_object(event))
@property
def state(self):
"""Return the state of the sensor."""
+10 -1
View File
@@ -4,7 +4,7 @@ import logging
import RFXtrx as rfxtrxmod
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import CONF_DEVICES
from homeassistant.const import CONF_DEVICES, STATE_ON
from homeassistant.core import callback
from . import (
@@ -91,6 +91,15 @@ async def async_setup_entry(
class RfxtrxSwitch(RfxtrxCommandEntity, SwitchEntity):
"""Representation of a RFXtrx switch."""
async def async_added_to_hass(self):
"""Restore device state."""
await super().async_added_to_hass()
if self._event is None:
old_state = await self.async_get_last_state()
if old_state is not None:
self._state = old_state.state == STATE_ON
def _apply_event(self, event):
"""Apply command from rfxtrx."""
super()._apply_event(event)