Refactor RFLink component (#17402)

* Start refactor of RFLink component

* alias _id not added correctly

Aliases for sensor not added correctly
And some debug traces.

* Update rflink.py

* Cleanup, fix review comments

* Call event handlers directly when processing initial event

* Use hass.async_create_task when adding discovered device

* Review comments

* Review comments
This commit is contained in:
emontnemery
2018-10-15 15:35:33 +02:00
committed by Martin Hjelmare
parent 0904ff45fe
commit 2ceb4d2d1e
5 changed files with 168 additions and 184 deletions

View File

@@ -4,16 +4,18 @@ Support for Rflink sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.rflink/
"""
from functools import partial
import logging
from homeassistant.components.rflink import (
CONF_ALIASES, CONF_ALIASSES, CONF_AUTOMATIC_ADD, CONF_DEVICES,
DATA_DEVICE_REGISTER, DATA_ENTITY_LOOKUP, DOMAIN, EVENT_KEY_ID,
EVENT_KEY_SENSOR, EVENT_KEY_UNIT, RflinkDevice, cv, remove_deprecated, vol)
DATA_DEVICE_REGISTER, DATA_ENTITY_LOOKUP, EVENT_KEY_ID,
EVENT_KEY_SENSOR, EVENT_KEY_UNIT, RflinkDevice, cv, remove_deprecated, vol,
SIGNAL_AVAILABILITY, SIGNAL_HANDLE_EVENT, TMP_ENTITY)
from homeassistant.components.sensor import (
PLATFORM_SCHEMA)
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, CONF_PLATFORM,
CONF_UNIT_OF_MEASUREMENT)
ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, CONF_UNIT_OF_MEASUREMENT)
from homeassistant.helpers.dispatcher import (async_dispatcher_connect)
DEPENDENCIES = ['rflink']
@@ -27,11 +29,10 @@ SENSOR_ICONS = {
CONF_SENSOR_TYPE = 'sensor_type'
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): DOMAIN,
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_AUTOMATIC_ADD, default=True): cv.boolean,
vol.Optional(CONF_DEVICES, default={}): vol.Schema({
cv.string: {
vol.Optional(CONF_DEVICES, default={}): {
cv.string: vol.Schema({
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_SENSOR_TYPE): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
@@ -40,9 +41,9 @@ PLATFORM_SCHEMA = vol.Schema({
# deprecated config options
vol.Optional(CONF_ALIASSES):
vol.All(cv.ensure_list, [cv.string]),
},
}),
})
})
},
}, extra=vol.ALLOW_EXTRA)
def lookup_unit_for_sensor_type(sensor_type):
@@ -56,7 +57,7 @@ def lookup_unit_for_sensor_type(sensor_type):
return UNITS.get(field_abbrev.get(sensor_type))
def devices_from_config(domain_config, hass=None):
def devices_from_config(domain_config):
"""Parse configuration and add Rflink sensor devices."""
devices = []
for device_id, config in domain_config[CONF_DEVICES].items():
@@ -64,41 +65,26 @@ def devices_from_config(domain_config, hass=None):
config[ATTR_UNIT_OF_MEASUREMENT] = lookup_unit_for_sensor_type(
config[CONF_SENSOR_TYPE])
remove_deprecated(config)
device = RflinkSensor(device_id, hass, **config)
device = RflinkSensor(device_id, **config)
devices.append(device)
# Register entity (and aliases) to listen to incoming rflink events
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][device_id].append(device)
aliases = config.get(CONF_ALIASES)
if aliases:
for _id in aliases:
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][_id].append(device)
return devices
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Rflink platform."""
async_add_entities(devices_from_config(config, hass))
async_add_entities(devices_from_config(config))
async def add_new_device(event):
"""Check if device is known, otherwise create device entity."""
device_id = event[EVENT_KEY_ID]
rflinksensor = partial(RflinkSensor, device_id, hass)
device = rflinksensor(event[EVENT_KEY_SENSOR], event[EVENT_KEY_UNIT])
device = RflinkSensor(device_id, event[EVENT_KEY_SENSOR],
event[EVENT_KEY_UNIT], initial_event=event)
# Add device entity
async_add_entities([device])
# Register entity to listen to incoming rflink events
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][device_id].append(device)
# Schedule task to process event after entity is created
hass.async_add_job(device.handle_event, event)
if config[CONF_AUTOMATIC_ADD]:
hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_SENSOR] = add_new_device
@@ -106,17 +92,43 @@ async def async_setup_platform(hass, config, async_add_entities,
class RflinkSensor(RflinkDevice):
"""Representation of a Rflink sensor."""
def __init__(self, device_id, hass, sensor_type, unit_of_measurement,
**kwargs):
def __init__(self, device_id, sensor_type, unit_of_measurement,
initial_event=None, **kwargs):
"""Handle sensor specific args and super init."""
self._sensor_type = sensor_type
self._unit_of_measurement = unit_of_measurement
super().__init__(device_id, hass, **kwargs)
super().__init__(device_id, initial_event=initial_event, **kwargs)
def _handle_event(self, event):
"""Domain specific event handler."""
self._state = event['value']
async def async_added_to_hass(self):
"""Register update callback."""
# Remove temporary bogus entity_id if added
tmp_entity = TMP_ENTITY.format(self._device_id)
if tmp_entity in self.hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][self._device_id]:
self.hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][self._device_id].remove(tmp_entity)
# Register id and aliases
self.hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][self._device_id].append(self.entity_id)
if self._aliases:
for _id in self._aliases:
self.hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_SENSOR][_id].append(self.entity_id)
async_dispatcher_connect(self.hass, SIGNAL_AVAILABILITY,
self._availability_callback)
async_dispatcher_connect(self.hass,
SIGNAL_HANDLE_EVENT.format(self.entity_id),
self.handle_event_callback)
# Process the initial event now that the entity is created
if self._initial_event:
self.handle_event_callback(self._initial_event)
@property
def unit_of_measurement(self):
"""Return measurement unit."""