From c6213b36ad0da3a885fdee0c0dd648804bab9f13 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 29 Jul 2021 21:08:53 +0200 Subject: [PATCH] Only disable a device if all associated config entries are disabled (#53681) --- homeassistant/helpers/device_registry.py | 11 +++++++ tests/helpers/test_device_registry.py | 42 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 9f09bbbf642..b22b1740a4f 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -670,6 +670,7 @@ def async_config_entry_disabled_by_changed( the config entry is disabled, enable devices in the registry that are associated with a config entry when the config entry is enabled and the devices are marked DISABLED_CONFIG_ENTRY. + Only disable a device if all associated config entries are disabled. """ devices = async_entries_for_config_entry(registry, config_entry.entry_id) @@ -681,10 +682,20 @@ def async_config_entry_disabled_by_changed( registry.async_update_device(device.id, disabled_by=None) return + enabled_config_entries = { + entry.entry_id + for entry in registry.hass.config_entries.async_entries() + if not entry.disabled_by + } + for device in devices: if device.disabled: # Device already disabled, do not overwrite continue + if len(device.config_entries) > 1 and device.config_entries.intersection( + enabled_config_entries + ): + continue registry.async_update_device(device.id, disabled_by=DISABLED_CONFIG_ENTRY) diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index 037e1aec8c2..557647c5c7f 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -1253,3 +1253,45 @@ async def test_disable_config_entry_disables_devices(hass, registry): entry2 = registry.async_get(entry2.id) assert entry2.disabled assert entry2.disabled_by == device_registry.DISABLED_USER + + +async def test_only_disable_device_if_all_config_entries_are_disabled(hass, registry): + """Test that we only disable device if all related config entries are disabled.""" + config_entry1 = MockConfigEntry(domain="light") + config_entry1.add_to_hass(hass) + config_entry2 = MockConfigEntry(domain="light") + config_entry2.add_to_hass(hass) + + registry.async_get_or_create( + config_entry_id=config_entry1.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + entry1 = registry.async_get_or_create( + config_entry_id=config_entry2.entry_id, + connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + ) + assert len(entry1.config_entries) == 2 + assert not entry1.disabled + + await hass.config_entries.async_set_disabled_by( + config_entry1.entry_id, config_entries.DISABLED_USER + ) + await hass.async_block_till_done() + + entry1 = registry.async_get(entry1.id) + assert not entry1.disabled + + await hass.config_entries.async_set_disabled_by( + config_entry2.entry_id, config_entries.DISABLED_USER + ) + await hass.async_block_till_done() + + entry1 = registry.async_get(entry1.id) + assert entry1.disabled + assert entry1.disabled_by == device_registry.DISABLED_CONFIG_ENTRY + + await hass.config_entries.async_set_disabled_by(config_entry1.entry_id, None) + await hass.async_block_till_done() + + entry1 = registry.async_get(entry1.id) + assert not entry1.disabled