1 """HTTP views to interact with the device registry."""
3 from __future__
import annotations
5 from typing
import Any, cast
7 import voluptuous
as vol
9 from homeassistant
import loader
20 """Enable the Device Registry views."""
22 websocket_api.async_register_command(hass, websocket_list_devices)
23 websocket_api.async_register_command(hass, websocket_update_device)
24 websocket_api.async_register_command(
25 hass, websocket_remove_config_entry_from_device
31 @websocket_api.websocket_command(
{
vol.Required("type"):
"config/device_registry/list",
39 """Handle list devices command."""
40 registry = dr.async_get(hass)
43 f
'{{"id":{msg["id"]},"type": "{websocket_api.TYPE_RESULT}",'
44 f
'"success":true,"result": ['
50 for entry
in registry.devices.values()
51 if entry.json_repr
is not None
54 msg_json = b
"".join((msg_json_prefix, inner, b
"]}"))
55 connection.send_message(msg_json)
59 @websocket_api.websocket_command(
{
vol.Required("type"):
"config/device_registry/update",
60 vol.Optional(
"area_id"): vol.Any(str,
None),
61 vol.Required(
"device_id"): str,
64 vol.Optional(
"disabled_by"): vol.Any(DeviceEntryDisabler.USER.value,
None),
65 vol.Optional(
"labels"): [str],
66 vol.Optional(
"name_by_user"): vol.Any(str,
None),
75 """Handle update device websocket command."""
76 registry = dr.async_get(hass)
79 msg_id = msg.pop(
"id")
81 if msg.get(
"disabled_by")
is not None:
86 msg[
"labels"] = set(msg[
"labels"])
88 entry = cast(DeviceEntry, registry.async_update_device(**msg))
90 connection.send_message(websocket_api.result_message(msg_id, entry.dict_repr))
93 @websocket_api.require_admin
94 @websocket_api.websocket_command(
{
"type": "config/device_registry/remove_config_entry",
"config_entry_id": str,
"device_id": str,
}
)
95 @websocket_api.async_response
101 """Remove config entry from a device."""
102 registry = dr.async_get(hass)
103 config_entry_id = msg[
"config_entry_id"]
104 device_id = msg[
"device_id"]
106 if (config_entry := hass.config_entries.async_get_entry(config_entry_id))
is None:
109 if not config_entry.supports_remove_device:
112 if (device_entry := registry.async_get(device_id))
is None:
115 if config_entry_id
not in device_entry.config_entries:
119 integration = await loader.async_get_integration(hass, config_entry.domain)
120 component = await integration.async_get_component()
124 if not await component.async_remove_config_entry_device(
125 hass, config_entry, device_entry
128 "Failed to remove device entry, rejected by integration"
132 if registry.async_get(device_id):
133 entry = registry.async_update_device(
134 device_id, remove_config_entry_id=config_entry_id
137 entry_as_dict = entry.dict_repr
if entry
else None
141 connection.send_message(websocket_api.result_message(msg[
"id"], entry_as_dict))
142
bool async_setup(HomeAssistant hass)
None websocket_update_device(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None websocket_list_devices(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
None websocket_remove_config_entry_from_device(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)