Repository: home-assistant/core
File: homeassistant/util/unit_conversion.py (Python)

Code before:

class AreaConverter(BaseUnitConverter):
    """Utility to convert area values."""

    UNIT_CLASS = "area"
    _UNIT_CONVERSION: dict[str | None, float] = {
        UnitOfArea.SQUARE_METERS: 1,
        UnitOfArea.SQUARE_CENTIMETERS: 1 / _CM2_TO_M2,
        UnitOfArea.SQUARE_MILLIMETERS: 1 / _MM2_TO_M2,
        UnitOfArea.SQUARE_KILOMETERS: 1 / _KM2_TO_M2,
        UnitOfArea.SQUARE_INCHES: 1 / _IN2_TO_M2,
        UnitOfArea.SQUARE_FEET: 1 / _FT2_TO_M2,
        UnitOfArea.SQUARE_YARDS: 1 / _YD2_TO_M2,
        UnitOfArea.SQUARE_MILES: 1 / _MI2_TO_M2,
        UnitOfArea.ACRES: 1 / _ACRE_TO_M2,
        UnitOfArea.HECTARES: 1 / _HECTARE_TO_M2,
    }
    VALID_UNITS = set(UnitOfArea)


class BloodGlucoseConcentrationConverter(BaseUnitConverter):
    """Utility to convert blood glucose concentration values."""

    UNIT_CLASS = "blood_glucose_concentration"
    _UNIT_CONVERSION: dict[str | None, float] = {
        UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: 18.0182,
        UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER: 1,
    }
    VALID_UNITS = set(UnitOfBloodGlucoseConcentration)


class CarbonMonoxideConcentrationConverter(BaseUnitConverter):
    """Convert carbon monoxide ratio to mass per volume.

    Using ambient temperature of 20°C and pressure of 1 ATM.
    """

    UNIT_CLASS = "carbon_monoxide"
    _UNIT_CONVERSION: dict[str | None, float] = {
        CONCENTRATION_PARTS_PER_BILLION: 1e9,
        CONCENTRATION_PARTS_PER_MILLION: 1e6,
        CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: (
            _CARBON_MONOXIDE_MOLAR_MASS / _AMBIENT_IDEAL_GAS_MOLAR_VOLUME * 1e3
        ),
        CONCENTRATION_MICROGRAMS_PER_CUBIC_METER: (
            _CARBON_MONOXIDE_MOLAR_MASS / _AMBIENT_IDEAL_GAS_MOLAR_VOLUME * 1e6
        ),
    }
    VALID_UNITS = {
        CONCENTRATION_PARTS_PER_BILLION,
        CONCENTRATION_PARTS_PER_MILLION,

Review comment (suggestion):
Even better would be to create a constant for the molar mass on line 106:

```python
_GLUCOSE_MOLAR_MASS = 180.16
```

```suggestion
        UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: (
            _GLUCOSE_MOLAR_MASS / 10
        ),
```

Code after:


class AreaConverter(BaseUnitConverter):
    """Utility to convert area values."""

    UNIT_CLASS = "area"
    _UNIT_CONVERSION: dict[str | None, float] = {
        UnitOfArea.SQUARE_METERS: 1,
        UnitOfArea.SQUARE_CENTIMETERS: 1 / _CM2_TO_M2,
        UnitOfArea.SQUARE_MILLIMETERS: 1 / _MM2_TO_M2,
        UnitOfArea.SQUARE_KILOMETERS: 1 / _KM2_TO_M2,
        UnitOfArea.SQUARE_INCHES: 1 / _IN2_TO_M2,
        UnitOfArea.SQUARE_FEET: 1 / _FT2_TO_M2,
        UnitOfArea.SQUARE_YARDS: 1 / _YD2_TO_M2,
        UnitOfArea.SQUARE_MILES: 1 / _MI2_TO_M2,
        UnitOfArea.ACRES: 1 / _ACRE_TO_M2,
        UnitOfArea.HECTARES: 1 / _HECTARE_TO_M2,
    }
    VALID_UNITS = set(UnitOfArea)


class BloodGlucoseConcentrationConverter(BaseUnitConverter):
    """Utility to convert blood glucose concentration values."""

    UNIT_CLASS = "blood_glucose_concentration"
    _UNIT_CONVERSION: dict[str | None, float] = {
        UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: (
            _GLUCOSE_MOLAR_MASS / 10
        ),
        UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER: 1,
    }
    VALID_UNITS = set(UnitOfBloodGlucoseConcentration)


class CarbonMonoxideConcentrationConverter(BaseUnitConverter):
    """Convert carbon monoxide ratio to mass per volume.

    Using ambient temperature of 20°C and pressure of 1 ATM.
    """

    UNIT_CLASS = "carbon_monoxide"
    _UNIT_CONVERSION: dict[str | None, float] = {
        CONCENTRATION_PARTS_PER_BILLION: 1e9,
        CONCENTRATION_PARTS_PER_MILLION: 1e6,
        CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: (
            _CARBON_MONOXIDE_MOLAR_MASS / _AMBIENT_IDEAL_GAS_MOLAR_VOLUME * 1e3
        ),
        CONCENTRATION_MICROGRAMS_PER_CUBIC_METER: (
            _CARBON_MONOXIDE_MOLAR_MASS / _AMBIENT_IDEAL_GAS_MOLAR_VOLUME * 1e6
        ),
    }