Reduce string copy needed to subscribe to entities (#102870)

This commit is contained in:
J. Nick Koston 2023-10-28 09:18:25 -05:00 committed by GitHub
parent 18fa5b8532
commit 5648dc6cd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 12 deletions

View file

@ -53,7 +53,7 @@ from homeassistant.util.json import format_unserializable_data
from . import const, decorators, messages
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"
@ -294,8 +294,9 @@ def _send_handle_get_states_response(
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
) -> None:
"""Send handle get states response."""
joined_states = ",".join(serialized_states)
connection.send_message(construct_result_message(msg_id, f"[{joined_states}]"))
connection.send_message(
construct_result_message(msg_id, f'[{",".join(serialized_states)}]')
)
@callback
@ -383,9 +384,8 @@ def _send_handle_entities_init_response(
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
) -> None:
"""Send handle entities init response."""
joined_states = ",".join(serialized_states)
connection.send_message(
construct_event_message(msg_id, f'{{"a":{{{joined_states}}}}}')
f'{{"id":{msg_id},"type":"event","event":{{"a":{{{",".join(serialized_states)}}}}}}}'
)

View file

@ -159,8 +159,7 @@ class WebSocketHandler:
messages.append(message)
messages_remaining -= 1
joined_messages = ",".join(messages)
coalesced_messages = f"[{joined_messages}]"
coalesced_messages = f'[{",".join(messages)}]'
if debug_enabled:
debug("%s: Sending %s", self.description, coalesced_messages)
await send_str(coalesced_messages)

View file

@ -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]:
"""Return an event message."""
return {"id": iden, "type": "event", "event": event}