1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Improve check of new_entity_id in entity_registry.async_update_entity (#78276)

* Improve check of new_entity_id in entity_registry.async_update_entity

* Fix race in rfxtrx config flow

* Make sure Event is created on time

* Rename poorly named variable

* Fix typing

* Correct typing of _handle_state_change
This commit is contained in:
Erik Montnemery
2022-09-28 08:43:58 +02:00
committed by GitHub
parent 69bf77be12
commit c38b1e7727
4 changed files with 111 additions and 22 deletions

View File

@@ -6,7 +6,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import EVENT_HOMEASSISTANT_START, STATE_UNAVAILABLE
from homeassistant.core import CoreState, callback
from homeassistant.core import CoreState, HomeAssistant, callback
from homeassistant.exceptions import MaxLengthExceeded
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory
@@ -597,6 +597,56 @@ async def test_update_entity_unique_id_conflict(registry):
assert registry.async_get_entity_id("light", "hue", "1234") == entry2.entity_id
async def test_update_entity_entity_id(registry):
"""Test entity's entity_id is updated."""
entry = registry.async_get_or_create("light", "hue", "5678")
assert registry.async_get_entity_id("light", "hue", "5678") == entry.entity_id
new_entity_id = "light.blah"
assert new_entity_id != entry.entity_id
with patch.object(registry, "async_schedule_save") as mock_schedule_save:
updated_entry = registry.async_update_entity(
entry.entity_id, new_entity_id=new_entity_id
)
assert updated_entry != entry
assert updated_entry.entity_id == new_entity_id
assert mock_schedule_save.call_count == 1
assert registry.async_get(entry.entity_id) is None
assert registry.async_get(new_entity_id) is not None
async def test_update_entity_entity_id_entity_id(hass: HomeAssistant, registry):
"""Test update raises when entity_id already in use."""
entry = registry.async_get_or_create("light", "hue", "5678")
entry2 = registry.async_get_or_create("light", "hue", "1234")
state_entity_id = "light.blah"
hass.states.async_set(state_entity_id, "on")
assert entry.entity_id != state_entity_id
assert entry2.entity_id != state_entity_id
# Try updating to a registered entity_id
with patch.object(
registry, "async_schedule_save"
) as mock_schedule_save, pytest.raises(ValueError):
registry.async_update_entity(entry.entity_id, new_entity_id=entry2.entity_id)
assert mock_schedule_save.call_count == 0
assert registry.async_get_entity_id("light", "hue", "5678") == entry.entity_id
assert registry.async_get(entry.entity_id) is entry
assert registry.async_get_entity_id("light", "hue", "1234") == entry2.entity_id
assert registry.async_get(entry2.entity_id) is entry2
# Try updating to an entity_id which is in the state machine
with patch.object(
registry, "async_schedule_save"
) as mock_schedule_save, pytest.raises(ValueError):
registry.async_update_entity(entry.entity_id, new_entity_id=state_entity_id)
assert mock_schedule_save.call_count == 0
assert registry.async_get_entity_id("light", "hue", "5678") == entry.entity_id
assert registry.async_get(entry.entity_id) is entry
assert registry.async_get(state_entity_id) is None
async def test_update_entity(registry):
"""Test updating entity."""
mock_config = MockConfigEntry(domain="light", entry_id="mock-id-1")