Skip to content

Commit

Permalink
Use UnitOfBloodGlucoseConcentration
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Oct 31, 2024
1 parent 60a9efd commit 729cee9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/nightscout/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DATE, CONCENTRATION_MILLIGRAMS_PER_DECILITER
from homeassistant.const import ATTR_DATE, UnitOfBloodGlucoseConcentration
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -38,7 +38,9 @@ class NightscoutSensor(SensorEntity):
"""Implementation of a Nightscout sensor."""

_attr_device_class = SensorDeviceClass.BLOOD_SUGAR_LEVEL
_attr_native_unit_of_measurement = CONCENTRATION_MILLIGRAMS_PER_DECILITER
_attr_native_unit_of_measurement = (
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER
)
_attr_icon = "mdi:cloud-question"

def __init__(self, api: NightscoutAPI, name: str, unique_id: str | None) -> None:
Expand Down
8 changes: 2 additions & 6 deletions homeassistant/components/number/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
LIGHT_LUX,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfApparentPower,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
UnitOfDataRate,
UnitOfElectricCurrent,
Expand Down Expand Up @@ -437,10 +436,7 @@ class NumberDeviceClass(StrEnum):
NumberDeviceClass.AQI: {None},
NumberDeviceClass.ATMOSPHERIC_PRESSURE: set(UnitOfPressure),
NumberDeviceClass.BATTERY: {PERCENTAGE},
NumberDeviceClass.BLOOD_SUGAR_LEVEL: {
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
},
NumberDeviceClass.BLOOD_SUGAR_LEVEL: set(UnitOfBloodGlucoseConcentration),
NumberDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION},
NumberDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION},
NumberDeviceClass.CONDUCTIVITY: set(UnitOfConductivity),
Expand Down
8 changes: 2 additions & 6 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
LIGHT_LUX,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfApparentPower,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
UnitOfDataRate,
UnitOfElectricCurrent,
Expand Down Expand Up @@ -534,10 +533,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.AQI: {None},
SensorDeviceClass.ATMOSPHERIC_PRESSURE: set(UnitOfPressure),
SensorDeviceClass.BATTERY: {PERCENTAGE},
SensorDeviceClass.BLOOD_SUGAR_LEVEL: {
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
},
SensorDeviceClass.BLOOD_SUGAR_LEVEL: set(UnitOfBloodGlucoseConcentration),
SensorDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION},
SensorDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION},
SensorDeviceClass.CONDUCTIVITY: set(UnitOfConductivity),
Expand Down
9 changes: 7 additions & 2 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,13 +1348,18 @@ class UnitOfPrecipitationDepth(StrEnum):
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER: Final = "µg/m³"
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: Final = "mg/m³"
CONCENTRATION_MICROGRAMS_PER_CUBIC_FOOT: Final = "μg/ft³"
CONCENTRATION_MILLIGRAMS_PER_DECILITER: Final = "mg/dL"
CONCENTRATION_MILLIMOLS_PER_LITER: Final = "mmol/L"
CONCENTRATION_PARTS_PER_CUBIC_METER: Final = "p/m³"
CONCENTRATION_PARTS_PER_MILLION: Final = "ppm"
CONCENTRATION_PARTS_PER_BILLION: Final = "ppb"


class UnitOfBloodGlucoseConcentration(StrEnum):
"""Blood glucose concentration units."""

MILLIGRAMS_PER_DECILITER = "mg/dL"
MILLIMOLS_PER_LITER = "mmol/L"


# Speed units
class UnitOfSpeed(StrEnum):
"""Speed units."""
Expand Down
12 changes: 4 additions & 8 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from functools import lru_cache

from homeassistant.const import (
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
UnitOfDataRate,
UnitOfElectricCurrent,
Expand Down Expand Up @@ -180,13 +179,10 @@ class BloodSugarLevelConverter(BaseUnitConverter):

UNIT_CLASS = "blood_sugar_level"
_UNIT_CONVERSION: dict[str | None, float] = {
CONCENTRATION_MILLIGRAMS_PER_DECILITER: 18,
CONCENTRATION_MILLIMOLS_PER_LITER: 1,
}
VALID_UNITS = {
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: 18,
UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER: 1,
}
VALID_UNITS = set(UnitOfBloodGlucoseConcentration)


class ConductivityConverter(BaseUnitConverter):
Expand Down
15 changes: 7 additions & 8 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import pytest

from homeassistant.const import (
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
UnitOfDataRate,
UnitOfElectricCurrent,
Expand Down Expand Up @@ -85,8 +84,8 @@
# Dict containing all converters with a corresponding unit ratio.
_GET_UNIT_RATIO: dict[type[BaseUnitConverter], tuple[str | None, str | None, float]] = {
BloodSugarLevelConverter: (
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
CONCENTRATION_MILLIMOLS_PER_LITER,
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER,
UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER,
18,
),
ConductivityConverter: (
Expand Down Expand Up @@ -142,15 +141,15 @@
BloodSugarLevelConverter: [
(
90,
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER,
5,
CONCENTRATION_MILLIMOLS_PER_LITER,
UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER,
),
(
1,
CONCENTRATION_MILLIMOLS_PER_LITER,
UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER,
18,
CONCENTRATION_MILLIGRAMS_PER_DECILITER,
UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER,
),
],
ConductivityConverter: [
Expand Down

0 comments on commit 729cee9

Please sign in to comment.