Filter rfxtrx configure devices option flow on existing config entry (#40975)

* Prompt only configure devices when device is in config entry

* Fix copy
This commit is contained in:
Rob Bierbooms 2020-10-13 13:51:42 +02:00 committed by GitHub
parent b6855816ed
commit c16f107f6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View file

@ -2,6 +2,7 @@
import asyncio import asyncio
import binascii import binascii
from collections import OrderedDict from collections import OrderedDict
import copy
import logging import logging
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
@ -304,6 +305,7 @@ async def async_setup_internal(hass, entry: config_entries.ConfigEntry):
config[CONF_DEVICE_ID] = device_id config[CONF_DEVICE_ID] = device_id
data = entry.data.copy() data = entry.data.copy()
data[CONF_DEVICES] = copy.deepcopy(entry.data[CONF_DEVICES])
event_code = binascii.hexlify(event.data).decode("ASCII") event_code = binascii.hexlify(event.data).decode("ASCII")
data[CONF_DEVICES][event_code] = config data[CONF_DEVICES][event_code] = config
hass.config_entries.async_update_entry(entry=entry, data=data) hass.config_entries.async_update_entry(entry=entry, data=data)

View file

@ -109,7 +109,8 @@ class OptionsFlow(config_entries.OptionsFlow):
f"{DOMAIN}_{CONF_REMOVE_DEVICE}_{device_id}" f"{DOMAIN}_{CONF_REMOVE_DEVICE}_{device_id}"
) )
self._device_registry.async_remove_device(entry_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( self.update_config_data(
global_options=self._global_options, devices=devices global_options=self._global_options, devices=devices
@ -142,19 +143,25 @@ class OptionsFlow(config_entries.OptionsFlow):
self._device_registry = device_registry self._device_registry = device_registry
self._device_entries = device_entries self._device_entries = device_entries
devices = { remove_devices = {
entry.id: entry.name_by_user if entry.name_by_user else entry.name entry.id: entry.name_by_user if entry.name_by_user else entry.name
for entry in device_entries 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 = { options = {
vol.Optional( vol.Optional(
CONF_AUTOMATIC_ADD, CONF_AUTOMATIC_ADD,
default=self._config_entry.data[CONF_AUTOMATIC_ADD], default=self._config_entry.data[CONF_AUTOMATIC_ADD],
): bool, ): bool,
vol.Optional(CONF_EVENT_CODE): str, vol.Optional(CONF_EVENT_CODE): str,
vol.Optional(CONF_DEVICE): vol.In(devices), vol.Optional(CONF_DEVICE): vol.In(configure_devices),
vol.Optional(CONF_REMOVE_DEVICE): cv.multi_select(devices), vol.Optional(CONF_REMOVE_DEVICE): cv.multi_select(remove_devices),
} }
return self.async_show_form( return self.async_show_form(
@ -366,6 +373,11 @@ class OptionsFlow(config_entries.OptionsFlow):
return False 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): def _get_device_data(self, entry_id):
"""Get event code based on device identifier.""" """Get event code based on device identifier."""
event_code = None event_code = None