Rewrite rfxtrx init logic to do away with global object (#37699)

* Rewrite init logic to do away with global object

* Put constant at end

* Use a set instead of list for device_ids
This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-10 14:52:07 +02:00
1 parent b45a952d61
commit 67038c6ba8
11 files changed
+365 -334

No files matched your search

+16 -73
View File
@@ -8,10 +8,8 @@ import voluptuous as vol
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_NAME,
ATTR_STATE,
CONF_DEVICE,
CONF_DEVICES,
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_START,
@@ -25,6 +23,8 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
from .const import DEVICE_PACKET_TYPE_LIGHTING4
DOMAIN = "rfxtrx"
DEFAULT_SIGNAL_REPETITIONS = 1
@@ -80,7 +80,6 @@ DATA_TYPES = OrderedDict(
]
)
RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__)
DATA_RFXOBJECT = "rfxobject"
@@ -207,31 +206,15 @@ def get_pt2262_cmd(device_id, data_bits):
return hex(data[-1] & mask)
def get_pt2262_device(device_id):
def find_possible_pt2262_device(device_ids, device_id):
"""Look for the device which id matches the given device_id parameter."""
for device in RFX_DEVICES.values():
if (
hasattr(device, "is_lighting4")
and device.masked_id is not None
and device.masked_id == get_pt2262_deviceid(device_id, device.data_bits)
):
_LOGGER.debug(
"rfxtrx: found matching device %s for %s", device_id, device.masked_id,
)
return device
return None
def find_possible_pt2262_device(device_id):
"""Look for the device which id matches the given device_id parameter."""
for dev_id, device in RFX_DEVICES.items():
if hasattr(device, "is_lighting4") and len(dev_id) == len(device_id):
for dev_id in device_ids:
if len(dev_id) == len(device_id):
size = None
for i, (char1, char2) in enumerate(zip(dev_id, device_id)):
if char1 != char2:
break
size = i
if size is not None:
size = len(dev_id) - size - 1
_LOGGER.info(
@@ -246,60 +229,19 @@ def find_possible_pt2262_device(device_id):
dev_id[-size:],
device_id[-size:],
)
return device
return dev_id
return None
def get_devices_from_config(config, device):
"""Read rfxtrx configuration."""
signal_repetitions = config[CONF_SIGNAL_REPETITIONS]
def get_device_id(device, data_bits=None):
"""Calculate a device id for device."""
id_string = device.id_string
if data_bits and device.packettype == DEVICE_PACKET_TYPE_LIGHTING4:
masked_id = get_pt2262_deviceid(id_string, data_bits)
if masked_id:
id_string = str(masked_id)
devices = []
for packet_id, entity_info in config[CONF_DEVICES].items():
event = get_rfx_object(packet_id)
if event is None:
_LOGGER.error("Invalid device: %s", packet_id)
continue
device_id = slugify(event.device.id_string.lower())
if device_id in RFX_DEVICES:
continue
_LOGGER.debug("Add %s rfxtrx", entity_info[ATTR_NAME])
# Check if i must fire event
fire_event = entity_info[ATTR_FIRE_EVENT]
datas = {ATTR_STATE: False, ATTR_FIRE_EVENT: fire_event}
new_device = device(
entity_info[ATTR_NAME], event.device, datas, signal_repetitions
)
RFX_DEVICES[device_id] = new_device
devices.append(new_device)
return devices
def get_new_device(event, config, device):
"""Add entity if not exist and the automatic_add is True."""
device_id = slugify(event.device.id_string.lower())
if device_id in RFX_DEVICES:
return
if not config[ATTR_AUTOMATIC_ADD]:
return
pkt_id = "".join(f"{x:02x}" for x in event.data)
_LOGGER.debug(
"Automatic add %s rfxtrx device (Class: %s Sub: %s Packet_id: %s)",
device_id,
event.device.__class__.__name__,
event.device.subtype,
pkt_id,
)
datas = {ATTR_STATE: False, ATTR_FIRE_EVENT: False}
signal_repetitions = config[CONF_SIGNAL_REPETITIONS]
new_device = device(pkt_id, event.device, datas, signal_repetitions, event=event)
RFX_DEVICES[device_id] = new_device
return new_device
return (f"{device.packettype:x}", f"{device.subtype:x}", id_string)
def fire_command_event(hass, entity_id, command):
@@ -330,7 +272,8 @@ class RfxtrxDevice(Entity):
self._device = device
self._state = datas[ATTR_STATE]
self._should_fire_event = datas[ATTR_FIRE_EVENT]
self._unique_id = f"{device.packettype:x}_{device.subtype:x}_{device.id_string}"
self._device_id = get_device_id(device)
self._unique_id = "_".join(x for x in self._device_id)
if event:
self._apply_event(event)