Rfxtrx fixup restore (#38039)

This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-22 15:27:04 +02:00
1 parent 0b32caa71f
commit 66e490e55f
11 files changed
+204 -186

No files matched your search

+27 -19
View File
@@ -23,6 +23,7 @@ from homeassistant.const import (
UNIT_PERCENTAGE,
UV_INDEX,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
@@ -343,35 +344,30 @@ def get_device_id(device, data_bits=None):
return (f"{device.packettype:x}", f"{device.subtype:x}", id_string)
class RfxtrxDevice(RestoreEntity):
class RfxtrxEntity(RestoreEntity):
"""Represents a Rfxtrx device.
Contains the common logic for Rfxtrx lights and switches.
"""
def __init__(self, device, device_id, signal_repetitions, event=None):
def __init__(self, device, device_id, event=None):
"""Initialize the device."""
self.signal_repetitions = signal_repetitions
self._name = f"{device.type_string} {device.id_string}"
self._device = device
self._event = None
self._state = None
self._event = event
self._device_id = device_id
self._unique_id = "_".join(x for x in self._device_id)
if event:
self._apply_event(event)
async def async_added_to_hass(self):
"""Restore RFXtrx device state (ON/OFF)."""
if self._event:
return
self._apply_event(self._event)
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(
SIGNAL_EVENT, self._handle_event
)
)
@property
def should_poll(self):
@@ -390,11 +386,6 @@ class RfxtrxDevice(RestoreEntity):
return None
return {ATTR_EVENT: "".join(f"{x:02x}" for x in self._event.data)}
@property
def is_on(self):
"""Return true if device is on."""
return self._state
@property
def assumed_state(self):
"""Return true if unable to access real state of entity."""
@@ -418,6 +409,23 @@ class RfxtrxDevice(RestoreEntity):
"""Apply a received event."""
self._event = event
@callback
def _handle_event(self, event, device_id):
"""Handle a reception of data, overridden by other classes."""
class RfxtrxCommandEntity(RfxtrxEntity):
"""Represents a Rfxtrx device.
Contains the common logic for Rfxtrx lights and switches.
"""
def __init__(self, device, device_id, signal_repetitions=1, event=None):
"""Initialzie a switch or light device."""
super().__init__(device, device_id, event=event)
self.signal_repetitions = signal_repetitions
self._state = None
def _send_command(self, command, brightness=0):
rfx_object = self.hass.data[DATA_RFXOBJECT]