Shuffle some device registry WS code (#84243)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Erik Montnemery 2022-12-19 21:56:07 +01:00 committed by GitHub
parent af272c6730
commit 0d8cd2d067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,24 +21,6 @@ from homeassistant.helpers.device_registry import (
async_get, async_get,
) )
WS_TYPE_LIST = "config/device_registry/list"
SCHEMA_WS_LIST = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{vol.Required("type"): WS_TYPE_LIST}
)
WS_TYPE_UPDATE = "config/device_registry/update"
SCHEMA_WS_UPDATE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend(
{
vol.Required("type"): WS_TYPE_UPDATE,
vol.Required("device_id"): str,
vol.Optional("area_id"): vol.Any(str, None),
vol.Optional("name_by_user"): vol.Any(str, None),
# We only allow setting disabled_by user via API.
# No Enum support like this in voluptuous, use .value
vol.Optional("disabled_by"): vol.Any(DeviceEntryDisabler.USER.value, None),
}
)
async def async_setup(hass): async def async_setup(hass):
"""Enable the Device Registry views.""" """Enable the Device Registry views."""
@ -51,14 +33,23 @@ async def async_setup(hass):
cached_list_devices = None cached_list_devices = None
@callback @callback
def websocket_list_devices(hass, connection, msg): @websocket_api.websocket_command(
{
vol.Required("type"): "config/device_registry/list",
}
)
def websocket_list_devices(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle list devices command.""" """Handle list devices command."""
nonlocal cached_list_devices nonlocal cached_list_devices
if not cached_list_devices: if not cached_list_devices:
registry = async_get(hass) registry = async_get(hass)
cached_list_devices = message_to_json( cached_list_devices = message_to_json(
websocket_api.result_message( websocket_api.result_message(
IDEN_TEMPLATE, IDEN_TEMPLATE, # type: ignore[arg-type]
[_entry_dict(entry) for entry in registry.devices.values()], [_entry_dict(entry) for entry in registry.devices.values()],
) )
) )
@ -72,12 +63,8 @@ async def async_setup(hass):
run_immediately=True, run_immediately=True,
) )
websocket_api.async_register_command( websocket_api.async_register_command(hass, websocket_list_devices)
hass, WS_TYPE_LIST, websocket_list_devices, SCHEMA_WS_LIST websocket_api.async_register_command(hass, websocket_update_device)
)
websocket_api.async_register_command(
hass, WS_TYPE_UPDATE, websocket_update_device, SCHEMA_WS_UPDATE
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, websocket_remove_config_entry_from_device hass, websocket_remove_config_entry_from_device
) )
@ -85,9 +72,24 @@ async def async_setup(hass):
@require_admin @require_admin
@websocket_api.websocket_command(
{
vol.Required("type"): "config/device_registry/update",
vol.Optional("area_id"): vol.Any(str, None),
vol.Required("device_id"): str,
# We only allow setting disabled_by user via API.
# No Enum support like this in voluptuous, use .value
vol.Optional("disabled_by"): vol.Any(DeviceEntryDisabler.USER.value, None),
vol.Optional("name_by_user"): vol.Any(str, None),
}
)
@callback @callback
def websocket_update_device(hass, connection, msg): def websocket_update_device(
"""Handle update area websocket command.""" hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle update device websocket command."""
registry = async_get(hass) registry = async_get(hass)
msg.pop("type") msg.pop("type")
@ -105,8 +107,8 @@ def websocket_update_device(hass, connection, msg):
@websocket_api.websocket_command( @websocket_api.websocket_command(
{ {
"type": "config/device_registry/remove_config_entry", "type": "config/device_registry/remove_config_entry",
"device_id": str,
"config_entry_id": str, "config_entry_id": str,
"device_id": str,
} }
) )
@websocket_api.async_response @websocket_api.async_response