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