Fix Hydrawise volume unit bug (#119988)
This commit is contained in:
parent
b3722d60cb
commit
4aa7a9faee
3 changed files with 51 additions and 7 deletions
|
@ -71,7 +71,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
||||||
key="daily_total_water_use",
|
key="daily_total_water_use",
|
||||||
translation_key="daily_total_water_use",
|
translation_key="daily_total_water_use",
|
||||||
device_class=SensorDeviceClass.VOLUME,
|
device_class=SensorDeviceClass.VOLUME,
|
||||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
value_fn=_get_controller_daily_total_water_use,
|
value_fn=_get_controller_daily_total_water_use,
|
||||||
),
|
),
|
||||||
|
@ -79,7 +78,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
||||||
key="daily_active_water_use",
|
key="daily_active_water_use",
|
||||||
translation_key="daily_active_water_use",
|
translation_key="daily_active_water_use",
|
||||||
device_class=SensorDeviceClass.VOLUME,
|
device_class=SensorDeviceClass.VOLUME,
|
||||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
value_fn=_get_controller_daily_active_water_use,
|
value_fn=_get_controller_daily_active_water_use,
|
||||||
),
|
),
|
||||||
|
@ -87,7 +85,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
||||||
key="daily_inactive_water_use",
|
key="daily_inactive_water_use",
|
||||||
translation_key="daily_inactive_water_use",
|
translation_key="daily_inactive_water_use",
|
||||||
device_class=SensorDeviceClass.VOLUME,
|
device_class=SensorDeviceClass.VOLUME,
|
||||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
value_fn=_get_controller_daily_inactive_water_use,
|
value_fn=_get_controller_daily_inactive_water_use,
|
||||||
),
|
),
|
||||||
|
@ -98,7 +95,6 @@ FLOW_ZONE_SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||||
key="daily_active_water_use",
|
key="daily_active_water_use",
|
||||||
translation_key="daily_active_water_use",
|
translation_key="daily_active_water_use",
|
||||||
device_class=SensorDeviceClass.VOLUME,
|
device_class=SensorDeviceClass.VOLUME,
|
||||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
value_fn=_get_zone_daily_active_water_use,
|
value_fn=_get_zone_daily_active_water_use,
|
||||||
),
|
),
|
||||||
|
@ -165,6 +161,17 @@ class HydrawiseSensor(HydrawiseEntity, SensorEntity):
|
||||||
|
|
||||||
entity_description: HydrawiseSensorEntityDescription
|
entity_description: HydrawiseSensorEntityDescription
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_unit_of_measurement(self) -> str | None:
|
||||||
|
"""Return the unit_of_measurement of the sensor."""
|
||||||
|
if self.entity_description.device_class != SensorDeviceClass.VOLUME:
|
||||||
|
return self.entity_description.native_unit_of_measurement
|
||||||
|
return (
|
||||||
|
UnitOfVolume.GALLONS
|
||||||
|
if self.coordinator.data.user.units.units_name == "imperial"
|
||||||
|
else UnitOfVolume.LITERS
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str | None:
|
def icon(self) -> str | None:
|
||||||
"""Icon of the entity based on the value."""
|
"""Icon of the entity based on the value."""
|
||||||
|
|
|
@ -15,6 +15,7 @@ from pydrawise.schema import (
|
||||||
Sensor,
|
Sensor,
|
||||||
SensorModel,
|
SensorModel,
|
||||||
SensorStatus,
|
SensorStatus,
|
||||||
|
UnitsSummary,
|
||||||
User,
|
User,
|
||||||
Zone,
|
Zone,
|
||||||
)
|
)
|
||||||
|
@ -85,7 +86,11 @@ def mock_auth() -> Generator[AsyncMock]:
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def user() -> User:
|
def user() -> User:
|
||||||
"""Hydrawise User fixture."""
|
"""Hydrawise User fixture."""
|
||||||
return User(customer_id=12345, email="asdf@asdf.com")
|
return User(
|
||||||
|
customer_id=12345,
|
||||||
|
email="asdf@asdf.com",
|
||||||
|
units=UnitsSummary(units_name="imperial"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
@ -3,13 +3,18 @@
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from pydrawise.schema import Controller, Zone
|
from pydrawise.schema import Controller, User, Zone
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy.assertion import SnapshotAssertion
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
from homeassistant.util.unit_system import (
|
||||||
|
METRIC_SYSTEM,
|
||||||
|
US_CUSTOMARY_SYSTEM,
|
||||||
|
UnitSystem,
|
||||||
|
)
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, snapshot_platform
|
from tests.common import MockConfigEntry, snapshot_platform
|
||||||
|
|
||||||
|
@ -45,7 +50,7 @@ async def test_suspended_state(
|
||||||
assert next_cycle.state == "unknown"
|
assert next_cycle.state == "unknown"
|
||||||
|
|
||||||
|
|
||||||
async def test_no_sensor_and_water_state2(
|
async def test_no_sensor_and_water_state(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
controller: Controller,
|
controller: Controller,
|
||||||
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
|
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
|
||||||
|
@ -63,3 +68,30 @@ async def test_no_sensor_and_water_state2(
|
||||||
sensor = hass.states.get("binary_sensor.home_controller_connectivity")
|
sensor = hass.states.get("binary_sensor.home_controller_connectivity")
|
||||||
assert sensor is not None
|
assert sensor is not None
|
||||||
assert sensor.state == "on"
|
assert sensor.state == "on"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("hydrawise_unit_system", "unit_system", "expected_state"),
|
||||||
|
[
|
||||||
|
("imperial", METRIC_SYSTEM, "454.6279552584"),
|
||||||
|
("imperial", US_CUSTOMARY_SYSTEM, "120.1"),
|
||||||
|
("metric", METRIC_SYSTEM, "120.1"),
|
||||||
|
("metric", US_CUSTOMARY_SYSTEM, "31.7270634882136"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_volume_unit_conversion(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
unit_system: UnitSystem,
|
||||||
|
hydrawise_unit_system: str,
|
||||||
|
expected_state: str,
|
||||||
|
user: User,
|
||||||
|
mock_add_config_entry: Callable[[], Awaitable[MockConfigEntry]],
|
||||||
|
) -> None:
|
||||||
|
"""Test volume unit conversion."""
|
||||||
|
hass.config.units = unit_system
|
||||||
|
user.units.units_name = hydrawise_unit_system
|
||||||
|
await mock_add_config_entry()
|
||||||
|
|
||||||
|
daily_active_water_use = hass.states.get("sensor.zone_one_daily_active_water_use")
|
||||||
|
assert daily_active_water_use is not None
|
||||||
|
assert daily_active_water_use.state == expected_state
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue