2018-07-06 21:05:34 +00:00
|
|
|
"""Test HomematicIP Cloud accesspoint."""
|
2024-03-08 13:50:04 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
2019-10-15 10:12:58 +00:00
|
|
|
from homematicip.aio.auth import AsyncAuth
|
|
|
|
from homematicip.base.base_connection import HmipConnectionError
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-01-27 20:34:15 +00:00
|
|
|
from homeassistant.components.homematicip_cloud import DOMAIN as HMIPC_DOMAIN
|
|
|
|
from homeassistant.components.homematicip_cloud.const import (
|
|
|
|
HMIPC_AUTHTOKEN,
|
|
|
|
HMIPC_HAPID,
|
|
|
|
HMIPC_NAME,
|
|
|
|
HMIPC_PIN,
|
2019-10-15 10:12:58 +00:00
|
|
|
)
|
2020-01-27 20:34:15 +00:00
|
|
|
from homeassistant.components.homematicip_cloud.errors import HmipcConnectionError
|
2019-10-15 10:12:58 +00:00
|
|
|
from homeassistant.components.homematicip_cloud.hap import (
|
|
|
|
HomematicipAuth,
|
|
|
|
HomematicipHAP,
|
|
|
|
)
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2023-02-08 17:12:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-02-14 04:36:06 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-10-06 09:54:26 +00:00
|
|
|
|
2024-08-19 09:06:50 +00:00
|
|
|
from .helper import HAPID, HAPPIN, HomeFactory
|
2019-10-15 10:12:58 +00:00
|
|
|
|
2023-01-25 09:01:51 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_auth_setup(hass: HomeAssistant) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test auth setup for client registration."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
|
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
with patch.object(hmip_auth, "get_auth"):
|
|
|
|
assert await hmip_auth.async_setup()
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_auth_setup_connection_error(hass: HomeAssistant) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test auth setup connection error behaviour."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
|
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
with patch.object(hmip_auth, "get_auth", side_effect=HmipcConnectionError):
|
|
|
|
assert not await hmip_auth.async_setup()
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_auth_auth_check_and_register(hass: HomeAssistant) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test auth client registration."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
|
|
|
|
|
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
hmip_auth.auth = Mock(spec=AsyncAuth)
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch.object(hmip_auth.auth, "isRequestAcknowledged", return_value=True),
|
|
|
|
patch.object(hmip_auth.auth, "requestAuthToken", return_value="ABC"),
|
|
|
|
patch.object(
|
|
|
|
hmip_auth.auth,
|
|
|
|
"confirmAuthToken",
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2020-01-27 20:34:15 +00:00
|
|
|
assert await hmip_auth.async_checkbutton()
|
|
|
|
assert await hmip_auth.async_register() == "ABC"
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_auth_auth_check_and_register_with_exception(hass: HomeAssistant) -> None:
|
2019-10-15 10:12:58 +00:00
|
|
|
"""Test auth client registration."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"}
|
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
hmip_auth.auth = Mock(spec=AsyncAuth)
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch.object(
|
|
|
|
hmip_auth.auth, "isRequestAcknowledged", side_effect=HmipConnectionError
|
|
|
|
),
|
|
|
|
patch.object(
|
|
|
|
hmip_auth.auth, "requestAuthToken", side_effect=HmipConnectionError
|
|
|
|
),
|
2020-01-27 20:34:15 +00:00
|
|
|
):
|
|
|
|
assert not await hmip_auth.async_checkbutton()
|
|
|
|
assert await hmip_auth.async_register() is False
|
2019-10-15 10:12:58 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_hap_setup_works(hass: HomeAssistant) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test a successful setup of a accesspoint."""
|
2023-01-25 09:01:51 +00:00
|
|
|
# This test should not be accessing the integration internals
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=HMIPC_DOMAIN,
|
|
|
|
data={HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"},
|
|
|
|
)
|
2018-07-06 21:05:34 +00:00
|
|
|
home = Mock()
|
2020-01-27 20:34:15 +00:00
|
|
|
hap = HomematicipHAP(hass, entry)
|
|
|
|
with patch.object(hap, "get_hap", return_value=home):
|
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 entry.setup_lock:
|
|
|
|
assert await hap.async_setup()
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
assert hap.home is home
|
|
|
|
|
|
|
|
|
2023-02-07 09:26:56 +00:00
|
|
|
async def test_hap_setup_connection_error() -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test a failed accesspoint setup."""
|
|
|
|
hass = Mock()
|
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
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=HMIPC_DOMAIN,
|
|
|
|
data={HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"},
|
|
|
|
)
|
2020-01-27 20:34:15 +00:00
|
|
|
hap = HomematicipHAP(hass, entry)
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch.object(hap, "get_hap", side_effect=HmipcConnectionError),
|
|
|
|
pytest.raises(ConfigEntryNotReady),
|
2020-01-27 20:34:15 +00:00
|
|
|
):
|
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 entry.setup_lock:
|
|
|
|
assert not await hap.async_setup()
|
2018-07-06 21:05:34 +00:00
|
|
|
|
2024-04-08 00:08:25 +00:00
|
|
|
assert not hass.async_run_hass_job.mock_calls
|
2019-10-06 09:54:26 +00:00
|
|
|
assert not hass.config_entries.flow.async_init.mock_calls
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:25:26 +00:00
|
|
|
async def test_hap_reset_unloads_entry_if_setup(
|
2024-08-19 09:06:50 +00:00
|
|
|
hass: HomeAssistant, default_mock_hap_factory: HomeFactory
|
2023-02-13 09:25:26 +00:00
|
|
|
) -> None:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Test calling reset while the entry has been setup."""
|
2020-02-14 06:56:17 +00:00
|
|
|
mock_hap = await default_mock_hap_factory.async_get_mock_hap()
|
|
|
|
assert hass.data[HMIPC_DOMAIN][HAPID] == mock_hap
|
2020-01-27 20:34:15 +00:00
|
|
|
config_entries = hass.config_entries.async_entries(HMIPC_DOMAIN)
|
|
|
|
assert len(config_entries) == 1
|
|
|
|
# hap_reset is called during unload
|
|
|
|
await hass.config_entries.async_unload(config_entries[0].entry_id)
|
|
|
|
# entry is unloaded
|
2021-05-20 17:19:20 +00:00
|
|
|
assert config_entries[0].state is ConfigEntryState.NOT_LOADED
|
2020-01-27 20:34:15 +00:00
|
|
|
assert hass.data[HMIPC_DOMAIN] == {}
|
2019-10-15 10:12:58 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:25:26 +00:00
|
|
|
async def test_hap_create(
|
2024-08-19 09:06:50 +00:00
|
|
|
hass: HomeAssistant, hmip_config_entry: MockConfigEntry, simple_mock_home
|
2023-02-13 09:25:26 +00:00
|
|
|
) -> None:
|
2019-10-15 10:12:58 +00:00
|
|
|
"""Mock AsyncHome to execute get_hap."""
|
|
|
|
hass.config.components.add(HMIPC_DOMAIN)
|
|
|
|
hap = HomematicipHAP(hass, hmip_config_entry)
|
|
|
|
assert hap
|
2020-03-14 18:35:15 +00:00
|
|
|
with patch.object(hap, "async_connect"):
|
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 hmip_config_entry.setup_lock:
|
|
|
|
assert await hap.async_setup()
|
2019-10-15 10:12:58 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:25:26 +00:00
|
|
|
async def test_hap_create_exception(
|
2024-08-19 09:06:50 +00:00
|
|
|
hass: HomeAssistant, hmip_config_entry: MockConfigEntry, mock_connection_init
|
2023-02-13 09:25:26 +00:00
|
|
|
) -> None:
|
2019-10-15 10:12:58 +00:00
|
|
|
"""Mock AsyncHome to execute get_hap."""
|
|
|
|
hass.config.components.add(HMIPC_DOMAIN)
|
2020-02-16 09:09:26 +00:00
|
|
|
|
2019-10-15 10:12:58 +00:00
|
|
|
hap = HomematicipHAP(hass, hmip_config_entry)
|
|
|
|
assert hap
|
|
|
|
|
2020-02-16 09:09:26 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.homematicip_cloud.hap.AsyncHome.get_current_state",
|
|
|
|
side_effect=Exception,
|
2019-10-15 10:12:58 +00:00
|
|
|
):
|
2020-02-16 09:09:26 +00:00
|
|
|
assert not await hap.async_setup()
|
2019-10-15 10:12:58 +00:00
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.homematicip_cloud.hap.AsyncHome.get_current_state",
|
|
|
|
side_effect=HmipConnectionError,
|
|
|
|
),
|
|
|
|
pytest.raises(ConfigEntryNotReady),
|
|
|
|
):
|
2019-10-15 10:12:58 +00:00
|
|
|
await hap.async_setup()
|
|
|
|
|
|
|
|
|
2023-02-13 09:25:26 +00:00
|
|
|
async def test_auth_create(hass: HomeAssistant, simple_mock_auth) -> None:
|
2019-10-15 10:12:58 +00:00
|
|
|
"""Mock AsyncAuth to execute get_auth."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: HAPID, HMIPC_PIN: HAPPIN, HMIPC_NAME: "hmip"}
|
2019-10-15 10:12:58 +00:00
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
assert hmip_auth
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.homematicip_cloud.hap.AsyncAuth",
|
|
|
|
return_value=simple_mock_auth,
|
|
|
|
):
|
2019-10-23 16:21:49 +00:00
|
|
|
assert await hmip_auth.async_setup()
|
2019-10-15 10:12:58 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hmip_auth.auth.pin == HAPPIN
|
|
|
|
|
|
|
|
|
2023-02-13 09:25:26 +00:00
|
|
|
async def test_auth_create_exception(hass: HomeAssistant, simple_mock_auth) -> None:
|
2019-10-15 10:12:58 +00:00
|
|
|
"""Mock AsyncAuth to execute get_auth."""
|
2020-01-27 20:34:15 +00:00
|
|
|
config = {HMIPC_HAPID: HAPID, HMIPC_PIN: HAPPIN, HMIPC_NAME: "hmip"}
|
2019-10-15 10:12:58 +00:00
|
|
|
hmip_auth = HomematicipAuth(hass, config)
|
|
|
|
simple_mock_auth.connectionRequest.side_effect = HmipConnectionError
|
|
|
|
assert hmip_auth
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.homematicip_cloud.hap.AsyncAuth",
|
|
|
|
return_value=simple_mock_auth,
|
|
|
|
):
|
2020-09-03 07:52:51 +00:00
|
|
|
assert not await hmip_auth.async_setup()
|
2019-10-15 10:12:58 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.homematicip_cloud.hap.AsyncAuth",
|
|
|
|
return_value=simple_mock_auth,
|
|
|
|
):
|
2019-10-23 16:21:49 +00:00
|
|
|
assert not await hmip_auth.get_auth(hass, HAPID, HAPPIN)
|