Switch rfxtrx to integration level config (#37742)
* Switch to integration level config * Switch to per device config rather than per entity type * All roller shutters should be added as covers (there are non lighting types) * Fixup tests that used invalid packets for platforms * Avoid variable re-use * Allow control events on sensors too That way we get signal level sensors for these too * Lint correction * Don't filter sensors from config Disable sensors from GUI if the entities are not wanted * Correct usage of ATTR_ instead of CONF_ * Make sure the logging when a new entity is added includes the event
This commit is contained in:
parent
16a947aa5f
commit
53844488d8
13 changed files with 430 additions and 900 deletions
|
@ -2,21 +2,15 @@
|
|||
import logging
|
||||
|
||||
import RFXtrx as rfxtrxmod
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASSES_SCHEMA,
|
||||
PLATFORM_SCHEMA,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.const import (
|
||||
CONF_COMMAND_OFF,
|
||||
CONF_COMMAND_ON,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_DEVICES,
|
||||
CONF_NAME,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv, event as evt
|
||||
from homeassistant.helpers import event as evt
|
||||
|
||||
from . import (
|
||||
CONF_AUTOMATIC_ADD,
|
||||
|
@ -36,29 +30,6 @@ from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTIN
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_DEVICES, default={}): {
|
||||
cv.string: vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
||||
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
|
||||
vol.Optional(CONF_OFF_DELAY): vol.Any(
|
||||
cv.time_period, cv.positive_timedelta
|
||||
),
|
||||
vol.Optional(CONF_DATA_BITS): cv.positive_int,
|
||||
vol.Optional(CONF_COMMAND_ON): cv.byte,
|
||||
vol.Optional(CONF_COMMAND_OFF): cv.byte,
|
||||
}
|
||||
)
|
||||
},
|
||||
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
def _get_device_data_bits(device, device_bits):
|
||||
"""Deduce data bits for device based on a cache of device bits."""
|
||||
data_bits = None
|
||||
|
@ -72,6 +43,9 @@ def _get_device_data_bits(device, device_bits):
|
|||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Binary Sensor platform to RFXtrx."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
sensors = []
|
||||
|
||||
device_ids = set()
|
||||
|
@ -79,11 +53,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
|
||||
pt2262_devices = []
|
||||
|
||||
for packet_id, entity in config[CONF_DEVICES].items():
|
||||
def supported(event):
|
||||
return isinstance(event, rfxtrxmod.ControlEvent)
|
||||
|
||||
for packet_id, entity in discovery_info[CONF_DEVICES].items():
|
||||
event = get_rfx_object(packet_id)
|
||||
if event is None:
|
||||
_LOGGER.error("Invalid device: %s", packet_id)
|
||||
continue
|
||||
if not supported(event):
|
||||
return
|
||||
|
||||
device_id = get_device_id(event.device, data_bits=entity.get(CONF_DATA_BITS))
|
||||
if device_id in device_ids:
|
||||
|
@ -96,7 +75,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
|
||||
device = RfxtrxBinarySensor(
|
||||
event.device,
|
||||
entity.get(CONF_NAME),
|
||||
entity.get(CONF_DEVICE_CLASS),
|
||||
entity[CONF_FIRE_EVENT],
|
||||
entity.get(CONF_OFF_DELAY),
|
||||
|
@ -110,7 +88,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
|
||||
def binary_sensor_update(event):
|
||||
"""Call for control updates from the RFXtrx gateway."""
|
||||
if not isinstance(event, rfxtrxmod.ControlEvent):
|
||||
if not supported(event):
|
||||
return
|
||||
|
||||
data_bits = _get_device_data_bits(event.device, device_bits)
|
||||
|
@ -121,19 +99,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
device_ids.add(device_id)
|
||||
|
||||
_LOGGER.info(
|
||||
"Added binary sensor (Device ID: %s Class: %s Sub: %s)",
|
||||
"Added binary sensor (Device ID: %s Class: %s Sub: %s Event: %s)",
|
||||
event.device.id_string.lower(),
|
||||
event.device.__class__.__name__,
|
||||
event.device.subtype,
|
||||
"".join(f"{x:02x}" for x in event.data),
|
||||
)
|
||||
pkt_id = "".join(f"{x:02x}" for x in event.data)
|
||||
sensor = RfxtrxBinarySensor(
|
||||
event.device, pkt_id, data_bits=data_bits, event=event
|
||||
)
|
||||
sensor = RfxtrxBinarySensor(event.device, data_bits=data_bits, event=event)
|
||||
add_entities([sensor])
|
||||
|
||||
# Subscribe to main RFXtrx events
|
||||
if config[CONF_AUTOMATIC_ADD]:
|
||||
if discovery_info[CONF_AUTOMATIC_ADD]:
|
||||
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, binary_sensor_update)
|
||||
|
||||
|
||||
|
@ -143,7 +119,6 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
|||
def __init__(
|
||||
self,
|
||||
device,
|
||||
name,
|
||||
device_class=None,
|
||||
should_fire=False,
|
||||
off_delay=None,
|
||||
|
@ -155,7 +130,7 @@ class RfxtrxBinarySensor(BinarySensorEntity):
|
|||
"""Initialize the RFXtrx sensor."""
|
||||
self.event = None
|
||||
self._device = device
|
||||
self._name = name
|
||||
self._name = f"{device.type_string} {device.id_string}"
|
||||
self._should_fire_event = should_fire
|
||||
self._device_class = device_class
|
||||
self._off_delay = off_delay
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue