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
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pydeconz
|
|
||||||
from pydeconz.websocket import State
|
from pydeconz.websocket import State
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
@ -10,8 +9,7 @@ from syrupy import SnapshotAssertion
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL
|
from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL
|
||||||
from homeassistant.components.deconz.const import 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.hub import DeconzHub
|
||||||
from homeassistant.components.deconz.hub import DeconzHub, get_deconz_api
|
|
||||||
from homeassistant.components.ssdp import (
|
from homeassistant.components.ssdp import (
|
||||||
ATTR_UPNP_MANUFACTURER_URL,
|
ATTR_UPNP_MANUFACTURER_URL,
|
||||||
ATTR_UPNP_SERIAL,
|
ATTR_UPNP_SERIAL,
|
||||||
|
@ -110,37 +108,3 @@ async def test_reset_after_successful_setup(
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result is True
|
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
|
import asyncio
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.deconz import (
|
import pydeconz
|
||||||
DeconzHub,
|
import pytest
|
||||||
async_setup_entry,
|
|
||||||
async_unload_entry,
|
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
|
||||||
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .conftest import ConfigEntryFactoryType
|
from .conftest import ConfigEntryFactoryType
|
||||||
|
@ -17,35 +19,38 @@ from .conftest import ConfigEntryFactoryType
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def setup_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None:
|
async def test_setup_entry(config_entry_setup: MockConfigEntry) -> None:
|
||||||
"""Test that setup entry works."""
|
"""Test successful setup of entry."""
|
||||||
with (
|
assert config_entry_setup.state is ConfigEntryState.LOADED
|
||||||
patch.object(DeconzHub, "async_setup", return_value=True),
|
assert config_entry_setup.options[CONF_MASTER_GATEWAY] is 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_successful(
|
@pytest.mark.parametrize(
|
||||||
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
("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:
|
) -> None:
|
||||||
"""Test setup entry is successful."""
|
"""Failed setup."""
|
||||||
assert hass.data[DECONZ_DOMAIN]
|
config_entry.add_to_hass(hass)
|
||||||
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."""
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.deconz.get_deconz_api",
|
"homeassistant.components.deconz.hub.api.DeconzSession.refresh_state",
|
||||||
side_effect=CannotConnect,
|
side_effect=side_effect,
|
||||||
):
|
):
|
||||||
await config_entry_factory()
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert hass.data[DECONZ_DOMAIN] == {}
|
assert config_entry.state is state
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_entry_fails_trigger_reauth_flow(
|
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,
|
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()
|
mock_flow_init.assert_called_once()
|
||||||
|
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||||
assert hass.data[DECONZ_DOMAIN] == {}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_setup_entry_multiple_gateways(
|
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)
|
config_entry2 = await config_entry_factory(entry2)
|
||||||
|
|
||||||
assert len(hass.data[DECONZ_DOMAIN]) == 2
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert hass.data[DECONZ_DOMAIN][config_entry.entry_id].master
|
assert config_entry2.state is ConfigEntryState.LOADED
|
||||||
assert not hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master
|
assert config_entry.options[CONF_MASTER_GATEWAY] is True
|
||||||
|
assert config_entry2.options[CONF_MASTER_GATEWAY] is False
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry(
|
async def test_unload_entry(
|
||||||
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test being able to unload an entry."""
|
"""Test being able to unload an entry."""
|
||||||
assert 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 await async_unload_entry(hass, config_entry_setup)
|
assert config_entry_setup.state is ConfigEntryState.NOT_LOADED
|
||||||
assert not hass.data[DECONZ_DOMAIN]
|
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry_multiple_gateways(
|
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)
|
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 await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
assert len(hass.data[DECONZ_DOMAIN]) == 1
|
assert config_entry2.options[CONF_MASTER_GATEWAY] is True
|
||||||
assert hass.data[DECONZ_DOMAIN][config_entry2.entry_id].master
|
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry_multiple_gateways_parallel(
|
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)
|
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(
|
await asyncio.gather(
|
||||||
hass.config_entries.async_unload(config_entry.entry_id),
|
hass.config_entries.async_unload(config_entry.entry_id),
|
||||||
hass.config_entries.async_unload(config_entry2.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