core/tests/components/config/test_entity_registry.py

365 lines
9.9 KiB
Python
Raw Normal View History

"""Test entity_registry API."""
from collections import OrderedDict
import pytest
from homeassistant.components.config import entity_registry
from homeassistant.helpers.entity_registry import RegistryEntry
from tests.common import MockEntity, MockEntityPlatform, mock_registry
@pytest.fixture
def client(hass, hass_ws_client):
"""Fixture that can interact with the config manager API."""
hass.loop.run_until_complete(entity_registry.async_setup(hass))
yield hass.loop.run_until_complete(hass_ws_client(hass))
async def test_list_entities(hass, client):
"""Test list entries."""
entities = OrderedDict()
2019-07-31 19:25:30 +00:00
entities["test_domain.name"] = RegistryEntry(
entity_id="test_domain.name",
unique_id="1234",
platform="test_platform",
name="Hello World",
)
2019-07-31 19:25:30 +00:00
entities["test_domain.no_name"] = RegistryEntry(
entity_id="test_domain.no_name", unique_id="6789", platform="test_platform"
)
mock_registry(hass, entities)
2019-07-31 19:25:30 +00:00
await client.send_json({"id": 5, "type": "config/entity_registry/list"})
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["result"] == [
{
2019-07-31 19:25:30 +00:00
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"entity_id": "test_domain.name",
"name": "Hello World",
"icon": None,
"original_name": None,
"original_icon": None,
2019-07-31 19:25:30 +00:00
"platform": "test_platform",
},
{
2019-07-31 19:25:30 +00:00
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"entity_id": "test_domain.no_name",
"name": None,
"icon": None,
"original_name": None,
"original_icon": None,
2019-07-31 19:25:30 +00:00
"platform": "test_platform",
},
]
async def test_get_entity(hass, client):
"""Test get entry."""
2019-07-31 19:25:30 +00:00
mock_registry(
hass,
{
"test_domain.name": RegistryEntry(
entity_id="test_domain.name",
unique_id="1234",
platform="test_platform",
name="Hello World",
),
"test_domain.no_name": RegistryEntry(
entity_id="test_domain.no_name",
unique_id="6789",
platform="test_platform",
),
},
)
await client.send_json(
{"id": 5, "type": "config/entity_registry/get", "entity_id": "test_domain.name"}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.name",
"name": "Hello World",
"icon": None,
"original_name": None,
"original_icon": None,
}
2019-07-31 19:25:30 +00:00
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/get",
"entity_id": "test_domain.no_name",
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.no_name",
"name": None,
"icon": None,
"original_name": None,
"original_icon": None,
}
async def test_update_entity(hass, client):
"""Test updating entity."""
registry = mock_registry(
2019-07-31 19:25:30 +00:00
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",
name="before update",
icon="icon:before update",
2019-07-31 19:25:30 +00:00
)
},
)
platform = MockEntityPlatform(hass)
2019-07-31 19:25:30 +00:00
entity = MockEntity(unique_id="1234")
await platform.async_add_entities([entity])
2019-07-31 19:25:30 +00:00
state = hass.states.get("test_domain.world")
assert state is not None
2019-07-31 19:25:30 +00:00
assert state.name == "before update"
assert state.attributes["icon"] == "icon:before update"
# UPDATE NAME & ICON
2019-07-31 19:25:30 +00:00
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"name": "after update",
"icon": "icon:after update",
2019-07-31 19:25:30 +00:00
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
2019-07-31 19:25:30 +00:00
"platform": "test_platform",
"entity_id": "test_domain.world",
"name": "after update",
"icon": "icon:after update",
"original_name": None,
"original_icon": None,
}
2019-07-31 19:25:30 +00:00
state = hass.states.get("test_domain.world")
assert state.name == "after update"
assert state.attributes["icon"] == "icon:after update"
# UPDATE DISABLED_BY TO USER
await client.send_json(
{
"id": 7,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"disabled_by": "user",
}
)
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
await client.send_json(
{
"id": 8,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"disabled_by": None,
}
)
msg = await client.receive_json()
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.world",
"name": "after update",
"icon": "icon:after update",
"original_name": None,
"original_icon": None,
}
async def test_update_entity_no_changes(hass, client):
"""Test update entity with no changes."""
2019-07-31 19:25:30 +00:00
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",
name="name of entity",
)
},
)
platform = MockEntityPlatform(hass)
2019-07-31 19:25:30 +00:00
entity = MockEntity(unique_id="1234")
await platform.async_add_entities([entity])
2019-07-31 19:25:30 +00:00
state = hass.states.get("test_domain.world")
assert state is not None
2019-07-31 19:25:30 +00:00
assert state.name == "name of entity"
2019-07-31 19:25:30 +00:00
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/update",
"entity_id": "test_domain.world",
"name": "name of entity",
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.world",
"name": "name of entity",
"icon": None,
"original_name": None,
"original_icon": None,
}
2019-07-31 19:25:30 +00:00
state = hass.states.get("test_domain.world")
assert state.name == "name of entity"
async def test_get_nonexisting_entity(client):
"""Test get entry with nonexisting entity."""
2019-07-31 19:25:30 +00:00
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/get",
"entity_id": "test_domain.no_name",
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert not msg["success"]
async def test_update_nonexisting_entity(client):
"""Test update a nonexisting entity."""
2019-07-31 19:25:30 +00:00
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/update",
"entity_id": "test_domain.no_name",
"name": "new-name",
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert not msg["success"]
async def test_update_entity_id(hass, client):
"""Test update entity id."""
2019-07-31 19:25:30 +00:00
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)
2019-07-31 19:25:30 +00:00
entity = MockEntity(unique_id="1234")
await platform.async_add_entities([entity])
2019-07-31 19:25:30 +00:00
assert hass.states.get("test_domain.world") is not None
2019-07-31 19:25:30 +00:00
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()
2019-07-31 19:25:30 +00:00
assert msg["result"] == {
"config_entry_id": None,
"device_id": None,
"disabled_by": None,
"platform": "test_platform",
"entity_id": "test_domain.planet",
"name": None,
"icon": None,
"original_name": None,
"original_icon": None,
}
2019-07-31 19:25:30 +00:00
assert hass.states.get("test_domain.world") is None
assert hass.states.get("test_domain.planet") is not None
async def test_remove_entity(hass, client):
"""Test removing entity."""
2019-07-31 19:25:30 +00:00
registry = 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",
name="before update",
)
},
)
await client.send_json(
{
"id": 6,
"type": "config/entity_registry/remove",
"entity_id": "test_domain.world",
}
)
msg = await client.receive_json()
2019-07-31 19:25:30 +00:00
assert msg["success"]
assert len(registry.entities) == 0