2019-01-27 11:34:49 +00:00
|
|
|
"""HomeKit controller session fixtures."""
|
2024-03-08 19:16:21 +01:00
|
|
|
|
2024-07-17 19:10:02 -05:00
|
|
|
from collections.abc import Callable, Generator
|
2019-01-27 11:34:49 +00:00
|
|
|
import datetime
|
2024-06-07 11:31:45 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2020-02-25 11:06:35 +00:00
|
|
|
from aiohomekit.testing import FakeController
|
2023-12-13 19:37:51 +01:00
|
|
|
from freezegun import freeze_time
|
2024-06-04 11:49:21 +02:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2019-01-27 11:34:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-06-29 11:39:24 -05:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2021-03-02 10:02:04 +02:00
|
|
|
from tests.components.light.conftest import mock_light_profiles # noqa: F401
|
2020-04-30 13:29:50 -07:00
|
|
|
|
2022-01-17 20:44:59 +00:00
|
|
|
pytest.register_assert_rewrite("tests.components.homekit_controller.common")
|
|
|
|
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2023-12-13 19:37:51 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-06 17:28:59 +02:00
|
|
|
def freeze_time_in_future() -> Generator[FrozenDateTimeFactory]:
|
2019-01-27 11:34:49 +00:00
|
|
|
"""Freeze time at a known point."""
|
2020-06-29 11:39:24 -05:00
|
|
|
now = dt_util.utcnow()
|
2020-11-05 16:34:56 +01:00
|
|
|
start_dt = datetime.datetime(now.year + 1, 1, 1, 0, 0, 0, tzinfo=now.tzinfo)
|
2023-12-13 19:37:51 +01:00
|
|
|
with freeze_time(start_dt) as frozen_time:
|
|
|
|
yield frozen_time
|
2020-02-25 11:06:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-06-07 11:31:45 +02:00
|
|
|
def controller() -> Generator[FakeController]:
|
2020-02-25 11:06:35 +00:00
|
|
|
"""Replace aiohomekit.Controller with an instance of aiohomekit.testing.FakeController."""
|
|
|
|
instance = FakeController()
|
2024-06-07 11:31:45 +02:00
|
|
|
with patch(
|
2022-02-11 19:26:35 +00:00
|
|
|
"homeassistant.components.homekit_controller.utils.Controller",
|
|
|
|
return_value=instance,
|
|
|
|
):
|
2020-02-25 11:06:35 +00:00
|
|
|
yield instance
|
2021-09-16 23:29:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-07 11:31:45 +02:00
|
|
|
def hk_mock_async_zeroconf(mock_async_zeroconf: MagicMock) -> None:
|
2021-11-18 22:23:20 -06:00
|
|
|
"""Auto mock zeroconf."""
|
2022-07-23 02:47:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-07 11:31:45 +02:00
|
|
|
def auto_mock_bluetooth(mock_bluetooth: None) -> None:
|
2022-07-23 02:47:02 -05:00
|
|
|
"""Auto mock bluetooth."""
|
2024-07-17 19:10:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def get_next_aid() -> Generator[Callable[[], int]]:
|
|
|
|
"""Generate a function that returns increasing accessory ids."""
|
|
|
|
id_counter = 0
|
|
|
|
|
|
|
|
def _get_id():
|
|
|
|
nonlocal id_counter
|
|
|
|
id_counter += 1
|
|
|
|
return id_counter
|
|
|
|
|
|
|
|
return _get_id
|