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:
Joakim Plate
2020-07-12 22:03:22 +02:00
committed by GitHub
parent 16a947aa5f
commit 53844488d8
13 changed files with 430 additions and 900 deletions

View File

@@ -1,23 +1,19 @@
"""Support for RFXtrx sensors."""
import logging
from RFXtrx import SensorEvent
import voluptuous as vol
from RFXtrx import ControlEvent, SensorEvent
from homeassistant.components.sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TEMPERATURE,
PLATFORM_SCHEMA,
)
from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES, CONF_NAME
import homeassistant.helpers.config_validation as cv
from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES
from homeassistant.helpers.entity import Entity
from . import (
CONF_AUTOMATIC_ADD,
CONF_DATA_TYPE,
CONF_FIRE_EVENT,
DATA_TYPES,
SIGNAL_EVENT,
@@ -27,24 +23,6 @@ from . import (
_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_FIRE_EVENT, default=False): cv.boolean,
vol.Optional(CONF_DATA_TYPE, default=[]): vol.All(
cv.ensure_list, [vol.In(DATA_TYPES.keys())]
),
}
)
},
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
},
extra=vol.ALLOW_EXTRA,
)
def _battery_convert(value):
"""Battery is given as a value between 0 and 9."""
@@ -76,43 +54,23 @@ CONVERT_FUNCTIONS = {
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the RFXtrx platform."""
if discovery_info is None:
return
data_ids = set()
def supported(event):
return isinstance(event, (ControlEvent, SensorEvent))
entities = []
for packet_id, entity_info in config[CONF_DEVICES].items():
for packet_id, entity_info 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):
continue
if entity_info[CONF_DATA_TYPE]:
data_types = entity_info[CONF_DATA_TYPE]
else:
data_types = list(set(event.values) & set(DATA_TYPES))
device_id = get_device_id(event.device)
for data_type in data_types:
data_id = (*device_id, data_type)
if data_id in data_ids:
continue
data_ids.add(data_id)
entity = RfxtrxSensor(
event.device,
entity_info[CONF_NAME],
data_type,
entity_info[CONF_FIRE_EVENT],
)
entities.append(entity)
add_entities(entities)
def sensor_update(event):
"""Handle sensor updates from the RFXtrx gateway."""
if not isinstance(event, SensorEvent):
return
pkt_id = "".join(f"{x:02x}" for x in event.data)
device_id = get_device_id(event.device)
for data_type in set(event.values) & set(DATA_TYPES):
data_id = (*device_id, data_type)
@@ -120,29 +78,49 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
continue
data_ids.add(data_id)
_LOGGER.debug(
"Added sensor (Device ID: %s Class: %s Sub: %s)",
entity = RfxtrxSensor(
event.device, data_type, entity_info[CONF_FIRE_EVENT],
)
entities.append(entity)
add_entities(entities)
def sensor_update(event):
"""Handle sensor updates from the RFXtrx gateway."""
if not supported(event):
return
device_id = get_device_id(event.device)
for data_type in set(event.values) & set(DATA_TYPES):
data_id = (*device_id, data_type)
if data_id in data_ids:
continue
data_ids.add(data_id)
_LOGGER.info(
"Added 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),
)
entity = RfxtrxSensor(event.device, pkt_id, data_type, event=event)
entity = RfxtrxSensor(event.device, data_type, event=event)
add_entities([entity])
# Subscribe to main RFXtrx events
if config[CONF_AUTOMATIC_ADD]:
if discovery_info[CONF_AUTOMATIC_ADD]:
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, sensor_update)
class RfxtrxSensor(Entity):
"""Representation of a RFXtrx sensor."""
def __init__(self, device, name, data_type, should_fire_event=False, event=None):
def __init__(self, device, data_type, should_fire_event=False, event=None):
"""Initialize the sensor."""
self.event = None
self._device = device
self._name = name
self._name = f"{device.type_string} {device.id_string} {data_type}"
self.should_fire_event = should_fire_event
self.data_type = data_type
self._unit_of_measurement = DATA_TYPES.get(data_type, "")
@@ -180,7 +158,7 @@ class RfxtrxSensor(Entity):
@property
def name(self):
"""Get the name of the sensor."""
return f"{self._name} {self.data_type}"
return self._name
@property
def device_state_attributes(self):