Skip to content

Commit

Permalink
Respect entity namespace for entity registry (#12313)
Browse files Browse the repository at this point in the history
* Respect entity namespace for entity registry

* Lint
  • Loading branch information
balloob authored Feb 12, 2018
1 parent 3b30504 commit eb7adc7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions homeassistant/helpers/entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def _async_add_entity(self, entity, update_before_add, component_entities,
else:
suggested_object_id = entity.name

if self.entity_namespace is not None:
suggested_object_id = '{} {}'.format(
self.entity_namespace, suggested_object_id)

entry = registry.async_get_or_create(
self.domain, self.platform_name, entity.unique_id,
suggested_object_id=suggested_object_id)
Expand Down
36 changes: 36 additions & 0 deletions tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@
DOMAIN = "test_domain"


class MockEntityPlatform(entity_platform.EntityPlatform):
"""Mock class with some mock defaults."""

def __init__(
self, *, hass,
logger=None,
domain='test',
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 TestHelpersEntityPlatform(unittest.TestCase):
"""Test homeassistant.helpers.entity_component module."""

Expand Down Expand Up @@ -454,3 +480,13 @@ def test_overriding_name_from_registry(hass):
state = hass.states.get('test_domain.world')
assert state is not None
assert state.name == 'Overridden'


@asyncio.coroutine
def test_registry_respect_entity_namespace(hass):
"""Test that the registry respects entity namespace."""
mock_registry(hass)
platform = MockEntityPlatform(hass=hass, entity_namespace='ns')
entity = MockEntity(unique_id='1234', name='Device Name')
yield from platform.async_add_entities([entity])
assert entity.entity_id == 'test.ns_device_name'

0 comments on commit eb7adc7

Please sign in to comment.