mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 02:38:10 +02:00
Avoid re-encoding the message id as bytes for every event/state change (#116460)
This commit is contained in:
@ -105,7 +105,7 @@ def pong_message(iden: int) -> dict[str, Any]:
|
||||
def _forward_events_check_permissions(
|
||||
send_message: Callable[[bytes | str | dict[str, Any] | Callable[[], str]], None],
|
||||
user: User,
|
||||
msg_id: int,
|
||||
message_id_as_bytes: bytes,
|
||||
event: Event,
|
||||
) -> None:
|
||||
"""Forward state changed events to websocket."""
|
||||
@ -118,17 +118,17 @@ def _forward_events_check_permissions(
|
||||
and not permissions.check_entity(event.data["entity_id"], POLICY_READ)
|
||||
):
|
||||
return
|
||||
send_message(messages.cached_event_message(msg_id, event))
|
||||
send_message(messages.cached_event_message(message_id_as_bytes, event))
|
||||
|
||||
|
||||
@callback
|
||||
def _forward_events_unconditional(
|
||||
send_message: Callable[[bytes | str | dict[str, Any] | Callable[[], str]], None],
|
||||
msg_id: int,
|
||||
message_id_as_bytes: bytes,
|
||||
event: Event,
|
||||
) -> None:
|
||||
"""Forward events to websocket."""
|
||||
send_message(messages.cached_event_message(msg_id, event))
|
||||
send_message(messages.cached_event_message(message_id_as_bytes, event))
|
||||
|
||||
|
||||
@callback
|
||||
@ -152,16 +152,18 @@ def handle_subscribe_events(
|
||||
)
|
||||
raise Unauthorized(user_id=connection.user.id)
|
||||
|
||||
message_id_as_bytes = str(msg["id"]).encode()
|
||||
|
||||
if event_type == EVENT_STATE_CHANGED:
|
||||
forward_events = partial(
|
||||
_forward_events_check_permissions,
|
||||
connection.send_message,
|
||||
connection.user,
|
||||
msg["id"],
|
||||
message_id_as_bytes,
|
||||
)
|
||||
else:
|
||||
forward_events = partial(
|
||||
_forward_events_unconditional, connection.send_message, msg["id"]
|
||||
_forward_events_unconditional, connection.send_message, message_id_as_bytes
|
||||
)
|
||||
|
||||
connection.subscriptions[msg["id"]] = hass.bus.async_listen(
|
||||
@ -366,7 +368,7 @@ def _forward_entity_changes(
|
||||
send_message: Callable[[str | bytes | dict[str, Any] | Callable[[], str]], None],
|
||||
entity_ids: set[str],
|
||||
user: User,
|
||||
msg_id: int,
|
||||
message_id_as_bytes: bytes,
|
||||
event: Event[EventStateChangedData],
|
||||
) -> None:
|
||||
"""Forward entity state changed events to websocket."""
|
||||
@ -382,7 +384,7 @@ def _forward_entity_changes(
|
||||
and not permissions.check_entity(event.data["entity_id"], POLICY_READ)
|
||||
):
|
||||
return
|
||||
send_message(messages.cached_state_diff_message(msg_id, event))
|
||||
send_message(messages.cached_state_diff_message(message_id_as_bytes, event))
|
||||
|
||||
|
||||
@callback
|
||||
@ -401,6 +403,7 @@ def handle_subscribe_entities(
|
||||
# state changed events or we will introduce a race condition
|
||||
# where some states are missed
|
||||
states = _async_get_allowed_states(hass, connection)
|
||||
message_id_as_bytes = str(msg["id"]).encode()
|
||||
connection.subscriptions[msg["id"]] = hass.bus.async_listen(
|
||||
EVENT_STATE_CHANGED,
|
||||
partial(
|
||||
@ -408,7 +411,7 @@ def handle_subscribe_entities(
|
||||
connection.send_message,
|
||||
entity_ids,
|
||||
connection.user,
|
||||
msg["id"],
|
||||
message_id_as_bytes,
|
||||
),
|
||||
)
|
||||
connection.send_result(msg["id"])
|
||||
|
Reference in New Issue
Block a user