2018-05-29 20:43:26 +00:00
|
|
|
"""HomeKit session fixtures."""
|
2021-08-25 14:47:39 +00:00
|
|
|
from contextlib import suppress
|
|
|
|
import os
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2019-12-08 17:16:49 +00:00
|
|
|
from pyhap.accessory_driver import AccessoryDriver
|
2018-05-29 20:43:26 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-08-25 14:47:39 +00:00
|
|
|
from homeassistant.components.device_tracker.legacy import YAML_DEVICES
|
2018-10-16 11:32:53 +00:00
|
|
|
from homeassistant.components.homekit.const import EVENT_HOMEKIT_CHANGED
|
2021-02-26 21:28:52 +00:00
|
|
|
|
2021-08-25 14:47:39 +00:00
|
|
|
from tests.common import async_capture_events, mock_device_registry, mock_registry
|
2018-10-16 11:32:53 +00:00
|
|
|
|
2018-05-29 20:43:26 +00:00
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def hk_driver(loop):
|
2018-05-29 20:43:26 +00:00
|
|
|
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
|
2021-05-31 18:47:12 +00:00
|
|
|
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"pyhap.accessory_driver.AccessoryEncoder"
|
2021-03-12 06:05:03 +00:00
|
|
|
), patch("pyhap.accessory_driver.HAPServer.async_stop"), patch(
|
|
|
|
"pyhap.accessory_driver.HAPServer.async_start"
|
|
|
|
), patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"pyhap.accessory_driver.AccessoryDriver.publish"
|
2020-04-16 02:39:31 +00:00
|
|
|
), patch(
|
|
|
|
"pyhap.accessory_driver.AccessoryDriver.persist"
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-07-06 22:58:53 +00:00
|
|
|
yield AccessoryDriver(pincode=b"123-45-678", address="127.0.0.1", loop=loop)
|
2018-10-16 11:32:53 +00:00
|
|
|
|
|
|
|
|
2021-08-25 14:47:39 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_hap(loop, mock_zeroconf):
|
|
|
|
"""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"
|
|
|
|
):
|
|
|
|
yield AccessoryDriver(pincode=b"123-45-678", address="127.0.0.1", loop=loop)
|
|
|
|
|
|
|
|
|
2018-10-16 11:32:53 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def events(hass):
|
|
|
|
"""Yield caught homekit_changed events."""
|
2021-02-26 21:28:52 +00:00
|
|
|
return async_capture_events(hass, EVENT_HOMEKIT_CHANGED)
|
2021-08-25 14:47:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="device_reg")
|
|
|
|
def device_reg_fixture(hass):
|
|
|
|
"""Return an empty, loaded, registry."""
|
|
|
|
return mock_device_registry(hass)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="entity_reg")
|
|
|
|
def entity_reg_fixture(hass):
|
|
|
|
"""Return an empty, loaded, registry."""
|
|
|
|
return mock_registry(hass)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def demo_cleanup(hass):
|
|
|
|
"""Clean up device tracker demo file."""
|
|
|
|
yield
|
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
os.remove(hass.config.path(YAML_DEVICES))
|