Add websocket type hints in components (#80654)

* Add websocket type hints in components

* Adjust

* Apply suggestion

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
epenet 2022-10-20 13:41:14 +02:00 committed by GitHub
parent ae7eb9cef9
commit 2c43606922
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 116 additions and 40 deletions

View file

@ -689,7 +689,11 @@ async def thingtalk_convert(
@websocket_api.websocket_command({"type": "cloud/tts/info"}) @websocket_api.websocket_command({"type": "cloud/tts/info"})
def tts_info(hass, connection, msg): def tts_info(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Fetch available tts info.""" """Fetch available tts info."""
connection.send_result( connection.send_result(
msg["id"], {"languages": [(lang, gender.value) for lang, gender in MAP_VOICE]} msg["id"], {"languages": [(lang, gender.value) for lang, gender in MAP_VOICE]}

View file

@ -1,8 +1,10 @@
"""HTTP views to interact with the area registry.""" """HTTP views to interact with the area registry."""
from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
from homeassistant.core import callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.area_registry import async_get from homeassistant.helpers.area_registry import async_get
@ -17,7 +19,11 @@ async def async_setup(hass):
@websocket_api.websocket_command({vol.Required("type"): "config/area_registry/list"}) @websocket_api.websocket_command({vol.Required("type"): "config/area_registry/list"})
@callback @callback
def websocket_list_areas(hass, connection, msg): def websocket_list_areas(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle list areas command.""" """Handle list areas command."""
registry = async_get(hass) registry = async_get(hass)
connection.send_result( connection.send_result(
@ -35,7 +41,11 @@ def websocket_list_areas(hass, connection, msg):
) )
@websocket_api.require_admin @websocket_api.require_admin
@callback @callback
def websocket_create_area(hass, connection, msg): def websocket_create_area(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Create area command.""" """Create area command."""
registry = async_get(hass) registry = async_get(hass)
@ -59,7 +69,11 @@ def websocket_create_area(hass, connection, msg):
) )
@websocket_api.require_admin @websocket_api.require_admin
@callback @callback
def websocket_delete_area(hass, connection, msg): def websocket_delete_area(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Delete area command.""" """Delete area command."""
registry = async_get(hass) registry = async_get(hass)
@ -81,7 +95,11 @@ def websocket_delete_area(hass, connection, msg):
) )
@websocket_api.require_admin @websocket_api.require_admin
@callback @callback
def websocket_update_area(hass, connection, msg): def websocket_update_area(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle update area websocket command.""" """Handle update area websocket command."""
registry = async_get(hass) registry = async_get(hass)

View file

@ -243,7 +243,11 @@ class OptionManagerFlowResourceView(FlowManagerResourceView):
@websocket_api.require_admin @websocket_api.require_admin
@websocket_api.websocket_command({"type": "config_entries/flow/progress"}) @websocket_api.websocket_command({"type": "config_entries/flow/progress"})
def config_entries_progress(hass, connection, msg): def config_entries_progress(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""List flows that are in progress but not started by a user. """List flows that are in progress but not started by a user.
Example of a non-user initiated flow is a discovered Hue hub that Example of a non-user initiated flow is a discovered Hue hub that

View file

@ -185,7 +185,7 @@ async def ws_validate(
async def ws_solar_forecast( async def ws_solar_forecast(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict, msg: dict[str, Any],
manager: EnergyManager, manager: EnergyManager,
) -> None: ) -> None:
"""Handle solar forecast command.""" """Handle solar forecast command."""

View file

@ -614,7 +614,7 @@ class ManifestJSONView(HomeAssistantView):
@callback @callback
@websocket_api.websocket_command({"type": "get_panels"}) @websocket_api.websocket_command({"type": "get_panels"})
def websocket_get_panels( def websocket_get_panels(
hass: HomeAssistant, connection: ActiveConnection, msg: dict hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Handle get panels command.""" """Handle get panels command."""
user_is_admin = connection.user.is_admin user_is_admin = connection.user.is_admin
@ -630,7 +630,7 @@ def websocket_get_panels(
@callback @callback
@websocket_api.websocket_command({"type": "frontend/get_themes"}) @websocket_api.websocket_command({"type": "frontend/get_themes"})
def websocket_get_themes( def websocket_get_themes(
hass: HomeAssistant, connection: ActiveConnection, msg: dict hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Handle get themes command.""" """Handle get themes command."""
if hass.config.safe_mode: if hass.config.safe_mode:

View file

@ -57,7 +57,11 @@ def _ensure_webhook_access(func):
vol.Required("confirm_id"): str, vol.Required("confirm_id"): str,
} }
) )
def handle_push_notification_confirm(hass, connection, msg): def handle_push_notification_confirm(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Confirm receipt of a push notification.""" """Confirm receipt of a push notification."""
channel: PushChannel | None = hass.data[DOMAIN][DATA_PUSH_CHANNEL].get( channel: PushChannel | None = hass.data[DOMAIN][DATA_PUSH_CHANNEL].get(
msg["webhook_id"] msg["webhook_id"]

View file

@ -496,7 +496,11 @@ async def async_reload_manual_mqtt_items(hass: HomeAssistant) -> None:
{vol.Required("type"): "mqtt/device/debug_info", vol.Required("device_id"): str} {vol.Required("type"): "mqtt/device/debug_info", vol.Required("device_id"): str}
) )
@callback @callback
def websocket_mqtt_info(hass, connection, msg): def websocket_mqtt_info(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Get MQTT debug info for device.""" """Get MQTT debug info for device."""
device_id = msg["device_id"] device_id = msg["device_id"]
mqtt_info = debug_info.info_for_device(hass, device_id) mqtt_info = debug_info.info_for_device(hass, device_id)

View file

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -546,8 +547,10 @@ class Person(collection.CollectionEntity, RestoreEntity):
@websocket_api.websocket_command({vol.Required(CONF_TYPE): "person/list"}) @websocket_api.websocket_command({vol.Required(CONF_TYPE): "person/list"})
def ws_list_person( def ws_list_person(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg hass: HomeAssistant,
): connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""List persons.""" """List persons."""
yaml, storage, _ = hass.data[DOMAIN] yaml, storage, _ = hass.data[DOMAIN]
connection.send_result( connection.send_result(

View file

@ -207,7 +207,7 @@ async def ws_validate_statistics(
) )
@callback @callback
def ws_clear_statistics( def ws_clear_statistics(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Clear statistics for a list of statistic_ids. """Clear statistics for a list of statistic_ids.
@ -246,7 +246,7 @@ async def ws_get_statistics_metadata(
) )
@callback @callback
def ws_update_statistics_metadata( def ws_update_statistics_metadata(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Update statistics metadata for a statistic_id. """Update statistics metadata for a statistic_id.
@ -269,7 +269,7 @@ def ws_update_statistics_metadata(
) )
@callback @callback
def ws_change_statistics_unit( def ws_change_statistics_unit(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Change the unit_of_measurement for a statistic_id. """Change the unit_of_measurement for a statistic_id.
@ -371,7 +371,7 @@ async def ws_adjust_sum_statistics(
) )
@callback @callback
def ws_import_statistics( def ws_import_statistics(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Import statistics.""" """Import statistics."""
metadata = msg["metadata"] metadata = msg["metadata"]
@ -391,7 +391,7 @@ def ws_import_statistics(
) )
@callback @callback
def ws_info( def ws_info(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Return status of the recorder.""" """Return status of the recorder."""
instance = get_instance(hass) instance = get_instance(hass)

View file

@ -46,7 +46,7 @@ def async_setup(hass: HomeAssistant) -> None:
} }
) )
def ws_ignore_issue( def ws_ignore_issue(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Fix an issue.""" """Fix an issue."""
async_ignore_issue(hass, msg["domain"], msg["issue_id"], msg["ignore"]) async_ignore_issue(hass, msg["domain"], msg["issue_id"], msg["ignore"])
@ -61,7 +61,7 @@ def ws_ignore_issue(
) )
@callback @callback
def ws_list_issues( def ws_list_issues(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Return a list of issues.""" """Return a list of issues."""

View file

@ -19,6 +19,7 @@ Other integrations may use this integration with these steps:
from __future__ import annotations from __future__ import annotations
import logging import logging
from typing import Any
import async_timeout import async_timeout
from rtsp_to_webrtc.client import get_adaptive_client from rtsp_to_webrtc.client import get_adaptive_client
@ -109,7 +110,7 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
) )
@callback @callback
def ws_get_settings( def ws_get_settings(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
) -> None: ) -> None:
"""Handle the websocket command.""" """Handle the websocket command."""
connection.send_result( connection.send_result(

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections import defaultdict, deque from collections import defaultdict, deque
import logging import logging
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -44,7 +45,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
} }
) )
@callback @callback
def websocket_search_related(hass, connection, msg): def websocket_search_related(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Handle search.""" """Handle search."""
searcher = Searcher( searcher = Searcher(
hass, hass,

View file

@ -360,8 +360,8 @@ class ClearCompletedItemsView(http.HomeAssistantView):
@callback @callback
def websocket_handle_items( def websocket_handle_items(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict, msg: dict[str, Any],
) -> None: ) -> None:
"""Handle get shopping_list items.""" """Handle get shopping_list items."""
connection.send_message( connection.send_message(
@ -372,7 +372,7 @@ def websocket_handle_items(
@websocket_api.async_response @websocket_api.async_response
async def websocket_handle_add( async def websocket_handle_add(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Handle add item to shopping_list.""" """Handle add item to shopping_list."""
@ -383,7 +383,7 @@ async def websocket_handle_add(
@websocket_api.async_response @websocket_api.async_response
async def websocket_handle_update( async def websocket_handle_update(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Handle update shopping_list item.""" """Handle update shopping_list item."""
@ -406,7 +406,7 @@ async def websocket_handle_update(
@websocket_api.async_response @websocket_api.async_response
async def websocket_handle_clear( async def websocket_handle_clear(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict[str, Any], msg: dict[str, Any],
) -> None: ) -> None:
"""Handle clearing shopping_list items.""" """Handle clearing shopping_list items."""
@ -422,8 +422,8 @@ async def websocket_handle_clear(
) )
def websocket_handle_reorder( def websocket_handle_reorder(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.connection.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: dict, msg: dict[str, Any],
) -> None: ) -> None:
"""Handle reordering shopping_list items.""" """Handle reordering shopping_list items."""
msg_id = msg.pop("id") msg_id = msg.pop("id")

View file

@ -3,6 +3,7 @@ from collections import OrderedDict, deque
import logging import logging
import re import re
import traceback import traceback
from typing import Any
import voluptuous as vol import voluptuous as vol
@ -240,8 +241,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@websocket_api.websocket_command({vol.Required("type"): "system_log/list"}) @websocket_api.websocket_command({vol.Required("type"): "system_log/list"})
@callback @callback
def list_errors( def list_errors(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any]
): ) -> None:
"""List all possible diagnostic handlers.""" """List all possible diagnostic handlers."""
connection.send_result( connection.send_result(
msg["id"], msg["id"],

View file

@ -135,7 +135,11 @@ async def websocket_trace_contexts(
vol.Optional("run_id"): str, vol.Optional("run_id"): str,
} }
) )
def websocket_breakpoint_set(hass, connection, msg): def websocket_breakpoint_set(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Set breakpoint.""" """Set breakpoint."""
key = f"{msg['domain']}.{msg['item_id']}" key = f"{msg['domain']}.{msg['item_id']}"
node = msg["node"] node = msg["node"]
@ -162,7 +166,11 @@ def websocket_breakpoint_set(hass, connection, msg):
vol.Optional("run_id"): str, vol.Optional("run_id"): str,
} }
) )
def websocket_breakpoint_clear(hass, connection, msg): def websocket_breakpoint_clear(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Clear breakpoint.""" """Clear breakpoint."""
key = f"{msg['domain']}.{msg['item_id']}" key = f"{msg['domain']}.{msg['item_id']}"
node = msg["node"] node = msg["node"]
@ -176,7 +184,11 @@ def websocket_breakpoint_clear(hass, connection, msg):
@callback @callback
@websocket_api.require_admin @websocket_api.require_admin
@websocket_api.websocket_command({vol.Required("type"): "trace/debug/breakpoint/list"}) @websocket_api.websocket_command({vol.Required("type"): "trace/debug/breakpoint/list"})
def websocket_breakpoint_list(hass, connection, msg): def websocket_breakpoint_list(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""List breakpoints.""" """List breakpoints."""
breakpoints = breakpoint_list(hass) breakpoints = breakpoint_list(hass)
for _breakpoint in breakpoints: for _breakpoint in breakpoints:
@ -191,7 +203,11 @@ def websocket_breakpoint_list(hass, connection, msg):
@websocket_api.websocket_command( @websocket_api.websocket_command(
{vol.Required("type"): "trace/debug/breakpoint/subscribe"} {vol.Required("type"): "trace/debug/breakpoint/subscribe"}
) )
def websocket_subscribe_breakpoint_events(hass, connection, msg): def websocket_subscribe_breakpoint_events(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Subscribe to breakpoint events.""" """Subscribe to breakpoint events."""
@callback @callback
@ -240,7 +256,11 @@ def websocket_subscribe_breakpoint_events(hass, connection, msg):
vol.Required("run_id"): str, vol.Required("run_id"): str,
} }
) )
def websocket_debug_continue(hass, connection, msg): def websocket_debug_continue(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Resume execution of halted script or automation.""" """Resume execution of halted script or automation."""
key = f"{msg['domain']}.{msg['item_id']}" key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"] run_id = msg["run_id"]
@ -260,7 +280,11 @@ def websocket_debug_continue(hass, connection, msg):
vol.Required("run_id"): str, vol.Required("run_id"): str,
} }
) )
def websocket_debug_step(hass, connection, msg): def websocket_debug_step(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Single step a halted script or automation.""" """Single step a halted script or automation."""
key = f"{msg['domain']}.{msg['item_id']}" key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"] run_id = msg["run_id"]
@ -280,7 +304,11 @@ def websocket_debug_step(hass, connection, msg):
vol.Required("run_id"): str, vol.Required("run_id"): str,
} }
) )
def websocket_debug_stop(hass, connection, msg): def websocket_debug_stop(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Stop a halted script or automation.""" """Stop a halted script or automation."""
key = f"{msg['domain']}.{msg['item_id']}" key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"] run_id = msg["run_id"]

View file

@ -168,7 +168,11 @@ class WebhookView(HomeAssistantView):
} }
) )
@callback @callback
def websocket_list(hass, connection, msg): def websocket_list(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Return a list of webhooks.""" """Return a list of webhooks."""
handlers = hass.data.setdefault(DOMAIN, {}) handlers = hass.data.setdefault(DOMAIN, {})
result = [ result = [