Allow renaming entities in entity registry (#12636)

* Allow renaming entities in entity registry

* Lint
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-02-24 10:53:59 -08:00
1 parent 2821820281
commit 6d431c3fc3
9 files changed
+333 -47

No files matched your search

+28 -1
View File
@@ -1,5 +1,6 @@
"""Test the helper method for writing tests."""
import asyncio
from datetime import timedelta
import functools as ft
import os
import sys
@@ -298,7 +299,7 @@ def mock_registry(hass, mock_entries=None):
"""Mock the Entity Registry."""
registry = entity_registry.EntityRegistry(hass)
registry.entities = mock_entries or {}
hass.data[entity_platform.DATA_REGISTRY] = registry
hass.data[entity_registry.DATA_REGISTRY] = registry
return registry
@@ -361,6 +362,32 @@ class MockPlatform(object):
self.async_setup_platform = mock_coro_func()
class MockEntityPlatform(entity_platform.EntityPlatform):
"""Mock class with some mock defaults."""
def __init__(
self, hass,
logger=None,
domain='test_domain',
platform_name='test_platform',
scan_interval=timedelta(seconds=15),
parallel_updates=0,
entity_namespace=None,
async_entities_added_callback=lambda: None
):
"""Initialize a mock entity platform."""
super().__init__(
hass=hass,
logger=logger,
domain=domain,
platform_name=platform_name,
scan_interval=scan_interval,
parallel_updates=parallel_updates,
entity_namespace=entity_namespace,
async_entities_added_callback=async_entities_added_callback,
)
class MockToggleDevice(entity.ToggleEntity):
"""Provide a mock toggle device."""
@@ -0,0 +1,134 @@
"""Test entity_registry API."""
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.helpers.entity_registry import RegistryEntry
from homeassistant.components.config import entity_registry
from tests.common import mock_registry, MockEntity, MockEntityPlatform
@pytest.fixture
def client(hass, test_client):
"""Fixture that can interact with the config manager API."""
hass.loop.run_until_complete(async_setup_component(hass, 'http', {}))
hass.loop.run_until_complete(entity_registry.async_setup(hass))
yield hass.loop.run_until_complete(test_client(hass.http.app))
async def test_get_entity(hass, client):
"""Test get entry."""
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',
),
})
resp = await client.get(
'/api/config/entity_registry/test_domain.name')
assert resp.status == 200
data = await resp.json()
assert data == {
'entity_id': 'test_domain.name',
'name': 'Hello World'
}
resp = await client.get(
'/api/config/entity_registry/test_domain.no_name')
assert resp.status == 200
data = await resp.json()
assert data == {
'entity_id': 'test_domain.no_name',
'name': None
}
async def test_update_entity(hass, client):
"""Test get entry."""
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'
)
})
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 == 'before update'
resp = await client.post(
'/api/config/entity_registry/test_domain.world', json={
'name': 'after update'
})
assert resp.status == 200
data = await resp.json()
assert data == {
'entity_id': 'test_domain.world',
'name': 'after update'
}
state = hass.states.get('test_domain.world')
assert state.name == 'after update'
async def test_update_entity_no_changes(hass, client):
"""Test get entry."""
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)
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 == 'name of entity'
resp = await client.post(
'/api/config/entity_registry/test_domain.world', json={
'name': 'name of entity'
})
assert resp.status == 200
data = await resp.json()
assert data == {
'entity_id': 'test_domain.world',
'name': 'name of entity'
}
state = hass.states.get('test_domain.world')
assert state.name == 'name of entity'
async def test_get_nonexisting_entity(client):
"""Test get entry."""
resp = await client.get(
'/api/config/entity_registry/test_domain.non_existing')
assert resp.status == 404
async def test_update_nonexisting_entity(client):
"""Test get entry."""
resp = await client.post(
'/api/config/entity_registry/test_domain.non_existing', json={
'name': 'some name'
})
assert resp.status == 404
+28 -27
View File
@@ -15,39 +15,13 @@ import homeassistant.util.dt as dt_util
from tests.common import (
get_test_home_assistant, MockPlatform, fire_time_changed, mock_registry,
MockEntity)
MockEntity, MockEntityPlatform)
_LOGGER = logging.getLogger(__name__)
DOMAIN = "test_domain"
PLATFORM = 'test_platform'
class MockEntityPlatform(entity_platform.EntityPlatform):
"""Mock class with some mock defaults."""
def __init__(
self, hass,
logger=None,
domain=DOMAIN,
platform_name=PLATFORM,
scan_interval=timedelta(seconds=15),
parallel_updates=0,
entity_namespace=None,
async_entities_added_callback=lambda: None
):
"""Initialize a mock entity platform."""
super().__init__(
hass=hass,
logger=logger,
domain=domain,
platform_name=platform_name,
scan_interval=scan_interval,
parallel_updates=parallel_updates,
entity_namespace=entity_namespace,
async_entities_added_callback=async_entities_added_callback,
)
class TestHelpersEntityPlatform(unittest.TestCase):
"""Test homeassistant.helpers.entity_component module."""
@@ -510,3 +484,30 @@ def test_registry_respect_entity_disabled(hass):
yield from platform.async_add_entities([entity])
assert entity.entity_id is None
assert hass.states.async_entity_ids() == []
async def test_entity_registry_updates(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='before update'
)
})
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 == 'before update'
registry.async_update_entity('test_domain.world', name='after update')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('test_domain.world')
assert state.name == 'after update'