From d9dba9142c81504c3b47155f4733e84c45af4f86 Mon Sep 17 00:00:00 2001 From: Rob Bierbooms Date: Tue, 21 Jul 2020 09:44:00 +0200 Subject: [PATCH] Move data on import in rfxtrx integration into ConfigEntry (#38022) * Move all data imported from yaml to ConfigEntry * Revert changes that prevent updating yaml entry * Cleanup code around time conversion --- homeassistant/components/rfxtrx/__init__.py | 27 +++++++++---------- .../components/rfxtrx/binary_sensor.py | 11 +++----- .../components/rfxtrx/config_flow.py | 1 - homeassistant/components/rfxtrx/const.py | 1 - homeassistant/components/rfxtrx/cover.py | 4 +-- homeassistant/components/rfxtrx/light.py | 4 +-- homeassistant/components/rfxtrx/sensor.py | 3 +-- homeassistant/components/rfxtrx/switch.py | 4 +-- 8 files changed, 23 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 10b036e9eb9..81b5cf93392 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -29,7 +29,6 @@ from homeassistant.helpers.restore_state import RestoreEntity from .const import ( ATTR_EVENT, - DATA_RFXTRX_CONFIG, DEVICE_PACKET_TYPE_LIGHTING4, EVENT_RFXTRX_EVENT, SERVICE_SEND, @@ -105,7 +104,9 @@ DEVICE_DATA_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_OFF_DELAY): vol.All( + cv.time_period, cv.positive_timedelta, lambda value: value.total_seconds() + ), vol.Optional(CONF_DATA_BITS): cv.positive_int, vol.Optional(CONF_COMMAND_ON): cv.byte, vol.Optional(CONF_COMMAND_OFF): cv.byte, @@ -135,21 +136,20 @@ CONFIG_SCHEMA = vol.Schema( async def async_setup(hass, config): """Set up the RFXtrx component.""" if DOMAIN not in config: - hass.data[DATA_RFXTRX_CONFIG] = BASE_SCHEMA({}) return True - hass.data[DATA_RFXTRX_CONFIG] = config[DOMAIN] + data = { + CONF_HOST: config[DOMAIN].get(CONF_HOST), + CONF_PORT: config[DOMAIN].get(CONF_PORT), + CONF_DEVICE: config[DOMAIN].get(CONF_DEVICE), + CONF_DEBUG: config[DOMAIN].get(CONF_DEBUG), + CONF_AUTOMATIC_ADD: config[DOMAIN].get(CONF_AUTOMATIC_ADD), + CONF_DEVICES: config[DOMAIN][CONF_DEVICES], + } hass.async_create_task( hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={ - CONF_HOST: config[DOMAIN].get(CONF_HOST), - CONF_PORT: config[DOMAIN].get(CONF_PORT), - CONF_DEVICE: config[DOMAIN].get(CONF_DEVICE), - CONF_DEBUG: config[DOMAIN][CONF_DEBUG], - }, + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=data, ) ) return True @@ -169,11 +169,10 @@ async def async_setup_entry(hass, entry: config_entries.ConfigEntry): def setup_internal(hass, config): """Set up the RFXtrx component.""" - # Setup some per device config device_events = set() device_bits = {} - for event_code, event_config in hass.data[DATA_RFXTRX_CONFIG][CONF_DEVICES].items(): + for event_code, event_config in config[CONF_DEVICES].items(): event = get_rfx_object(event_code) device_id = get_device_id( event.device, data_bits=event_config.get(CONF_DATA_BITS) diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index 20782766e29..82e8765fb49 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -24,12 +24,7 @@ from . import ( get_pt2262_cmd, get_rfx_object, ) -from .const import ( - COMMAND_OFF_LIST, - COMMAND_ON_LIST, - DATA_RFXTRX_CONFIG, - DEVICE_PACKET_TYPE_LIGHTING4, -) +from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTING4 _LOGGER = logging.getLogger(__name__) @@ -43,7 +38,7 @@ async def async_setup_entry( device_ids = set() pt2262_devices = [] - discovery_info = hass.data[DATA_RFXTRX_CONFIG] + discovery_info = config_entry.data def supported(event): return isinstance(event, rfxtrxmod.ControlEvent) @@ -197,5 +192,5 @@ class RfxtrxBinarySensor(RfxtrxEntity, BinarySensorEntity): self.async_write_ha_state() self._delay_listener = evt.async_call_later( - self.hass, self._off_delay.total_seconds(), off_delay_listener + self.hass, self._off_delay, off_delay_listener ) diff --git a/homeassistant/components/rfxtrx/config_flow.py b/homeassistant/components/rfxtrx/config_flow.py index 0bd8854ca41..0cdaa8146ec 100644 --- a/homeassistant/components/rfxtrx/config_flow.py +++ b/homeassistant/components/rfxtrx/config_flow.py @@ -16,7 +16,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): async def async_step_import(self, import_config=None): """Handle the initial step.""" - await self.async_set_unique_id(DOMAIN) self._abort_if_unique_id_configured(import_config) return self.async_create_entry(title="RFXTRX", data=import_config) diff --git a/homeassistant/components/rfxtrx/const.py b/homeassistant/components/rfxtrx/const.py index 7626c082f45..cd3c4b9a62e 100644 --- a/homeassistant/components/rfxtrx/const.py +++ b/homeassistant/components/rfxtrx/const.py @@ -21,5 +21,4 @@ SERVICE_SEND = "send" DEVICE_PACKET_TYPE_LIGHTING4 = 0x13 -DATA_RFXTRX_CONFIG = "rfxtrx_config" EVENT_RFXTRX_EVENT = "rfxtrx_event" diff --git a/homeassistant/components/rfxtrx/cover.py b/homeassistant/components/rfxtrx/cover.py index e8ed8498580..db3f5d38131 100644 --- a/homeassistant/components/rfxtrx/cover.py +++ b/homeassistant/components/rfxtrx/cover.py @@ -14,7 +14,7 @@ from . import ( get_device_id, get_rfx_object, ) -from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG +from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST _LOGGER = logging.getLogger(__name__) @@ -23,7 +23,7 @@ async def async_setup_entry( hass, config_entry, async_add_entities, ): """Set up config entry.""" - discovery_info = hass.data[DATA_RFXTRX_CONFIG] + discovery_info = config_entry.data device_ids = set() def supported(event): diff --git a/homeassistant/components/rfxtrx/light.py b/homeassistant/components/rfxtrx/light.py index 9a986b96bb9..81633f847c4 100644 --- a/homeassistant/components/rfxtrx/light.py +++ b/homeassistant/components/rfxtrx/light.py @@ -20,7 +20,7 @@ from . import ( get_device_id, get_rfx_object, ) -from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG +from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST _LOGGER = logging.getLogger(__name__) @@ -31,7 +31,7 @@ async def async_setup_entry( hass, config_entry, async_add_entities, ): """Set up config entry.""" - discovery_info = hass.data[DATA_RFXTRX_CONFIG] + discovery_info = config_entry.data device_ids = set() def supported(event): diff --git a/homeassistant/components/rfxtrx/sensor.py b/homeassistant/components/rfxtrx/sensor.py index 129e4f6f9d5..337af41940f 100644 --- a/homeassistant/components/rfxtrx/sensor.py +++ b/homeassistant/components/rfxtrx/sensor.py @@ -20,7 +20,6 @@ from . import ( get_device_id, get_rfx_object, ) -from .const import DATA_RFXTRX_CONFIG _LOGGER = logging.getLogger(__name__) @@ -57,7 +56,7 @@ async def async_setup_entry( hass, config_entry, async_add_entities, ): """Set up platform.""" - discovery_info = hass.data[DATA_RFXTRX_CONFIG] + discovery_info = config_entry.data data_ids = set() def supported(event): diff --git a/homeassistant/components/rfxtrx/switch.py b/homeassistant/components/rfxtrx/switch.py index 3b0290f5546..4b5c6919910 100644 --- a/homeassistant/components/rfxtrx/switch.py +++ b/homeassistant/components/rfxtrx/switch.py @@ -17,7 +17,7 @@ from . import ( get_device_id, get_rfx_object, ) -from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DATA_RFXTRX_CONFIG +from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST DATA_SWITCH = f"{DOMAIN}_switch" @@ -28,7 +28,7 @@ async def async_setup_entry( hass, config_entry, async_add_entities, ): """Set up config entry.""" - discovery_info = hass.data[DATA_RFXTRX_CONFIG] + discovery_info = config_entry.data device_ids = set() def supported(event):