From babc183834159d99d254ad09f43d74d4ce9ba9c1 Mon Sep 17 00:00:00 2001 From: Arnie97 Date: Tue, 6 May 2025 16:59:09 +0800 Subject: [PATCH] Allow liter for gas sensor device class (#141518) --- homeassistant/components/energy/sensor.py | 1 + homeassistant/components/energy/validate.py | 1 + homeassistant/components/number/const.py | 3 ++- homeassistant/components/sensor/const.py | 3 ++- homeassistant/util/unit_system.py | 1 + tests/components/energy/test_sensor.py | 2 +- tests/components/energy/test_validate.py | 4 ++-- tests/util/test_unit_system.py | 7 ++++++- 8 files changed, 16 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/energy/sensor.py b/homeassistant/components/energy/sensor.py index 062601eb4c5..3dc857d75d9 100644 --- a/homeassistant/components/energy/sensor.py +++ b/homeassistant/components/energy/sensor.py @@ -52,6 +52,7 @@ VALID_ENERGY_UNITS_GAS = { UnitOfVolume.CENTUM_CUBIC_FEET, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS, + UnitOfVolume.LITERS, *VALID_ENERGY_UNITS, } VALID_VOLUME_UNITS_WATER: set[str] = { diff --git a/homeassistant/components/energy/validate.py b/homeassistant/components/energy/validate.py index cfacbe48b97..0f46678994f 100644 --- a/homeassistant/components/energy/validate.py +++ b/homeassistant/components/energy/validate.py @@ -50,6 +50,7 @@ GAS_USAGE_UNITS: dict[str, tuple[UnitOfEnergy | UnitOfVolume, ...]] = { UnitOfVolume.CENTUM_CUBIC_FEET, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS, + UnitOfVolume.LITERS, ), } GAS_PRICE_UNITS = tuple( diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 280edb819d4..58fa8ed1012 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -196,7 +196,7 @@ class NumberDeviceClass(StrEnum): """Gas. Unit of measurement: - - SI / metric: `m³` + - SI / metric: `L`, `m³` - USCS / imperial: `ft³`, `CCF` """ @@ -472,6 +472,7 @@ DEVICE_CLASS_UNITS: dict[NumberDeviceClass, set[type[StrEnum] | str | None]] = { UnitOfVolume.CENTUM_CUBIC_FEET, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS, + UnitOfVolume.LITERS, }, NumberDeviceClass.HUMIDITY: {PERCENTAGE}, NumberDeviceClass.ILLUMINANCE: {LIGHT_LUX}, diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index c845980e9df..2a8ac8099ab 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -225,7 +225,7 @@ class SensorDeviceClass(StrEnum): """Gas. Unit of measurement: - - SI / metric: `m³` + - SI / metric: `L`, `m³` - USCS / imperial: `ft³`, `CCF` """ @@ -571,6 +571,7 @@ DEVICE_CLASS_UNITS: dict[SensorDeviceClass, set[type[StrEnum] | str | None]] = { UnitOfVolume.CENTUM_CUBIC_FEET, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS, + UnitOfVolume.LITERS, }, SensorDeviceClass.HUMIDITY: {PERCENTAGE}, SensorDeviceClass.ILLUMINANCE: {LIGHT_LUX}, diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 055f435503f..31f74377a16 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -355,6 +355,7 @@ US_CUSTOMARY_SYSTEM = UnitSystem( ("distance", UnitOfLength.MILLIMETERS): UnitOfLength.INCHES, # Convert non-USCS volumes of gas meters ("gas", UnitOfVolume.CUBIC_METERS): UnitOfVolume.CUBIC_FEET, + ("gas", UnitOfVolume.LITERS): UnitOfVolume.CUBIC_FEET, # Convert non-USCS precipitation ("precipitation", UnitOfLength.CENTIMETERS): UnitOfLength.INCHES, ("precipitation", UnitOfLength.MILLIMETERS): UnitOfLength.INCHES, diff --git a/tests/components/energy/test_sensor.py b/tests/components/energy/test_sensor.py index a438842f8a5..a9a249a8498 100644 --- a/tests/components/energy/test_sensor.py +++ b/tests/components/energy/test_sensor.py @@ -994,7 +994,7 @@ async def test_cost_sensor_handle_late_price_sensor( @pytest.mark.parametrize( "unit", - [UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS], + [UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS, UnitOfVolume.LITERS], ) async def test_cost_sensor_handle_gas( setup_integration, hass: HomeAssistant, hass_storage: dict[str, Any], unit diff --git a/tests/components/energy/test_validate.py b/tests/components/energy/test_validate.py index d7f0485139f..6389ac0b372 100644 --- a/tests/components/energy/test_validate.py +++ b/tests/components/energy/test_validate.py @@ -856,7 +856,7 @@ async def test_validation_gas( "affected_entities": {("sensor.gas_consumption_1", "beers")}, "translation_placeholders": { "energy_units": "GJ, kWh, MJ, MWh, Wh", - "gas_units": "CCF, ft³, m³", + "gas_units": "CCF, ft³, m³, L", }, }, { @@ -885,7 +885,7 @@ async def test_validation_gas( "affected_entities": {("sensor.gas_price_2", "EUR/invalid")}, "translation_placeholders": { "price_units": ( - "EUR/GJ, EUR/kWh, EUR/MJ, EUR/MWh, EUR/Wh, EUR/CCF, EUR/ft³, EUR/m³" + "EUR/GJ, EUR/kWh, EUR/MJ, EUR/MWh, EUR/Wh, EUR/CCF, EUR/ft³, EUR/m³, EUR/L" ) }, }, diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index ddefe92de42..87a9729700e 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -434,6 +434,7 @@ def test_get_unit_system_invalid(key: str) -> None: UnitOfVolume.CUBIC_METERS, ), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, UnitOfVolume.CUBIC_METERS), + (SensorDeviceClass.GAS, UnitOfVolume.LITERS, None), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, None), (SensorDeviceClass.GAS, "very_much", None), # Test precipitation conversion @@ -573,7 +574,10 @@ UNCONVERTED_UNITS_METRIC_SYSTEM = { UnitOfLength.METERS, UnitOfLength.MILLIMETERS, ), - SensorDeviceClass.GAS: (UnitOfVolume.CUBIC_METERS,), + SensorDeviceClass.GAS: ( + UnitOfVolume.CUBIC_METERS, + UnitOfVolume.LITERS, + ), SensorDeviceClass.PRECIPITATION: ( UnitOfLength.CENTIMETERS, UnitOfLength.MILLIMETERS, @@ -687,6 +691,7 @@ def test_metric_converted_units(device_class: SensorDeviceClass) -> None: # Test gas meter conversion (SensorDeviceClass.GAS, UnitOfVolume.CENTUM_CUBIC_FEET, None), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_METERS, UnitOfVolume.CUBIC_FEET), + (SensorDeviceClass.GAS, UnitOfVolume.LITERS, UnitOfVolume.CUBIC_FEET), (SensorDeviceClass.GAS, UnitOfVolume.CUBIC_FEET, None), (SensorDeviceClass.GAS, "very_much", None), # Test precipitation conversion