2020-01-31 22:47:40 +00:00
|
|
|
"""Test helpers for Hue."""
|
2024-03-08 18:16:21 +00:00
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
import asyncio
|
2020-04-01 18:42:22 +00:00
|
|
|
from collections import deque
|
2024-07-05 12:40:23 +00:00
|
|
|
from collections.abc import Generator
|
2021-05-14 20:39:57 +00:00
|
|
|
import logging
|
2023-10-09 12:14:07 +00:00
|
|
|
from typing import Any
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2020-01-31 22:47:40 +00:00
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
import aiohue.v1 as aiohue_v1
|
|
|
|
import aiohue.v2 as aiohue_v2
|
|
|
|
from aiohue.v2.controllers.events import EventType
|
2020-01-31 22:47:40 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-04-01 18:42:22 +00:00
|
|
|
from homeassistant.components import hue
|
2021-11-16 19:59:17 +00:00
|
|
|
from homeassistant.components.hue.v1 import sensor_base as hue_sensor_base
|
2023-10-09 12:14:07 +00:00
|
|
|
from homeassistant.components.hue.v2.device import async_setup_devices
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2024-07-05 12:40:23 +00:00
|
|
|
from homeassistant.const import Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-11-16 19:59:17 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2024-07-05 12:40:23 +00:00
|
|
|
from homeassistant.util.json import JsonArrayType
|
2020-04-01 18:42:22 +00:00
|
|
|
|
2024-06-10 10:52:34 +00:00
|
|
|
from .const import FAKE_BRIDGE, FAKE_BRIDGE_DEVICE
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
from tests.common import MockConfigEntry, load_json_array_fixture
|
2021-11-16 19:59:17 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-07-05 12:40:23 +00:00
|
|
|
def no_request_delay() -> Generator[None]:
|
2020-01-31 22:47:40 +00:00
|
|
|
"""Make the request refresh delay 0 for instant tests."""
|
2021-11-16 19:59:17 +00:00
|
|
|
with patch("homeassistant.components.hue.const.REQUEST_REFRESH_DELAY", 0):
|
2020-01-31 22:47:40 +00:00
|
|
|
yield
|
2020-04-01 18:42:22 +00:00
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
def create_mock_bridge(hass: HomeAssistant, api_version: int = 1) -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Create a mocked HueBridge instance."""
|
2020-04-01 18:42:22 +00:00
|
|
|
bridge = Mock(
|
|
|
|
hass=hass,
|
|
|
|
authorized=True,
|
2021-05-20 07:08:23 +00:00
|
|
|
config_entry=None,
|
2020-04-01 18:42:22 +00:00
|
|
|
reset_jobs=[],
|
2021-11-16 19:59:17 +00:00
|
|
|
api_version=api_version,
|
2020-04-01 18:42:22 +00:00
|
|
|
spec=hue.HueBridge,
|
|
|
|
)
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
bridge.logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
if bridge.api_version == 2:
|
2024-07-05 12:40:23 +00:00
|
|
|
bridge.api = create_mock_api_v2()
|
2021-11-16 19:59:17 +00:00
|
|
|
bridge.mock_requests = bridge.api.mock_requests
|
|
|
|
else:
|
2024-07-05 12:40:23 +00:00
|
|
|
bridge.api = create_mock_api_v1()
|
2021-11-16 19:59:17 +00:00
|
|
|
bridge.sensor_manager = hue_sensor_base.SensorManager(bridge)
|
|
|
|
bridge.mock_requests = bridge.api.mock_requests
|
|
|
|
bridge.mock_light_responses = bridge.api.mock_light_responses
|
|
|
|
bridge.mock_group_responses = bridge.api.mock_group_responses
|
|
|
|
bridge.mock_sensor_responses = bridge.api.mock_sensor_responses
|
|
|
|
|
|
|
|
async def async_initialize_bridge():
|
2021-05-20 07:08:23 +00:00
|
|
|
if bridge.config_entry:
|
|
|
|
hass.data.setdefault(hue.DOMAIN, {})[bridge.config_entry.entry_id] = bridge
|
2023-10-09 12:14:07 +00:00
|
|
|
if bridge.api_version == 2:
|
|
|
|
await async_setup_devices(bridge)
|
2021-05-20 07:08:23 +00:00
|
|
|
return True
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
bridge.async_initialize_bridge = async_initialize_bridge
|
2021-05-20 07:08:23 +00:00
|
|
|
|
2022-03-23 04:59:06 +00:00
|
|
|
async def async_request_call(task, *args, **kwargs):
|
2021-11-16 19:59:17 +00:00
|
|
|
await task(*args, **kwargs)
|
2020-04-01 18:42:22 +00:00
|
|
|
|
|
|
|
bridge.async_request_call = async_request_call
|
2021-05-20 07:08:23 +00:00
|
|
|
|
|
|
|
async def async_reset():
|
|
|
|
if bridge.config_entry:
|
|
|
|
hass.data[hue.DOMAIN].pop(bridge.config_entry.entry_id)
|
|
|
|
return True
|
|
|
|
|
|
|
|
bridge.async_reset = async_reset
|
|
|
|
|
2020-04-01 18:42:22 +00:00
|
|
|
return bridge
|
|
|
|
|
|
|
|
|
2020-10-11 19:01:49 +00:00
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_api_v1() -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock the Hue V1 api."""
|
2024-07-05 12:40:23 +00:00
|
|
|
return create_mock_api_v1()
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_api_v2() -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock the Hue V2 api."""
|
2024-07-05 12:40:23 +00:00
|
|
|
return create_mock_api_v2()
|
2021-05-14 20:39:57 +00:00
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
def create_mock_api_v1() -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Create a mock V1 API."""
|
|
|
|
api = Mock(spec=aiohue_v1.HueBridgeV1)
|
|
|
|
api.initialize = AsyncMock()
|
2020-10-11 19:01:49 +00:00
|
|
|
api.mock_requests = []
|
|
|
|
api.mock_light_responses = deque()
|
|
|
|
api.mock_group_responses = deque()
|
|
|
|
api.mock_sensor_responses = deque()
|
|
|
|
api.mock_scene_responses = deque()
|
|
|
|
|
|
|
|
async def mock_request(method, path, **kwargs):
|
|
|
|
kwargs["method"] = method
|
|
|
|
kwargs["path"] = path
|
|
|
|
api.mock_requests.append(kwargs)
|
|
|
|
|
|
|
|
if path == "lights":
|
|
|
|
return api.mock_light_responses.popleft()
|
|
|
|
if path == "groups":
|
|
|
|
return api.mock_group_responses.popleft()
|
|
|
|
if path == "sensors":
|
|
|
|
return api.mock_sensor_responses.popleft()
|
|
|
|
if path == "scenes":
|
|
|
|
return api.mock_scene_responses.popleft()
|
|
|
|
return None
|
|
|
|
|
2021-05-14 20:39:57 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-05-20 07:08:23 +00:00
|
|
|
api.config = Mock(
|
2021-11-16 19:59:17 +00:00
|
|
|
bridge_id="ff:ff:ff:ff:ff:ff",
|
|
|
|
mac_address="aa:bb:cc:dd:ee:ff",
|
|
|
|
model_id="BSB002",
|
2021-05-20 07:08:23 +00:00
|
|
|
apiversion="9.9.9",
|
2021-11-16 19:59:17 +00:00
|
|
|
software_version="1935144040",
|
2021-05-20 07:08:23 +00:00
|
|
|
)
|
|
|
|
api.config.name = "Home"
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
api.lights = aiohue_v1.Lights(logger, {}, mock_request)
|
|
|
|
api.groups = aiohue_v1.Groups(logger, {}, mock_request)
|
|
|
|
api.sensors = aiohue_v1.Sensors(logger, {}, mock_request)
|
|
|
|
api.scenes = aiohue_v1.Scenes(logger, {}, mock_request)
|
2020-10-11 19:01:49 +00:00
|
|
|
return api
|
|
|
|
|
|
|
|
|
2024-04-26 00:12:36 +00:00
|
|
|
@pytest.fixture(scope="package")
|
2024-07-05 12:40:23 +00:00
|
|
|
def v2_resources_test_data() -> JsonArrayType:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Load V2 resources mock data."""
|
2024-07-05 12:40:23 +00:00
|
|
|
return load_json_array_fixture("hue/v2_resources.json")
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
def create_mock_api_v2() -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Create a mock V2 API."""
|
|
|
|
api = Mock(spec=aiohue_v2.HueBridgeV2)
|
|
|
|
api.initialize = AsyncMock()
|
|
|
|
api.mock_requests = []
|
|
|
|
|
|
|
|
api.logger = logging.getLogger(__name__)
|
2023-10-09 12:14:07 +00:00
|
|
|
api.config = aiohue_v2.ConfigController(api)
|
2021-11-16 19:59:17 +00:00
|
|
|
api.events = aiohue_v2.EventStream(api)
|
|
|
|
api.devices = aiohue_v2.DevicesController(api)
|
|
|
|
api.lights = aiohue_v2.LightsController(api)
|
|
|
|
api.sensors = aiohue_v2.SensorsController(api)
|
|
|
|
api.groups = aiohue_v2.GroupsController(api)
|
|
|
|
api.scenes = aiohue_v2.ScenesController(api)
|
|
|
|
|
|
|
|
async def mock_request(method, path, **kwargs):
|
|
|
|
kwargs["method"] = method
|
|
|
|
kwargs["path"] = path
|
|
|
|
api.mock_requests.append(kwargs)
|
|
|
|
return kwargs.get("json")
|
|
|
|
|
|
|
|
api.request = mock_request
|
|
|
|
|
2023-10-09 12:14:07 +00:00
|
|
|
async def load_test_data(data: list[dict[str, Any]]):
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Load test data into controllers."""
|
2023-10-09 12:14:07 +00:00
|
|
|
|
|
|
|
# append default bridge if none explicitly given in test data
|
|
|
|
if not any(x for x in data if x["type"] == "bridge"):
|
|
|
|
data.append(FAKE_BRIDGE)
|
|
|
|
data.append(FAKE_BRIDGE_DEVICE)
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
await asyncio.gather(
|
|
|
|
api.config.initialize(data),
|
|
|
|
api.devices.initialize(data),
|
|
|
|
api.lights.initialize(data),
|
|
|
|
api.scenes.initialize(data),
|
|
|
|
api.sensors.initialize(data),
|
|
|
|
api.groups.initialize(data),
|
|
|
|
)
|
|
|
|
|
|
|
|
def emit_event(event_type, data):
|
|
|
|
"""Emit an event from a (hue resource) dict."""
|
2022-02-16 01:26:13 +00:00
|
|
|
api.events.emit(EventType(event_type), data)
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
api.load_test_data = load_test_data
|
|
|
|
api.emit_event = emit_event
|
|
|
|
# mock context manager too
|
|
|
|
api.__aenter__ = AsyncMock(return_value=api)
|
|
|
|
api.__aexit__ = AsyncMock()
|
|
|
|
return api
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_bridge_v1(hass: HomeAssistant) -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock a Hue bridge with V1 api."""
|
|
|
|
return create_mock_bridge(hass, api_version=1)
|
|
|
|
|
|
|
|
|
2020-04-01 18:42:22 +00:00
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_bridge_v2(hass: HomeAssistant) -> Mock:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock a Hue bridge with V2 api."""
|
|
|
|
return create_mock_bridge(hass, api_version=2)
|
2020-04-01 18:42:22 +00:00
|
|
|
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_config_entry_v1() -> MockConfigEntry:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock a config entry for a Hue V1 bridge."""
|
|
|
|
return create_config_entry(api_version=1)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-07-05 12:40:23 +00:00
|
|
|
def mock_config_entry_v2() -> MockConfigEntry:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock a config entry."""
|
|
|
|
return create_config_entry(api_version=2)
|
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
def create_config_entry(
|
|
|
|
api_version: int = 1, host: str = "mock-host"
|
|
|
|
) -> MockConfigEntry:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock a config entry for a Hue bridge."""
|
|
|
|
return MockConfigEntry(
|
|
|
|
domain=hue.DOMAIN,
|
|
|
|
title=f"Mock bridge {api_version}",
|
|
|
|
data={"host": host, "api_version": api_version, "api_key": ""},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
async def setup_component(hass: HomeAssistant) -> None:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Mock setup Hue component."""
|
|
|
|
with patch.object(hue, "async_setup_entry", return_value=True):
|
|
|
|
assert (
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
hue.DOMAIN,
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-05 12:40:23 +00:00
|
|
|
async def setup_bridge(
|
|
|
|
hass: HomeAssistant, mock_bridge: Mock, config_entry: MockConfigEntry
|
|
|
|
) -> None:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Load the Hue integration with the provided bridge."""
|
|
|
|
mock_bridge.config_entry = config_entry
|
2021-12-01 13:53:30 +00:00
|
|
|
with patch.object(
|
|
|
|
hue.migration, "is_v2_bridge", return_value=mock_bridge.api_version == 2
|
|
|
|
):
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
with patch("homeassistant.components.hue.HueBridge", return_value=mock_bridge):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def setup_platform(
|
2024-07-05 12:40:23 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_bridge: Mock,
|
|
|
|
platforms: list[Platform] | tuple[Platform] | Platform,
|
|
|
|
hostname: str | None = None,
|
|
|
|
) -> None:
|
2021-11-16 19:59:17 +00:00
|
|
|
"""Load the Hue integration with the provided bridge for given platform(s)."""
|
|
|
|
if not isinstance(platforms, (list, tuple)):
|
|
|
|
platforms = [platforms]
|
2020-04-01 18:42:22 +00:00
|
|
|
if hostname is None:
|
|
|
|
hostname = "mock-host"
|
|
|
|
hass.config.components.add(hue.DOMAIN)
|
2021-11-16 19:59:17 +00:00
|
|
|
config_entry = create_config_entry(
|
|
|
|
api_version=mock_bridge.api_version, host=hostname
|
2020-04-01 18:42:22 +00:00
|
|
|
)
|
|
|
|
mock_bridge.config_entry = config_entry
|
|
|
|
hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge}
|
2021-11-16 19:59:17 +00:00
|
|
|
|
2020-04-01 18:42:22 +00:00
|
|
|
# simulate a full setup by manually adding the bridge config entry
|
2021-11-16 19:59:17 +00:00
|
|
|
await setup_bridge(hass, mock_bridge, config_entry)
|
|
|
|
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
2024-06-13 01:06:11 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, platforms)
|
2020-04-01 18:42:22 +00:00
|
|
|
|
|
|
|
# and make sure it completes before going further
|
|
|
|
await hass.async_block_till_done()
|