1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00

Only disable a device if all associated config entries are disabled (#53681)

This commit is contained in:
Robert Svensson 2021-07-29 21:08:53 +02:00 committed by GitHub
parent 204426009f
commit c6213b36ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -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)

View File

@ -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