2018-10-11 08:55:22 +00:00
|
|
|
"""Volume conversion util functions."""
|
2021-07-18 12:43:47 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-22 15:49:45 +00:00
|
|
|
from homeassistant.const import ( # pylint: disable=unused-import # noqa: F401
|
2019-12-09 15:42:10 +00:00
|
|
|
UNIT_NOT_RECOGNIZED_TEMPLATE,
|
|
|
|
VOLUME,
|
2021-08-11 16:58:19 +00:00
|
|
|
VOLUME_CUBIC_FEET,
|
|
|
|
VOLUME_CUBIC_METERS,
|
2019-12-09 15:42:10 +00:00
|
|
|
VOLUME_FLUID_OUNCE,
|
|
|
|
VOLUME_GALLONS,
|
2019-07-31 19:25:30 +00:00
|
|
|
VOLUME_LITERS,
|
|
|
|
VOLUME_MILLILITERS,
|
|
|
|
)
|
2018-10-11 08:55:22 +00:00
|
|
|
|
2022-09-22 15:49:45 +00:00
|
|
|
from .unit_conversion import VolumeConverter
|
2022-09-07 14:18:00 +00:00
|
|
|
|
2022-09-22 15:49:45 +00:00
|
|
|
UNIT_CONVERSION = VolumeConverter.UNIT_CONVERSION
|
|
|
|
VALID_UNITS = VolumeConverter.VALID_UNITS
|
2022-09-22 06:50:08 +00:00
|
|
|
|
2018-10-11 08:55:22 +00:00
|
|
|
|
2021-08-11 16:58:19 +00:00
|
|
|
def liter_to_gallon(liter: float) -> float:
|
2018-10-11 08:55:22 +00:00
|
|
|
"""Convert a volume measurement in Liter to Gallon."""
|
2022-09-22 15:49:45 +00:00
|
|
|
# Need to add warning when core migration finished
|
2022-09-07 14:18:00 +00:00
|
|
|
return _convert(liter, VOLUME_LITERS, VOLUME_GALLONS)
|
2018-10-11 08:55:22 +00:00
|
|
|
|
|
|
|
|
2021-08-11 16:58:19 +00:00
|
|
|
def gallon_to_liter(gallon: float) -> float:
|
2018-10-11 08:55:22 +00:00
|
|
|
"""Convert a volume measurement in Gallon to Liter."""
|
2022-09-22 15:49:45 +00:00
|
|
|
# Need to add warning when core migration finished
|
2022-09-07 14:18:00 +00:00
|
|
|
return _convert(gallon, VOLUME_GALLONS, VOLUME_LITERS)
|
2018-10-11 08:55:22 +00:00
|
|
|
|
|
|
|
|
2021-08-11 16:58:19 +00:00
|
|
|
def cubic_meter_to_cubic_feet(cubic_meter: float) -> float:
|
|
|
|
"""Convert a volume measurement in cubic meter to cubic feet."""
|
2022-09-22 15:49:45 +00:00
|
|
|
# Need to add warning when core migration finished
|
2022-09-07 14:18:00 +00:00
|
|
|
return _convert(cubic_meter, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET)
|
2021-08-11 16:58:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def cubic_feet_to_cubic_meter(cubic_feet: float) -> float:
|
|
|
|
"""Convert a volume measurement in cubic feet to cubic meter."""
|
2022-09-22 15:49:45 +00:00
|
|
|
# Need to add warning when core migration finished
|
2022-09-07 14:18:00 +00:00
|
|
|
return _convert(cubic_feet, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS)
|
2021-08-11 16:58:19 +00:00
|
|
|
|
|
|
|
|
2021-07-20 12:13:51 +00:00
|
|
|
def convert(volume: float, from_unit: str, to_unit: str) -> float:
|
2022-09-21 10:37:31 +00:00
|
|
|
"""Convert a volume from one unit to another."""
|
2022-09-22 15:49:45 +00:00
|
|
|
# Need to add warning when core migration finished
|
|
|
|
return VolumeConverter.convert(volume, from_unit, to_unit)
|
2022-09-07 14:18:00 +00:00
|
|
|
|
2018-10-11 08:55:22 +00:00
|
|
|
|
2022-09-07 14:18:00 +00:00
|
|
|
def _convert(volume: float, from_unit: str, to_unit: str) -> float:
|
2022-09-21 10:37:31 +00:00
|
|
|
"""Convert a volume from one unit to another, bypassing checks."""
|
|
|
|
cubic_meter = volume / UNIT_CONVERSION[from_unit]
|
|
|
|
return cubic_meter * UNIT_CONVERSION[to_unit]
|