| 1 |
"""YoLink Unit convert helper.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
from collections.abc import Callable |
| 5 |
from functools import lru_cache |
| 6 |
from enum import IntEnum |
| 7 |
|
| 8 |
from .exception import YoLinkError |
| 9 |
from .const import UNIT_NOT_RECOGNIZED_TEMPLATE |
| 10 |
|
| 11 |
|
| 12 |
class UnitOfVolume(IntEnum): |
| 13 |
"""Unit of meter.""" |
| 14 |
|
| 15 |
GALLONS = 0 |
| 16 |
CENTUM_CUBIC_FEET = 1 |
| 17 |
CUBIC_METERS = 2 |
| 18 |
LITERS = 3 |
| 19 |
|
| 20 |
|
| 21 |
_IN_TO_M = 0.0254 |
| 22 |
_FOOT_TO_M = _IN_TO_M * 12 |
| 23 |
_L_TO_CUBIC_METER = 0.001 ³ |
| 24 |
_GALLON_TO_CUBIC_METER = 231 * pow(_IN_TO_M, 3) |
| 25 |
_CUBIC_FOOT_TO_CUBIC_METER = pow(_FOOT_TO_M, 3) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class BaseUnitConverter: |
| 30 |
"""Define the format of a conversion utility.""" |
| 31 |
|
| 32 |
UNIT_CLASS: str |
| 33 |
NORMALIZED_UNIT: str | None |
| 34 |
VALID_UNITS: set[str | None] |
| 35 |
|
| 36 |
_UNIT_CONVERSION: dict[str | None, float] |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def convert(cls, value: float, from_unit: str | None, to_unit: str | None) -> float: |
| 40 |
"""Convert one unit of measurement to another.""" |
| 41 |
return cls.converter_factory(from_unit, to_unit)(value) |
| 42 |
|
| 43 |
@classmethod |
| 44 |
@lru_cache |
| 45 |
def converter_factory( |
| 46 |
cls, from_unit: str | None, to_unit: str | None |
| 47 |
) -> Callable[[float], float]: |
| 48 |
"""Return a function to convert one unit of measurement to another.""" |
| 49 |
if from_unit == to_unit: |
| 50 |
return lambda value: value |
| 51 |
from_ratio, to_ratio = cls._get_from_to_ratio(from_unit, to_unit) |
| 52 |
return lambda val: (val / from_ratio) * to_ratio |
| 53 |
|
| 54 |
@classmethod |
| 55 |
def _get_from_to_ratio( |
| 56 |
cls, from_unit: str | None, to_unit: str | None |
| 57 |
) -> tuple[float, float]: |
| 58 |
"""Get unit ratio between units of measurement.""" |
| 59 |
unit_conversion = cls._UNIT_CONVERSION |
| 60 |
try: |
| 61 |
return unit_conversion[from_unit], unit_conversion[to_unit] |
| 62 |
except KeyError as err: |
| 63 |
raise YoLinkError( |
| 64 |
UNIT_NOT_RECOGNIZED_TEMPLATE.format(err.args[0], cls.UNIT_CLASS) |
| 65 |
) from err |
| 66 |
|
| 67 |
@classmethod |
| 68 |
@lru_cache |
| 69 |
def converter_factory_allow_none( |
| 70 |
cls, from_unit: str | None, to_unit: str | None |
| 71 |
) -> Callable[[float | None], float | None]: |
| 72 |
"""Return a function to convert one unit of measurement to another which allows None.""" |
| 73 |
if from_unit == to_unit: |
| 74 |
return lambda value: value |
| 75 |
from_ratio, to_ratio = cls._get_from_to_ratio(from_unit, to_unit) |
| 76 |
return lambda val: None if val is None else (val / from_ratio) * to_ratio |
| 77 |
|
| 78 |
@classmethod |
| 79 |
@lru_cache |
| 80 |
def get_unit_ratio(cls, from_unit: str | None, to_unit: str | None) -> float: |
| 81 |
"""Get unit ratio between units of measurement.""" |
| 82 |
from_ratio, to_ratio = cls._get_from_to_ratio(from_unit, to_unit) |
| 83 |
return from_ratio / to_ratio |
| 84 |
|
| 85 |
|
| 86 |
class VolumeConverter(BaseUnitConverter): |
| 87 |
"""Utility to convert volume values.""" |
| 88 |
|
| 89 |
UNIT_CLASS = "volume" |
| 90 |
NORMALIZED_UNIT = UnitOfVolume.CUBIC_METERS |
| 91 |
³ |
| 92 |
_UNIT_CONVERSION: dict[str | None, float] = { |
| 93 |
UnitOfVolume.LITERS: 1 / _L_TO_CUBIC_METER, |
| 94 |
UnitOfVolume.GALLONS: 1 / _GALLON_TO_CUBIC_METER, |
| 95 |
UnitOfVolume.CUBIC_METERS: 1, |
| 96 |
UnitOfVolume.CENTUM_CUBIC_FEET: 1 / (100 * _CUBIC_FOOT_TO_CUBIC_METER), |
| 97 |
} |
| 98 |
VALID_UNITS = { |
| 99 |
UnitOfVolume.LITERS, |
| 100 |
UnitOfVolume.GALLONS, |
| 101 |
UnitOfVolume.CUBIC_METERS, |
| 102 |
UnitOfVolume.CENTUM_CUBIC_FEET, |
| 103 |
} |