2023-10-23 19:32:20 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
2024-08-29 23:08:13 +02:00
|
|
|
from collections.abc import Awaitable
|
2023-10-23 19:32:20 +02:00
|
|
|
from datetime import datetime, timezone
|
|
|
|
from functools import partial
|
2024-08-29 23:08:13 +02:00
|
|
|
import time
|
|
|
|
from typing import Callable
|
2023-11-17 20:11:36 +01:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2023-11-09 00:26:51 +01:00
|
|
|
|
2023-11-11 20:06:27 +01:00
|
|
|
from google.protobuf import message
|
2023-11-09 00:26:51 +01:00
|
|
|
from zeroconf import Zeroconf
|
2023-11-11 20:43:31 +01:00
|
|
|
from zeroconf.asyncio import AsyncZeroconf
|
2023-10-23 19:32:20 +02:00
|
|
|
|
2023-11-25 14:11:34 +01:00
|
|
|
from aioesphomeapi import APIClient
|
2023-11-24 16:42:56 +01:00
|
|
|
from aioesphomeapi._frame_helper import APINoiseFrameHelper, APIPlaintextFrameHelper
|
2023-11-11 00:14:00 +01:00
|
|
|
from aioesphomeapi._frame_helper.plain_text import _cached_varuint_to_bytes
|
2023-11-23 13:36:30 +01:00
|
|
|
from aioesphomeapi.api_pb2 import (
|
|
|
|
ConnectResponse,
|
|
|
|
HelloResponse,
|
|
|
|
PingRequest,
|
|
|
|
PingResponse,
|
|
|
|
)
|
2023-11-11 00:14:00 +01:00
|
|
|
from aioesphomeapi.connection import APIConnection
|
|
|
|
from aioesphomeapi.core import MESSAGE_TYPE_TO_PROTO
|
|
|
|
|
2023-10-23 19:32:20 +02:00
|
|
|
UTC = timezone.utc
|
|
|
|
_MONOTONIC_RESOLUTION = time.get_clock_info("monotonic").resolution
|
|
|
|
# We use a partial here since it is implemented in native code
|
|
|
|
# and avoids the global lookup of UTC
|
|
|
|
utcnow: partial[datetime] = partial(datetime.now, UTC)
|
|
|
|
utcnow.__doc__ = "Get now in UTC time."
|
|
|
|
|
2023-11-11 00:14:00 +01:00
|
|
|
PROTO_TO_MESSAGE_TYPE = {v: k for k, v in MESSAGE_TYPE_TO_PROTO.items()}
|
|
|
|
|
2023-10-23 19:32:20 +02:00
|
|
|
|
2023-11-24 16:42:56 +01:00
|
|
|
def mock_data_received(
|
|
|
|
protocol: APINoiseFrameHelper | APIPlaintextFrameHelper, data: bytes
|
|
|
|
) -> None:
|
|
|
|
"""Mock data received on the protocol."""
|
|
|
|
try:
|
|
|
|
protocol.data_received(data)
|
|
|
|
except Exception as err: # pylint: disable=broad-except
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
loop.call_soon(
|
|
|
|
protocol.connection_lost,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-09 00:26:51 +01:00
|
|
|
def get_mock_zeroconf() -> MagicMock:
|
2023-11-17 20:11:36 +01:00
|
|
|
with patch("zeroconf.Zeroconf.start"):
|
|
|
|
zc = Zeroconf()
|
|
|
|
zc.close = MagicMock()
|
|
|
|
return zc
|
2023-11-09 00:26:51 +01:00
|
|
|
|
|
|
|
|
2023-11-17 20:11:36 +01:00
|
|
|
def get_mock_async_zeroconf() -> AsyncZeroconf:
|
|
|
|
aiozc = AsyncZeroconf(zc=get_mock_zeroconf())
|
|
|
|
aiozc.async_close = AsyncMock()
|
|
|
|
return aiozc
|
2023-11-11 20:43:31 +01:00
|
|
|
|
|
|
|
|
2023-11-09 00:26:51 +01:00
|
|
|
class Estr(str):
|
|
|
|
"""A subclassed string."""
|
|
|
|
|
|
|
|
|
2024-11-01 17:46:10 +01:00
|
|
|
def generate_split_plaintext_packet(msg: message.Message) -> list[bytes]:
|
2023-11-17 20:11:36 +01:00
|
|
|
type_ = PROTO_TO_MESSAGE_TYPE[msg.__class__]
|
|
|
|
bytes_ = msg.SerializeToString()
|
2024-11-01 17:46:10 +01:00
|
|
|
return [
|
|
|
|
b"\0",
|
|
|
|
_cached_varuint_to_bytes(len(bytes_)),
|
|
|
|
_cached_varuint_to_bytes(type_),
|
|
|
|
bytes_,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_plaintext_packet(msg: message.Message) -> bytes:
|
|
|
|
return b"".join(generate_split_plaintext_packet(msg))
|
2023-11-11 00:14:00 +01:00
|
|
|
|
|
|
|
|
2023-10-23 19:32:20 +02:00
|
|
|
def as_utc(dattim: datetime) -> datetime:
|
|
|
|
"""Return a datetime as UTC time."""
|
|
|
|
if dattim.tzinfo == UTC:
|
|
|
|
return dattim
|
|
|
|
return dattim.astimezone(UTC)
|
|
|
|
|
|
|
|
|
|
|
|
def async_fire_time_changed(
|
|
|
|
datetime_: datetime | None = None, fire_all: bool = False
|
|
|
|
) -> None:
|
|
|
|
"""Fire a time changed event at an exact microsecond.
|
|
|
|
|
|
|
|
Consider that it is not possible to actually achieve an exact
|
|
|
|
microsecond in production as the event loop is not precise enough.
|
|
|
|
If your code relies on this level of precision, consider a different
|
|
|
|
approach, as this is only for testing.
|
|
|
|
"""
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
if datetime_ is None:
|
|
|
|
utc_datetime = datetime.now(UTC)
|
|
|
|
else:
|
|
|
|
utc_datetime = as_utc(datetime_)
|
|
|
|
|
|
|
|
timestamp = utc_datetime.timestamp()
|
|
|
|
for task in list(loop._scheduled):
|
|
|
|
if not isinstance(task, asyncio.TimerHandle):
|
|
|
|
continue
|
|
|
|
if task.cancelled():
|
|
|
|
continue
|
|
|
|
|
|
|
|
mock_seconds_into_future = timestamp - time.time()
|
|
|
|
future_seconds = task.when() - (loop.time() + _MONOTONIC_RESOLUTION)
|
|
|
|
|
|
|
|
if fire_all or mock_seconds_into_future >= future_seconds:
|
|
|
|
task._run()
|
|
|
|
task.cancel()
|
2023-11-11 00:14:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def connect(conn: APIConnection, login: bool = True):
|
|
|
|
"""Wrapper for connection logic to do both parts."""
|
|
|
|
await conn.start_connection()
|
|
|
|
await conn.finish_connection(login=login)
|
2023-11-11 20:06:27 +01:00
|
|
|
|
|
|
|
|
2023-11-25 14:11:34 +01:00
|
|
|
async def connect_client(
|
|
|
|
client: APIClient,
|
|
|
|
login: bool = True,
|
|
|
|
on_stop: Callable[[bool], Awaitable[None]] | None = None,
|
|
|
|
) -> None:
|
|
|
|
"""Wrapper for connection logic to do both parts."""
|
|
|
|
await client.start_connection(on_stop=on_stop)
|
|
|
|
await client.finish_connection(login=login)
|
|
|
|
|
|
|
|
|
2023-11-25 16:58:30 +01:00
|
|
|
def send_plaintext_hello(
|
|
|
|
protocol: APIPlaintextFrameHelper,
|
|
|
|
major: int | None = None,
|
|
|
|
minor: int | None = None,
|
|
|
|
) -> None:
|
2023-11-11 20:06:27 +01:00
|
|
|
hello_response: message.Message = HelloResponse()
|
2023-11-25 16:58:30 +01:00
|
|
|
hello_response.api_version_major = 1 if major is None else major
|
|
|
|
hello_response.api_version_minor = 9 if minor is None else minor
|
2023-11-11 20:06:27 +01:00
|
|
|
hello_response.name = "fake"
|
2023-11-17 20:11:36 +01:00
|
|
|
protocol.data_received(generate_plaintext_packet(hello_response))
|
2023-11-11 20:06:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
def send_plaintext_connect_response(
|
|
|
|
protocol: APIPlaintextFrameHelper, invalid_password: bool
|
|
|
|
) -> None:
|
|
|
|
connect_response: message.Message = ConnectResponse()
|
|
|
|
connect_response.invalid_password = invalid_password
|
2023-11-17 20:11:36 +01:00
|
|
|
protocol.data_received(generate_plaintext_packet(connect_response))
|
2023-11-21 14:01:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def send_ping_response(protocol: APIPlaintextFrameHelper) -> None:
|
|
|
|
ping_response: message.Message = PingResponse()
|
|
|
|
protocol.data_received(generate_plaintext_packet(ping_response))
|
2023-11-21 14:56:31 +01:00
|
|
|
|
|
|
|
|
2023-11-23 13:36:30 +01:00
|
|
|
def send_ping_request(protocol: APIPlaintextFrameHelper) -> None:
|
|
|
|
ping_request: message.Message = PingRequest()
|
|
|
|
protocol.data_received(generate_plaintext_packet(ping_request))
|
|
|
|
|
|
|
|
|
2023-11-21 14:56:31 +01:00
|
|
|
def get_mock_protocol(conn: APIConnection):
|
|
|
|
protocol = APIPlaintextFrameHelper(
|
|
|
|
connection=conn,
|
|
|
|
client_info="mock",
|
|
|
|
log_name="mock_device",
|
|
|
|
)
|
|
|
|
transport = MagicMock()
|
|
|
|
protocol.connection_made(transport)
|
|
|
|
return protocol
|