2020-07-30 18:41:18 +00:00
|
|
|
"""Test sensor of Airly integration."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2020-07-30 18:41:18 +00:00
|
|
|
from datetime import timedelta
|
2023-01-13 13:35:52 +00:00
|
|
|
from http import HTTPStatus
|
2024-01-22 19:59:24 +00:00
|
|
|
from unittest.mock import patch
|
2023-01-13 13:35:52 +00:00
|
|
|
|
|
|
|
from airly.exceptions import AirlyError
|
2024-01-22 19:59:24 +00:00
|
|
|
from syrupy import SnapshotAssertion
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2024-01-22 19:59:24 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
|
2023-02-08 11:16:23 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-09 13:24:34 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-07-30 18:41:18 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.util.dt import utcnow
|
|
|
|
|
2022-09-19 07:48:25 +00:00
|
|
|
from . import API_POINT_URL, init_integration
|
2020-12-17 15:44:46 +00:00
|
|
|
|
2020-07-30 18:41:18 +00:00
|
|
|
from tests.common import async_fire_time_changed, load_fixture
|
2023-02-08 11:16:23 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-11-09 19:46:20 +00:00
|
|
|
async def test_sensor(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
|
|
|
entity_registry: er.EntityRegistry,
|
2024-01-22 19:59:24 +00:00
|
|
|
snapshot: SnapshotAssertion,
|
2023-11-09 19:46:20 +00:00
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test states of the sensor."""
|
2024-01-22 19:59:24 +00:00
|
|
|
with patch("homeassistant.components.airly.PLATFORMS", [Platform.SENSOR]):
|
|
|
|
entry = await init_integration(hass, aioclient_mock)
|
2021-06-28 08:15:56 +00:00
|
|
|
|
2024-01-22 19:59:24 +00:00
|
|
|
entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2024-01-22 19:59:24 +00:00
|
|
|
assert entity_entries
|
|
|
|
for entity_entry in entity_entries:
|
|
|
|
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
|
|
|
|
assert (state := hass.states.get(entity_entry.entity_id))
|
|
|
|
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_availability(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Ensure that we mark the entities unavailable correctly when service is offline."""
|
2020-12-17 15:44:46 +00:00
|
|
|
await init_integration(hass, aioclient_mock)
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
state = hass.states.get("sensor.home_humidity")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2023-02-04 19:54:36 +00:00
|
|
|
assert state.state == "68.35"
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
aioclient_mock.clear_requests()
|
2023-01-13 13:35:52 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
API_POINT_URL, exc=AirlyError(HTTPStatus.NOT_FOUND, {"message": "Not found"})
|
|
|
|
)
|
2020-07-30 18:41:18 +00:00
|
|
|
future = utcnow() + timedelta(minutes=60)
|
2020-12-17 15:44:46 +00:00
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
state = hass.states.get("sensor.home_humidity")
|
|
|
|
assert state
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
aioclient_mock.clear_requests()
|
2021-11-02 03:47:05 +00:00
|
|
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
|
2020-07-30 18:41:18 +00:00
|
|
|
future = utcnow() + timedelta(minutes=120)
|
2020-12-17 15:44:46 +00:00
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
state = hass.states.get("sensor.home_humidity")
|
|
|
|
assert state
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2023-02-04 19:54:36 +00:00
|
|
|
assert state.state == "68.35"
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_manual_update_entity(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2022-01-18 03:22:11 +00:00
|
|
|
"""Test manual update entity via service homeassistant/update_entity."""
|
2020-12-17 15:44:46 +00:00
|
|
|
await init_integration(hass, aioclient_mock)
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
call_count = aioclient_mock.call_count
|
2020-07-30 18:41:18 +00:00
|
|
|
await async_setup_component(hass, "homeassistant", {})
|
2020-12-17 15:44:46 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"homeassistant",
|
|
|
|
"update_entity",
|
|
|
|
{ATTR_ENTITY_ID: ["sensor.home_humidity"]},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert aioclient_mock.call_count == call_count + 1
|