mirror of
https://github.com/home-assistant/core.git
synced 2025-08-04 21:25:13 +02:00
Skip processing websocket_api schema if has no arguments
About 40% of the websocket commands on first connection have no arguments. We can skip processing the schema for these cases
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable, Hashable
|
from collections.abc import Callable, Hashable
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, Literal
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@@ -65,9 +65,9 @@ class ActiveConnection:
|
|||||||
self.last_id = 0
|
self.last_id = 0
|
||||||
self.can_coalesce = False
|
self.can_coalesce = False
|
||||||
self.supported_features: dict[str, float] = {}
|
self.supported_features: dict[str, float] = {}
|
||||||
self.handlers: dict[str, tuple[MessageHandler, vol.Schema]] = self.hass.data[
|
self.handlers: dict[str, tuple[MessageHandler, vol.Schema | Literal[False]]] = (
|
||||||
const.DOMAIN
|
self.hass.data[const.DOMAIN]
|
||||||
]
|
)
|
||||||
self.binary_handlers: list[BinaryHandler | None] = []
|
self.binary_handlers: list[BinaryHandler | None] = []
|
||||||
current_connection.set(self)
|
current_connection.set(self)
|
||||||
|
|
||||||
@@ -185,6 +185,7 @@ class ActiveConnection:
|
|||||||
or (
|
or (
|
||||||
not (cur_id := msg.get("id"))
|
not (cur_id := msg.get("id"))
|
||||||
or type(cur_id) is not int # noqa: E721
|
or type(cur_id) is not int # noqa: E721
|
||||||
|
or cur_id < 0
|
||||||
or not (type_ := msg.get("type"))
|
or not (type_ := msg.get("type"))
|
||||||
or type(type_) is not str # noqa: E721
|
or type(type_) is not str # noqa: E721
|
||||||
)
|
)
|
||||||
@@ -220,7 +221,7 @@ class ActiveConnection:
|
|||||||
handler, schema = handler_schema
|
handler, schema = handler_schema
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handler(self.hass, self, schema(msg))
|
handler(self.hass, self, msg if schema is False else schema(msg))
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # pylint: disable=broad-except
|
||||||
self.async_handle_exception(msg, err)
|
self.async_handle_exception(msg, err)
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ def websocket_command(
|
|||||||
The schema must be either a dictionary where the keys are voluptuous markers, or
|
The schema must be either a dictionary where the keys are voluptuous markers, or
|
||||||
a voluptuous.All schema where the first item is a voluptuous Mapping schema.
|
a voluptuous.All schema where the first item is a voluptuous Mapping schema.
|
||||||
"""
|
"""
|
||||||
if isinstance(schema, dict):
|
if is_dict := isinstance(schema, dict):
|
||||||
command = schema["type"]
|
command = schema["type"]
|
||||||
else:
|
else:
|
||||||
command = schema.validators[0].schema["type"]
|
command = schema.validators[0].schema["type"]
|
||||||
@@ -145,9 +145,13 @@ def websocket_command(
|
|||||||
def decorate(func: const.WebSocketCommandHandler) -> const.WebSocketCommandHandler:
|
def decorate(func: const.WebSocketCommandHandler) -> const.WebSocketCommandHandler:
|
||||||
"""Decorate ws command function."""
|
"""Decorate ws command function."""
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
if isinstance(schema, dict):
|
if is_dict and len(schema) == 1: # type only empty schema
|
||||||
|
func._ws_schema = False # type: ignore[attr-defined]
|
||||||
|
elif is_dict:
|
||||||
func._ws_schema = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend(schema) # type: ignore[attr-defined]
|
func._ws_schema = messages.BASE_COMMAND_MESSAGE_SCHEMA.extend(schema) # type: ignore[attr-defined]
|
||||||
else:
|
else:
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
assert not isinstance(schema, dict)
|
||||||
extended_schema = vol.All(
|
extended_schema = vol.All(
|
||||||
schema.validators[0].extend(
|
schema.validators[0].extend(
|
||||||
messages.BASE_COMMAND_MESSAGE_SCHEMA.schema
|
messages.BASE_COMMAND_MESSAGE_SCHEMA.schema
|
||||||
|
Reference in New Issue
Block a user