Skip to content

Commit

Permalink
Fix Hydrawise volume unit bug (#119988)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskistler authored and frenck committed Jun 21, 2024
1 parent 96adf98 commit 5edf480
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
15 changes: 11 additions & 4 deletions homeassistant/components/hydrawise/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,20 @@ def _get_controller_daily_total_water_use(sensor: HydrawiseSensor) -> float | No
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,
),
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,
),
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,
),
Expand All @@ -98,7 +95,6 @@ def _get_controller_daily_total_water_use(sensor: HydrawiseSensor) -> float | No
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,
),
Expand Down Expand Up @@ -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."""
Expand Down
7 changes: 6 additions & 1 deletion tests/components/hydrawise/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Sensor,
SensorModel,
SensorStatus,
UnitsSummary,
User,
Zone,
)
Expand Down Expand Up @@ -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="[email protected]")
return User(
customer_id=12345,
email="[email protected]",
units=UnitsSummary(units_name="imperial"),
)


@pytest.fixture
Expand Down
36 changes: 34 additions & 2 deletions tests/components/hydrawise/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]],
Expand All @@ -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

0 comments on commit 5edf480

Please sign in to comment.