core/tests/components/hue/test_bridge.py

165 lines
5.8 KiB
Python
Raw Normal View History

"""Test Hue bridge."""
import asyncio
from unittest.mock import patch
2021-01-01 21:31:56 +00:00
from aiohttp import client_exceptions
from aiohue.errors import Unauthorized
from aiohue.v1 import HueBridgeV1
from aiohue.v2 import HueBridgeV2
import pytest
from homeassistant.components.hue import bridge
from homeassistant.components.hue.const import (
CONF_ALLOW_HUE_GROUPS,
CONF_ALLOW_UNREACHABLE,
DOMAIN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from tests.common import MockConfigEntry
async def test_bridge_setup_v1(hass: HomeAssistant, mock_api_v1) -> None:
"""Test a successful setup for V1 bridge."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
options={CONF_ALLOW_HUE_GROUPS: False, CONF_ALLOW_UNREACHABLE: False},
)
with (
patch.object(bridge, "HueBridgeV1", return_value=mock_api_v1),
patch.object(hass.config_entries, "async_forward_entry_setups") as mock_forward,
):
hue_bridge = bridge.HueBridge(hass, config_entry)
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
async with config_entry.setup_lock:
assert await hue_bridge.async_initialize_bridge() is True
assert hue_bridge.api is mock_api_v1
assert isinstance(hue_bridge.api, HueBridgeV1)
assert hue_bridge.api_version == 1
assert len(mock_forward.mock_calls) == 1
forward_entries = set(mock_forward.mock_calls[0][1][1])
2020-04-05 00:20:09 +00:00
assert forward_entries == {"light", "binary_sensor", "sensor"}
async def test_bridge_setup_v2(hass: HomeAssistant, mock_api_v2) -> None:
"""Test a successful setup for V2 bridge."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 2},
)
with (
patch.object(bridge, "HueBridgeV2", return_value=mock_api_v2),
patch.object(hass.config_entries, "async_forward_entry_setups") as mock_forward,
):
hue_bridge = bridge.HueBridge(hass, config_entry)
assert await hue_bridge.async_initialize_bridge() is True
assert hue_bridge.api is mock_api_v2
assert isinstance(hue_bridge.api, HueBridgeV2)
assert hue_bridge.api_version == 2
assert len(mock_forward.mock_calls) == 1
forward_entries = set(mock_forward.mock_calls[0][1][1])
assert forward_entries == {
"light",
"binary_sensor",
"event",
"sensor",
"switch",
"scene",
}
async def test_bridge_setup_invalid_api_key(hass: HomeAssistant) -> None:
"""Test we start config flow if username is no longer whitelisted."""
entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
options={CONF_ALLOW_HUE_GROUPS: False, CONF_ALLOW_UNREACHABLE: False},
)
hue_bridge = bridge.HueBridge(hass, entry)
with (
patch.object(hue_bridge.api, "initialize", side_effect=Unauthorized),
patch.object(hass.config_entries.flow, "async_init") as mock_init,
):
assert await hue_bridge.async_initialize_bridge() is False
assert len(mock_init.mock_calls) == 1
assert mock_init.mock_calls[0][2]["data"] == {"host": "1.2.3.4"}
async def test_bridge_setup_timeout(hass: HomeAssistant) -> None:
"""Test we retry to connect if we cannot connect."""
entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
options={CONF_ALLOW_HUE_GROUPS: False, CONF_ALLOW_UNREACHABLE: False},
)
hue_bridge = bridge.HueBridge(hass, entry)
with (
patch.object(
hue_bridge.api,
"initialize",
side_effect=client_exceptions.ServerDisconnectedError,
),
pytest.raises(ConfigEntryNotReady),
):
await hue_bridge.async_initialize_bridge()
async def test_reset_unloads_entry_if_setup(hass: HomeAssistant, mock_api_v1) -> None:
"""Test calling reset while the entry has been setup."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
options={CONF_ALLOW_HUE_GROUPS: False, CONF_ALLOW_UNREACHABLE: False},
)
with (
patch.object(bridge, "HueBridgeV1", return_value=mock_api_v1),
patch.object(hass.config_entries, "async_forward_entry_setups") as mock_forward,
):
hue_bridge = bridge.HueBridge(hass, config_entry)
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
async with config_entry.setup_lock:
assert await hue_bridge.async_initialize_bridge() is True
await asyncio.sleep(0)
2020-10-11 19:01:49 +00:00
assert len(hass.services.async_services()) == 0
assert len(mock_forward.mock_calls) == 1
with patch.object(
2020-04-28 17:31:22 +00:00
hass.config_entries, "async_forward_entry_unload", return_value=True
) as mock_forward:
assert await hue_bridge.async_reset()
assert len(mock_forward.mock_calls) == 3
assert len(hass.services.async_services()) == 0
async def test_handle_unauthorized(hass: HomeAssistant, mock_api_v1) -> None:
"""Test handling an unauthorized error on update."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
options={CONF_ALLOW_HUE_GROUPS: False, CONF_ALLOW_UNREACHABLE: False},
)
with patch.object(bridge, "HueBridgeV1", return_value=mock_api_v1):
hue_bridge = bridge.HueBridge(hass, config_entry)
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
async with config_entry.setup_lock:
assert await hue_bridge.async_initialize_bridge() is True
with patch.object(bridge, "create_config_flow") as mock_create:
await hue_bridge.handle_unauthorized_error()
assert hue_bridge.authorized is False
assert len(mock_create.mock_calls) == 1
assert mock_create.mock_calls[0][1][1] == "1.2.3.4"