2018-02-24 10:53:59 -08:00
|
|
|
"""HTTP views to interact with the entity registry."""
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-12-12 16:30:42 +01:00
|
|
|
from homeassistant.core import callback
|
2018-02-24 10:53:59 -08:00
|
|
|
from homeassistant.helpers.entity_registry import async_get_registry
|
2018-06-06 01:08:36 -07:00
|
|
|
from homeassistant.components import websocket_api
|
2018-10-01 11:21:00 +02:00
|
|
|
from homeassistant.components.websocket_api.const import ERR_NOT_FOUND
|
2019-01-30 09:50:32 -08:00
|
|
|
from homeassistant.components.websocket_api.decorators import (
|
2019-07-31 12:25:30 -07:00
|
|
|
async_response,
|
|
|
|
require_admin,
|
|
|
|
)
|
2018-06-06 01:08:36 -07:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
WS_TYPE_LIST = "config/entity_registry/list"
|
|
|
|
SCHEMA_WS_LIST = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|
|
|
{vol.Required("type"): WS_TYPE_LIST}
|
|
|
|
)
|
|
|
|
|
|
|
|
WS_TYPE_GET = "config/entity_registry/get"
|
|
|
|
SCHEMA_WS_GET = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|
|
|
{vol.Required("type"): WS_TYPE_GET, vol.Required("entity_id"): cv.entity_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
WS_TYPE_UPDATE = "config/entity_registry/update"
|
|
|
|
SCHEMA_WS_UPDATE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required("type"): WS_TYPE_UPDATE,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
WS_TYPE_REMOVE = "config/entity_registry/remove"
|
|
|
|
SCHEMA_WS_REMOVE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
|
|
|
|
{vol.Required("type"): WS_TYPE_REMOVE, vol.Required("entity_id"): cv.entity_id}
|
|
|
|
)
|
2019-01-30 09:50:32 -08:00
|
|
|
|
2018-02-24 10:53:59 -08:00
|
|
|
|
|
|
|
async def async_setup(hass):
|
|
|
|
"""Enable the Entity Registry views."""
|
2018-09-14 11:57:18 +02:00
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 12:25:30 -07:00
|
|
|
WS_TYPE_LIST, websocket_list_entities, SCHEMA_WS_LIST
|
2018-09-14 11:57:18 +02:00
|
|
|
)
|
2018-06-06 01:08:36 -07:00
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 12:25:30 -07:00
|
|
|
WS_TYPE_GET, websocket_get_entity, SCHEMA_WS_GET
|
2018-06-06 01:08:36 -07:00
|
|
|
)
|
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 12:25:30 -07:00
|
|
|
WS_TYPE_UPDATE, websocket_update_entity, SCHEMA_WS_UPDATE
|
2018-06-06 01:08:36 -07:00
|
|
|
)
|
2019-01-30 09:50:32 -08:00
|
|
|
hass.components.websocket_api.async_register_command(
|
2019-07-31 12:25:30 -07:00
|
|
|
WS_TYPE_REMOVE, websocket_remove_entity, SCHEMA_WS_REMOVE
|
2019-01-30 09:50:32 -08:00
|
|
|
)
|
2018-02-24 10:53:59 -08:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-10-01 11:21:00 +02:00
|
|
|
@async_response
|
|
|
|
async def websocket_list_entities(hass, connection, msg):
|
2018-09-14 11:57:18 +02:00
|
|
|
"""Handle list registry entries command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2018-10-01 11:21:00 +02:00
|
|
|
registry = await async_get_registry(hass)
|
2019-07-31 12:25:30 -07:00
|
|
|
connection.send_message(
|
|
|
|
websocket_api.result_message(
|
|
|
|
msg["id"], [_entry_dict(entry) for entry in registry.entities.values()]
|
|
|
|
)
|
|
|
|
)
|
2018-10-01 11:21:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
@async_response
|
|
|
|
async def websocket_get_entity(hass, connection, msg):
|
2018-06-06 01:08:36 -07:00
|
|
|
"""Handle get entity registry entry command.
|
2018-02-24 10:53:59 -08:00
|
|
|
|
2018-06-06 01:08:36 -07:00
|
|
|
Async friendly.
|
|
|
|
"""
|
2018-10-01 11:21:00 +02:00
|
|
|
registry = await async_get_registry(hass)
|
2019-07-31 12:25:30 -07:00
|
|
|
entry = registry.entities.get(msg["entity_id"])
|
2018-02-24 10:53:59 -08:00
|
|
|
|
2018-10-01 11:21:00 +02:00
|
|
|
if entry is None:
|
2019-07-31 12:25:30 -07:00
|
|
|
connection.send_message(
|
|
|
|
websocket_api.error_message(msg["id"], ERR_NOT_FOUND, "Entity not found")
|
|
|
|
)
|
2018-10-01 11:21:00 +02:00
|
|
|
return
|
2018-02-24 10:53:59 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
connection.send_message(websocket_api.result_message(msg["id"], _entry_dict(entry)))
|
2018-06-06 01:08:36 -07:00
|
|
|
|
|
|
|
|
2019-01-30 09:50:32 -08:00
|
|
|
@require_admin
|
2018-10-01 11:21:00 +02:00
|
|
|
@async_response
|
|
|
|
async def websocket_update_entity(hass, connection, msg):
|
2018-12-11 17:05:49 +01:00
|
|
|
"""Handle update entity websocket command.
|
2018-06-06 01:08:36 -07:00
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2018-10-01 11:21:00 +02:00
|
|
|
registry = await async_get_registry(hass)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if msg["entity_id"] not in registry.entities:
|
|
|
|
connection.send_message(
|
|
|
|
websocket_api.error_message(msg["id"], ERR_NOT_FOUND, "Entity not found")
|
|
|
|
)
|
2018-10-01 11:21:00 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
changes = {}
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if "name" in msg:
|
|
|
|
changes["name"] = msg["name"]
|
|
|
|
|
|
|
|
if "new_entity_id" in msg and msg["new_entity_id"] != msg["entity_id"]:
|
|
|
|
changes["new_entity_id"] = msg["new_entity_id"]
|
|
|
|
if hass.states.get(msg["new_entity_id"]) is not None:
|
|
|
|
connection.send_message(
|
|
|
|
websocket_api.error_message(
|
|
|
|
msg["id"], "invalid_info", "Entity is already registered"
|
|
|
|
)
|
|
|
|
)
|
2018-12-11 20:20:57 +01:00
|
|
|
return
|
2018-10-01 11:21:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
if changes:
|
2019-07-31 12:25:30 -07:00
|
|
|
entry = registry.async_update_entity(msg["entity_id"], **changes)
|
2018-10-01 11:21:00 +02:00
|
|
|
except ValueError as err:
|
2019-07-31 12:25:30 -07:00
|
|
|
connection.send_message(
|
|
|
|
websocket_api.error_message(msg["id"], "invalid_info", str(err))
|
|
|
|
)
|
2018-10-01 11:21:00 +02:00
|
|
|
else:
|
2019-07-31 12:25:30 -07:00
|
|
|
connection.send_message(
|
|
|
|
websocket_api.result_message(msg["id"], _entry_dict(entry))
|
|
|
|
)
|
2018-02-24 10:53:59 -08:00
|
|
|
|
|
|
|
|
2019-01-30 09:50:32 -08:00
|
|
|
@require_admin
|
|
|
|
@async_response
|
|
|
|
async def websocket_remove_entity(hass, connection, msg):
|
|
|
|
"""Handle remove entity websocket command.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
registry = await async_get_registry(hass)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if msg["entity_id"] not in registry.entities:
|
|
|
|
connection.send_message(
|
|
|
|
websocket_api.error_message(msg["id"], ERR_NOT_FOUND, "Entity not found")
|
|
|
|
)
|
2019-01-30 09:50:32 -08:00
|
|
|
return
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
registry.async_remove(msg["entity_id"])
|
|
|
|
connection.send_message(websocket_api.result_message(msg["id"]))
|
2019-01-30 09:50:32 -08:00
|
|
|
|
|
|
|
|
2018-02-24 10:53:59 -08:00
|
|
|
@callback
|
|
|
|
def _entry_dict(entry):
|
2018-08-24 11:28:43 +03:00
|
|
|
"""Convert entry to API format."""
|
2018-02-24 10:53:59 -08:00
|
|
|
return {
|
2019-07-31 12:25:30 -07:00
|
|
|
"config_entry_id": entry.config_entry_id,
|
|
|
|
"device_id": entry.device_id,
|
|
|
|
"disabled_by": entry.disabled_by,
|
|
|
|
"entity_id": entry.entity_id,
|
|
|
|
"name": entry.name,
|
|
|
|
"platform": entry.platform,
|
2018-02-24 10:53:59 -08:00
|
|
|
}
|