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 authored and Martin Hjelmare committed 2018-10-15 15:35:33 +02:00
1 parent 0904ff45fe
commit 2ceb4d2d1e
5 files changed
+168 -184

No files matched your search

+16 -50
View File
@@ -7,18 +7,16 @@ https://home-assistant.io/components/light.rflink/
import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS, Light)
from homeassistant.components.rflink import (
CONF_ALIASES, CONF_ALIASSES, CONF_AUTOMATIC_ADD, CONF_DEVICE_DEFAULTS,
CONF_DEVICES, CONF_FIRE_EVENT, CONF_GROUP, CONF_GROUP_ALIASES,
CONF_GROUP_ALIASSES, CONF_IGNORE_DEVICES, CONF_NOGROUP_ALIASES,
CONF_NOGROUP_ALIASSES, CONF_SIGNAL_REPETITIONS, DATA_DEVICE_REGISTER,
DATA_ENTITY_GROUP_LOOKUP, DATA_ENTITY_LOOKUP, DEVICE_DEFAULTS_SCHEMA,
DOMAIN, EVENT_KEY_COMMAND, EVENT_KEY_ID, SwitchableRflinkDevice, cv,
DEVICE_DEFAULTS_SCHEMA,
EVENT_KEY_COMMAND, EVENT_KEY_ID, SwitchableRflinkDevice, cv,
remove_deprecated, vol)
from homeassistant.const import (
CONF_NAME, CONF_PLATFORM, CONF_TYPE, STATE_UNKNOWN)
from homeassistant.helpers.deprecation import get_deprecated
from homeassistant.const import (CONF_NAME, CONF_TYPE)
DEPENDENCIES = ['rflink']
@@ -29,14 +27,13 @@ TYPE_SWITCHABLE = 'switchable'
TYPE_HYBRID = 'hybrid'
TYPE_TOGGLE = 'toggle'
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): DOMAIN,
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_IGNORE_DEVICES): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_DEVICE_DEFAULTS, default=DEVICE_DEFAULTS_SCHEMA({})):
DEVICE_DEFAULTS_SCHEMA,
DEVICE_DEFAULTS_SCHEMA,
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.Optional(CONF_TYPE):
vol.Any(TYPE_DIMMABLE, TYPE_SWITCHABLE,
@@ -57,9 +54,9 @@ PLATFORM_SCHEMA = vol.Schema({
vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_NOGROUP_ALIASSES):
vol.All(cv.ensure_list, [cv.string]),
},
}),
})
})
},
}, extra=vol.ALLOW_EXTRA)
def entity_type_for_device_id(device_id):
@@ -98,7 +95,7 @@ def entity_class_for_type(entity_type):
return entity_device_mapping.get(entity_type, RflinkLight)
def devices_from_config(domain_config, hass=None):
def devices_from_config(domain_config):
"""Parse configuration and add Rflink light devices."""
devices = []
for device_id, config in domain_config[CONF_DEVICES].items():
@@ -124,40 +121,16 @@ def devices_from_config(domain_config, hass=None):
"repetitions. Please set 'dimmable' or 'switchable' "
"type explicitly in configuration", device_id)
device = entity_class(device_id, hass, **device_config)
device = entity_class(device_id, **device_config)
devices.append(device)
# Register entity (and aliases) to listen to incoming rflink events
# Device id and normal aliases respond to normal and group command
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_COMMAND][device_id].append(device)
if config[CONF_GROUP]:
hass.data[DATA_ENTITY_GROUP_LOOKUP][
EVENT_KEY_COMMAND][device_id].append(device)
for _id in get_deprecated(config, CONF_ALIASES, CONF_ALIASSES):
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_COMMAND][_id].append(device)
hass.data[DATA_ENTITY_GROUP_LOOKUP][
EVENT_KEY_COMMAND][_id].append(device)
# group_aliases only respond to group commands
for _id in get_deprecated(
config, CONF_GROUP_ALIASES, CONF_GROUP_ALIASSES):
hass.data[DATA_ENTITY_GROUP_LOOKUP][
EVENT_KEY_COMMAND][_id].append(device)
# nogroup_aliases only respond to normal commands
for _id in get_deprecated(
config, CONF_NOGROUP_ALIASES, CONF_NOGROUP_ALIASSES):
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_COMMAND][_id].append(device)
return devices
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Rflink light 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 add to list of known devices."""
@@ -167,16 +140,9 @@ async def async_setup_platform(hass, config, async_add_entities,
entity_class = entity_class_for_type(entity_type)
device_config = config[CONF_DEVICE_DEFAULTS]
device = entity_class(device_id, hass, **device_config)
device = entity_class(device_id, initial_event=event, **device_config)
async_add_entities([device])
# Register entity to listen to incoming Rflink events
hass.data[DATA_ENTITY_LOOKUP][
EVENT_KEY_COMMAND][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_COMMAND] = add_new_device
@@ -277,7 +243,7 @@ class ToggleRflinkLight(SwitchableRflinkDevice, Light):
if command == 'on':
# if the state is unknown or false, it gets set as true
# if the state is true, it gets set as false
self._state = self._state in [STATE_UNKNOWN, False]
self._state = self._state in [None, False]
async def async_turn_on(self, **kwargs):
"""Turn the device on."""