From c16f107f6b03f9c000414a84ce56eb51070ced3c Mon Sep 17 00:00:00 2001 From: Rob Bierbooms Date: Tue, 13 Oct 2020 13:51:42 +0200 Subject: [PATCH] Filter rfxtrx configure devices option flow on existing config entry (#40975) * Prompt only configure devices when device is in config entry * Fix copy --- homeassistant/components/rfxtrx/__init__.py | 2 ++ .../components/rfxtrx/config_flow.py | 20 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 22cc3ea4c9a..9c0615fdb5e 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -2,6 +2,7 @@ import asyncio import binascii from collections import OrderedDict +import copy import logging import RFXtrx as rfxtrxmod @@ -304,6 +305,7 @@ async def async_setup_internal(hass, entry: config_entries.ConfigEntry): config[CONF_DEVICE_ID] = device_id data = entry.data.copy() + data[CONF_DEVICES] = copy.deepcopy(entry.data[CONF_DEVICES]) event_code = binascii.hexlify(event.data).decode("ASCII") data[CONF_DEVICES][event_code] = config hass.config_entries.async_update_entry(entry=entry, data=data) diff --git a/homeassistant/components/rfxtrx/config_flow.py b/homeassistant/components/rfxtrx/config_flow.py index db7ca49691a..e1d30405b15 100644 --- a/homeassistant/components/rfxtrx/config_flow.py +++ b/homeassistant/components/rfxtrx/config_flow.py @@ -109,7 +109,8 @@ class OptionsFlow(config_entries.OptionsFlow): f"{DOMAIN}_{CONF_REMOVE_DEVICE}_{device_id}" ) self._device_registry.async_remove_device(entry_id) - devices[event_code] = None + if event_code is not None: + devices[event_code] = None self.update_config_data( global_options=self._global_options, devices=devices @@ -142,19 +143,25 @@ class OptionsFlow(config_entries.OptionsFlow): self._device_registry = device_registry self._device_entries = device_entries - devices = { + remove_devices = { entry.id: entry.name_by_user if entry.name_by_user else entry.name for entry in device_entries } + configure_devices = { + entry.id: entry.name_by_user if entry.name_by_user else entry.name + for entry in device_entries + if self._get_device_event_code(entry.id) is not None + } + options = { vol.Optional( CONF_AUTOMATIC_ADD, default=self._config_entry.data[CONF_AUTOMATIC_ADD], ): bool, vol.Optional(CONF_EVENT_CODE): str, - vol.Optional(CONF_DEVICE): vol.In(devices), - vol.Optional(CONF_REMOVE_DEVICE): cv.multi_select(devices), + vol.Optional(CONF_DEVICE): vol.In(configure_devices), + vol.Optional(CONF_REMOVE_DEVICE): cv.multi_select(remove_devices), } return self.async_show_form( @@ -366,6 +373,11 @@ class OptionsFlow(config_entries.OptionsFlow): return False + def _get_device_event_code(self, entry_id): + data = self._get_device_data(entry_id) + + return data[CONF_EVENT_CODE] + def _get_device_data(self, entry_id): """Get event code based on device identifier.""" event_code = None