mirror of
https://github.com/home-assistant/core.git
synced 2025-04-28 11:17:53 +00: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:
parent
769cf19052
commit
77978a979b
@ -3,8 +3,9 @@ import RFXtrx as rfxtrxmod
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.cover import PLATFORM_SCHEMA, CoverDevice
|
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 import config_validation as cv
|
||||||
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
CONF_AUTOMATIC_ADD,
|
CONF_AUTOMATIC_ADD,
|
||||||
@ -62,9 +63,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
RECEIVED_EVT_SUBSCRIBERS.append(cover_update)
|
RECEIVED_EVT_SUBSCRIBERS.append(cover_update)
|
||||||
|
|
||||||
|
|
||||||
class RfxtrxCover(RfxtrxDevice, CoverDevice):
|
class RfxtrxCover(RfxtrxDevice, CoverDevice, RestoreEntity):
|
||||||
"""Representation of a RFXtrx cover."""
|
"""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
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
"""Return the polling state. No polling available in RFXtrx cover."""
|
"""Return the polling state. No polling available in RFXtrx cover."""
|
||||||
|
@ -10,8 +10,9 @@ from homeassistant.components.light import (
|
|||||||
SUPPORT_BRIGHTNESS,
|
SUPPORT_BRIGHTNESS,
|
||||||
Light,
|
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 import config_validation as cv
|
||||||
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
CONF_AUTOMATIC_ADD,
|
CONF_AUTOMATIC_ADD,
|
||||||
@ -72,14 +73,37 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
RECEIVED_EVT_SUBSCRIBERS.append(light_update)
|
RECEIVED_EVT_SUBSCRIBERS.append(light_update)
|
||||||
|
|
||||||
|
|
||||||
class RfxtrxLight(RfxtrxDevice, Light):
|
class RfxtrxLight(RfxtrxDevice, Light, RestoreEntity):
|
||||||
"""Representation of a RFXtrx light."""
|
"""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
|
@property
|
||||||
def brightness(self):
|
def brightness(self):
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
return self._brightness
|
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
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
|
@ -5,8 +5,9 @@ import RFXtrx as rfxtrxmod
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
|
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 import config_validation as cv
|
||||||
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
CONF_AUTOMATIC_ADD,
|
CONF_AUTOMATIC_ADD,
|
||||||
@ -67,9 +68,17 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
|||||||
RECEIVED_EVT_SUBSCRIBERS.append(switch_update)
|
RECEIVED_EVT_SUBSCRIBERS.append(switch_update)
|
||||||
|
|
||||||
|
|
||||||
class RfxtrxSwitch(RfxtrxDevice, SwitchDevice):
|
class RfxtrxSwitch(RfxtrxDevice, SwitchDevice, RestoreEntity):
|
||||||
"""Representation of a RFXtrx switch."""
|
"""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):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
self._send_command("turn_on")
|
self._send_command("turn_on")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user