Add support for CCF volume unit (#84029)

pull/84242/head
epenet 2022-12-19 14:57:04 +01:00 committed by GitHub
parent 3351846b0e
commit f4c13645d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 103 deletions

View File

@ -48,11 +48,13 @@ VALID_ENERGY_UNITS: set[str] = {
}
VALID_ENERGY_UNITS_GAS = {
UnitOfVolume.CUBIC_FEET,
UnitOfVolume.CENTUM_CUBIC_FEET,
UnitOfVolume.CUBIC_METERS,
*VALID_ENERGY_UNITS,
}
VALID_VOLUME_UNITS_WATER: set[str] = {
UnitOfVolume.CUBIC_FEET,
UnitOfVolume.CENTUM_CUBIC_FEET,
UnitOfVolume.CUBIC_METERS,
UnitOfVolume.GALLONS,
UnitOfVolume.LITERS,

View File

@ -134,7 +134,9 @@ class NumberDeviceClass(StrEnum):
GAS = "gas"
"""Gas.
Unit of measurement: ``, `ft³`
Unit of measurement:
- SI / metric: ``
- USCS / imperial: `ft³`, `CCF`
"""
HUMIDITY = "humidity"
@ -307,7 +309,7 @@ class NumberDeviceClass(StrEnum):
Unit of measurement: `VOLUME_*` units
- SI / metric: `mL`, `L`, ``
- USCS / imperial: `fl. oz.`, `ft³`, `gal` (warning: volumes expressed in
- USCS / imperial: `f`, `CCF`, `fl. oz.`, `gal` (warning: volumes expressed in
USCS/imperial units are currently assumed to be US volumes)
"""
@ -316,7 +318,7 @@ class NumberDeviceClass(StrEnum):
Unit of measurement:
- SI / metric: ``, `L`
- USCS / imperial: `ft³`, `gal` (warning: volumes expressed in
- USCS / imperial: `ft³`, `CCF`, `gal` (warning: volumes expressed in
USCS/imperial units are currently assumed to be US volumes)
"""

View File

@ -198,7 +198,9 @@ class SensorDeviceClass(StrEnum):
GAS = "gas"
"""Gas.
Unit of measurement: ``, `ft³`
Unit of measurement:
- SI / metric: ``
- USCS / imperial: `ft³`, `CCF`
"""
HUMIDITY = "humidity"
@ -371,7 +373,7 @@ class SensorDeviceClass(StrEnum):
Unit of measurement: `VOLUME_*` units
- SI / metric: `mL`, `L`, ``
- USCS / imperial: `fl. oz.`, `ft³`, `gal` (warning: volumes expressed in
- USCS / imperial: `f`, `CCF`, `fl. oz.`, `gal` (warning: volumes expressed in
USCS/imperial units are currently assumed to be US volumes)
"""
@ -380,7 +382,7 @@ class SensorDeviceClass(StrEnum):
Unit of measurement:
- SI / metric: ``, `L`
- USCS / imperial: `ft³`, `gal` (warning: volumes expressed in
- USCS / imperial: `ft³`, `CCF`, `gal` (warning: volumes expressed in
USCS/imperial units are currently assumed to be US volumes)
"""

View File

@ -723,6 +723,7 @@ class UnitOfVolume(StrEnum):
"""Volume units."""
CUBIC_FEET = "ft³"
CENTUM_CUBIC_FEET = "CCF"
CUBIC_METERS = ""
LITERS = "L"
MILLILITERS = "mL"

View File

@ -339,6 +339,7 @@ class VolumeConverter(BaseUnitConverter):
UnitOfVolume.FLUID_OUNCES: 1 / _FLUID_OUNCE_TO_CUBIC_METER,
UnitOfVolume.CUBIC_METERS: 1,
UnitOfVolume.CUBIC_FEET: 1 / _CUBIC_FOOT_TO_CUBIC_METER,
UnitOfVolume.CENTUM_CUBIC_FEET: 1 / (100 * _CUBIC_FOOT_TO_CUBIC_METER),
}
VALID_UNITS = {
UnitOfVolume.LITERS,
@ -347,4 +348,5 @@ class VolumeConverter(BaseUnitConverter):
UnitOfVolume.FLUID_OUNCES,
UnitOfVolume.CUBIC_METERS,
UnitOfVolume.CUBIC_FEET,
UnitOfVolume.CENTUM_CUBIC_FEET,
}

View File

@ -4057,7 +4057,7 @@ async def test_validate_statistics_unit_change_equivalent_units(
@pytest.mark.parametrize(
"attributes, unit1, unit2, supported_unit",
[
(NONE_SENSOR_ATTRIBUTES, "", "m3", "L, fl. oz., ft³, gal, mL, m³"),
(NONE_SENSOR_ATTRIBUTES, "", "m3", "CCF, L, fl. oz., ft³, gal, mL, m³"),
],
)
async def test_validate_statistics_unit_change_equivalent_units_2(

View File

@ -564,110 +564,45 @@ def test_temperature_convert_with_interval(
@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[
(5, UnitOfVolume.LITERS, pytest.approx(1.32086), UnitOfVolume.GALLONS),
(5, UnitOfVolume.GALLONS, pytest.approx(18.92706), UnitOfVolume.LITERS),
(
5,
UnitOfVolume.CUBIC_METERS,
pytest.approx(176.5733335),
UnitOfVolume.CUBIC_FEET,
),
(
500,
UnitOfVolume.CUBIC_FEET,
pytest.approx(14.1584233),
UnitOfVolume.CUBIC_METERS,
),
(
500,
UnitOfVolume.CUBIC_FEET,
pytest.approx(14.1584233),
UnitOfVolume.CUBIC_METERS,
),
(
500,
UnitOfVolume.CUBIC_FEET,
pytest.approx(478753.2467),
UnitOfVolume.FLUID_OUNCES,
),
(500, UnitOfVolume.CUBIC_FEET, pytest.approx(3740.25974), UnitOfVolume.GALLONS),
(
500,
UnitOfVolume.CUBIC_FEET,
pytest.approx(14158.42329599),
UnitOfVolume.LITERS,
),
(
500,
UnitOfVolume.CUBIC_FEET,
pytest.approx(14158423.29599),
UnitOfVolume.MILLILITERS,
),
(5, UnitOfVolume.LITERS, 1.32086, UnitOfVolume.GALLONS),
(5, UnitOfVolume.GALLONS, 18.92706, UnitOfVolume.LITERS),
(5, UnitOfVolume.CUBIC_METERS, 176.5733335, UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.CUBIC_FEET, 14.1584233, UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.CUBIC_FEET, 14.1584233, UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.CUBIC_FEET, 478753.2467, UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.CUBIC_FEET, 3740.25974, UnitOfVolume.GALLONS),
(500, UnitOfVolume.CUBIC_FEET, 14158.42329599, UnitOfVolume.LITERS),
(500, UnitOfVolume.CUBIC_FEET, 14158423.29599, UnitOfVolume.MILLILITERS),
(500, UnitOfVolume.CUBIC_METERS, 500, UnitOfVolume.CUBIC_METERS),
(
500,
UnitOfVolume.CUBIC_METERS,
pytest.approx(16907011.35),
UnitOfVolume.FLUID_OUNCES,
),
(
500,
UnitOfVolume.CUBIC_METERS,
pytest.approx(132086.02617),
UnitOfVolume.GALLONS,
),
(500, UnitOfVolume.CUBIC_METERS, 16907011.35, UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.CUBIC_METERS, 132086.02617, UnitOfVolume.GALLONS),
(500, UnitOfVolume.CUBIC_METERS, 500000, UnitOfVolume.LITERS),
(500, UnitOfVolume.CUBIC_METERS, 500000000, UnitOfVolume.MILLILITERS),
(
500,
UnitOfVolume.FLUID_OUNCES,
pytest.approx(0.52218967),
UnitOfVolume.CUBIC_FEET,
),
(
500,
UnitOfVolume.FLUID_OUNCES,
pytest.approx(0.014786764),
UnitOfVolume.CUBIC_METERS,
),
(500, UnitOfVolume.FLUID_OUNCES, 0.52218967, UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.FLUID_OUNCES, 0.014786764, UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.FLUID_OUNCES, 3.90625, UnitOfVolume.GALLONS),
(500, UnitOfVolume.FLUID_OUNCES, pytest.approx(14.786764), UnitOfVolume.LITERS),
(
500,
UnitOfVolume.FLUID_OUNCES,
pytest.approx(14786.764),
UnitOfVolume.MILLILITERS,
),
(500, UnitOfVolume.GALLONS, pytest.approx(66.84027), UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.GALLONS, pytest.approx(1.892706), UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.FLUID_OUNCES, 14.786764, UnitOfVolume.LITERS),
(500, UnitOfVolume.FLUID_OUNCES, 14786.764, UnitOfVolume.MILLILITERS),
(500, UnitOfVolume.GALLONS, 66.84027, UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.GALLONS, 1.892706, UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.GALLONS, 64000, UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.GALLONS, pytest.approx(1892.70589), UnitOfVolume.LITERS),
(
500,
UnitOfVolume.GALLONS,
pytest.approx(1892705.89),
UnitOfVolume.MILLILITERS,
),
(500, UnitOfVolume.LITERS, pytest.approx(17.65733), UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.GALLONS, 1892.70589, UnitOfVolume.LITERS),
(500, UnitOfVolume.GALLONS, 1892705.89, UnitOfVolume.MILLILITERS),
(500, UnitOfVolume.LITERS, 17.65733, UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.LITERS, 0.5, UnitOfVolume.CUBIC_METERS),
(500, UnitOfVolume.LITERS, pytest.approx(16907.011), UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.LITERS, pytest.approx(132.086), UnitOfVolume.GALLONS),
(500, UnitOfVolume.LITERS, 16907.011, UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.LITERS, 132.086, UnitOfVolume.GALLONS),
(500, UnitOfVolume.LITERS, 500000, UnitOfVolume.MILLILITERS),
(
500,
UnitOfVolume.MILLILITERS,
pytest.approx(0.01765733),
UnitOfVolume.CUBIC_FEET,
),
(500, UnitOfVolume.MILLILITERS, 0.01765733, UnitOfVolume.CUBIC_FEET),
(500, UnitOfVolume.MILLILITERS, 0.0005, UnitOfVolume.CUBIC_METERS),
(
500,
UnitOfVolume.MILLILITERS,
pytest.approx(16.907),
UnitOfVolume.FLUID_OUNCES,
),
(500, UnitOfVolume.MILLILITERS, pytest.approx(0.132086), UnitOfVolume.GALLONS),
(500, UnitOfVolume.MILLILITERS, 16.907, UnitOfVolume.FLUID_OUNCES),
(500, UnitOfVolume.MILLILITERS, 0.132086, UnitOfVolume.GALLONS),
(500, UnitOfVolume.MILLILITERS, 0.5, UnitOfVolume.LITERS),
(5, UnitOfVolume.CENTUM_CUBIC_FEET, 500, UnitOfVolume.CUBIC_FEET),
(5, UnitOfVolume.CENTUM_CUBIC_FEET, 14.15842, UnitOfVolume.CUBIC_METERS),
(5, UnitOfVolume.CENTUM_CUBIC_FEET, 478753.24, UnitOfVolume.FLUID_OUNCES),
(5, UnitOfVolume.CENTUM_CUBIC_FEET, 3740.26, UnitOfVolume.GALLONS),
(5, UnitOfVolume.CENTUM_CUBIC_FEET, 14158.42, UnitOfVolume.LITERS),
],
)
def test_volume_convert(
@ -677,4 +612,4 @@ def test_volume_convert(
to_unit: str,
) -> None:
"""Test conversion to other units."""
assert VolumeConverter.convert(value, from_unit, to_unit) == expected
assert VolumeConverter.convert(value, from_unit, to_unit) == pytest.approx(expected)