Fix Hydrawise volume unit bug (#119988)
This commit is contained in:
parent
96adf98625
commit
5edf480a15
3 changed files with 51 additions and 7 deletions
|
@ -71,7 +71,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
|||
key="daily_total_water_use",
|
||||
translation_key="daily_total_water_use",
|
||||
device_class=SensorDeviceClass.VOLUME,
|
||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
||||
suggested_display_precision=1,
|
||||
value_fn=_get_controller_daily_total_water_use,
|
||||
),
|
||||
|
@ -79,7 +78,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
|||
key="daily_active_water_use",
|
||||
translation_key="daily_active_water_use",
|
||||
device_class=SensorDeviceClass.VOLUME,
|
||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
||||
suggested_display_precision=1,
|
||||
value_fn=_get_controller_daily_active_water_use,
|
||||
),
|
||||
|
@ -87,7 +85,6 @@ FLOW_CONTROLLER_SENSORS: tuple[HydrawiseSensorEntityDescription, ...] = (
|
|||
key="daily_inactive_water_use",
|
||||
translation_key="daily_inactive_water_use",
|
||||
device_class=SensorDeviceClass.VOLUME,
|
||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
||||
suggested_display_precision=1,
|
||||
value_fn=_get_controller_daily_inactive_water_use,
|
||||
),
|
||||
|
@ -98,7 +95,6 @@ FLOW_ZONE_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|||
key="daily_active_water_use",
|
||||
translation_key="daily_active_water_use",
|
||||
device_class=SensorDeviceClass.VOLUME,
|
||||
native_unit_of_measurement=UnitOfVolume.GALLONS,
|
||||
suggested_display_precision=1,
|
||||
value_fn=_get_zone_daily_active_water_use,
|
||||
),
|
||||
|
@ -165,6 +161,17 @@ class HydrawiseSensor(HydrawiseEntity, SensorEntity):
|
|||
|
||||
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
|
||||
def icon(self) -> str | None:
|
||||
"""Icon of the entity based on the value."""
|
||||
|
|
|
@ -15,6 +15,7 @@ from pydrawise.schema import (
|
|||
Sensor,
|
||||
SensorModel,
|
||||
SensorStatus,
|
||||
UnitsSummary,
|
||||
User,
|
||||
Zone,
|
||||
)
|
||||
|
@ -84,7 +85,11 @@ def mock_auth() -> Generator[AsyncMock, None, None]:
|
|||
@pytest.fixture
|
||||
def user() -> User:
|
||||
"""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
|
||||
|
|
|
@ -3,13 +3,18 @@
|
|||
from collections.abc import Awaitable, Callable
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydrawise.schema import Controller, Zone
|
||||
from pydrawise.schema import Controller, User, 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 homeassistant.util.unit_system import (
|
||||
METRIC_SYSTEM,
|
||||
US_CUSTOMARY_SYSTEM,
|
||||
UnitSystem,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
@ -45,7 +50,7 @@ async def test_suspended_state(
|
|||
assert next_cycle.state == "unknown"
|
||||
|
||||
|
||||
async def test_no_sensor_and_water_state2(
|
||||
async def test_no_sensor_and_water_state(
|
||||
hass: HomeAssistant,
|
||||
controller: Controller,
|
||||
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")
|
||||
assert sensor is not None
|
||||
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
Reference in a new issue