Add websocket type hints in components (#80533)

This commit is contained in:
epenet 2022-10-19 04:15:23 +02:00 committed by GitHub
parent eca45f9dd0
commit 60640b4436
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 20 deletions

View file

@ -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]

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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"),