From 60640b443670fa1226f1823a8ddd68ed089552fd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 19 Oct 2022 04:15:23 +0200 Subject: [PATCH] Add websocket type hints in components (#80533) --- homeassistant/components/cloud/http_api.py | 79 ++++++++++++++++--- .../components/mobile_app/websocket_api.py | 9 ++- homeassistant/components/mqtt/__init__.py | 6 +- .../components/trace/websocket_api.py | 19 ++++- homeassistant/components/webhook/__init__.py | 6 +- 5 files changed, 99 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index ebeb79dcd2a..90b8bad108f 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -4,6 +4,7 @@ import dataclasses from functools import wraps from http import HTTPStatus import logging +from typing import Any import aiohttp import async_timeout @@ -282,7 +283,11 @@ class CloudForgotPasswordView(HomeAssistantView): @websocket_api.websocket_command({vol.Required("type"): "cloud/status"}) @websocket_api.async_response -async def websocket_cloud_status(hass, connection, msg): +async def websocket_cloud_status( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for account info. Async friendly. @@ -316,7 +321,11 @@ def _require_cloud_login(handler): @_require_cloud_login @websocket_api.websocket_command({vol.Required("type"): "cloud/subscription"}) @websocket_api.async_response -async def websocket_subscription(hass, connection, msg): +async def websocket_subscription( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for account info.""" cloud = hass.data[DOMAIN] try: @@ -347,7 +356,11 @@ async def websocket_subscription(hass, connection, msg): } ) @websocket_api.async_response -async def websocket_update_prefs(hass, connection, msg): +async def websocket_update_prefs( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for account info.""" cloud = hass.data[DOMAIN] @@ -392,7 +405,11 @@ async def websocket_update_prefs(hass, connection, msg): ) @websocket_api.async_response @_ws_handle_cloud_errors -async def websocket_hook_create(hass, connection, msg): +async def websocket_hook_create( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for account info.""" cloud = hass.data[DOMAIN] hook = await cloud.cloudhooks.async_create(msg["webhook_id"], False) @@ -408,7 +425,11 @@ async def websocket_hook_create(hass, connection, msg): ) @websocket_api.async_response @_ws_handle_cloud_errors -async def websocket_hook_delete(hass, connection, msg): +async def websocket_hook_delete( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for account info.""" cloud = hass.data[DOMAIN] await cloud.cloudhooks.async_delete(msg["webhook_id"]) @@ -470,7 +491,11 @@ async def _account_data(hass: HomeAssistant, cloud: Cloud): @websocket_api.websocket_command({"type": "cloud/remote/connect"}) @websocket_api.async_response @_ws_handle_cloud_errors -async def websocket_remote_connect(hass, connection, msg): +async def websocket_remote_connect( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for connect remote.""" cloud = hass.data[DOMAIN] await cloud.client.prefs.async_update(remote_enabled=True) @@ -482,7 +507,11 @@ async def websocket_remote_connect(hass, connection, msg): @websocket_api.websocket_command({"type": "cloud/remote/disconnect"}) @websocket_api.async_response @_ws_handle_cloud_errors -async def websocket_remote_disconnect(hass, connection, msg): +async def websocket_remote_disconnect( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle request for disconnect remote.""" cloud = hass.data[DOMAIN] await cloud.client.prefs.async_update(remote_enabled=False) @@ -494,7 +523,11 @@ async def websocket_remote_disconnect(hass, connection, msg): @websocket_api.websocket_command({"type": "cloud/google_assistant/entities"}) @websocket_api.async_response @_ws_handle_cloud_errors -async def google_assistant_list(hass, connection, msg): +async def google_assistant_list( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """List all google assistant entities.""" cloud = hass.data[DOMAIN] gconf = await cloud.client.get_google_config() @@ -528,7 +561,11 @@ async def google_assistant_list(hass, connection, msg): ) @websocket_api.async_response @_ws_handle_cloud_errors -async def google_assistant_update(hass, connection, msg): +async def google_assistant_update( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Update google assistant config.""" cloud = hass.data[DOMAIN] changes = dict(msg) @@ -547,7 +584,11 @@ async def google_assistant_update(hass, connection, msg): @websocket_api.websocket_command({"type": "cloud/alexa/entities"}) @websocket_api.async_response @_ws_handle_cloud_errors -async def alexa_list(hass, connection, msg): +async def alexa_list( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """List all alexa entities.""" cloud = hass.data[DOMAIN] alexa_config = await cloud.client.get_alexa_config() @@ -578,7 +619,11 @@ async def alexa_list(hass, connection, msg): ) @websocket_api.async_response @_ws_handle_cloud_errors -async def alexa_update(hass, connection, msg): +async def alexa_update( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Update alexa entity config.""" cloud = hass.data[DOMAIN] changes = dict(msg) @@ -596,7 +641,11 @@ async def alexa_update(hass, connection, msg): @_require_cloud_login @websocket_api.websocket_command({"type": "cloud/alexa/sync"}) @websocket_api.async_response -async def alexa_sync(hass, connection, msg): +async def alexa_sync( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Sync with Alexa.""" cloud = hass.data[DOMAIN] alexa_config = await cloud.client.get_alexa_config() @@ -622,7 +671,11 @@ async def alexa_sync(hass, connection, msg): @websocket_api.websocket_command({"type": "cloud/thingtalk/convert", "query": str}) @websocket_api.async_response -async def thingtalk_convert(hass, connection, msg): +async def thingtalk_convert( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Convert a query.""" cloud = hass.data[DOMAIN] diff --git a/homeassistant/components/mobile_app/websocket_api.py b/homeassistant/components/mobile_app/websocket_api.py index 4b0863d77af..5225100662c 100644 --- a/homeassistant/components/mobile_app/websocket_api.py +++ b/homeassistant/components/mobile_app/websocket_api.py @@ -2,11 +2,12 @@ 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 .const import CONF_USER_ID, DATA_CONFIG_ENTRIES, DATA_PUSH_CHANNEL, DOMAIN from .push_notification import PushChannel @@ -88,7 +89,11 @@ def handle_push_notification_confirm(hass, connection, msg): ) @_ensure_webhook_access @websocket_api.async_response -async def handle_push_notification_channel(hass, connection, msg): +async def handle_push_notification_channel( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Set up a direct push notification channel.""" webhook_id = msg["webhook_id"] registered_channels: dict[str, PushChannel] = hass.data[DOMAIN][DATA_PUSH_CHANNEL] diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 36299c72608..a63b6137fea 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -511,7 +511,11 @@ def websocket_mqtt_info(hass, connection, msg): } ) @websocket_api.async_response -async def websocket_subscribe(hass, connection, msg): +async def websocket_subscribe( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Subscribe to a MQTT topic.""" if not connection.user.is_admin: raise Unauthorized diff --git a/homeassistant/components/trace/websocket_api.py b/homeassistant/components/trace/websocket_api.py index 2ea58db895a..ba79ae68968 100644 --- a/homeassistant/components/trace/websocket_api.py +++ b/homeassistant/components/trace/websocket_api.py @@ -1,5 +1,6 @@ """Websocket API for automation.""" import json +from typing import Any import voluptuous as vol @@ -54,7 +55,11 @@ def async_setup(hass: HomeAssistant) -> None: } ) @websocket_api.async_response -async def websocket_trace_get(hass, connection, msg): +async def websocket_trace_get( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Get a script or automation trace.""" key = f"{msg['domain']}.{msg['item_id']}" run_id = msg["run_id"] @@ -83,7 +88,11 @@ async def websocket_trace_get(hass, connection, msg): } ) @websocket_api.async_response -async def websocket_trace_list(hass, connection, msg): +async def websocket_trace_list( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Summarize script and automation traces.""" wanted_domain = msg["domain"] key = f"{msg['domain']}.{msg['item_id']}" if "item_id" in msg else None @@ -102,7 +111,11 @@ async def websocket_trace_list(hass, connection, msg): } ) @websocket_api.async_response -async def websocket_trace_contexts(hass, connection, msg): +async def websocket_trace_contexts( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Retrieve contexts we have traces for.""" key = f"{msg['domain']}.{msg['item_id']}" if "item_id" in msg else None diff --git a/homeassistant/components/webhook/__init__.py b/homeassistant/components/webhook/__init__.py index 449de006bf9..f78d9059ee9 100644 --- a/homeassistant/components/webhook/__init__.py +++ b/homeassistant/components/webhook/__init__.py @@ -195,7 +195,11 @@ def websocket_list(hass, connection, msg): } ) @websocket_api.async_response -async def websocket_handle(hass, connection, msg): +async def websocket_handle( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: """Handle an incoming webhook via the WS API.""" request = MockRequest( content=msg["body"].encode("utf-8"),