|
| 1 | +"""Test the air-Q coordinator.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from aioairq import DeviceInfo as AirQDeviceInfo |
| 7 | +import pytest |
| 8 | + |
| 9 | +from homeassistant.components.airq import AirQCoordinator |
| 10 | +from homeassistant.components.airq.const import DOMAIN |
| 11 | +from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD |
| 12 | +from homeassistant.core import HomeAssistant |
| 13 | +from homeassistant.helpers.device_registry import DeviceInfo |
| 14 | + |
| 15 | +from tests.common import MockConfigEntry |
| 16 | + |
| 17 | +pytestmark = pytest.mark.usefixtures("mock_setup_entry") |
| 18 | +MOCKED_ENTRY = MockConfigEntry( |
| 19 | + domain=DOMAIN, |
| 20 | + data={ |
| 21 | + CONF_IP_ADDRESS: "192.168.0.0", |
| 22 | + CONF_PASSWORD: "password", |
| 23 | + }, |
| 24 | + unique_id="123-456", |
| 25 | +) |
| 26 | + |
| 27 | +TEST_DEVICE_INFO = AirQDeviceInfo( |
| 28 | + id="id", |
| 29 | + name="name", |
| 30 | + model="model", |
| 31 | + sw_version="sw", |
| 32 | + hw_version="hw", |
| 33 | +) |
| 34 | +TEST_DEVICE_DATA = {"co2": 500.0, "Status": "OK"} |
| 35 | +STATUS_WARMUP = { |
| 36 | + "co": "co sensor still in warm up phase; waiting time = 18 s", |
| 37 | + "tvoc": "tvoc sensor still in warm up phase; waiting time = 18 s", |
| 38 | + "so2": "so2 sensor still in warm up phase; waiting time = 17 s", |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +async def test_logging_in_coordinator_first_update_data( |
| 43 | + hass: HomeAssistant, caplog: pytest.LogCaptureFixture |
| 44 | +) -> None: |
| 45 | + """Test that the first AirQCoordinator._async_update_data call logs necessary setup. |
| 46 | +
|
| 47 | + The fields of AirQCoordinator.device_info that are specific to the device are only |
| 48 | + populated upon the first call to AirQCoordinator._async_update_data. The one field |
| 49 | + which is actually necessary is 'name', and its absence is checked and logged, |
| 50 | + as well as its being set. |
| 51 | + """ |
| 52 | + caplog.set_level(logging.DEBUG) |
| 53 | + coordinator = AirQCoordinator(hass, MOCKED_ENTRY) |
| 54 | + |
| 55 | + # check that the name _is_ missing |
| 56 | + assert "name" not in coordinator.device_info |
| 57 | + |
| 58 | + # First call: fetch missing device info |
| 59 | + with ( |
| 60 | + patch("aioairq.AirQ.fetch_device_info", return_value=TEST_DEVICE_INFO), |
| 61 | + patch("aioairq.AirQ.get_latest_data", return_value=TEST_DEVICE_DATA), |
| 62 | + ): |
| 63 | + await coordinator._async_update_data() |
| 64 | + |
| 65 | + # check that the missing name is logged... |
| 66 | + assert ( |
| 67 | + "'name' not found in AirQCoordinator.device_info, fetching from the device" |
| 68 | + in caplog.text |
| 69 | + ) |
| 70 | + # ...and fixed |
| 71 | + assert coordinator.device_info.get("name") == TEST_DEVICE_INFO["name"] |
| 72 | + assert ( |
| 73 | + f"Updated AirQCoordinator.device_info for 'name' {TEST_DEVICE_INFO['name']}" |
| 74 | + in caplog.text |
| 75 | + ) |
| 76 | + |
| 77 | + # Also that no warming up sensors is found as none are mocked |
| 78 | + assert "Following sensors are still warming up" not in caplog.text |
| 79 | + |
| 80 | + |
| 81 | +async def test_logging_in_coordinator_subsequent_update_data( |
| 82 | + hass: HomeAssistant, caplog: pytest.LogCaptureFixture |
| 83 | +) -> None: |
| 84 | + """Test that the second AirQCoordinator._async_update_data call has nothing to log. |
| 85 | +
|
| 86 | + The second call is emulated by setting up AirQCoordinator.device_info correctly, |
| 87 | + instead of actually calling the _async_update_data, which would populate the log |
| 88 | + with the messages we want to see not being repeated. |
| 89 | + """ |
| 90 | + caplog.set_level(logging.DEBUG) |
| 91 | + coordinator = AirQCoordinator(hass, MOCKED_ENTRY) |
| 92 | + coordinator.device_info.update(DeviceInfo(**TEST_DEVICE_INFO)) |
| 93 | + |
| 94 | + with ( |
| 95 | + patch("aioairq.AirQ.fetch_device_info", return_value=TEST_DEVICE_INFO), |
| 96 | + patch("aioairq.AirQ.get_latest_data", return_value=TEST_DEVICE_DATA), |
| 97 | + ): |
| 98 | + await coordinator._async_update_data() |
| 99 | + # check that the name _is not_ missing |
| 100 | + assert "name" in coordinator.device_info |
| 101 | + # and that nothing of the kind is logged |
| 102 | + assert ( |
| 103 | + "'name' not found in AirQCoordinator.device_info, fetching from the device" |
| 104 | + not in caplog.text |
| 105 | + ) |
| 106 | + assert ( |
| 107 | + f"Updated AirQCoordinator.device_info for 'name' {TEST_DEVICE_INFO['name']}" |
| 108 | + not in caplog.text |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +async def test_logging_when_warming_up_sensor_present( |
| 113 | + hass: HomeAssistant, caplog: pytest.LogCaptureFixture |
| 114 | +) -> None: |
| 115 | + """Test that warming up sensors are logged.""" |
| 116 | + caplog.set_level(logging.DEBUG) |
| 117 | + coordinator = AirQCoordinator(hass, MOCKED_ENTRY) |
| 118 | + with ( |
| 119 | + patch("aioairq.AirQ.fetch_device_info", return_value=TEST_DEVICE_INFO), |
| 120 | + patch( |
| 121 | + "aioairq.AirQ.get_latest_data", |
| 122 | + return_value=TEST_DEVICE_DATA | {"Status": STATUS_WARMUP}, |
| 123 | + ), |
| 124 | + ): |
| 125 | + await coordinator._async_update_data() |
| 126 | + assert ( |
| 127 | + f"Following sensors are still warming up: {set(STATUS_WARMUP.keys())}" |
| 128 | + in caplog.text |
| 129 | + ) |
0 commit comments