|
| 1 | +"""Fixtures for GIOS integration tests.""" |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator |
| 4 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 5 | + |
| 6 | +from gios.model import GiosSensors, GiosStation, Sensor as GiosSensor |
| 7 | +import pytest |
| 8 | + |
| 9 | +from homeassistant.components.gios.const import DOMAIN |
| 10 | +from homeassistant.core import HomeAssistant |
| 11 | + |
| 12 | +from . import setup_integration |
| 13 | + |
| 14 | +from tests.common import MockConfigEntry |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_config_entry() -> MockConfigEntry: |
| 19 | + """Return the default mocked config entry.""" |
| 20 | + return MockConfigEntry( |
| 21 | + domain=DOMAIN, |
| 22 | + title="Home", |
| 23 | + unique_id="123", |
| 24 | + data={"station_id": 123, "name": "Home"}, |
| 25 | + entry_id="86129426118ae32020417a53712d6eef", |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def mock_gios_sensors() -> GiosSensors: |
| 31 | + """Return the default mocked gios sensors.""" |
| 32 | + return GiosSensors( |
| 33 | + aqi=GiosSensor(name="AQI", id=None, index=None, value="good"), |
| 34 | + c6h6=GiosSensor(name="benzene", id=658, index="very_good", value=0.23789), |
| 35 | + co=GiosSensor(name="carbon monoxide", id=660, index="good", value=251.874), |
| 36 | + no=GiosSensor(name="nitrogen monoxide", id=664, index=None, value=5.1), |
| 37 | + no2=GiosSensor(name="nitrogen dioxide", id=665, index="good", value=7.13411), |
| 38 | + nox=GiosSensor(name="nitrogen oxides", id=666, index=None, value=5.5), |
| 39 | + o3=GiosSensor(name="ozone", id=667, index="good", value=95.7768), |
| 40 | + pm10=GiosSensor( |
| 41 | + name="particulate matter 10", id=14395, index="good", value=16.8344 |
| 42 | + ), |
| 43 | + pm25=GiosSensor(name="particulate matter 2.5", id=670, index="good", value=4), |
| 44 | + so2=GiosSensor(name="sulfur dioxide", id=672, index="very_good", value=4.35478), |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def mock_gios_stations() -> dict[int, GiosStation]: |
| 50 | + """Return the default mocked gios stations.""" |
| 51 | + return { |
| 52 | + 123: GiosStation(id=123, name="Test Name 1", latitude=99.99, longitude=88.88), |
| 53 | + 321: GiosStation(id=321, name="Test Name 2", latitude=77.77, longitude=66.66), |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +async def mock_gios( |
| 59 | + hass: HomeAssistant, |
| 60 | + mock_gios_stations: dict[int, GiosStation], |
| 61 | + mock_gios_sensors: GiosSensors, |
| 62 | +) -> AsyncGenerator[MagicMock]: |
| 63 | + """Yield a mocked GIOS client.""" |
| 64 | + with ( |
| 65 | + patch("homeassistant.components.gios.Gios", autospec=True) as mock_gios, |
| 66 | + patch("homeassistant.components.gios.config_flow.Gios", new=mock_gios), |
| 67 | + ): |
| 68 | + mock_gios.create = AsyncMock(return_value=mock_gios) |
| 69 | + mock_gios.async_update = AsyncMock(return_value=mock_gios_sensors) |
| 70 | + mock_gios.measurement_stations = mock_gios_stations |
| 71 | + mock_gios.station_id = 123 |
| 72 | + mock_gios.station_name = mock_gios_stations[mock_gios.station_id].name |
| 73 | + |
| 74 | + yield mock_gios |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +async def init_integration( |
| 79 | + hass: HomeAssistant, |
| 80 | + mock_config_entry: MockConfigEntry, |
| 81 | + mock_gios: MagicMock, |
| 82 | +) -> None: |
| 83 | + """Set up the GIOS integration for testing.""" |
| 84 | + await setup_integration(hass, mock_config_entry) |
0 commit comments