1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00

Restore state for Rfxtrx devices (#30309)

* Restore state rfxtrx switch

* Restore state RFXtrx lights

* Restore state RFXtrx covers

* Restore comment

* Remove line

* Remove logging

* fix black

* Fix typo
This commit is contained in:
Ernst Klamer 2020-01-02 07:59:13 +01:00 committed by Daniel Høyer Iversen
parent 769cf19052
commit 77978a979b
3 changed files with 48 additions and 6 deletions

View File

@ -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."""

View File

@ -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."""

View File

@ -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")