2021-09-09 07:45:58 +00:00
|
|
|
"""Test the Flipr sensor."""
|
2024-03-08 13:50:04 +00:00
|
|
|
|
2024-09-11 21:34:29 +00:00
|
|
|
from unittest.mock import AsyncMock
|
2021-07-21 19:35:44 +00:00
|
|
|
|
2022-03-02 00:21:47 +00:00
|
|
|
from flipr_api.exceptions import FliprError
|
|
|
|
|
2022-06-20 18:29:50 +00:00
|
|
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
|
2024-09-11 21:34:29 +00:00
|
|
|
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, UnitOfTemperature
|
2021-07-21 19:35:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-03-01 08:11:14 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2024-09-11 21:34:29 +00:00
|
|
|
|
|
|
|
from . import setup_integration
|
2021-07-21 19:35:44 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
2024-09-11 21:34:29 +00:00
|
|
|
async def test_sensors(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
mock_flipr_client: AsyncMock,
|
|
|
|
) -> None:
|
|
|
|
"""Test the creation and values of the Flipr binary sensors."""
|
2021-07-21 19:35:44 +00:00
|
|
|
|
2024-09-11 21:34:29 +00:00
|
|
|
await setup_integration(hass, mock_config_entry)
|
2021-07-21 19:35:44 +00:00
|
|
|
|
2021-09-07 07:52:42 +00:00
|
|
|
# Check entity unique_id value that is generated in FliprEntity base class.
|
2023-03-01 08:11:14 +00:00
|
|
|
entity = entity_registry.async_get("sensor.flipr_myfliprid_red_ox")
|
2021-09-07 07:52:42 +00:00
|
|
|
assert entity.unique_id == "myfliprid-red_ox"
|
|
|
|
|
2021-07-21 19:35:44 +00:00
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_ph")
|
|
|
|
assert state
|
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
2022-06-20 18:29:50 +00:00
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state.state == "7.03"
|
|
|
|
|
2023-06-27 11:20:30 +00:00
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_water_temperature")
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state
|
2023-01-15 13:46:45 +00:00
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS
|
2022-06-20 18:29:50 +00:00
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state.state == "10.5"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_last_measured")
|
|
|
|
assert state
|
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
|
2022-06-20 18:29:50 +00:00
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is None
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state.state == "2021-02-15T09:10:32+00:00"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_red_ox")
|
|
|
|
assert state
|
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "mV"
|
2022-06-20 18:29:50 +00:00
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state.state == "657.58"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_chlorine")
|
|
|
|
assert state
|
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "mV"
|
2022-06-20 18:29:50 +00:00
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
2021-07-21 19:35:44 +00:00
|
|
|
assert state.state == "0.23654886"
|
2022-03-02 00:21:47 +00:00
|
|
|
|
2023-06-27 11:20:30 +00:00
|
|
|
state = hass.states.get("sensor.flipr_myfliprid_battery")
|
2022-11-16 08:42:31 +00:00
|
|
|
assert state
|
|
|
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
|
|
|
|
assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT
|
|
|
|
assert state.state == "95.0"
|
|
|
|
|
2022-03-02 00:21:47 +00:00
|
|
|
|
2023-03-01 08:11:14 +00:00
|
|
|
async def test_error_flipr_api_sensors(
|
2024-09-11 21:34:29 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
mock_flipr_client: AsyncMock,
|
2023-03-01 08:11:14 +00:00
|
|
|
) -> None:
|
2022-03-02 00:21:47 +00:00
|
|
|
"""Test the Flipr sensors error."""
|
|
|
|
|
2024-09-11 21:34:29 +00:00
|
|
|
mock_flipr_client.get_pool_measure_latest.side_effect = FliprError(
|
|
|
|
"Error during flipr data retrieval..."
|
|
|
|
)
|
2022-03-02 00:21:47 +00:00
|
|
|
|
2024-09-11 21:34:29 +00:00
|
|
|
await setup_integration(hass, mock_config_entry)
|
2022-03-02 00:21:47 +00:00
|
|
|
|
|
|
|
# Check entity is not generated because of the FliprError raised.
|
2023-03-01 08:11:14 +00:00
|
|
|
entity = entity_registry.async_get("sensor.flipr_myfliprid_red_ox")
|
2022-03-02 00:21:47 +00:00
|
|
|
assert entity is None
|