def websocket_remove_device(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Delete device.""" device_id = msg["device_id"] dev_registry = dr.async_get(hass) if not (device := dev_registry.async_get(device_id)): connection.send_error(msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found") return
async def websocket_install_config_update( hass: HomeAssistant, connection: ActiveConnection, msg: dict, entry: ConfigEntry, client: Client, ) -> None: """Check for config updates.""" success = await client.driver.async_install_config_update() connection.send_result(msg[ID], success)
async def websocket_update_log_config( hass: HomeAssistant, connection: ActiveConnection, msg: dict, entry: ConfigEntry, client: Client, ) -> None: """Update the driver log config.""" await client.driver.async_update_log_config(LogConfig(**msg[CONFIG])) connection.send_result(msg[ID], )
async def websocket_usb_scan( hass: HomeAssistant, connection: ActiveConnection, msg: dict, ) -> None: """Scan for new usb devices.""" usb_discovery: USBDiscovery = hass.data[DOMAIN] if not usb_discovery.observer_active: await usb_discovery.async_request_scan_serial() connection.send_result(msg["id"])
async def websocket_update_log_config( hass: HomeAssistant, connection: ActiveConnection, msg: dict ) -> None: """Update the driver log config.""" entry_id = msg[ENTRY_ID] client = hass.data[DOMAIN][entry_id][DATA_CLIENT] await client.driver.async_update_log_config(LogConfig(**msg[CONFIG])) connection.send_result( msg[ID], )
async def websocket_jobs( hass: HomeAssistant, connection: ActiveConnection, msg: dict, mass: MusicAssistant, ) -> None: """Return running background jobs.""" connection.send_result( msg[ID], [x.to_dict() for x in mass.jobs], )
async def async_get_mass_func(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Lookup mass object in hass data and provide to function.""" mass = hass.data.get(DOMAIN) if mass is None: connection.send_error(msg[ID], ERR_NOT_LOADED, "Music Assistant is not (yet) loaded") return await orig_func(hass, connection, msg, mass)
def websocket_get_panels(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Handle get panels command.""" user_is_admin = connection.user.is_admin panels = { panel_key: panel.to_response() for panel_key, panel in connection.hass.data[DATA_PANELS].items() if user_is_admin or not panel.require_admin } connection.send_message(websocket_api.result_message(msg["id"], panels))
async def websocket_node_state( hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node, ) -> None: """Get the state data of a Z-Wave JS node.""" connection.send_result( msg[ID], {**node.data, "values": [value.data for value in node.values.values()]}, )
async def websocket_get_log_config(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Cancel removing a node from the Z-Wave network.""" entry_id = msg[ENTRY_ID] client = hass.data[DOMAIN][entry_id][DATA_CLIENT] result = await client.driver.async_get_log_config() connection.send_result( msg[ID], dataclasses.asdict(result), )
async def websocket_device_automation_list_triggers( hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] ) -> None: """Handle request for device triggers.""" device_id = msg["device_id"] triggers = ( await async_get_device_automations( hass, DeviceAutomationType.TRIGGER, [device_id] ) ).get(device_id) connection.send_result(msg["id"], triggers)
async def websocket_device_automation_list_conditions( hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] ) -> None: """Handle request for device conditions.""" device_id = msg["device_id"] conditions = ( await async_get_device_automations( hass, DeviceAutomationType.CONDITION, [device_id] ) ).get(device_id) connection.send_result(msg["id"], conditions)
async def websocket_node_state( hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node, ) -> None: """Get the state data of a Z-Wave JS node.""" connection.send_result( msg[ID], node.data, )
async def websocket_add_group(hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]) -> None: """Add a new ZHA group.""" zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY] group_name: str = msg[GROUP_NAME] group_id: int | None = msg.get(GROUP_ID) members: list[GroupMember] | None = msg.get(ATTR_MEMBERS) group = await zha_gateway.async_create_zigpy_group(group_name, members, group_id) connection.send_result(msg[ID], group.group_info)
async def websocket_get_log_config( hass: HomeAssistant, connection: ActiveConnection, msg: dict ) -> None: """Get log configuration for the Z-Wave JS driver.""" entry_id = msg[ENTRY_ID] client = hass.data[DOMAIN][entry_id][DATA_CLIENT] result = await client.driver.async_get_log_config() connection.send_result( msg[ID], dataclasses.asdict(result), )
async def websocket_subscribe(hass: HomeAssistant, connection: ActiveConnection, msg: dict): """Subscribe to supervisor events.""" @callback def forward_messages(data): """Forward events to websocket.""" connection.send_message(websocket_api.event_message(msg[WS_ID], data)) connection.subscriptions[msg[WS_ID]] = async_dispatcher_connect( hass, EVENT_SUPERVISOR_EVENT, forward_messages) connection.send_message(websocket_api.result_message(msg[WS_ID]))
async def websocket_subscribe_log_updates( hass: HomeAssistant, connection: ActiveConnection, msg: dict, entry: ConfigEntry, client: Client, ) -> None: """Subscribe to log message events from the server.""" driver = client.driver @callback def async_cleanup() -> None: """Remove signal listeners.""" hass.async_create_task(driver.async_stop_listening_logs()) for unsub in unsubs: unsub() @callback def log_messages(event: dict) -> None: log_msg: LogMessage = event["log_message"] connection.send_message( websocket_api.event_message( msg[ID], { "type": "log_message", "log_message": { "timestamp": log_msg.timestamp, "level": log_msg.level, "primary_tags": log_msg.primary_tags, "message": log_msg.formatted_message, }, }, )) @callback def log_config_updates(event: dict) -> None: log_config: LogConfig = event["log_config"] connection.send_message( websocket_api.event_message( msg[ID], { "type": "log_config", "log_config": dataclasses.asdict(log_config), }, )) msg[DATA_UNSUBSCRIBE] = unsubs = [ driver.on("logging", log_messages), driver.on("log config updated", log_config_updates), ] connection.subscriptions[msg["id"]] = async_cleanup await driver.async_start_listening_logs() connection.send_result(msg[ID])
async def websocket_stop_exclusion( hass: HomeAssistant, connection: ActiveConnection, msg: dict ) -> None: """Cancel removing a node from the Z-Wave network.""" entry_id = msg[ENTRY_ID] client = hass.data[DOMAIN][entry_id][DATA_CLIENT] controller = client.driver.controller result = await controller.async_stop_exclusion() connection.send_result( msg[ID], result, )
async def async_get_entry_func(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Provide user specific data and store to function.""" entry_id = msg[ENTRY_ID] entry = hass.config_entries.async_get_entry(entry_id) if entry is None: connection.send_error(msg[ID], ERR_NOT_FOUND, f"Config entry {entry_id} not found") return client = hass.data[DOMAIN][entry_id][DATA_CLIENT] await orig_func(hass, connection, msg, entry, client)
async def websocket_thumb( hass: HomeAssistant, connection: ActiveConnection, msg: dict, mass: MusicAssistant, ) -> None: """Return (resized) image thumb from url or local image.""" res = await mass.metadata.get_thumbnail(msg["path"], msg.get("size"), True) connection.send_result( msg[ID], res, )
async def websocket_ping_node( hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node, ) -> None: """Ping a Z-Wave JS node.""" result = await node.async_ping() connection.send_result( msg[ID], result, )
async def websocket_get_log_config( hass: HomeAssistant, connection: ActiveConnection, msg: dict, entry: ConfigEntry, client: Client, ) -> None: """Get log configuration for the Z-Wave JS driver.""" connection.send_result( msg[ID], dataclasses.asdict(client.driver.log_config), )
async def websocket_playerqueues( hass: HomeAssistant, connection: ActiveConnection, msg: dict, mass: MusicAssistant, ) -> None: """Return all player queue's.""" result = [item.to_dict() for item in mass.players.player_queues] connection.send_result( msg[ID], result, )
async def websocket_track_preview( hass: HomeAssistant, connection: ActiveConnection, msg: dict, mass: MusicAssistant, ) -> None: """Return track preview url.""" url = await mass.streams.get_preview_url(msg[PROVIDER], msg[ITEM_ID]) connection.send_result( msg[ID], url, )
async def websocket_data_collection_status( hass: HomeAssistant, connection: ActiveConnection, msg: dict, entry: ConfigEntry, client: Client, ) -> None: """Return data collection preference and status.""" result = { OPTED_IN: entry.data.get(CONF_DATA_COLLECTION_OPTED_IN), ENABLED: await client.driver.async_is_statistics_enabled(), } connection.send_result(msg[ID], result)
async def websocket_get_device(hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]) -> None: """Get ZHA devices.""" zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY] ieee: EUI64 = msg[ATTR_IEEE] if not (zha_device := zha_gateway.devices.get(ieee)): connection.send_message( websocket_api.error_message(msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Device not found")) return
async def websocket_get_group(hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]) -> None: """Get ZHA group.""" zha_gateway: ZHAGateway = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY] group_id: int = msg[GROUP_ID] if not (zha_group := zha_gateway.groups.get(group_id)): connection.send_message( websocket_api.error_message(msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Group not found")) return
async def handle_integration_list(hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]) -> None: """Handle integrations command.""" domains = await async_get_application_credentials(hass) result = { "domains": domains, "integrations": { domain: await _async_integration_config(hass, domain) for domain in domains }, } connection.send_result(msg["id"], result)
async def websocket_get_translations(hass: HomeAssistant, connection: ActiveConnection, msg: dict) -> None: """Handle get translations command.""" resources = await async_get_translations( hass, msg["language"], msg["category"], msg.get("integration"), msg.get("config_flow"), ) connection.send_message( websocket_api.result_message(msg["id"], {"resources": resources}))
async def websocket_subscribe_firmware_update_status( hass: HomeAssistant, connection: ActiveConnection, msg: dict, node: Node, ) -> None: """Subscribe to the status of a firmware update.""" @callback def async_cleanup() -> None: """Remove signal listeners.""" for unsub in unsubs: unsub() @callback def forward_progress(event: dict) -> None: progress: FirmwareUpdateProgress = event["firmware_update_progress"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], **_get_firmware_update_progress_dict(progress), }, ) ) @callback def forward_finished(event: dict) -> None: finished: FirmwareUpdateFinished = event["firmware_update_finished"] connection.send_message( websocket_api.event_message( msg[ID], { "event": event["event"], "status": finished.status, "wait_time": finished.wait_time, }, ) ) msg[DATA_UNSUBSCRIBE] = unsubs = [ node.on("firmware update progress", forward_progress), node.on("firmware update finished", forward_finished), ] connection.subscriptions[msg["id"]] = async_cleanup progress = node.firmware_update_progress connection.send_result( msg[ID], _get_firmware_update_progress_dict(progress) if progress else None )