2021-05-07 13:59:29 +00:00
|
|
|
"""Test sensor of Nettigo Air Monitor integration."""
|
2024-03-08 13:55:15 +00:00
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
from datetime import timedelta
|
2021-11-18 01:00:19 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2024-04-20 10:34:27 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2021-05-07 13:59:29 +00:00
|
|
|
from nettigo_air_monitor import ApiError
|
2024-05-27 10:54:10 +00:00
|
|
|
import pytest
|
2024-04-20 10:34:27 +00:00
|
|
|
from syrupy import SnapshotAssertion
|
2024-05-27 10:54:10 +00:00
|
|
|
from tenacity import RetryError
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2024-05-27 10:54:10 +00:00
|
|
|
from homeassistant.components.nam.const import DEFAULT_UPDATE_INTERVAL, DOMAIN
|
2024-04-20 10:34:27 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
STATE_UNAVAILABLE,
|
2024-04-20 10:34:27 +00:00
|
|
|
Platform,
|
2023-01-15 14:45:00 +00:00
|
|
|
UnitOfTemperature,
|
2021-05-07 13:59:29 +00:00
|
|
|
)
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-07 13:59:29 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
2024-04-20 10:34:27 +00:00
|
|
|
from . import INCOMPLETE_NAM_DATA, init_integration
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2024-04-20 16:43:33 +00:00
|
|
|
from tests.common import (
|
|
|
|
async_fire_time_changed,
|
|
|
|
load_json_object_fixture,
|
|
|
|
snapshot_platform,
|
|
|
|
)
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
|
2024-06-04 14:18:42 +00:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
2024-04-20 10:34:27 +00:00
|
|
|
async def test_sensor(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test states of the air_quality."""
|
2024-05-20 09:06:03 +00:00
|
|
|
await hass.config.async_set_time_zone("UTC")
|
2024-04-20 10:34:27 +00:00
|
|
|
freezer.move_to("2024-04-20 12:00:00+00:00")
|
2022-08-28 19:54:00 +00:00
|
|
|
|
2024-04-20 10:34:27 +00:00
|
|
|
with patch("homeassistant.components.nam.PLATFORMS", [Platform.SENSOR]):
|
|
|
|
entry = await init_integration(hass)
|
2021-06-24 19:58:37 +00:00
|
|
|
|
2024-04-20 16:43:33 +00:00
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
|
2021-06-24 19:58:37 +00:00
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
async def test_sensor_disabled(
|
|
|
|
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
|
|
|
) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test sensor disabled by default."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
entry = entity_registry.async_get("sensor.nettigo_air_monitor_signal_strength")
|
2021-05-07 13:59:29 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == "aa:bb:cc:dd:ee:ff-signal"
|
|
|
|
assert entry.disabled
|
2021-12-15 21:25:40 +00:00
|
|
|
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
# Test enabling entity
|
2023-11-15 09:33:11 +00:00
|
|
|
updated_entry = entity_registry.async_update_entity(
|
2024-03-16 22:45:18 +00:00
|
|
|
entry.entity_id, disabled_by=None
|
2021-05-07 13:59:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert updated_entry != entry
|
|
|
|
assert updated_entry.disabled is False
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_incompleta_data_after_device_restart(hass: HomeAssistant) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test states of the air_quality after device restart."""
|
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_heca_temperature")
|
|
|
|
assert state
|
|
|
|
assert state.state == "8.0"
|
2021-12-21 20:54:58 +00:00
|
|
|
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TEMPERATURE
|
2023-01-15 14:45:00 +00:00
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS
|
2021-05-07 13:59:29 +00:00
|
|
|
|
|
|
|
future = utcnow() + timedelta(minutes=6)
|
2021-11-18 01:00:19 +00:00
|
|
|
update_response = Mock(json=AsyncMock(return_value=INCOMPLETE_NAM_DATA))
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nam.NettigoAirMonitor.initialize"),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.nam.NettigoAirMonitor._async_http_request",
|
|
|
|
return_value=update_response,
|
|
|
|
),
|
2021-05-07 13:59:29 +00:00
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_heca_temperature")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
|
2024-05-27 10:54:10 +00:00
|
|
|
@pytest.mark.parametrize("exc", [ApiError("API Error"), RetryError])
|
|
|
|
async def test_availability(
|
|
|
|
hass: HomeAssistant, freezer: FrozenDateTimeFactory, exc: Exception
|
|
|
|
) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Ensure that we mark the entities unavailable correctly when device causes an error."""
|
2024-04-20 10:34:27 +00:00
|
|
|
nam_data = load_json_object_fixture("nam/nam_data.json")
|
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_bme280_temperature")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == "7.6"
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nam.NettigoAirMonitor.initialize"),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.nam.NettigoAirMonitor._async_http_request",
|
2024-05-27 10:54:10 +00:00
|
|
|
side_effect=exc,
|
2024-03-25 23:02:16 +00:00
|
|
|
),
|
2021-05-07 13:59:29 +00:00
|
|
|
):
|
2024-05-27 10:54:10 +00:00
|
|
|
freezer.tick(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
2021-05-07 13:59:29 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_bme280_temperature")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
2021-11-18 01:00:19 +00:00
|
|
|
update_response = Mock(json=AsyncMock(return_value=nam_data))
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nam.NettigoAirMonitor.initialize"),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.nam.NettigoAirMonitor._async_http_request",
|
|
|
|
return_value=update_response,
|
|
|
|
),
|
2021-05-07 13:59:29 +00:00
|
|
|
):
|
2024-05-27 10:54:10 +00:00
|
|
|
freezer.tick(DEFAULT_UPDATE_INTERVAL)
|
|
|
|
async_fire_time_changed(hass)
|
2021-05-07 13:59:29 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.nettigo_air_monitor_bme280_temperature")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
|
|
|
assert state.state == "7.6"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_manual_update_entity(hass: HomeAssistant) -> None:
|
2021-05-07 13:59:29 +00:00
|
|
|
"""Test manual update entity via service homeasasistant/update_entity."""
|
2024-04-20 10:34:27 +00:00
|
|
|
nam_data = load_json_object_fixture("nam/nam_data.json")
|
|
|
|
|
2021-05-07 13:59:29 +00:00
|
|
|
await init_integration(hass)
|
|
|
|
|
|
|
|
await async_setup_component(hass, "homeassistant", {})
|
|
|
|
|
2021-11-18 01:00:19 +00:00
|
|
|
update_response = Mock(json=AsyncMock(return_value=nam_data))
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("homeassistant.components.nam.NettigoAirMonitor.initialize"),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.nam.NettigoAirMonitor._async_http_request",
|
|
|
|
return_value=update_response,
|
|
|
|
) as mock_get_data,
|
|
|
|
):
|
2021-05-07 13:59:29 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"homeassistant",
|
|
|
|
"update_entity",
|
|
|
|
{ATTR_ENTITY_ID: ["sensor.nettigo_air_monitor_bme280_temperature"]},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert mock_get_data.call_count == 1
|
2021-06-22 13:44:53 +00:00
|
|
|
|
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
async def test_unique_id_migration(
|
|
|
|
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
|
|
|
) -> None:
|
2021-06-22 13:44:53 +00:00
|
|
|
"""Test states of the unique_id migration."""
|
2023-11-15 09:33:11 +00:00
|
|
|
entity_registry.async_get_or_create(
|
2021-06-22 13:44:53 +00:00
|
|
|
SENSOR_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
"aa:bb:cc:dd:ee:ff-temperature",
|
|
|
|
suggested_object_id="nettigo_air_monitor_dht22_temperature",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
entity_registry.async_get_or_create(
|
2021-06-22 13:44:53 +00:00
|
|
|
SENSOR_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
"aa:bb:cc:dd:ee:ff-humidity",
|
|
|
|
suggested_object_id="nettigo_air_monitor_dht22_humidity",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
await init_integration(hass)
|
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
entry = entity_registry.async_get("sensor.nettigo_air_monitor_dht22_temperature")
|
2021-06-22 13:44:53 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == "aa:bb:cc:dd:ee:ff-dht22_temperature"
|
|
|
|
|
2023-11-15 09:33:11 +00:00
|
|
|
entry = entity_registry.async_get("sensor.nettigo_air_monitor_dht22_humidity")
|
2021-06-22 13:44:53 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.unique_id == "aa:bb:cc:dd:ee:ff-dht22_humidity"
|