Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hydrawise volume unit bug #119988

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 7 additions & 3 deletions tests/components/hydrawise/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Common fixtures for the Hydrawise tests."""

from collections.abc import Awaitable, Callable
from collections.abc import Awaitable, Callable, Generator
epenet marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime, timedelta
from unittest.mock import AsyncMock, patch

Expand All @@ -15,11 +15,11 @@
Sensor,
SensorModel,
SensorStatus,
UnitsSummary,
User,
Zone,
)
import pytest
from typing_extensions import Generator
epenet marked this conversation as resolved.
Show resolved Hide resolved

from homeassistant.components.hydrawise.const import DOMAIN
from homeassistant.const import CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME
Expand Down Expand Up @@ -85,7 +85,11 @@ def mock_auth() -> Generator[AsyncMock]:
@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
32 changes: 30 additions & 2 deletions tests/components/hydrawise/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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 IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem

from tests.common import MockConfigEntry, snapshot_platform

Expand Down Expand Up @@ -45,7 +46,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 +64,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", IMPERIAL_SYSTEM, "120.1"),
("metric", METRIC_SYSTEM, "120.1"),
("metric", IMPERIAL_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