Files
core/tests/components/vistapool/test_sensor.py
T
2026-08-30 17:10:37 +02:00

165 lines
5.4 KiB
Python

"""Tests for the Vistapool sensor platform."""
from __future__ import annotations
from collections.abc import Generator
from typing import Any
from unittest.mock import AsyncMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
@pytest.fixture
def _only_sensor_platform() -> Generator[None]:
"""Restrict integration setup to the sensor platform for the snapshot."""
with patch("homeassistant.components.vistapool.PLATFORMS", [Platform.SENSOR]):
yield
@pytest.mark.usefixtures("_only_sensor_platform", "entity_registry_enabled_by_default")
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
mock_vistapool_client: AsyncMock,
) -> None:
"""Test sensor entities when every module-gated sensor is present."""
mock_vistapool_client.fetch_pool_data.return_value = {
"main": {
"hasCD": 1,
"hasCL": 1,
"hasPH": 1,
"hasRX": 1,
"hasUV": 1,
"hasHidro": 1,
"RSSI": -65,
"temperature": 25.5,
"version": 1,
},
"hidro": {"is_electrolysis": True, "current": 50},
"modules": {
"ph": {"current": "742"},
"rx": {"current": 707},
"cl": {"current": "120"},
"cd": {"current": "150"},
"uv": {"current": "100"},
},
}
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_sensors_default_modules(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_vistapool_client: AsyncMock,
mock_pool_data: dict[str, Any],
) -> None:
"""Test sensors come up with the expected states for the default fixture."""
mock_vistapool_client.fetch_pool_data.return_value = mock_pool_data
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
# Always-on sensors.
assert hass.states.get("sensor.my_pool_temperature").state == "25.5"
assert hass.states.get("sensor.my_pool_filtration_intel_time") is not None
# Module-gated; the fixture sets hasPH=1, hasRX=1, hasHidro=1 with
# is_electrolysis=True.
assert hass.states.get("sensor.my_pool_ph").state == "7.42"
assert hass.states.get("sensor.my_pool_redox_potential").state == "707"
assert hass.states.get("sensor.my_pool_electrolysis").state == "5.0"
# Module-gated and disabled in the fixture (hasCD=0, hasCL=0, hasUV=0).
assert hass.states.get("sensor.my_pool_conductivity") is None
assert hass.states.get("sensor.my_pool_chlorine") is None
assert hass.states.get("sensor.my_pool_uv") is None
async def test_sensors_hydrolysis_branch(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_vistapool_client: AsyncMock,
) -> None:
"""Test the hydrolysis (non-electrolysis) branch creates the right sensor."""
mock_vistapool_client.fetch_pool_data.return_value = {
"main": {"hasHidro": 1, "version": 1},
"hidro": {"is_electrolysis": False, "current": 50},
}
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("sensor.my_pool_hydrolysis") is not None
assert hass.states.get("sensor.my_pool_electrolysis") is None
async def test_sensors_all_modules_enabled(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_vistapool_client: AsyncMock,
) -> None:
"""Test every module-gated sensor comes up when all `has*` flags are set."""
mock_vistapool_client.fetch_pool_data.return_value = {
"main": {
"hasCD": 1,
"hasCL": 1,
"hasPH": 1,
"hasRX": 1,
"hasUV": 1,
"hasHidro": 1,
"version": 1,
},
"hidro": {"is_electrolysis": True, "current": 50},
}
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
for name in (
"conductivity",
"chlorine",
"ph",
"redox_potential",
"uv",
"electrolysis",
):
assert hass.states.get(f"sensor.my_pool_{name}") is not None
async def test_sensors_multi_pool(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_vistapool_client: AsyncMock,
mock_pool_data: dict[str, Any],
) -> None:
"""Test setup creates sensors for every pool on the account."""
mock_vistapool_client.get_pools.return_value = {
"pool_a": "Pool A",
"pool_b": "Pool B",
}
mock_vistapool_client.fetch_pool_data.return_value = mock_pool_data
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("sensor.pool_a_temperature").state == "25.5"
assert hass.states.get("sensor.pool_b_temperature").state == "25.5"