Allow changing entity ID (#15637)

* Allow changing entity ID

* Add support to websocket command

* Address comments

* Error handling
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2018-07-24 14:12:53 +02:00
1 parent fbeaa57604
commit d9cf8fcfe8
7 files changed
+206 -22

No files matched your search

@@ -20,6 +20,7 @@ SCHEMA_WS_UPDATE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('entity_id'): cv.entity_id,
# If passed in, we update value. Passing None will remove old value.
vol.Optional('name'): vol.Any(str, None),
vol.Optional('new_entity_id'): str,
})
@@ -74,13 +75,28 @@ def websocket_update_entity(hass, connection, msg):
msg['id'], websocket_api.ERR_NOT_FOUND, 'Entity not found'))
return
entry = registry.async_update_entity(
msg['entity_id'], name=msg['name'])
connection.send_message_outside(websocket_api.result_message(
msg['id'], _entry_dict(entry)
))
changes = {}
hass.async_add_job(update_entity())
if 'name' in msg:
changes['name'] = msg['name']
if 'new_entity_id' in msg:
changes['new_entity_id'] = msg['new_entity_id']
try:
if changes:
entry = registry.async_update_entity(
msg['entity_id'], **changes)
except ValueError as err:
connection.send_message_outside(websocket_api.error_message(
msg['id'], 'invalid_info', str(err)
))
else:
connection.send_message_outside(websocket_api.result_message(
msg['id'], _entry_dict(entry)
))
hass.async_create_task(update_entity())
@callback
+25 -1
View File
@@ -82,6 +82,9 @@ class Entity:
# Name in the entity registry
registry_name = None
# Hold list for functions to call on remove.
_on_remove = None
@property
def should_poll(self) -> bool:
"""Return True if entity has to be polled for state.
@@ -324,8 +327,19 @@ class Entity:
if self.parallel_updates:
self.parallel_updates.release()
@callback
def async_on_remove(self, func):
"""Add a function to call when entity removed."""
if self._on_remove is None:
self._on_remove = []
self._on_remove.append(func)
async def async_remove(self):
"""Remove entity from Home Assistant."""
if self._on_remove is not None:
while self._on_remove:
self._on_remove.pop()()
if self.platform is not None:
await self.platform.async_remove_entity(self.entity_id)
else:
@@ -335,7 +349,17 @@ class Entity:
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."""
+1 -1
View File
@@ -283,7 +283,7 @@ class EntityPlatform:
entity.entity_id = entry.entity_id
entity.registry_name = entry.name
entry.add_update_listener(entity)
entity.async_on_remove(entry.add_update_listener(entity))
# We won't generate an entity ID if the platform has already set one
# We will however make sure that platform cannot pick a registered ID
+32 -8
View File
@@ -19,10 +19,10 @@ import weakref
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'
@@ -63,8 +63,13 @@ class RegistryEntry:
"""Listen for when entry is updated.
Listener: Callback function(old_entry, new_entry)
Returns function to unlisten.
"""
self.update_listeners.append(weakref.ref(listener))
weak_listener = weakref.ref(listener)
self.update_listeners.append(weak_listener)
return lambda: self.update_listeners.remove(weak_listener)
class EntityRegistry:
@@ -133,13 +138,18 @@ class EntityRegistry:
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]
@@ -152,6 +162,20 @@ class EntityRegistry:
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