From b2fd8836c91c328fb8e0bbee7c38aec8dbcf883e Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 27 Jun 2024 08:23:40 +0200 Subject: [PATCH] Enable disabled devices when adding new config entry --- homeassistant/helpers/device_registry.py | 6 ++++ tests/helpers/test_device_registry.py | 36 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index cfafa63ec3a..3d0d17e99f4 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -926,6 +926,12 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]): if add_config_entry.entry_id not in old.config_entries: config_entries = old.config_entries | {add_config_entry.entry_id} + if ( + old.disabled_by + and not add_config_entry.disabled_by + and disabled_by is UNDEFINED + ): + disabled_by = None if ( remove_config_entry_id is not UNDEFINED diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index fa57cc7557e..63664bd44bc 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -3052,3 +3052,39 @@ async def test_primary_config_entry( model="model", ) assert device.primary_config_entry == mock_config_entry_1.entry_id + + +@pytest.mark.parametrize("disabled_by", list(dr.DeviceEntryDisabler)) +async def test_add_config_entry_to_disabled_device( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + disabled_by: dr.DeviceEntryDisabler, +) -> None: + """Test adding config entry to a disabled device.""" + mock_config_entry_1 = MockConfigEntry(title=None) + mock_config_entry_1.add_to_hass(hass) + mock_config_entry_2 = MockConfigEntry( + disabled_by=config_entries.ConfigEntryDisabler.USER, title=None + ) + mock_config_entry_2.add_to_hass(hass) + mock_config_entry_3 = MockConfigEntry(title=None) + mock_config_entry_3.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry_1.entry_id, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + disabled_by=disabled_by, + ) + assert device.disabled_by == disabled_by + + device = device_registry.async_update_device( + device.id, add_config_entry_id=mock_config_entry_2.entry_id + ) + assert device + assert device.disabled_by == disabled_by + + device = device_registry.async_update_device( + device.id, add_config_entry_id=mock_config_entry_3.entry_id + ) + assert device + assert device.disabled_by is None