2018-05-29 22:43:26 +02:00
|
|
|
"""HomeKit session fixtures."""
|
2021-08-25 09:47:39 -05:00
|
|
|
from contextlib import suppress
|
|
|
|
import os
|
2021-01-01 22:31:56 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2018-05-29 22:43:26 +02:00
|
|
|
import pytest
|
|
|
|
|
2021-08-25 09:47:39 -05:00
|
|
|
from homeassistant.components.device_tracker.legacy import YAML_DEVICES
|
2022-11-08 04:15:16 -06:00
|
|
|
from homeassistant.components.homekit.accessories import HomeDriver
|
2022-10-14 09:58:09 -10:00
|
|
|
from homeassistant.components.homekit.const import BRIDGE_NAME, EVENT_HOMEKIT_CHANGED
|
|
|
|
from homeassistant.components.homekit.iidmanager import AccessoryIIDStorage
|
2021-02-26 13:28:52 -08:00
|
|
|
|
2023-03-01 16:54:00 +01:00
|
|
|
from tests.common import async_capture_events
|
2018-10-16 05:32:53 -06:00
|
|
|
|
2018-05-29 22:43:26 +02:00
|
|
|
|
2020-07-06 15:58:53 -07:00
|
|
|
@pytest.fixture
|
2022-10-14 09:58:09 -10:00
|
|
|
def iid_storage(hass):
|
|
|
|
"""Mock the iid storage."""
|
|
|
|
with patch.object(AccessoryIIDStorage, "_async_schedule_save"):
|
|
|
|
yield AccessoryIIDStorage(hass, "")
|
|
|
|
|
|
|
|
|
2023-01-26 18:05:05 +01:00
|
|
|
@pytest.fixture
|
2022-11-29 22:36:36 +01:00
|
|
|
def run_driver(hass, event_loop, iid_storage):
|
2022-10-14 09:58:09 -10:00
|
|
|
"""Return a custom AccessoryDriver instance for HomeKit accessory init.
|
|
|
|
|
|
|
|
This mock does not mock async_stop, so the driver will not be stopped
|
|
|
|
"""
|
|
|
|
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryEncoder"
|
|
|
|
), patch("pyhap.accessory_driver.HAPServer"), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.publish"
|
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.persist"
|
|
|
|
):
|
|
|
|
yield HomeDriver(
|
|
|
|
hass,
|
|
|
|
pincode=b"123-45-678",
|
|
|
|
entry_id="",
|
|
|
|
entry_title="mock entry",
|
|
|
|
bridge_name=BRIDGE_NAME,
|
2022-11-08 04:15:16 -06:00
|
|
|
iid_storage=iid_storage,
|
2022-10-14 09:58:09 -10:00
|
|
|
address="127.0.0.1",
|
2022-11-29 22:36:36 +01:00
|
|
|
loop=event_loop,
|
2022-10-14 09:58:09 -10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-11-29 22:36:36 +01:00
|
|
|
def hk_driver(hass, event_loop, iid_storage):
|
2018-05-29 22:43:26 +02:00
|
|
|
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
|
2021-05-31 13:47:12 -05:00
|
|
|
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
|
2019-07-31 12:25:30 -07:00
|
|
|
"pyhap.accessory_driver.AccessoryEncoder"
|
2021-03-11 20:05:03 -10:00
|
|
|
), patch("pyhap.accessory_driver.HAPServer.async_stop"), patch(
|
|
|
|
"pyhap.accessory_driver.HAPServer.async_start"
|
|
|
|
), patch(
|
2019-07-31 12:25:30 -07:00
|
|
|
"pyhap.accessory_driver.AccessoryDriver.publish"
|
2020-04-15 21:39:31 -05:00
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.persist"
|
2019-07-31 12:25:30 -07:00
|
|
|
):
|
2022-10-14 09:58:09 -10:00
|
|
|
yield HomeDriver(
|
|
|
|
hass,
|
|
|
|
pincode=b"123-45-678",
|
|
|
|
entry_id="",
|
|
|
|
entry_title="mock entry",
|
|
|
|
bridge_name=BRIDGE_NAME,
|
2022-11-08 04:15:16 -06:00
|
|
|
iid_storage=iid_storage,
|
2022-10-14 09:58:09 -10:00
|
|
|
address="127.0.0.1",
|
2022-11-29 22:36:36 +01:00
|
|
|
loop=event_loop,
|
2022-10-14 09:58:09 -10:00
|
|
|
)
|
2018-10-16 05:32:53 -06:00
|
|
|
|
|
|
|
|
2021-08-25 09:47:39 -05:00
|
|
|
@pytest.fixture
|
2022-11-29 22:36:36 +01:00
|
|
|
def mock_hap(hass, event_loop, iid_storage, mock_zeroconf):
|
2021-08-25 09:47:39 -05:00
|
|
|
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
|
|
|
|
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryEncoder"
|
|
|
|
), patch("pyhap.accessory_driver.HAPServer.async_stop"), patch(
|
|
|
|
"pyhap.accessory_driver.HAPServer.async_start"
|
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.publish"
|
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.async_start"
|
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.async_stop"
|
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.persist"
|
|
|
|
):
|
2022-10-14 09:58:09 -10:00
|
|
|
yield HomeDriver(
|
|
|
|
hass,
|
|
|
|
pincode=b"123-45-678",
|
|
|
|
entry_id="",
|
|
|
|
entry_title="mock entry",
|
|
|
|
bridge_name=BRIDGE_NAME,
|
2022-11-08 04:15:16 -06:00
|
|
|
iid_storage=iid_storage,
|
2022-10-14 09:58:09 -10:00
|
|
|
address="127.0.0.1",
|
2022-11-29 22:36:36 +01:00
|
|
|
loop=event_loop,
|
2022-10-14 09:58:09 -10:00
|
|
|
)
|
2021-08-25 09:47:39 -05:00
|
|
|
|
|
|
|
|
2018-10-16 05:32:53 -06:00
|
|
|
@pytest.fixture
|
|
|
|
def events(hass):
|
|
|
|
"""Yield caught homekit_changed events."""
|
2021-02-26 13:28:52 -08:00
|
|
|
return async_capture_events(hass, EVENT_HOMEKIT_CHANGED)
|
2021-08-25 09:47:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def demo_cleanup(hass):
|
|
|
|
"""Clean up device tracker demo file."""
|
|
|
|
yield
|
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
os.remove(hass.config.path(YAML_DEVICES))
|