2022-12-22 21:00:17 +00:00
|
|
|
"""Tests for rainbird sensor platform."""
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-11-07 22:24:34 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2022-12-22 21:00:17 +00:00
|
|
|
from homeassistant.const import Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-10-01 15:12:44 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2022-12-22 21:00:17 +00:00
|
|
|
|
2023-11-07 22:24:34 +00:00
|
|
|
from .conftest import CONFIG_ENTRY_DATA, RAIN_DELAY, RAIN_DELAY_OFF
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2022-12-22 21:00:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def platforms() -> list[str]:
|
|
|
|
"""Fixture to specify platforms to test."""
|
|
|
|
return [Platform.SENSOR]
|
|
|
|
|
|
|
|
|
2023-11-07 22:24:34 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
async def setup_config_entry(
|
|
|
|
hass: HomeAssistant, config_entry: MockConfigEntry
|
|
|
|
) -> list[Platform]:
|
|
|
|
"""Fixture to setup the config entry."""
|
|
|
|
await config_entry.async_setup(hass)
|
|
|
|
assert config_entry.state == ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
|
2022-12-22 21:00:17 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("rain_delay_response", "expected_state"),
|
2023-01-07 17:34:01 +00:00
|
|
|
[(RAIN_DELAY, "16"), (RAIN_DELAY_OFF, "0")],
|
2022-12-22 21:00:17 +00:00
|
|
|
)
|
|
|
|
async def test_sensors(
|
|
|
|
hass: HomeAssistant,
|
2023-10-01 15:12:44 +00:00
|
|
|
entity_registry: er.EntityRegistry,
|
2023-01-07 17:34:01 +00:00
|
|
|
expected_state: str,
|
2022-12-22 21:00:17 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test sensor platform."""
|
|
|
|
|
2023-07-06 23:03:01 +00:00
|
|
|
raindelay = hass.states.get("sensor.rain_bird_controller_raindelay")
|
2022-12-22 21:00:17 +00:00
|
|
|
assert raindelay is not None
|
2023-01-07 17:34:01 +00:00
|
|
|
assert raindelay.state == expected_state
|
|
|
|
assert raindelay.attributes == {
|
2023-07-06 23:03:01 +00:00
|
|
|
"friendly_name": "Rain Bird Controller Raindelay",
|
2023-01-07 17:34:01 +00:00
|
|
|
"icon": "mdi:water-off",
|
|
|
|
}
|
2023-10-01 15:12:44 +00:00
|
|
|
|
|
|
|
entity_entry = entity_registry.async_get("sensor.rain_bird_controller_raindelay")
|
|
|
|
assert entity_entry
|
|
|
|
assert entity_entry.unique_id == "1263613994342-raindelay"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("config_entry_unique_id", "config_entry_data"),
|
|
|
|
[
|
|
|
|
# Config entry setup without a unique id since it had no serial number
|
|
|
|
(
|
|
|
|
None,
|
|
|
|
{
|
|
|
|
**CONFIG_ENTRY_DATA,
|
|
|
|
"serial_number": 0,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
# Legacy case for old config entries with serial number 0 preserves old behavior
|
|
|
|
(
|
|
|
|
"0",
|
|
|
|
{
|
|
|
|
**CONFIG_ENTRY_DATA,
|
|
|
|
"serial_number": 0,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_sensor_no_unique_id(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
config_entry_unique_id: str | None,
|
|
|
|
) -> None:
|
|
|
|
"""Test sensor platform with no unique id."""
|
|
|
|
|
|
|
|
raindelay = hass.states.get("sensor.rain_bird_controller_raindelay")
|
|
|
|
assert raindelay is not None
|
|
|
|
assert raindelay.attributes.get("friendly_name") == "Rain Bird Controller Raindelay"
|
|
|
|
|
|
|
|
entity_entry = entity_registry.async_get("sensor.rain_bird_controller_raindelay")
|
|
|
|
assert (entity_entry is None) == (config_entry_unique_id is None)
|