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>
pull/80671/head
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"})
def tts_info(hass, connection, msg):
def tts_info(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Fetch available tts info."""
connection.send_result(
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."""
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.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"})
@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."""
registry = async_get(hass)
connection.send_result(
@ -35,7 +41,11 @@ def websocket_list_areas(hass, connection, msg):
)
@websocket_api.require_admin
@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."""
registry = async_get(hass)
@ -59,7 +69,11 @@ def websocket_create_area(hass, connection, msg):
)
@websocket_api.require_admin
@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."""
registry = async_get(hass)
@ -81,7 +95,11 @@ def websocket_delete_area(hass, connection, msg):
)
@websocket_api.require_admin
@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."""
registry = async_get(hass)

View File

@ -243,7 +243,11 @@ class OptionManagerFlowResourceView(FlowManagerResourceView):
@websocket_api.require_admin
@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.
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(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict,
msg: dict[str, Any],
manager: EnergyManager,
) -> None:
"""Handle solar forecast command."""

View File

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

View File

@ -57,7 +57,11 @@ def _ensure_webhook_access(func):
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."""
channel: PushChannel | None = hass.data[DOMAIN][DATA_PUSH_CHANNEL].get(
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}
)
@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."""
device_id = msg["device_id"]
mqtt_info = debug_info.info_for_device(hass, device_id)

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from collections import defaultdict, deque
import logging
from typing import Any
import voluptuous as vol
@ -44,7 +45,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
}
)
@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."""
searcher = Searcher(
hass,

View File

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

View File

@ -3,6 +3,7 @@ from collections import OrderedDict, deque
import logging
import re
import traceback
from typing import Any
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"})
@callback
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."""
connection.send_result(
msg["id"],

View File

@ -135,7 +135,11 @@ async def websocket_trace_contexts(
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."""
key = f"{msg['domain']}.{msg['item_id']}"
node = msg["node"]
@ -162,7 +166,11 @@ def websocket_breakpoint_set(hass, connection, msg):
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."""
key = f"{msg['domain']}.{msg['item_id']}"
node = msg["node"]
@ -176,7 +184,11 @@ def websocket_breakpoint_clear(hass, connection, msg):
@callback
@websocket_api.require_admin
@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."""
breakpoints = breakpoint_list(hass)
for _breakpoint in breakpoints:
@ -191,7 +203,11 @@ def websocket_breakpoint_list(hass, connection, msg):
@websocket_api.websocket_command(
{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."""
@callback
@ -240,7 +256,11 @@ def websocket_subscribe_breakpoint_events(hass, connection, msg):
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."""
key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"]
@ -260,7 +280,11 @@ def websocket_debug_continue(hass, connection, msg):
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."""
key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"]
@ -280,7 +304,11 @@ def websocket_debug_step(hass, connection, msg):
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."""
key = f"{msg['domain']}.{msg['item_id']}"
run_id = msg["run_id"]

View File

@ -168,7 +168,11 @@ class WebhookView(HomeAssistantView):
}
)
@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."""
handlers = hass.data.setdefault(DOMAIN, {})
result = [