mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Reduce string copy needed to subscribe to entities (#102870)
This commit is contained in:
parent
18fa5b8532
commit
5648dc6cd1
@ -53,7 +53,7 @@ from homeassistant.util.json import format_unserializable_data
|
|||||||
|
|
||||||
from . import const, decorators, messages
|
from . import const, decorators, messages
|
||||||
from .connection import ActiveConnection
|
from .connection import ActiveConnection
|
||||||
from .messages import construct_event_message, construct_result_message
|
from .messages import construct_result_message
|
||||||
|
|
||||||
ALL_SERVICE_DESCRIPTIONS_JSON_CACHE = "websocket_api_all_service_descriptions_json"
|
ALL_SERVICE_DESCRIPTIONS_JSON_CACHE = "websocket_api_all_service_descriptions_json"
|
||||||
|
|
||||||
@ -294,8 +294,9 @@ def _send_handle_get_states_response(
|
|||||||
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Send handle get states response."""
|
"""Send handle get states response."""
|
||||||
joined_states = ",".join(serialized_states)
|
connection.send_message(
|
||||||
connection.send_message(construct_result_message(msg_id, f"[{joined_states}]"))
|
construct_result_message(msg_id, f'[{",".join(serialized_states)}]')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -383,9 +384,8 @@ def _send_handle_entities_init_response(
|
|||||||
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Send handle entities init response."""
|
"""Send handle entities init response."""
|
||||||
joined_states = ",".join(serialized_states)
|
|
||||||
connection.send_message(
|
connection.send_message(
|
||||||
construct_event_message(msg_id, f'{{"a":{{{joined_states}}}}}')
|
f'{{"id":{msg_id},"type":"event","event":{{"a":{{{",".join(serialized_states)}}}}}}}'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -159,8 +159,7 @@ class WebSocketHandler:
|
|||||||
messages.append(message)
|
messages.append(message)
|
||||||
messages_remaining -= 1
|
messages_remaining -= 1
|
||||||
|
|
||||||
joined_messages = ",".join(messages)
|
coalesced_messages = f'[{",".join(messages)}]'
|
||||||
coalesced_messages = f"[{joined_messages}]"
|
|
||||||
if debug_enabled:
|
if debug_enabled:
|
||||||
debug("%s: Sending %s", self.description, coalesced_messages)
|
debug("%s: Sending %s", self.description, coalesced_messages)
|
||||||
await send_str(coalesced_messages)
|
await send_str(coalesced_messages)
|
||||||
|
@ -74,11 +74,6 @@ def error_message(iden: int | None, code: str, message: str) -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def construct_event_message(iden: int, payload: str) -> str:
|
|
||||||
"""Construct an event message JSON."""
|
|
||||||
return f'{{"id":{iden},"type":"event","event":{payload}}}'
|
|
||||||
|
|
||||||
|
|
||||||
def event_message(iden: int, event: Any) -> dict[str, Any]:
|
def event_message(iden: int, event: Any) -> dict[str, Any]:
|
||||||
"""Return an event message."""
|
"""Return an event message."""
|
||||||
return {"id": iden, "type": "event", "event": event}
|
return {"id": iden, "type": "event", "event": event}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user