2021-05-07 13:59:29 +00:00
|
|
|
"""Test init of Nettigo Air Monitor integration."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2023-02-21 23:41:46 +00:00
|
|
|
from nettigo_air_monitor import ApiError, AuthFailedError
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2021-06-24 19:58:37 +00:00
|
|
|
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.components.nam.const import DOMAIN
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-06-24 19:58:37 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2022-09-19 07:46:59 +00:00
|
|
|
from . import init_integration
|
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_async_setup_entry(hass: HomeAssistant) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test a successful setup entry."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
2023-04-14 07:43:15 +00:00
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_sds011_pm2_5")
|
2021-05-07 13:59:29 +00:00
|
|
|
assert state is not None
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2023-02-07 08:14:20 +00:00
|
|
|
assert state.state == "11.0"
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_config_not_ready(hass: HomeAssistant) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test for setup failure if the connection to the device fails."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="10.10.2.3",
|
|
|
|
unique_id="aa:bb:cc:dd:ee:ff",
|
|
|
|
data={"host": "10.10.2.3"},
|
|
|
|
)
|
2022-10-30 19:01:10 +00:00
|
|
|
entry.add_to_hass(hass)
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
with patch(
|
2021-11-18 01:00:19 +00:00
|
|
|
"homeassistant.components.nam.NettigoAirMonitor.initialize",
|
2021-05-07 13:59:29 +00:00
|
|
|
side_effect=ApiError("API Error"),
|
|
|
|
):
|
2022-10-30 19:01:10 +00:00
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_config_not_ready_while_checking_credentials(hass: HomeAssistant) -> None:
|
2022-10-30 19:01:10 +00:00
|
|
|
"""Test for setup failure if the connection fails while checking credentials."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="10.10.2.3",
|
|
|
|
unique_id="aa:bb:cc:dd:ee:ff",
|
|
|
|
data={"host": "10.10.2.3"},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch("homeassistant.components.nam.NettigoAirMonitor.initialize"), patch(
|
|
|
|
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
|
|
|
|
side_effect=ApiError("API Error"),
|
|
|
|
):
|
2021-05-07 13:59:29 +00:00
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_config_auth_failed(hass: HomeAssistant) -> None:
|
2021-11-18 01:00:19 +00:00
|
|
|
"""Test for setup failure if the auth fails."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="10.10.2.3",
|
|
|
|
unique_id="aa:bb:cc:dd:ee:ff",
|
|
|
|
data={"host": "10.10.2.3"},
|
|
|
|
)
|
2022-10-30 19:01:10 +00:00
|
|
|
entry.add_to_hass(hass)
|
2021-11-18 01:00:19 +00:00
|
|
|
|
|
|
|
with patch(
|
2022-06-07 16:56:11 +00:00
|
|
|
"homeassistant.components.nam.NettigoAirMonitor.async_check_credentials",
|
2023-02-21 23:41:46 +00:00
|
|
|
side_effect=AuthFailedError("Authorization has failed"),
|
2021-11-18 01:00:19 +00:00
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
assert entry.state is ConfigEntryState.SETUP_ERROR
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_unload_entry(hass: HomeAssistant) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test successful unload of entry."""
|
|
|
|
entry = await init_integration(hass)
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
2021-05-07 13:59:29 +00:00
|
|
|
assert not hass.data.get(DOMAIN)
|
2021-06-24 19:58:37 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_remove_air_quality_entities(hass: HomeAssistant) -> None:
|
2021-06-24 19:58:37 +00:00
|
|
|
"""Test remove air_quality entities from registry."""
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
|
|
|
|
registry.async_get_or_create(
|
|
|
|
AIR_QUALITY_PLATFORM,
|
|
|
|
DOMAIN,
|
|
|
|
"aa:bb:cc:dd:ee:ff-sds011",
|
|
|
|
suggested_object_id="nettigo_air_monitor_sds011",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
registry.async_get_or_create(
|
|
|
|
AIR_QUALITY_PLATFORM,
|
|
|
|
DOMAIN,
|
|
|
|
"aa:bb:cc:dd:ee:ff-sps30",
|
|
|
|
suggested_object_id="nettigo_air_monitor_sps30",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
entry = registry.async_get("air_quality.nettigo_air_monitor_sds011")
|
|
|
|
assert entry is None
|
|
|
|
|
|
|
|
entry = registry.async_get("air_quality.nettigo_air_monitor_sps30")
|
|
|
|
assert entry is None
|