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:
1 parent
16a947aa5f
commit
53844488d8
13 files changed
+430
-900
No files matched your search
@@ -6,10 +6,15 @@ import logging
|
||||
import RFXtrx as rfxtrxmod
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_STATE,
|
||||
CONF_COMMAND_OFF,
|
||||
CONF_COMMAND_ON,
|
||||
CONF_DEVICE,
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_DEVICES,
|
||||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
@@ -20,6 +25,7 @@ from homeassistant.const import (
|
||||
UV_INDEX,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.discovery import load_platform
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import slugify
|
||||
|
||||
@@ -29,15 +35,9 @@ DOMAIN = "rfxtrx"
|
||||
|
||||
DEFAULT_SIGNAL_REPETITIONS = 1
|
||||
|
||||
ATTR_AUTOMATIC_ADD = "automatic_add"
|
||||
ATTR_DEVICE = "device"
|
||||
ATTR_DEBUG = "debug"
|
||||
ATTR_FIRE_EVENT = "fire_event"
|
||||
ATTR_DATA_TYPE = "data_type"
|
||||
ATTR_DUMMY = "dummy"
|
||||
CONF_DATA_BITS = "data_bits"
|
||||
CONF_AUTOMATIC_ADD = "automatic_add"
|
||||
CONF_DATA_TYPE = "data_type"
|
||||
CONF_SIGNAL_REPETITIONS = "signal_repetitions"
|
||||
CONF_FIRE_EVENT = "fire_event"
|
||||
CONF_DUMMY = "dummy"
|
||||
@@ -83,10 +83,25 @@ DATA_TYPES = OrderedDict(
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DATA_RFXOBJECT = "rfxobject"
|
||||
|
||||
|
||||
DEVICE_SCHEMA = vol.Schema(
|
||||
{
|
||||
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_SIGNAL_REPETITIONS, default=1): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
BASE_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_DEBUG, default=False): cv.boolean,
|
||||
vol.Optional(CONF_DUMMY, default=False): cv.boolean,
|
||||
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
||||
vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -121,11 +136,11 @@ def setup(hass, config):
|
||||
# Callback to HA registered components.
|
||||
hass.helpers.dispatcher.dispatcher_send(SIGNAL_EVENT, event)
|
||||
|
||||
device = config[DOMAIN].get(ATTR_DEVICE)
|
||||
device = config[DOMAIN].get(CONF_DEVICE)
|
||||
host = config[DOMAIN].get(CONF_HOST)
|
||||
port = config[DOMAIN].get(CONF_PORT)
|
||||
debug = config[DOMAIN][ATTR_DEBUG]
|
||||
dummy_connection = config[DOMAIN][ATTR_DUMMY]
|
||||
debug = config[DOMAIN][CONF_DEBUG]
|
||||
dummy_connection = config[DOMAIN][CONF_DUMMY]
|
||||
|
||||
if dummy_connection:
|
||||
rfx_object = rfxtrxmod.Connect(
|
||||
@@ -154,6 +169,13 @@ def setup(hass, config):
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown_rfxtrx)
|
||||
|
||||
hass.data[DATA_RFXOBJECT] = rfx_object
|
||||
|
||||
load_platform(hass, "switch", DOMAIN, config[DOMAIN], config)
|
||||
load_platform(hass, "light", DOMAIN, config[DOMAIN], config)
|
||||
load_platform(hass, "cover", DOMAIN, config[DOMAIN], config)
|
||||
load_platform(hass, "binary_sensor", DOMAIN, config[DOMAIN], config)
|
||||
load_platform(hass, "sensor", DOMAIN, config[DOMAIN], config)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -265,10 +287,10 @@ class RfxtrxDevice(Entity):
|
||||
Contains the common logic for Rfxtrx lights and switches.
|
||||
"""
|
||||
|
||||
def __init__(self, name, device, datas, signal_repetitions, event=None):
|
||||
def __init__(self, device, datas, signal_repetitions, event=None):
|
||||
"""Initialize the device."""
|
||||
self.signal_repetitions = signal_repetitions
|
||||
self._name = name
|
||||
self._name = f"{device.type_string} {device.id_string}"
|
||||
self._device = device
|
||||
self._state = datas[ATTR_STATE]
|
||||
self._should_fire_event = datas[ATTR_FIRE_EVENT]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
"""Support for RFXtrx covers."""
|
||||
import logging
|
||||
|
||||
import RFXtrx as rfxtrxmod
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.cover import PLATFORM_SCHEMA, CoverEntity
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, CONF_NAME, STATE_OPEN
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.components.cover import CoverEntity
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_OPEN
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from . import (
|
||||
@@ -23,36 +19,27 @@ from . import (
|
||||
)
|
||||
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_DEVICES, default={}): {
|
||||
cv.string: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
},
|
||||
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
||||
vol.Optional(
|
||||
CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
|
||||
): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the RFXtrx cover."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
device_ids = set()
|
||||
|
||||
def supported(event):
|
||||
return event.device.known_to_be_rollershutter
|
||||
|
||||
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
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
if device_id in device_ids:
|
||||
@@ -60,20 +47,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
device_ids.add(device_id)
|
||||
|
||||
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]}
|
||||
entity = RfxtrxCover(
|
||||
entity_info[CONF_NAME], event.device, datas, config[CONF_SIGNAL_REPETITIONS]
|
||||
)
|
||||
entity = RfxtrxCover(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS])
|
||||
entities.append(entity)
|
||||
|
||||
add_entities(entities)
|
||||
|
||||
def cover_update(event):
|
||||
"""Handle cover updates from the RFXtrx gateway."""
|
||||
if (
|
||||
not isinstance(event.device, rfxtrxmod.LightingDevice)
|
||||
or event.device.known_to_be_dimmable
|
||||
or not event.device.known_to_be_rollershutter
|
||||
):
|
||||
if not supported(event):
|
||||
return
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
@@ -82,21 +63,21 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
device_ids.add(device_id)
|
||||
|
||||
_LOGGER.info(
|
||||
"Added cover (Device ID: %s Class: %s Sub: %s)",
|
||||
"Added cover (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)
|
||||
datas = {ATTR_STATE: False, ATTR_FIRE_EVENT: False}
|
||||
entity = RfxtrxCover(
|
||||
pkt_id, event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event
|
||||
event.device, datas, DEFAULT_SIGNAL_REPETITIONS, 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, cover_update)
|
||||
|
||||
|
||||
|
||||
@@ -2,16 +2,13 @@
|
||||
import logging
|
||||
|
||||
import RFXtrx as rfxtrxmod
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
PLATFORM_SCHEMA,
|
||||
SUPPORT_BRIGHTNESS,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, CONF_NAME, STATE_ON
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_ON
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from . import (
|
||||
@@ -30,37 +27,31 @@ from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_DEVICES, default={}): {
|
||||
cv.string: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
},
|
||||
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
||||
vol.Optional(
|
||||
CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
|
||||
): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
|
||||
SUPPORT_RFXTRX = SUPPORT_BRIGHTNESS
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the RFXtrx platform."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
device_ids = set()
|
||||
|
||||
def supported(event):
|
||||
return (
|
||||
isinstance(event.device, rfxtrxmod.LightingDevice)
|
||||
and event.device.known_to_be_dimmable
|
||||
)
|
||||
|
||||
# Add switch from config file
|
||||
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):
|
||||
return
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
if device_id in device_ids:
|
||||
@@ -68,9 +59,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
device_ids.add(device_id)
|
||||
|
||||
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]}
|
||||
entity = RfxtrxLight(
|
||||
entity_info[CONF_NAME], event.device, datas, config[CONF_SIGNAL_REPETITIONS]
|
||||
)
|
||||
entity = RfxtrxLight(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS])
|
||||
|
||||
entities.append(entity)
|
||||
|
||||
@@ -78,10 +67,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
|
||||
def light_update(event):
|
||||
"""Handle light updates from the RFXtrx gateway."""
|
||||
if (
|
||||
not isinstance(event.device, rfxtrxmod.LightingDevice)
|
||||
or not event.device.known_to_be_dimmable
|
||||
):
|
||||
if not supported(event):
|
||||
return
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
@@ -89,23 +75,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
return
|
||||
device_ids.add(device_id)
|
||||
|
||||
_LOGGER.debug(
|
||||
"Added light (Device ID: %s Class: %s Sub: %s)",
|
||||
_LOGGER.info(
|
||||
"Added light (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)
|
||||
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: False}
|
||||
entity = RfxtrxLight(
|
||||
pkt_id, event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event
|
||||
event.device, datas, DEFAULT_SIGNAL_REPETITIONS, 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, light_update)
|
||||
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -2,11 +2,9 @@
|
||||
import logging
|
||||
|
||||
import RFXtrx as rfxtrxmod
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, CONF_NAME, STATE_ON
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.const import ATTR_STATE, CONF_DEVICES, STATE_ON
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from . import (
|
||||
@@ -15,6 +13,7 @@ from . import (
|
||||
CONF_FIRE_EVENT,
|
||||
CONF_SIGNAL_REPETITIONS,
|
||||
DEFAULT_SIGNAL_REPETITIONS,
|
||||
DOMAIN,
|
||||
SIGNAL_EVENT,
|
||||
RfxtrxDevice,
|
||||
fire_command_event,
|
||||
@@ -23,37 +22,34 @@ from . import (
|
||||
)
|
||||
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DATA_SWITCH = f"{DOMAIN}_switch"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_DEVICES, default={}): {
|
||||
cv.string: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_FIRE_EVENT, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
},
|
||||
vol.Optional(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
|
||||
vol.Optional(
|
||||
CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS
|
||||
): vol.Coerce(int),
|
||||
}
|
||||
)
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
||||
"""Set up the RFXtrx platform."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
device_ids = set()
|
||||
|
||||
def supported(event):
|
||||
return (
|
||||
isinstance(event.device, rfxtrxmod.LightingDevice)
|
||||
and not event.device.known_to_be_dimmable
|
||||
and not event.device.known_to_be_rollershutter
|
||||
)
|
||||
|
||||
# Add switch from config file
|
||||
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
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
if device_id in device_ids:
|
||||
@@ -61,20 +57,14 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
||||
device_ids.add(device_id)
|
||||
|
||||
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: entity_info[CONF_FIRE_EVENT]}
|
||||
entity = RfxtrxSwitch(
|
||||
entity_info[CONF_NAME], event.device, datas, config[CONF_SIGNAL_REPETITIONS]
|
||||
)
|
||||
entity = RfxtrxSwitch(event.device, datas, entity_info[CONF_SIGNAL_REPETITIONS])
|
||||
entities.append(entity)
|
||||
|
||||
add_entities_callback(entities)
|
||||
|
||||
def switch_update(event):
|
||||
"""Handle sensor updates from the RFXtrx gateway."""
|
||||
if (
|
||||
not isinstance(event.device, rfxtrxmod.LightingDevice)
|
||||
or event.device.known_to_be_dimmable
|
||||
or event.device.known_to_be_rollershutter
|
||||
):
|
||||
if not supported(event):
|
||||
return
|
||||
|
||||
device_id = get_device_id(event.device)
|
||||
@@ -83,21 +73,21 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
||||
device_ids.add(device_id)
|
||||
|
||||
_LOGGER.info(
|
||||
"Added switch (Device ID: %s Class: %s Sub: %s)",
|
||||
"Added switch (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)
|
||||
datas = {ATTR_STATE: None, ATTR_FIRE_EVENT: False}
|
||||
entity = RfxtrxSwitch(
|
||||
pkt_id, event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event
|
||||
event.device, datas, DEFAULT_SIGNAL_REPETITIONS, event=event
|
||||
)
|
||||
add_entities_callback([entity])
|
||||
|
||||
# Subscribe to main RFXtrx events
|
||||
if config[CONF_AUTOMATIC_ADD]:
|
||||
if discovery_info[CONF_AUTOMATIC_ADD]:
|
||||
hass.helpers.dispatcher.dispatcher_connect(SIGNAL_EVENT, switch_update)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user