From 70a771b6ed00795ffe860f0c8754f2b7a345aa2d Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Tue, 22 Mar 2022 04:40:33 +0100 Subject: [PATCH] Respect disable_new_entities for new device_tracker entities (#68148) --- .../components/device_tracker/config_entry.py | 12 ++++++- .../device_tracker/test_config_entry.py | 33 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index adabd297c55..c9b8534c2bc 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -149,9 +149,19 @@ def _async_register_mac( return # Make sure entity has a config entry and was disabled by the - # default disable logic in the integration. + # default disable logic in the integration and new entities + # are allowed to be added. if ( entity_entry.config_entry_id is None + or ( + ( + config_entry := hass.config_entries.async_get_entry( + entity_entry.config_entry_id + ) + ) + is not None + and config_entry.pref_disable_new_entities + ) or entity_entry.disabled_by != er.RegistryEntryDisabler.INTEGRATION ): return diff --git a/tests/components/device_tracker/test_config_entry.py b/tests/components/device_tracker/test_config_entry.py index 5134123074e..73b07d31026 100644 --- a/tests/components/device_tracker/test_config_entry.py +++ b/tests/components/device_tracker/test_config_entry.py @@ -137,6 +137,39 @@ async def test_register_mac(hass): assert entity_entry_1.disabled_by is None +async def test_register_mac_ignored(hass): + """Test ignoring registering a mac.""" + dev_reg = dr.async_get(hass) + ent_reg = er.async_get(hass) + + config_entry = MockConfigEntry(domain="test", pref_disable_new_entities=True) + config_entry.add_to_hass(hass) + + mac1 = "12:34:56:AB:CD:EF" + + entity_entry_1 = ent_reg.async_get_or_create( + "device_tracker", + "test", + mac1 + "yo1", + original_name="name 1", + config_entry=config_entry, + disabled_by=er.RegistryEntryDisabler.INTEGRATION, + ) + + ce._async_register_mac(hass, "test", mac1, mac1 + "yo1") + + dev_reg.async_get_or_create( + config_entry_id=config_entry.entry_id, + connections={(dr.CONNECTION_NETWORK_MAC, mac1)}, + ) + + await hass.async_block_till_done() + + entity_entry_1 = ent_reg.async_get(entity_entry_1.entity_id) + + assert entity_entry_1.disabled_by == er.RegistryEntryDisabler.INTEGRATION + + async def test_connected_device_registered(hass): """Test dispatch on connected device being registered."""