From 1f8156e26c2d9c6b604946a7903ee9c0316adbb5 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 11 Dec 2018 17:05:49 +0100 Subject: [PATCH 1/3] Fail if new entity_id is in hass.states --- homeassistant/components/config/entity_registry.py | 2 +- homeassistant/helpers/entity_registry.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 1ede76d0fd8..15eed32a8cd 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -88,7 +88,7 @@ async def websocket_get_entity(hass, connection, msg): @async_response async def websocket_update_entity(hass, connection, msg): - """Handle get camera thumbnail websocket command. + """Handle update entity websocket command. Async friendly. """ diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index 57c8bcf0af8..fdd9f178321 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -171,7 +171,9 @@ class EntityRegistry: changes['device_id'] = device_id if new_entity_id is not _UNDEF and new_entity_id != old.entity_id: - if self.async_is_registered(new_entity_id): + if (self.async_is_registered(new_entity_id) or new_entity_id in + self.hass.states.async_entity_ids( + split_entity_id(entity_id)[0])): raise ValueError('Entity is already registered') if not valid_entity_id(new_entity_id): From c7492b0feb39f6bfe8c28e28f5aa521854c5d1ee Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 11 Dec 2018 20:20:57 +0100 Subject: [PATCH 2/3] Move check to websocket --- homeassistant/components/config/entity_registry.py | 7 ++++++- homeassistant/helpers/entity_registry.py | 4 +--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 15eed32a8cd..13a968dd101 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -1,7 +1,7 @@ """HTTP views to interact with the entity registry.""" import voluptuous as vol -from homeassistant.core import callback +from homeassistant.core import callback, split_entity_id from homeassistant.helpers.entity_registry import async_get_registry from homeassistant.components import websocket_api from homeassistant.components.websocket_api.const import ERR_NOT_FOUND @@ -106,6 +106,11 @@ async def websocket_update_entity(hass, connection, msg): if 'new_entity_id' in msg: changes['new_entity_id'] = msg['new_entity_id'] + if (msg['new_entity_id'] in hass.states.async_entity_ids( + split_entity_id(msg['new_entity_id'])[0])): + connection.send_message(websocket_api.error_message( + msg['id'], ERR_NOT_FOUND, 'Entity is already registered')) + return try: if changes: diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index fdd9f178321..57c8bcf0af8 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -171,9 +171,7 @@ class EntityRegistry: changes['device_id'] = device_id if new_entity_id is not _UNDEF and new_entity_id != old.entity_id: - if (self.async_is_registered(new_entity_id) or new_entity_id in - self.hass.states.async_entity_ids( - split_entity_id(entity_id)[0])): + if self.async_is_registered(new_entity_id): raise ValueError('Entity is already registered') if not valid_entity_id(new_entity_id): From d03dfd985bff83e726e52a7ba1934d7e2278aed3 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 12 Dec 2018 16:30:42 +0100 Subject: [PATCH 3/3] Review comments --- homeassistant/components/config/entity_registry.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 13a968dd101..71833a2e42d 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -1,7 +1,7 @@ """HTTP views to interact with the entity registry.""" import voluptuous as vol -from homeassistant.core import callback, split_entity_id +from homeassistant.core import callback from homeassistant.helpers.entity_registry import async_get_registry from homeassistant.components import websocket_api from homeassistant.components.websocket_api.const import ERR_NOT_FOUND @@ -106,10 +106,9 @@ async def websocket_update_entity(hass, connection, msg): if 'new_entity_id' in msg: changes['new_entity_id'] = msg['new_entity_id'] - if (msg['new_entity_id'] in hass.states.async_entity_ids( - split_entity_id(msg['new_entity_id'])[0])): + if hass.states.get(msg['new_entity_id']) is not None: connection.send_message(websocket_api.error_message( - msg['id'], ERR_NOT_FOUND, 'Entity is already registered')) + msg['id'], 'invalid_info', 'Entity is already registered')) return try: