mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Migrate entity registry to using websocket (#14830)
* Migrate to using websocket * Lint
This commit is contained in:
committed by
Pascal Vizeli
parent
4bccb0d2a1
commit
a6880c452f
@@ -2,48 +2,85 @@
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.components.http.data_validator import RequestDataValidator
|
||||
from homeassistant.helpers.entity_registry import async_get_registry
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
DEPENDENCIES = ['websocket_api']
|
||||
|
||||
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),
|
||||
})
|
||||
|
||||
|
||||
async def async_setup(hass):
|
||||
"""Enable the Entity Registry views."""
|
||||
hass.http.register_view(ConfigManagerEntityView)
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_GET, websocket_get_entity,
|
||||
SCHEMA_WS_GET
|
||||
)
|
||||
hass.components.websocket_api.async_register_command(
|
||||
WS_TYPE_UPDATE, websocket_update_entity,
|
||||
SCHEMA_WS_UPDATE
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
class ConfigManagerEntityView(HomeAssistantView):
|
||||
"""View to interact with an entity registry entry."""
|
||||
@callback
|
||||
def websocket_get_entity(hass, connection, msg):
|
||||
"""Handle get entity registry entry command.
|
||||
|
||||
url = '/api/config/entity_registry/{entity_id}'
|
||||
name = 'api:config:entity_registry:entity'
|
||||
|
||||
async def get(self, request, entity_id):
|
||||
"""Get the entity registry settings for an entity."""
|
||||
hass = request.app['hass']
|
||||
Async friendly.
|
||||
"""
|
||||
async def retrieve_entity():
|
||||
"""Get entity from registry."""
|
||||
registry = await async_get_registry(hass)
|
||||
entry = registry.entities.get(entity_id)
|
||||
entry = registry.entities.get(msg['entity_id'])
|
||||
|
||||
if entry is None:
|
||||
return self.json_message('Entry not found', 404)
|
||||
connection.send_message_outside(websocket_api.error_message(
|
||||
msg['id'], websocket_api.ERR_NOT_FOUND, 'Entity not found'))
|
||||
return
|
||||
|
||||
return self.json(_entry_dict(entry))
|
||||
connection.send_message_outside(websocket_api.result_message(
|
||||
msg['id'], _entry_dict(entry)
|
||||
))
|
||||
|
||||
@RequestDataValidator(vol.Schema({
|
||||
# If passed in, we update value. Passing None will remove old value.
|
||||
vol.Optional('name'): vol.Any(str, None),
|
||||
}))
|
||||
async def post(self, request, entity_id, data):
|
||||
"""Update the entity registry settings for an entity."""
|
||||
hass = request.app['hass']
|
||||
hass.async_add_job(retrieve_entity())
|
||||
|
||||
|
||||
@callback
|
||||
def websocket_update_entity(hass, connection, msg):
|
||||
"""Handle get camera thumbnail websocket command.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
async def update_entity():
|
||||
"""Get entity from registry."""
|
||||
registry = await async_get_registry(hass)
|
||||
|
||||
if entity_id not in registry.entities:
|
||||
return self.json_message('Entry not found', 404)
|
||||
if msg['entity_id'] not in registry.entities:
|
||||
connection.send_message_outside(websocket_api.error_message(
|
||||
msg['id'], websocket_api.ERR_NOT_FOUND, 'Entity not found'))
|
||||
return
|
||||
|
||||
entry = registry.async_update_entity(entity_id, **data)
|
||||
return self.json(_entry_dict(entry))
|
||||
entry = registry.async_update_entity(
|
||||
msg['entity_id'], name=msg['name'])
|
||||
connection.send_message_outside(websocket_api.result_message(
|
||||
msg['id'], _entry_dict(entry)
|
||||
))
|
||||
|
||||
hass.async_add_job(update_entity())
|
||||
|
||||
|
||||
@callback
|
||||
|
||||
Reference in New Issue
Block a user