From eee1ede5bba4a95e33b0d9d901cbe6319a220779 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 20 Oct 2022 15:57:43 +0200 Subject: [PATCH] Add websocket type hints in lovelace (#80537) --- .../components/lovelace/websocket.py | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/lovelace/websocket.py b/homeassistant/components/lovelace/websocket.py index 66ec7f22b09..423ba3117ea 100644 --- a/homeassistant/components/lovelace/websocket.py +++ b/homeassistant/components/lovelace/websocket.py @@ -1,23 +1,31 @@ """Websocket API for Lovelace.""" +from __future__ import annotations + from functools import wraps +from typing import Any import voluptuous as vol from homeassistant.components import websocket_api -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv from .const import CONF_URL_PATH, DOMAIN, ConfigNotFound +from .dashboard import LovelaceStorage def _handle_errors(func): """Handle error with WebSocket calls.""" @wraps(func) - async def send_with_error_handling(hass, connection, msg): + async def send_with_error_handling( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + ) -> None: url_path = msg.get(CONF_URL_PATH) - config = hass.data[DOMAIN]["dashboards"].get(url_path) + config: LovelaceStorage | None = hass.data[DOMAIN]["dashboards"].get(url_path) if config is None: connection.send_error( @@ -44,7 +52,11 @@ def _handle_errors(func): @websocket_api.websocket_command({"type": "lovelace/resources"}) @websocket_api.async_response -async def websocket_lovelace_resources(hass, connection, msg): +async def websocket_lovelace_resources( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Send Lovelace UI resources over WebSocket configuration.""" resources = hass.data[DOMAIN]["resources"] @@ -64,7 +76,12 @@ async def websocket_lovelace_resources(hass, connection, msg): ) @websocket_api.async_response @_handle_errors -async def websocket_lovelace_config(hass, connection, msg, config): +async def websocket_lovelace_config( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + config: LovelaceStorage, +) -> None: """Send Lovelace UI config over WebSocket configuration.""" return await config.async_load(msg["force"]) @@ -79,7 +96,12 @@ async def websocket_lovelace_config(hass, connection, msg, config): ) @websocket_api.async_response @_handle_errors -async def websocket_lovelace_save_config(hass, connection, msg, config): +async def websocket_lovelace_save_config( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + config: LovelaceStorage, +) -> None: """Save Lovelace UI configuration.""" await config.async_save(msg["config"]) @@ -93,14 +115,23 @@ async def websocket_lovelace_save_config(hass, connection, msg, config): ) @websocket_api.async_response @_handle_errors -async def websocket_lovelace_delete_config(hass, connection, msg, config): +async def websocket_lovelace_delete_config( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + config: LovelaceStorage, +) -> None: """Delete Lovelace UI configuration.""" await config.async_delete() @websocket_api.websocket_command({"type": "lovelace/dashboards/list"}) @callback -def websocket_lovelace_dashboards(hass, connection, msg): +def websocket_lovelace_dashboards( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Delete Lovelace UI configuration.""" connection.send_result( msg["id"],