Move check to websocket

This commit is contained in:
Erik 2018-12-11 20:20:57 +01:00 committed by Paulus Schoutsen
parent 4231775e04
commit b7b55f941c
2 changed files with 7 additions and 4 deletions

View File

@ -1,7 +1,7 @@
"""HTTP views to interact with the entity registry.""" """HTTP views to interact with the entity registry."""
import voluptuous as vol 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.helpers.entity_registry import async_get_registry
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
from homeassistant.components.websocket_api.const import ERR_NOT_FOUND 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: if 'new_entity_id' in msg:
changes['new_entity_id'] = msg['new_entity_id'] 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: try:
if changes: if changes:

View File

@ -171,9 +171,7 @@ class EntityRegistry:
changes['device_id'] = device_id changes['device_id'] = device_id
if new_entity_id is not _UNDEF and new_entity_id != old.entity_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 if self.async_is_registered(new_entity_id):
self.hass.states.async_entity_ids(
split_entity_id(entity_id)[0])):
raise ValueError('Entity is already registered') raise ValueError('Entity is already registered')
if not valid_entity_id(new_entity_id): if not valid_entity_id(new_entity_id):