Avoid bytes to string to bytes conversion in websocket api (#108139)
This commit is contained in:
parent
ad35113e86
commit
60ab360fe7
16 changed files with 137 additions and 93 deletions
|
@ -104,7 +104,7 @@ def pong_message(iden: int) -> dict[str, Any]:
|
|||
|
||||
@callback
|
||||
def _forward_events_check_permissions(
|
||||
send_message: Callable[[str | dict[str, Any] | Callable[[], str]], None],
|
||||
send_message: Callable[[bytes | str | dict[str, Any] | Callable[[], str]], None],
|
||||
user: User,
|
||||
msg_id: int,
|
||||
event: Event,
|
||||
|
@ -124,7 +124,7 @@ def _forward_events_check_permissions(
|
|||
|
||||
@callback
|
||||
def _forward_events_unconditional(
|
||||
send_message: Callable[[str | dict[str, Any] | Callable[[], str]], None],
|
||||
send_message: Callable[[bytes | str | dict[str, Any] | Callable[[], str]], None],
|
||||
msg_id: int,
|
||||
event: Event,
|
||||
) -> None:
|
||||
|
@ -352,17 +352,17 @@ def handle_get_states(
|
|||
|
||||
|
||||
def _send_handle_get_states_response(
|
||||
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
||||
connection: ActiveConnection, msg_id: int, serialized_states: list[bytes]
|
||||
) -> None:
|
||||
"""Send handle get states response."""
|
||||
connection.send_message(
|
||||
construct_result_message(msg_id, f'[{",".join(serialized_states)}]')
|
||||
construct_result_message(msg_id, b"[" + b",".join(serialized_states) + b"]")
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _forward_entity_changes(
|
||||
send_message: Callable[[str | dict[str, Any] | Callable[[], str]], None],
|
||||
send_message: Callable[[str | bytes | dict[str, Any] | Callable[[], str]], None],
|
||||
entity_ids: set[str],
|
||||
user: User,
|
||||
msg_id: int,
|
||||
|
@ -444,11 +444,19 @@ def handle_subscribe_entities(
|
|||
|
||||
|
||||
def _send_handle_entities_init_response(
|
||||
connection: ActiveConnection, msg_id: int, serialized_states: list[str]
|
||||
connection: ActiveConnection, msg_id: int, serialized_states: list[bytes]
|
||||
) -> None:
|
||||
"""Send handle entities init response."""
|
||||
connection.send_message(
|
||||
f'{{"id":{msg_id},"type":"event","event":{{"a":{{{",".join(serialized_states)}}}}}}}'
|
||||
b"".join(
|
||||
(
|
||||
b'{"id":',
|
||||
str(msg_id).encode(),
|
||||
b',"type":"event","event":{"a":{',
|
||||
b",".join(serialized_states),
|
||||
b"}}}",
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
@ -474,7 +482,7 @@ async def handle_get_services(
|
|||
) -> None:
|
||||
"""Handle get services command."""
|
||||
payload = await _async_get_all_descriptions_json(hass)
|
||||
connection.send_message(construct_result_message(msg["id"], payload))
|
||||
connection.send_message(construct_result_message(msg["id"], payload.encode()))
|
||||
|
||||
|
||||
@callback
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue