hass-core/tests/components/hydrawise/test_sensor.py
Thomas Kistler 14fcf7be8e
Add flow and rain sensor support to Hydrawise (#116303)
* Add flow and rain sensor support to Hydrawise

* Address comments

* Cleanup

* Review comments

* Address review comments

* Added tests

* Add icon translations

* Add snapshot tests

* Clean up binary sensor

* Mypy cleanup

* Another mypy error

* Reviewer feedback

* Clear next_cycle sensor when the value is unknown

* Reviewer feedback

* Reviewer feedback

* Remove assert

* Restructure switches, sensors, and binary sensors

* Reviewer feedback

* Reviewer feedback
2024-05-07 21:26:10 +02:00

65 lines
2.3 KiB
Python

"""Test Hydrawise sensor."""
from collections.abc import Awaitable, Callable
from unittest.mock import patch
from pydrawise.schema import Controller, Zone
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.mark.freeze_time("2023-10-01 00:00:00+00:00")
async def test_all_sensors(
hass: HomeAssistant,
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test that all sensors are working."""
with patch(
"homeassistant.components.hydrawise.PLATFORMS",
[Platform.SENSOR],
):
config_entry = await mock_add_config_entry()
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
@pytest.mark.freeze_time("2023-10-01 00:00:00+00:00")
async def test_suspended_state(
hass: HomeAssistant,
zones: list[Zone],
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
) -> None:
"""Test sensor states."""
zones[0].scheduled_runs.next_run = None
await mock_add_config_entry()
next_cycle = hass.states.get("sensor.zone_one_next_cycle")
assert next_cycle is not None
assert next_cycle.state == "unknown"
async def test_no_sensor_and_water_state2(
hass: HomeAssistant,
controller: Controller,
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
) -> None:
"""Test rain sensor, flow sensor, and water use in the absence of flow and rain sensors."""
controller.sensors = []
await mock_add_config_entry()
assert hass.states.get("sensor.zone_one_daily_active_water_use") is None
assert hass.states.get("sensor.zone_two_daily_active_water_use") is None
assert hass.states.get("sensor.home_controller_daily_active_water_use") is None
assert hass.states.get("sensor.home_controller_daily_inactive_water_use") is None
assert hass.states.get("binary_sensor.home_controller_rain_sensor") is None
sensor = hass.states.get("binary_sensor.home_controller_connectivity")
assert sensor is not None
assert sensor.state == "on"