Allow overriding name via entity registry (#12292)
* Allow overriding name via entity registry * Update requirements
This commit is contained in:
1 parent
8b9eab196c
commit
17e5740a0c
11 files changed
+91
-21
No files matched your search
+2
-2
@@ -317,10 +317,10 @@ def mock_component(hass, component):
|
||||
hass.config.components.add(component)
|
||||
|
||||
|
||||
def mock_registry(hass):
|
||||
def mock_registry(hass, mock_entries=None):
|
||||
"""Mock the Entity Registry."""
|
||||
registry = entity_registry.EntityRegistry(hass)
|
||||
registry.entities = {}
|
||||
registry.entities = mock_entries or {}
|
||||
hass.data[entity_platform.DATA_REGISTRY] = registry
|
||||
return registry
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import homeassistant.loader as loader
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.helpers.entity_component import (
|
||||
EntityComponent, DEFAULT_SCAN_INTERVAL)
|
||||
from homeassistant.helpers import entity_platform
|
||||
from homeassistant.helpers import entity_platform, entity_registry
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
@@ -433,3 +433,24 @@ def test_entity_with_name_and_entity_id_getting_registered(hass):
|
||||
MockEntity(unique_id='1234', name='bla',
|
||||
entity_id='test_domain.world')])
|
||||
assert 'test_domain.world' in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_overriding_name_from_registry(hass):
|
||||
"""Test that we can override a name via the Entity Registry."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
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_domain',
|
||||
name='Overridden'
|
||||
)
|
||||
})
|
||||
yield from component.async_add_entities([
|
||||
MockEntity(unique_id='1234', name='Device Name')])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
assert state is not None
|
||||
assert state.name == 'Overridden'
|
||||
@@ -9,6 +9,9 @@ from homeassistant.helpers import entity_registry
|
||||
from tests.common import mock_registry
|
||||
|
||||
|
||||
YAML__OPEN_PATH = 'homeassistant.util.yaml.open'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def registry(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
@@ -82,13 +85,12 @@ def test_save_timer_reset_on_subsequent_save(hass, registry):
|
||||
@asyncio.coroutine
|
||||
def test_loading_saving_data(hass, registry):
|
||||
"""Test that we load/save data correctly."""
|
||||
yaml_path = 'homeassistant.util.yaml.open'
|
||||
orig_entry1 = registry.async_get_or_create('light', 'hue', '1234')
|
||||
orig_entry2 = registry.async_get_or_create('light', 'hue', '5678')
|
||||
|
||||
assert len(registry.entities) == 2
|
||||
|
||||
with patch(yaml_path, mock_open(), create=True) as mock_write:
|
||||
with patch(YAML__OPEN_PATH, mock_open(), create=True) as mock_write:
|
||||
yield from registry._async_save()
|
||||
|
||||
# Mock open calls are: open file, context enter, write, context leave
|
||||
@@ -98,7 +100,7 @@ def test_loading_saving_data(hass, registry):
|
||||
registry2 = entity_registry.EntityRegistry(hass)
|
||||
|
||||
with patch('os.path.isfile', return_value=True), \
|
||||
patch(yaml_path, mock_open(read_data=written), create=True):
|
||||
patch(YAML__OPEN_PATH, mock_open(read_data=written), create=True):
|
||||
yield from registry2._async_load()
|
||||
|
||||
# Ensure same order
|
||||
@@ -133,3 +135,30 @@ def test_is_registered(registry):
|
||||
entry = registry.async_get_or_create('light', 'hue', '1234')
|
||||
assert registry.async_is_registered(entry.entity_id)
|
||||
assert not registry.async_is_registered('light.non_existing')
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_loading_extra_values(hass):
|
||||
"""Test we load extra data from the registry."""
|
||||
written = """
|
||||
test.named:
|
||||
platform: super_platform
|
||||
unique_id: with-name
|
||||
name: registry override
|
||||
test.no_name:
|
||||
platform: super_platform
|
||||
unique_id: without-name
|
||||
"""
|
||||
|
||||
registry = entity_registry.EntityRegistry(hass)
|
||||
|
||||
with patch('os.path.isfile', return_value=True), \
|
||||
patch(YAML__OPEN_PATH, mock_open(read_data=written), create=True):
|
||||
yield from registry._async_load()
|
||||
|
||||
entry_with_name = registry.async_get_or_create(
|
||||
'test', 'super_platform', 'with-name')
|
||||
entry_without_name = registry.async_get_or_create(
|
||||
'test', 'super_platform', 'without-name')
|
||||
assert entry_with_name.name == 'registry override'
|
||||
assert entry_without_name.name is None
|
||||
Reference in new issue
Block a user