Allow changing entity ID (#15637)
* Allow changing entity ID * Add support to websocket command * Address comments * Error handling
This commit is contained in:
1 parent
fbeaa57604
commit
d9cf8fcfe8
7 files changed
+206
-22
No files matched your search
@@ -54,8 +54,8 @@ async def test_get_entity(hass, client):
|
||||
}
|
||||
|
||||
|
||||
async def test_update_entity(hass, client):
|
||||
"""Test get entry."""
|
||||
async def test_update_entity_name(hass, client):
|
||||
"""Test updating entity name."""
|
||||
mock_registry(hass, {
|
||||
'test_domain.world': RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
@@ -92,7 +92,7 @@ async def test_update_entity(hass, client):
|
||||
|
||||
|
||||
async def test_update_entity_no_changes(hass, client):
|
||||
"""Test get entry."""
|
||||
"""Test update entity with no changes."""
|
||||
mock_registry(hass, {
|
||||
'test_domain.world': RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
@@ -129,7 +129,7 @@ async def test_update_entity_no_changes(hass, client):
|
||||
|
||||
|
||||
async def test_get_nonexisting_entity(client):
|
||||
"""Test get entry."""
|
||||
"""Test get entry with nonexisting entity."""
|
||||
await client.send_json({
|
||||
'id': 6,
|
||||
'type': 'config/entity_registry/get',
|
||||
@@ -141,7 +141,7 @@ async def test_get_nonexisting_entity(client):
|
||||
|
||||
|
||||
async def test_update_nonexisting_entity(client):
|
||||
"""Test get entry."""
|
||||
"""Test update a nonexisting entity."""
|
||||
await client.send_json({
|
||||
'id': 6,
|
||||
'type': 'config/entity_registry/update',
|
||||
@@ -151,3 +151,37 @@ async def test_update_nonexisting_entity(client):
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert not msg['success']
|
||||
|
||||
|
||||
async def test_update_entity_id(hass, client):
|
||||
"""Test update entity id."""
|
||||
mock_registry(hass, {
|
||||
'test_domain.world': RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
)
|
||||
})
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
assert hass.states.get('test_domain.world') is not None
|
||||
|
||||
await client.send_json({
|
||||
'id': 6,
|
||||
'type': 'config/entity_registry/update',
|
||||
'entity_id': 'test_domain.world',
|
||||
'new_entity_id': 'test_domain.planet',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['result'] == {
|
||||
'entity_id': 'test_domain.planet',
|
||||
'name': None
|
||||
}
|
||||
|
||||
assert hass.states.get('test_domain.world') is None
|
||||
assert hass.states.get('test_domain.planet') is not None
|
||||
@@ -400,3 +400,15 @@ def test_async_remove_no_platform(hass):
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
yield from ent.async_remove()
|
||||
assert len(hass.states.async_entity_ids()) == 0
|
||||
|
||||
|
||||
async def test_async_remove_runs_callbacks(hass):
|
||||
"""Test async_remove method when no platform set."""
|
||||
result = []
|
||||
|
||||
ent = entity.Entity()
|
||||
ent.hass = hass
|
||||
ent.entity_id = 'test.test'
|
||||
ent.async_on_remove(lambda: result.append(1))
|
||||
await ent.async_remove()
|
||||
assert len(result) == 1
|
||||
@@ -5,6 +5,8 @@ import unittest
|
||||
from unittest.mock import patch, Mock, MagicMock
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
@@ -487,7 +489,7 @@ def test_registry_respect_entity_disabled(hass):
|
||||
assert hass.states.async_entity_ids() == []
|
||||
|
||||
|
||||
async def test_entity_registry_updates(hass):
|
||||
async def test_entity_registry_updates_name(hass):
|
||||
"""Test that updates on the entity registry update platform entities."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
@@ -602,3 +604,75 @@ def test_not_fails_with_adding_empty_entities_(hass):
|
||||
yield from component.async_add_entities([])
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 0
|
||||
|
||||
|
||||
async def test_entity_registry_updates_entity_id(hass):
|
||||
"""Test that updates on the entity registry update platform entities."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
name='Some name'
|
||||
)
|
||||
})
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
assert state is not None
|
||||
assert state.name == 'Some name'
|
||||
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='test_domain.planet')
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('test_domain.world') is None
|
||||
assert hass.states.get('test_domain.planet') is not None
|
||||
|
||||
|
||||
async def test_entity_registry_updates_invalid_entity_id(hass):
|
||||
"""Test that we can't update to an invalid entity id."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
name='Some name'
|
||||
),
|
||||
'test_domain.existing': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.existing',
|
||||
unique_id='5678',
|
||||
platform='test_platform',
|
||||
),
|
||||
})
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
assert state is not None
|
||||
assert state.name == 'Some name'
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='test_domain.existing')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='invalid_entity_id')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='diff_domain.world')
|
||||
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('test_domain.world') is not None
|
||||
assert hass.states.get('invalid_entity_id') is None
|
||||
assert hass.states.get('diff_domain.world') is None
|
||||
Reference in new issue
Block a user