diff --git a/homeassistant/components/rfxtrx/cover.py b/homeassistant/components/rfxtrx/cover.py index 4806fd9a6b7..e1eb6ae77f5 100644 --- a/homeassistant/components/rfxtrx/cover.py +++ b/homeassistant/components/rfxtrx/cover.py @@ -3,8 +3,9 @@ import RFXtrx as rfxtrxmod import voluptuous as vol from homeassistant.components.cover import PLATFORM_SCHEMA, CoverDevice -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, STATE_OPEN from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.restore_state import RestoreEntity from . import ( CONF_AUTOMATIC_ADD, @@ -62,9 +63,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None): RECEIVED_EVT_SUBSCRIBERS.append(cover_update) -class RfxtrxCover(RfxtrxDevice, CoverDevice): +class RfxtrxCover(RfxtrxDevice, CoverDevice, RestoreEntity): """Representation of a RFXtrx cover.""" + async def async_added_to_hass(self): + """Restore RFXtrx cover device state (OPEN/CLOSE).""" + await super().async_added_to_hass() + + old_state = await self.async_get_last_state() + if old_state is not None: + self._state = old_state.state == STATE_OPEN + @property def should_poll(self): """Return the polling state. No polling available in RFXtrx cover.""" diff --git a/homeassistant/components/rfxtrx/light.py b/homeassistant/components/rfxtrx/light.py index a29b8bfa660..9c0157870cb 100644 --- a/homeassistant/components/rfxtrx/light.py +++ b/homeassistant/components/rfxtrx/light.py @@ -10,8 +10,9 @@ from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, Light, ) -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, STATE_ON from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.restore_state import RestoreEntity from . import ( CONF_AUTOMATIC_ADD, @@ -72,14 +73,37 @@ def setup_platform(hass, config, add_entities, discovery_info=None): RECEIVED_EVT_SUBSCRIBERS.append(light_update) -class RfxtrxLight(RfxtrxDevice, Light): +class RfxtrxLight(RfxtrxDevice, Light, RestoreEntity): """Representation of a RFXtrx light.""" + async def async_added_to_hass(self): + """Restore RFXtrx device state (ON/OFF).""" + await super().async_added_to_hass() + + old_state = await self.async_get_last_state() + if old_state is not None: + self._state = old_state.state == STATE_ON + + # Restore the brightness of dimmable devices + if ( + old_state is not None + and old_state.attributes.get(ATTR_BRIGHTNESS) is not None + ): + self._brightness = int(old_state.attributes[ATTR_BRIGHTNESS]) + @property def brightness(self): """Return the brightness of this light between 0..255.""" return self._brightness + @property + def device_state_attributes(self): + """Return the device state attributes.""" + attr = {} + if self._brightness is not None: + attr[ATTR_BRIGHTNESS] = self._brightness + return attr + @property def supported_features(self): """Flag supported features.""" diff --git a/homeassistant/components/rfxtrx/switch.py b/homeassistant/components/rfxtrx/switch.py index 49bcd1b3924..05e4a37ab44 100644 --- a/homeassistant/components/rfxtrx/switch.py +++ b/homeassistant/components/rfxtrx/switch.py @@ -5,8 +5,9 @@ import RFXtrx as rfxtrxmod import voluptuous as vol from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, STATE_ON from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.restore_state import RestoreEntity from . import ( CONF_AUTOMATIC_ADD, @@ -67,9 +68,17 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None): RECEIVED_EVT_SUBSCRIBERS.append(switch_update) -class RfxtrxSwitch(RfxtrxDevice, SwitchDevice): +class RfxtrxSwitch(RfxtrxDevice, SwitchDevice, RestoreEntity): """Representation of a RFXtrx switch.""" + async def async_added_to_hass(self): + """Restore RFXtrx switch device state (ON/OFF).""" + await super().async_added_to_hass() + + old_state = await self.async_get_last_state() + if old_state is not None: + self._state = old_state.state == STATE_ON + def turn_on(self, **kwargs): """Turn the device on.""" self._send_command("turn_on")