2018-11-06 09:34:24 +00:00
|
|
|
"""Test deCONZ gateway."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2024-07-05 14:59:10 +00:00
|
|
|
from collections.abc import Callable
|
2022-02-23 12:10:35 +00:00
|
|
|
from unittest.mock import patch
|
2019-09-19 21:44:09 +00:00
|
|
|
|
2019-12-09 11:25:35 +00:00
|
|
|
import pydeconz
|
2022-08-05 23:34:27 +00:00
|
|
|
from pydeconz.websocket import State
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-12-01 18:38:20 +00:00
|
|
|
from homeassistant.components import ssdp
|
2021-04-20 15:34:11 +00:00
|
|
|
from homeassistant.components.alarm_control_panel import (
|
|
|
|
DOMAIN as ALARM_CONTROL_PANEL_DOMAIN,
|
|
|
|
)
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
2022-02-16 16:55:30 +00:00
|
|
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
|
|
|
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
|
|
|
from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL
|
|
|
|
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
|
|
|
|
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
|
2024-03-18 21:08:06 +00:00
|
|
|
from homeassistant.components.deconz.hub import DeconzHub, get_deconz_api
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
|
|
|
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|
|
|
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
2021-10-20 09:23:24 +00:00
|
|
|
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN
|
2022-08-31 03:33:05 +00:00
|
|
|
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
2021-09-18 19:59:04 +00:00
|
|
|
from homeassistant.components.siren import DOMAIN as SIREN_DOMAIN
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.ssdp import (
|
|
|
|
ATTR_UPNP_MANUFACTURER_URL,
|
|
|
|
ATTR_UPNP_SERIAL,
|
|
|
|
ATTR_UPNP_UDN,
|
|
|
|
)
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
2024-07-05 18:47:43 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, ConfigEntry
|
|
|
|
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
|
2023-02-08 12:01:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-22 17:41:49 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2021-02-09 07:31:29 +00:00
|
|
|
|
2024-07-05 18:47:43 +00:00
|
|
|
from .conftest import BRIDGEID, HOST, PORT
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2023-02-08 12:01:44 +00:00
|
|
|
async def test_gateway_setup(
|
2023-11-09 17:06:53 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
2024-07-05 14:59:10 +00:00
|
|
|
config_entry_factory: Callable[[], ConfigEntry],
|
2023-02-08 12:01:44 +00:00
|
|
|
) -> None:
|
2019-09-19 21:44:09 +00:00
|
|
|
"""Successful setup."""
|
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
|
|
|
# Patching async_forward_entry_setup* is not advisable, and should be refactored
|
|
|
|
# in the future.
|
2019-09-19 21:44:09 +00:00
|
|
|
with patch(
|
2024-03-23 19:26:38 +00:00
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setups",
|
2019-09-19 21:44:09 +00:00
|
|
|
return_value=True,
|
|
|
|
) as forward_entry_setup:
|
2024-07-05 14:59:10 +00:00
|
|
|
config_entry = await config_entry_factory()
|
2024-03-18 21:08:06 +00:00
|
|
|
gateway = DeconzHub.get_hub(hass, config_entry)
|
2019-09-19 21:44:09 +00:00
|
|
|
assert gateway.bridgeid == BRIDGEID
|
|
|
|
assert gateway.master is True
|
2024-03-13 21:49:49 +00:00
|
|
|
assert gateway.config.allow_clip_sensor is False
|
|
|
|
assert gateway.config.allow_deconz_groups is True
|
|
|
|
assert gateway.config.allow_new_devices is True
|
2019-09-19 21:44:09 +00:00
|
|
|
|
|
|
|
assert len(gateway.deconz_ids) == 0
|
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert forward_entry_setup.mock_calls[0][1] == (
|
2021-04-20 15:34:11 +00:00
|
|
|
config_entry,
|
2024-03-23 19:26:38 +00:00
|
|
|
[
|
|
|
|
ALARM_CONTROL_PANEL_DOMAIN,
|
|
|
|
BINARY_SENSOR_DOMAIN,
|
|
|
|
BUTTON_DOMAIN,
|
|
|
|
CLIMATE_DOMAIN,
|
|
|
|
COVER_DOMAIN,
|
|
|
|
FAN_DOMAIN,
|
|
|
|
LIGHT_DOMAIN,
|
|
|
|
LOCK_DOMAIN,
|
|
|
|
NUMBER_DOMAIN,
|
|
|
|
SCENE_DOMAIN,
|
|
|
|
SELECT_DOMAIN,
|
|
|
|
SENSOR_DOMAIN,
|
|
|
|
SIREN_DOMAIN,
|
|
|
|
SWITCH_DOMAIN,
|
|
|
|
],
|
2021-04-20 15:34:11 +00:00
|
|
|
)
|
2019-09-19 21:44:09 +00:00
|
|
|
|
2021-10-22 17:41:49 +00:00
|
|
|
gateway_entry = device_registry.async_get_device(
|
|
|
|
identifiers={(DECONZ_DOMAIN, gateway.bridgeid)}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert gateway_entry.configuration_url == f"http://{HOST}:{PORT}"
|
2021-11-23 09:04:33 +00:00
|
|
|
assert gateway_entry.entry_type is dr.DeviceEntryType.SERVICE
|
2021-10-22 17:41:49 +00:00
|
|
|
|
|
|
|
|
2024-07-05 14:59:10 +00:00
|
|
|
@pytest.mark.parametrize("config_entry_source", [SOURCE_HASSIO])
|
2023-02-08 12:01:44 +00:00
|
|
|
async def test_gateway_device_configuration_url_when_addon(
|
2023-11-09 17:06:53 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
2024-07-05 14:59:10 +00:00
|
|
|
config_entry_factory: Callable[[], ConfigEntry],
|
2023-02-08 12:01:44 +00:00
|
|
|
) -> None:
|
2021-10-22 17:41:49 +00:00
|
|
|
"""Successful setup."""
|
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
|
|
|
# Patching async_forward_entry_setup* is not advisable, and should be refactored
|
|
|
|
# in the future.
|
2021-10-22 17:41:49 +00:00
|
|
|
with patch(
|
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
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setups",
|
2021-10-22 17:41:49 +00:00
|
|
|
return_value=True,
|
|
|
|
):
|
2024-07-05 14:59:10 +00:00
|
|
|
config_entry = await config_entry_factory()
|
2024-03-18 21:08:06 +00:00
|
|
|
gateway = DeconzHub.get_hub(hass, config_entry)
|
2021-10-22 17:41:49 +00:00
|
|
|
|
|
|
|
gateway_entry = device_registry.async_get_device(
|
|
|
|
identifiers={(DECONZ_DOMAIN, gateway.bridgeid)}
|
|
|
|
)
|
|
|
|
|
2021-11-14 10:49:02 +00:00
|
|
|
assert (
|
|
|
|
gateway_entry.configuration_url == "homeassistant://hassio/ingress/core_deconz"
|
|
|
|
)
|
2021-10-22 17:41:49 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
|
2024-07-05 14:59:10 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"sensor_payload",
|
|
|
|
[
|
|
|
|
{
|
2021-03-12 20:03:29 +00:00
|
|
|
"1": {
|
|
|
|
"name": "presence",
|
|
|
|
"type": "ZHAPresence",
|
|
|
|
"state": {"presence": False},
|
|
|
|
"config": {"on": True, "reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 14:59:10 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.usefixtures("config_entry_setup")
|
|
|
|
async def test_connection_status_signalling(
|
2024-07-05 20:35:06 +00:00
|
|
|
hass: HomeAssistant, mock_websocket_state
|
2024-07-05 14:59:10 +00:00
|
|
|
) -> None:
|
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
2021-03-12 20:03:29 +00:00
|
|
|
assert hass.states.get("binary_sensor.presence").state == STATE_OFF
|
|
|
|
|
2024-07-05 20:35:06 +00:00
|
|
|
await mock_websocket_state(State.RETRYING)
|
2021-03-12 20:03:29 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2021-03-12 20:03:29 +00:00
|
|
|
assert hass.states.get("binary_sensor.presence").state == STATE_UNAVAILABLE
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2024-07-05 20:35:06 +00:00
|
|
|
await mock_websocket_state(State.RUNNING)
|
2019-09-19 21:44:09 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2021-03-12 20:03:29 +00:00
|
|
|
assert hass.states.get("binary_sensor.presence").state == STATE_OFF
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2023-02-08 12:01:44 +00:00
|
|
|
async def test_update_address(
|
2024-07-05 14:59:10 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
2023-02-08 12:01:44 +00:00
|
|
|
) -> None:
|
2019-09-19 21:44:09 +00:00
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
2024-07-05 14:59:10 +00:00
|
|
|
gateway = DeconzHub.get_hub(hass, config_entry_setup)
|
2019-09-19 21:44:09 +00:00
|
|
|
assert gateway.api.host == "1.2.3.4"
|
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.deconz.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-08 18:23:56 +00:00
|
|
|
) as mock_setup_entry:
|
|
|
|
await hass.config_entries.flow.async_init(
|
2020-10-17 16:20:06 +00:00
|
|
|
DECONZ_DOMAIN,
|
2021-12-01 18:38:20 +00:00
|
|
|
data=ssdp.SsdpServiceInfo(
|
|
|
|
ssdp_st="mock_st",
|
|
|
|
ssdp_usn="mock_usn",
|
|
|
|
ssdp_location="http://2.3.4.5:80/",
|
|
|
|
upnp={
|
|
|
|
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
|
|
|
|
ATTR_UPNP_SERIAL: BRIDGEID,
|
|
|
|
ATTR_UPNP_UDN: "uuid:456DEF",
|
|
|
|
},
|
|
|
|
),
|
2020-10-17 16:20:06 +00:00
|
|
|
context={"source": SOURCE_SSDP},
|
2020-08-08 18:23:56 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
assert gateway.api.host == "2.3.4.5"
|
2020-08-08 18:23:56 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2023-02-08 12:01:44 +00:00
|
|
|
async def test_reset_after_successful_setup(
|
2024-07-05 14:59:10 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
2023-02-08 12:01:44 +00:00
|
|
|
) -> None:
|
2019-09-19 21:44:09 +00:00
|
|
|
"""Make sure that connection status triggers a dispatcher send."""
|
2024-07-05 14:59:10 +00:00
|
|
|
gateway = DeconzHub.get_hub(hass, config_entry_setup)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
result = await gateway.async_reset()
|
|
|
|
await hass.async_block_till_done()
|
2018-11-06 09:34:24 +00:00
|
|
|
|
2019-09-19 21:44:09 +00:00
|
|
|
assert result is True
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2024-07-05 18:47:43 +00:00
|
|
|
async def test_get_deconz_api(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
2018-11-06 09:34:24 +00:00
|
|
|
"""Successful call."""
|
2021-09-18 07:05:08 +00:00
|
|
|
with patch("pydeconz.DeconzSession.refresh_state", return_value=True):
|
2024-03-13 21:49:49 +00:00
|
|
|
assert await get_deconz_api(hass, config_entry)
|
2018-11-06 09:34:24 +00:00
|
|
|
|
|
|
|
|
2022-02-23 12:10:35 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("side_effect", "raised_exception"),
|
2022-02-23 12:10:35 +00:00
|
|
|
[
|
2024-02-05 10:31:33 +00:00
|
|
|
(TimeoutError, CannotConnect),
|
2022-02-23 12:10:35 +00:00
|
|
|
(pydeconz.RequestError, CannotConnect),
|
2022-04-29 20:55:55 +00:00
|
|
|
(pydeconz.ResponseError, CannotConnect),
|
2022-02-23 12:10:35 +00:00
|
|
|
(pydeconz.Unauthorized, AuthenticationRequired),
|
|
|
|
],
|
|
|
|
)
|
2024-03-09 17:32:31 +00:00
|
|
|
async def test_get_deconz_api_fails(
|
2024-07-05 18:47:43 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
side_effect: Exception,
|
|
|
|
raised_exception: Exception,
|
2023-02-11 07:26:13 +00:00
|
|
|
) -> None:
|
2018-11-06 09:34:24 +00:00
|
|
|
"""Failed call."""
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"pydeconz.DeconzSession.refresh_state",
|
|
|
|
side_effect=side_effect,
|
|
|
|
),
|
|
|
|
pytest.raises(raised_exception),
|
|
|
|
):
|
2024-03-13 21:49:49 +00:00
|
|
|
assert await get_deconz_api(hass, config_entry)
|