Less use of hass.data[DECONZ_DOMAIN] in deCONZ tests (#122657)
* Less use of hass.data[DECONZ_DOMAIN] in deCONZ tests * Fix review comment * Change patch path
This commit is contained in:
parent
57a5c7c8b6
commit
58419f14e8
2 changed files with 54 additions and 84 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pydeconz
|
||||
from pydeconz.websocket import State
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
@ -10,8 +9,7 @@ from syrupy import SnapshotAssertion
|
|||
from homeassistant.components import ssdp
|
||||
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
|
||||
from homeassistant.components.deconz.hub import DeconzHub, get_deconz_api
|
||||
from homeassistant.components.deconz.hub import DeconzHub
|
||||
from homeassistant.components.ssdp import (
|
||||
ATTR_UPNP_MANUFACTURER_URL,
|
||||
ATTR_UPNP_SERIAL,
|
||||
|
@ -110,37 +108,3 @@ async def test_reset_after_successful_setup(
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert result is True
|
||||
|
||||
|
||||
async def test_get_deconz_api(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Successful call."""
|
||||
with patch("pydeconz.DeconzSession.refresh_state", return_value=True):
|
||||
assert await get_deconz_api(hass, config_entry)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "raised_exception"),
|
||||
[
|
||||
(TimeoutError, CannotConnect),
|
||||
(pydeconz.RequestError, CannotConnect),
|
||||
(pydeconz.ResponseError, CannotConnect),
|
||||
(pydeconz.Unauthorized, AuthenticationRequired),
|
||||
],
|
||||
)
|
||||
async def test_get_deconz_api_fails(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
side_effect: Exception,
|
||||
raised_exception: Exception,
|
||||
) -> None:
|
||||
"""Failed call."""
|
||||
with (
|
||||
patch(
|
||||
"pydeconz.DeconzSession.refresh_state",
|
||||
side_effect=side_effect,
|
||||
),
|
||||
pytest.raises(raised_exception),
|
||||
):
|
||||
assert await get_deconz_api(hass, config_entry)
|
||||
|
|
|
@ -3,13 +3,15 @@
|
|||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.deconz import (
|
||||
DeconzHub,
|
||||
async_setup_entry,
|
||||
async_unload_entry,
|
||||
import pydeconz
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.deconz.const import (
|
||||
CONF_MASTER_GATEWAY,
|
||||
DOMAIN as DECONZ_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
|
||||
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
|
||||
from homeassistant.components.deconz.errors import AuthenticationRequired
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .conftest import ConfigEntryFactoryType
|
||||
|
@ -17,35 +19,38 @@ from .conftest import ConfigEntryFactoryType
|
|||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
|
||||
"""Test that setup entry works."""
|
||||
with (
|
||||
patch.object(DeconzHub, "async_setup", return_value=True),
|
||||
patch.object(DeconzHub, "async_update_device_registry", return_value=True),
|
||||
):
|
||||
assert await async_setup_entry(hass, entry) is True
|
||||
async def test_setup_entry(config_entry_setup: MockConfigEntry) -> None:
|
||||
"""Test successful setup of entry."""
|
||||
assert config_entry_setup.state is ConfigEntryState.LOADED
|
||||
assert config_entry_setup.options[CONF_MASTER_GATEWAY] is True
|
||||
|
||||
|
||||
async def test_setup_entry_successful(
|
||||
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "state"),
|
||||
[
|
||||
# Failed authentication trigger a reauthentication flow
|
||||
(pydeconz.Unauthorized, ConfigEntryState.SETUP_ERROR),
|
||||
# Connection fails
|
||||
(TimeoutError, ConfigEntryState.SETUP_RETRY),
|
||||
(pydeconz.RequestError, ConfigEntryState.SETUP_RETRY),
|
||||
(pydeconz.ResponseError, ConfigEntryState.SETUP_RETRY),
|
||||
],
|
||||
)
|
||||
async def test_get_deconz_api_fails(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
side_effect: Exception,
|
||||
state: ConfigEntryState,
|
||||
) -> None:
|
||||
"""Test setup entry is successful."""
|
||||
assert hass.data[DECONZ_DOMAIN]
|
||||
assert config_entry_setup.entry_id in hass.data[DECONZ_DOMAIN]
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry_setup.entry_id].master
|
||||
|
||||
|
||||
async def test_setup_entry_fails_config_entry_not_ready(
|
||||
hass: HomeAssistant, config_entry_factory: ConfigEntryFactoryType
|
||||
) -> None:
|
||||
"""Failed authentication trigger a reauthentication flow."""
|
||||
"""Failed setup."""
|
||||
config_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.deconz.get_deconz_api",
|
||||
side_effect=CannotConnect,
|
||||
"homeassistant.components.deconz.hub.api.DeconzSession.refresh_state",
|
||||
side_effect=side_effect,
|
||||
):
|
||||
await config_entry_factory()
|
||||
|
||||
assert hass.data[DECONZ_DOMAIN] == {}
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.state is state
|
||||
|
||||
|
||||
async def test_setup_entry_fails_trigger_reauth_flow(
|
||||
|
@ -59,10 +64,9 @@ async def test_setup_entry_fails_trigger_reauth_flow(
|
|||
),
|
||||
patch.object(hass.config_entries.flow, "async_init") as mock_flow_init,
|
||||
):
|
||||
await config_entry_factory()
|
||||
config_entry = await config_entry_factory()
|
||||
mock_flow_init.assert_called_once()
|
||||
|
||||
assert hass.data[DECONZ_DOMAIN] == {}
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_setup_entry_multiple_gateways(
|
||||
|
@ -79,19 +83,19 @@ async def test_setup_entry_multiple_gateways(
|
|||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry.entry_id].master
|
||||
assert not hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert config_entry2.state is ConfigEntryState.LOADED
|
||||
assert config_entry.options[CONF_MASTER_GATEWAY] is True
|
||||
assert config_entry2.options[CONF_MASTER_GATEWAY] is False
|
||||
|
||||
|
||||
async def test_unload_entry(
|
||||
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test being able to unload an entry."""
|
||||
assert hass.data[DECONZ_DOMAIN]
|
||||
|
||||
assert await async_unload_entry(hass, config_entry_setup)
|
||||
assert not hass.data[DECONZ_DOMAIN]
|
||||
assert config_entry_setup.state is ConfigEntryState.LOADED
|
||||
assert await hass.config_entries.async_unload(config_entry_setup.entry_id)
|
||||
assert config_entry_setup.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
async def test_unload_entry_multiple_gateways(
|
||||
|
@ -108,12 +112,12 @@ async def test_unload_entry_multiple_gateways(
|
|||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert config_entry2.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await async_unload_entry(hass, config_entry)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 1
|
||||
assert hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master
|
||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert config_entry2.options[CONF_MASTER_GATEWAY] is True
|
||||
|
||||
|
||||
async def test_unload_entry_multiple_gateways_parallel(
|
||||
|
@ -130,11 +134,13 @@ async def test_unload_entry_multiple_gateways_parallel(
|
|||
)
|
||||
config_entry2 = await config_entry_factory(entry2)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert config_entry2.state is ConfigEntryState.LOADED
|
||||
|
||||
await asyncio.gather(
|
||||
hass.config_entries.async_unload(config_entry.entry_id),
|
||||
hass.config_entries.async_unload(config_entry2.entry_id),
|
||||
)
|
||||
|
||||
assert len(hass.data[DECONZ_DOMAIN]) == 0
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert config_entry2.state is ConfigEntryState.NOT_LOADED
|
||||
|
|
Loading…
Add table
Reference in a new issue