Reload config entry when entity enabled in entity registry, remove entity if disabled. (#26120)
* Reload config entry when disabled_by updated in entity registry * Add types * Remove entities that get disabled * Remove unnecessary domain checks. * Attach handler in async_setup * Remove unused var * Type * Fix test * Fix tests
This commit is contained in:
1 parent
a4eeaac24c
commit
f704a8e90e
7 files changed
+219
-12
No files matched your search
@@ -163,6 +163,7 @@ async def test_update_entity(hass, client):
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert hass.states.get("test_domain.world") is None
|
||||
assert registry.entities["test_domain.world"].disabled_by == "user"
|
||||
|
||||
# UPDATE DISABLED_BY TO NONE
|
||||
|
||||
@@ -526,3 +526,34 @@ async def test_warn_disabled(hass, caplog):
|
||||
ent.async_write_ha_state()
|
||||
assert hass.states.get("hello.world") is None
|
||||
assert caplog.text == ""
|
||||
|
||||
|
||||
async def test_disabled_in_entity_registry(hass):
|
||||
"""Test entity is removed if we disable entity registry entry."""
|
||||
entry = entity_registry.RegistryEntry(
|
||||
entity_id="hello.world",
|
||||
unique_id="test-unique-id",
|
||||
platform="test-platform",
|
||||
disabled_by="user",
|
||||
)
|
||||
registry = mock_registry(hass, {"hello.world": entry})
|
||||
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.entity_id = "hello.world"
|
||||
ent.registry_entry = entry
|
||||
ent.platform = MagicMock(platform_name="test-platform")
|
||||
|
||||
await ent.async_internal_added_to_hass()
|
||||
ent.async_write_ha_state()
|
||||
assert hass.states.get("hello.world") is None
|
||||
|
||||
entry2 = registry.async_update_entity("hello.world", disabled_by=None)
|
||||
await hass.async_block_till_done()
|
||||
assert entry2 != entry
|
||||
assert ent.registry_entry == entry2
|
||||
|
||||
entry3 = registry.async_update_entity("hello.world", disabled_by="user")
|
||||
await hass.async_block_till_done()
|
||||
assert entry3 != entry2
|
||||
assert ent.registry_entry == entry3
|
||||
@@ -219,6 +219,7 @@ async def test_updating_config_entry_id(hass, registry, update_events):
|
||||
assert update_events[0]["entity_id"] == entry.entity_id
|
||||
assert update_events[1]["action"] == "update"
|
||||
assert update_events[1]["entity_id"] == entry.entity_id
|
||||
assert update_events[1]["changes"] == ["config_entry_id"]
|
||||
|
||||
|
||||
async def test_removing_config_entry_id(hass, registry, update_events):
|
||||
|
||||
@@ -20,6 +20,7 @@ from tests.common import (
|
||||
MockEntity,
|
||||
mock_integration,
|
||||
mock_entity_platform,
|
||||
mock_registry,
|
||||
)
|
||||
|
||||
|
||||
@@ -925,3 +926,78 @@ async def test_init_custom_integration(hass):
|
||||
return_value=mock_coro(integration),
|
||||
):
|
||||
await hass.config_entries.flow.async_init("bla")
|
||||
|
||||
|
||||
async def test_support_entry_unload(hass):
|
||||
"""Test unloading entry."""
|
||||
assert await config_entries.support_entry_unload(hass, "light")
|
||||
assert not await config_entries.support_entry_unload(hass, "auth")
|
||||
|
||||
|
||||
async def test_reload_entry_entity_registry_ignores_no_entry(hass):
|
||||
"""Test reloading entry in entity registry skips if no config entry linked."""
|
||||
handler = config_entries.EntityRegistryDisabledHandler(hass)
|
||||
registry = mock_registry(hass)
|
||||
|
||||
# Test we ignore entities without config entry
|
||||
entry = registry.async_get_or_create("light", "hue", "123")
|
||||
registry.async_update_entity(entry.entity_id, disabled_by="user")
|
||||
await hass.async_block_till_done()
|
||||
assert not handler.changed
|
||||
assert handler._remove_call_later is None
|
||||
|
||||
|
||||
async def test_reload_entry_entity_registry_works(hass):
|
||||
"""Test we schedule an entry to be reloaded if disabled_by is updated."""
|
||||
handler = config_entries.EntityRegistryDisabledHandler(hass)
|
||||
handler.async_setup()
|
||||
registry = mock_registry(hass)
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain="comp", state=config_entries.ENTRY_STATE_LOADED
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
mock_setup_entry = MagicMock(return_value=mock_coro(True))
|
||||
mock_unload_entry = MagicMock(return_value=mock_coro(True))
|
||||
mock_integration(
|
||||
hass,
|
||||
MockModule(
|
||||
"comp",
|
||||
async_setup_entry=mock_setup_entry,
|
||||
async_unload_entry=mock_unload_entry,
|
||||
),
|
||||
)
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
# Only changing disabled_by should update trigger
|
||||
entity_entry = registry.async_get_or_create(
|
||||
"light", "hue", "123", config_entry=config_entry
|
||||
)
|
||||
registry.async_update_entity(entity_entry.entity_id, name="yo")
|
||||
await hass.async_block_till_done()
|
||||
assert not handler.changed
|
||||
assert handler._remove_call_later is None
|
||||
|
||||
# Disable entity, we should not do anything, only act when enabled.
|
||||
registry.async_update_entity(entity_entry.entity_id, disabled_by="user")
|
||||
await hass.async_block_till_done()
|
||||
assert not handler.changed
|
||||
assert handler._remove_call_later is None
|
||||
|
||||
# Enable entity, check we are reloading config entry.
|
||||
registry.async_update_entity(entity_entry.entity_id, disabled_by=None)
|
||||
await hass.async_block_till_done()
|
||||
assert handler.changed == {config_entry.entry_id}
|
||||
assert handler._remove_call_later is not None
|
||||
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
dt.utcnow()
|
||||
+ timedelta(
|
||||
seconds=config_entries.EntityRegistryDisabledHandler.RELOAD_AFTER_UPDATE_DELAY
|
||||
+ 1
|
||||
),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_unload_entry.mock_calls) == 1
|
||||
Reference in new issue
Block a user