Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion homeassistant/helpers/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,17 @@ async def async_remove(self):
def async_registry_updated(self, old, new):
"""Called when the entity registry has been updated."""
self.registry_name = new.name
self.async_schedule_update_ha_state()

if new.entity_id == self.entity_id:
self.async_schedule_update_ha_state()
return

async def readd():
"""Remove and add entity again."""
await self.async_remove()
await self.platform.async_add_entities([self])

self.hass.async_create_task(readd())

def __eq__(self, other):
"""Return the comparison."""
Expand Down
33 changes: 26 additions & 7 deletions homeassistant/helpers/entity_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import attr

from ..core import callback, split_entity_id
from ..loader import bind_hass
from ..util import ensure_unique_string, slugify
from ..util.yaml import load_yaml, save_yaml
from homeassistant.core import callback, split_entity_id, valid_entity_id
from homeassistant.loader import bind_hass
from homeassistant.util import ensure_unique_string, slugify
from homeassistant.util.yaml import load_yaml, save_yaml

PATH_REGISTRY = 'entity_registry.yaml'
DATA_REGISTRY = 'entity_registry'
Expand Down Expand Up @@ -133,13 +133,18 @@ def async_get_or_create(self, domain, platform, unique_id, *,
return entity

@callback
def async_update_entity(self, entity_id, *, name=_UNDEF):
def async_update_entity(self, entity_id, *, name=_UNDEF,
new_entity_id=_UNDEF):
"""Update properties of an entity."""
return self._async_update_entity(entity_id, name=name)
return self._async_update_entity(
entity_id,
name=name,
new_entity_id=new_entity_id
)

@callback
def _async_update_entity(self, entity_id, *, name=_UNDEF,
config_entry_id=_UNDEF):
config_entry_id=_UNDEF, new_entity_id=_UNDEF):
"""Private facing update properties method."""
old = self.entities[entity_id]

Expand All @@ -152,6 +157,20 @@ def _async_update_entity(self, entity_id, *, name=_UNDEF,
config_entry_id != old.config_entry_id):
changes['config_entry_id'] = config_entry_id

if new_entity_id is not _UNDEF and new_entity_id != old.entity_id:
if self.async_is_registered(new_entity_id):
raise ValueError('Entity is already registered')

if not valid_entity_id(new_entity_id):
raise ValueError('Invalid entity ID')

if (split_entity_id(new_entity_id)[0] !=
split_entity_id(entity_id)[0]):
raise ValueError('New entity ID should be same domain')

self.entities.pop(entity_id)
entity_id = changes['entity_id'] = new_entity_id

if not changes:
return old

Expand Down
76 changes: 75 additions & 1 deletion tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
state = hass.states.get('test_domain.planet')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing assertion?



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
state = hass.states.get('invalid_entity_id') is None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird. Missing assertion?

state = hass.states.get('diff_domain.world') is None