From 7ad4a1f3aae7ba34462395c92532d56cf121233f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:11:13 +0000 Subject: [PATCH 1/8] Add blood sugar level device class --- homeassistant/components/nightscout/sensor.py | 7 +++--- homeassistant/components/number/const.py | 12 ++++++++++ homeassistant/components/sensor/const.py | 15 ++++++++++++ .../components/sensor/device_condition.py | 3 +++ .../components/sensor/device_trigger.py | 3 +++ homeassistant/components/sensor/icons.json | 3 +++ homeassistant/components/sensor/strings.json | 5 ++++ homeassistant/const.py | 2 ++ homeassistant/util/unit_conversion.py | 16 +++++++++++++ tests/util/test_unit_conversion.py | 23 +++++++++++++++++++ 10 files changed, 86 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index 92291bdc4f9957..7f726dd3a96777 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -9,9 +9,9 @@ from aiohttp import ClientError from py_nightscout import Api as NightscoutAPI -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_DATE +from homeassistant.const import ATTR_DATE, CONCENTRATION_MILLIGRAMS_PER_DECILITER from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -37,7 +37,8 @@ async def async_setup_entry( class NightscoutSensor(SensorEntity): """Implementation of a Nightscout sensor.""" - _attr_native_unit_of_measurement = "mg/dL" + _attr_device_class = SensorDeviceClass.BLOOD_SUGAR_LEVEL + _attr_native_unit_of_measurement = CONCENTRATION_MILLIGRAMS_PER_DECILITER _attr_icon = "mdi:cloud-question" def __init__(self, api: NightscoutAPI, name: str, unique_id: str | None) -> None: diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index ad95c9b5358b7b..9005a08b5e1fdf 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -10,6 +10,8 @@ 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, @@ -109,6 +111,12 @@ class NumberDeviceClass(StrEnum): Unit of measurement: `%` """ + BLOOD_SUGAR_LEVEL = "blood_sugar_level" + """Blood sugar level. + + Unit of measurement: `mg/dL`, `mmol/L` + """ + CO = "carbon_monoxide" """Carbon Monoxide gas concentration. @@ -429,6 +437,10 @@ 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.CO: {CONCENTRATION_PARTS_PER_MILLION}, NumberDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION}, NumberDeviceClass.CONDUCTIVITY: set(UnitOfConductivity), diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index da0b48a23a0406..ff535d2b0db226 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -10,6 +10,8 @@ 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, @@ -47,6 +49,7 @@ ) from homeassistant.util.unit_conversion import ( BaseUnitConverter, + BloodSugarLevelConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -127,6 +130,12 @@ class SensorDeviceClass(StrEnum): Unit of measurement: `%` """ + BLOOD_SUGAR_LEVEL = "blood_sugar_level" + """Blood sugar level. + + Unit of measurement: `mg/dL`, `mmol/L` + """ + CO = "carbon_monoxide" """Carbon Monoxide gas concentration. @@ -493,6 +502,7 @@ class SensorStateClass(StrEnum): UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = { SensorDeviceClass.ATMOSPHERIC_PRESSURE: PressureConverter, + SensorDeviceClass.BLOOD_SUGAR_LEVEL: BloodSugarLevelConverter, SensorDeviceClass.CONDUCTIVITY: ConductivityConverter, SensorDeviceClass.CURRENT: ElectricCurrentConverter, SensorDeviceClass.DATA_RATE: DataRateConverter, @@ -524,6 +534,10 @@ 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.CO: {CONCENTRATION_PARTS_PER_MILLION}, SensorDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION}, SensorDeviceClass.CONDUCTIVITY: set(UnitOfConductivity), @@ -599,6 +613,7 @@ class SensorStateClass(StrEnum): SensorDeviceClass.AQI: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.ATMOSPHERIC_PRESSURE: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.BATTERY: {SensorStateClass.MEASUREMENT}, + SensorDeviceClass.BLOOD_SUGAR_LEVEL: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CO: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CO2: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CONDUCTIVITY: {SensorStateClass.MEASUREMENT}, diff --git a/homeassistant/components/sensor/device_condition.py b/homeassistant/components/sensor/device_condition.py index f2b51899312cda..02cea835c0f524 100644 --- a/homeassistant/components/sensor/device_condition.py +++ b/homeassistant/components/sensor/device_condition.py @@ -37,6 +37,7 @@ CONF_IS_AQI = "is_aqi" CONF_IS_ATMOSPHERIC_PRESSURE = "is_atmospheric_pressure" CONF_IS_BATTERY_LEVEL = "is_battery_level" +CONF_IS_BLOOD_SUGAR_LEVEL = "is_blood_sugar_level" CONF_IS_CO = "is_carbon_monoxide" CONF_IS_CO2 = "is_carbon_dioxide" CONF_IS_CONDUCTIVITY = "is_conductivity" @@ -87,6 +88,7 @@ SensorDeviceClass.AQI: [{CONF_TYPE: CONF_IS_AQI}], SensorDeviceClass.ATMOSPHERIC_PRESSURE: [{CONF_TYPE: CONF_IS_ATMOSPHERIC_PRESSURE}], SensorDeviceClass.BATTERY: [{CONF_TYPE: CONF_IS_BATTERY_LEVEL}], + SensorDeviceClass.BLOOD_SUGAR_LEVEL: [{CONF_TYPE: CONF_IS_BLOOD_SUGAR_LEVEL}], SensorDeviceClass.CO: [{CONF_TYPE: CONF_IS_CO}], SensorDeviceClass.CO2: [{CONF_TYPE: CONF_IS_CO2}], SensorDeviceClass.CONDUCTIVITY: [{CONF_TYPE: CONF_IS_CONDUCTIVITY}], @@ -151,6 +153,7 @@ CONF_IS_AQI, CONF_IS_ATMOSPHERIC_PRESSURE, CONF_IS_BATTERY_LEVEL, + CONF_IS_BLOOD_SUGAR_LEVEL, CONF_IS_CO, CONF_IS_CO2, CONF_IS_CONDUCTIVITY, diff --git a/homeassistant/components/sensor/device_trigger.py b/homeassistant/components/sensor/device_trigger.py index b07b3fac11e456..22e0aad9d132a0 100644 --- a/homeassistant/components/sensor/device_trigger.py +++ b/homeassistant/components/sensor/device_trigger.py @@ -36,6 +36,7 @@ CONF_AQI = "aqi" CONF_ATMOSPHERIC_PRESSURE = "atmospheric_pressure" CONF_BATTERY_LEVEL = "battery_level" +CONF_BLOOD_SUGAR_LEVEL = "blood_sugar_level" CONF_CO = "carbon_monoxide" CONF_CO2 = "carbon_dioxide" CONF_CONDUCTIVITY = "conductivity" @@ -86,6 +87,7 @@ SensorDeviceClass.AQI: [{CONF_TYPE: CONF_AQI}], SensorDeviceClass.ATMOSPHERIC_PRESSURE: [{CONF_TYPE: CONF_ATMOSPHERIC_PRESSURE}], SensorDeviceClass.BATTERY: [{CONF_TYPE: CONF_BATTERY_LEVEL}], + SensorDeviceClass.BLOOD_SUGAR_LEVEL: [{CONF_TYPE: CONF_BLOOD_SUGAR_LEVEL}], SensorDeviceClass.CO: [{CONF_TYPE: CONF_CO}], SensorDeviceClass.CO2: [{CONF_TYPE: CONF_CO2}], SensorDeviceClass.CONDUCTIVITY: [{CONF_TYPE: CONF_CONDUCTIVITY}], @@ -151,6 +153,7 @@ CONF_AQI, CONF_ATMOSPHERIC_PRESSURE, CONF_BATTERY_LEVEL, + CONF_BLOOD_SUGAR_LEVEL, CONF_CO, CONF_CO2, CONF_CONDUCTIVITY, diff --git a/homeassistant/components/sensor/icons.json b/homeassistant/components/sensor/icons.json index 6132fcbc1e92e4..a377501905d096 100644 --- a/homeassistant/components/sensor/icons.json +++ b/homeassistant/components/sensor/icons.json @@ -12,6 +12,9 @@ "atmospheric_pressure": { "default": "mdi:thermometer-lines" }, + "blood_sugar_level": { + "default": "mdi:spoon-sugar" + }, "carbon_dioxide": { "default": "mdi:molecule-co2" }, diff --git a/homeassistant/components/sensor/strings.json b/homeassistant/components/sensor/strings.json index 71bead342c40b7..a4d9bebc22365e 100644 --- a/homeassistant/components/sensor/strings.json +++ b/homeassistant/components/sensor/strings.json @@ -6,6 +6,7 @@ "is_aqi": "Current {entity_name} air quality index", "is_atmospheric_pressure": "Current {entity_name} atmospheric pressure", "is_battery_level": "Current {entity_name} battery level", + "is_blood_sugar_level": "Current {entity_name} blood sugar level", "is_carbon_monoxide": "Current {entity_name} carbon monoxide concentration level", "is_carbon_dioxide": "Current {entity_name} carbon dioxide concentration level", "is_conductivity": "Current {entity_name} conductivity", @@ -56,6 +57,7 @@ "aqi": "{entity_name} air quality index changes", "atmospheric_pressure": "{entity_name} atmospheric pressure changes", "battery_level": "{entity_name} battery level changes", + "blood_sugar_level": "{entity_name} blood sugar level changes", "carbon_monoxide": "{entity_name} carbon monoxide concentration changes", "carbon_dioxide": "{entity_name} carbon dioxide concentration changes", "conductivity": "{entity_name} conductivity changes", @@ -149,6 +151,9 @@ "battery": { "name": "Battery" }, + "blood_sugar_level": { + "name": "Blood sugar level" + }, "carbon_monoxide": { "name": "Carbon monoxide" }, diff --git a/homeassistant/const.py b/homeassistant/const.py index 1da3b819f9f826..82bc3c8c4f3f20 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1348,6 +1348,8 @@ 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" diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 6bc595bd487752..6e88abac14580f 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -6,6 +6,8 @@ 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, @@ -173,6 +175,20 @@ class DistanceConverter(BaseUnitConverter): } +class BloodSugarLevelConverter(BaseUnitConverter): + """Utility to convert blood sugar level values.""" + + UNIT_CLASS = "blood_sugar_level" + _UNIT_CONVERSION: dict[str | None, float] = { + CONCENTRATION_MILLIGRAMS_PER_DECILITER: 1, + CONCENTRATION_MILLIMOLS_PER_LITER: 1 / 180, + } + VALID_UNITS = { + CONCENTRATION_MILLIGRAMS_PER_DECILITER, + CONCENTRATION_MILLIMOLS_PER_LITER, + } + + class ConductivityConverter(BaseUnitConverter): """Utility to convert electric current values.""" diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index 3b8fd3bc46659b..5aabde847ff7f6 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -8,6 +8,8 @@ import pytest from homeassistant.const import ( + CONCENTRATION_MILLIGRAMS_PER_DECILITER, + CONCENTRATION_MILLIMOLS_PER_LITER, CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_PARTS_PER_MILLION, PERCENTAGE, @@ -32,6 +34,7 @@ from homeassistant.util import unit_conversion from homeassistant.util.unit_conversion import ( BaseUnitConverter, + BloodSugarLevelConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -59,6 +62,7 @@ _ALL_CONVERTERS: dict[type[BaseUnitConverter], list[str | None]] = { converter: sorted(converter.VALID_UNITS, key=lambda x: (x is None, x)) for converter in ( + BloodSugarLevelConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -80,6 +84,11 @@ # 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, + 1 / 18, + ), ConductivityConverter: ( UnitOfConductivity.MICROSIEMENS_PER_CM, UnitOfConductivity.MILLISIEMENS_PER_CM, @@ -130,6 +139,20 @@ _CONVERTED_VALUE: dict[ type[BaseUnitConverter], list[tuple[float, str | None, float, str | None]] ] = { + BloodSugarLevelConverter: [ + ( + 1, + CONCENTRATION_MILLIGRAMS_PER_DECILITER, + 0.05556, + CONCENTRATION_MILLIMOLS_PER_LITER, + ), + ( + 1, + CONCENTRATION_MILLIMOLS_PER_LITER, + 18, + CONCENTRATION_MILLIGRAMS_PER_DECILITER, + ), + ], ConductivityConverter: [ # Deprecated to deprecated (5, UnitOfConductivity.SIEMENS, 5e3, UnitOfConductivity.MILLISIEMENS), From 23076c8156117c10536cad9a7648b24fe7ae8920 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:40:53 +0000 Subject: [PATCH 2/8] Fix ratio --- homeassistant/util/unit_conversion.py | 4 ++-- tests/util/test_unit_conversion.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 6e88abac14580f..82a85e4e803075 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -180,8 +180,8 @@ class BloodSugarLevelConverter(BaseUnitConverter): UNIT_CLASS = "blood_sugar_level" _UNIT_CONVERSION: dict[str | None, float] = { - CONCENTRATION_MILLIGRAMS_PER_DECILITER: 1, - CONCENTRATION_MILLIMOLS_PER_LITER: 1 / 180, + CONCENTRATION_MILLIGRAMS_PER_DECILITER: 18, + CONCENTRATION_MILLIMOLS_PER_LITER: 1, } VALID_UNITS = { CONCENTRATION_MILLIGRAMS_PER_DECILITER, diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index 5aabde847ff7f6..4ba7eb64169a96 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -87,7 +87,7 @@ BloodSugarLevelConverter: ( CONCENTRATION_MILLIGRAMS_PER_DECILITER, CONCENTRATION_MILLIMOLS_PER_LITER, - 1 / 18, + 18, ), ConductivityConverter: ( UnitOfConductivity.MICROSIEMENS_PER_CM, @@ -141,9 +141,9 @@ ] = { BloodSugarLevelConverter: [ ( - 1, + 90, CONCENTRATION_MILLIGRAMS_PER_DECILITER, - 0.05556, + 5, CONCENTRATION_MILLIMOLS_PER_LITER, ), ( From 29ea957e6ab5f4b73e8aef89ac32a524057e5ddd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 29 Oct 2024 08:14:59 +0000 Subject: [PATCH 3/8] Update recorder --- homeassistant/components/recorder/statistics.py | 3 +++ homeassistant/components/recorder/websocket_api.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 4ffe7c72971d8e..dda04fdd64b94a 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -28,6 +28,7 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import ( BaseUnitConverter, + BloodSugarLevelConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -128,6 +129,8 @@ STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = { + **{unit: BloodSugarLevelConverter for unit in BloodSugarLevelConverter.VALID_UNITS}, + **{unit: ConductivityConverter for unit in ConductivityConverter.VALID_UNITS}, **{unit: ConductivityConverter for unit in ConductivityConverter.VALID_UNITS}, **{unit: DataRateConverter for unit in DataRateConverter.VALID_UNITS}, **{unit: DistanceConverter for unit in DistanceConverter.VALID_UNITS}, diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index ac917e903df25e..7d0591a236b6ba 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -16,6 +16,7 @@ from homeassistant.helpers.json import json_bytes from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import ( + BloodSugarLevelConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -54,6 +55,7 @@ UNIT_SCHEMA = vol.Schema( { + vol.Optional("blood_sugar_level"): vol.In(BloodSugarLevelConverter.VALID_UNITS), vol.Optional("conductivity"): vol.In(ConductivityConverter.VALID_UNITS), vol.Optional("data_rate"): vol.In(DataRateConverter.VALID_UNITS), vol.Optional("distance"): vol.In(DistanceConverter.VALID_UNITS), From 7e9ddb896c1982525400c851645e407948448004 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:42:05 +0000 Subject: [PATCH 4/8] Use UnitOfBloodGlucoseConcentration --- homeassistant/components/nightscout/sensor.py | 6 ++++-- homeassistant/components/number/const.py | 8 ++------ homeassistant/components/sensor/const.py | 8 ++------ homeassistant/const.py | 9 +++++++-- homeassistant/util/unit_conversion.py | 12 ++++-------- tests/util/test_unit_conversion.py | 15 +++++++-------- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index 7f726dd3a96777..b965644ede8e4d 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -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 @@ -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: diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 9005a08b5e1fdf..28cedc3d25886c 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -10,8 +10,6 @@ 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, @@ -19,6 +17,7 @@ SIGNAL_STRENGTH_DECIBELS, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfApparentPower, + UnitOfBloodGlucoseConcentration, UnitOfConductivity, UnitOfDataRate, UnitOfElectricCurrent, @@ -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), diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index ff535d2b0db226..3d4963fbd7ad5e 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -10,8 +10,6 @@ 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, @@ -19,6 +17,7 @@ SIGNAL_STRENGTH_DECIBELS, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfApparentPower, + UnitOfBloodGlucoseConcentration, UnitOfConductivity, UnitOfDataRate, UnitOfElectricCurrent, @@ -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), diff --git a/homeassistant/const.py b/homeassistant/const.py index 82bc3c8c4f3f20..41068748fd8ea3 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -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.""" diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 82a85e4e803075..b235345a22b1bd 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -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, @@ -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): diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index 4ba7eb64169a96..cc0f673fe58f16 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -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, @@ -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: ( @@ -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: [ From 25b0284bed31cfc0520ee6931b35550f68354568 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:06:14 +0000 Subject: [PATCH 5/8] Rename --- homeassistant/components/nightscout/sensor.py | 2 +- homeassistant/components/number/const.py | 4 ++-- homeassistant/components/number/icons.json | 3 +++ homeassistant/components/number/strings.json | 3 +++ homeassistant/components/sensor/const.py | 8 ++++---- homeassistant/components/sensor/device_condition.py | 8 +++++--- homeassistant/components/sensor/device_trigger.py | 8 +++++--- homeassistant/components/sensor/icons.json | 2 +- homeassistant/components/sensor/strings.json | 6 +++--- homeassistant/util/unit_conversion.py | 2 +- 10 files changed, 28 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index b965644ede8e4d..620349ec3c3ff5 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -37,7 +37,7 @@ async def async_setup_entry( class NightscoutSensor(SensorEntity): """Implementation of a Nightscout sensor.""" - _attr_device_class = SensorDeviceClass.BLOOD_SUGAR_LEVEL + _attr_device_class = SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION _attr_native_unit_of_measurement = ( UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER ) diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 28cedc3d25886c..b07231845c1b3f 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -110,7 +110,7 @@ class NumberDeviceClass(StrEnum): Unit of measurement: `%` """ - BLOOD_SUGAR_LEVEL = "blood_sugar_level" + BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration" """Blood sugar level. Unit of measurement: `mg/dL`, `mmol/L` @@ -436,7 +436,7 @@ class NumberDeviceClass(StrEnum): NumberDeviceClass.AQI: {None}, NumberDeviceClass.ATMOSPHERIC_PRESSURE: set(UnitOfPressure), NumberDeviceClass.BATTERY: {PERCENTAGE}, - NumberDeviceClass.BLOOD_SUGAR_LEVEL: set(UnitOfBloodGlucoseConcentration), + NumberDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: set(UnitOfBloodGlucoseConcentration), NumberDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION}, NumberDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION}, NumberDeviceClass.CONDUCTIVITY: set(UnitOfConductivity), diff --git a/homeassistant/components/number/icons.json b/homeassistant/components/number/icons.json index a122aaecb09465..5e0fc6e44d2612 100644 --- a/homeassistant/components/number/icons.json +++ b/homeassistant/components/number/icons.json @@ -15,6 +15,9 @@ "battery": { "default": "mdi:battery" }, + "blood_glucose_concentration": { + "default": "mdi:spoon-sugar" + }, "carbon_dioxide": { "default": "mdi:molecule-co2" }, diff --git a/homeassistant/components/number/strings.json b/homeassistant/components/number/strings.json index 580385172e38a3..b9aec880ecc22f 100644 --- a/homeassistant/components/number/strings.json +++ b/homeassistant/components/number/strings.json @@ -43,6 +43,9 @@ "battery": { "name": "[%key:component::sensor::entity_component::battery::name%]" }, + "blood_glucose_concentration": { + "name": "[%key:component::sensor::entity_component::blood_glucose_concentration::name%]" + }, "carbon_dioxide": { "name": "[%key:component::sensor::entity_component::carbon_dioxide::name%]" }, diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 3d4963fbd7ad5e..1d920f38d574c3 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -129,7 +129,7 @@ class SensorDeviceClass(StrEnum): Unit of measurement: `%` """ - BLOOD_SUGAR_LEVEL = "blood_sugar_level" + BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration" """Blood sugar level. Unit of measurement: `mg/dL`, `mmol/L` @@ -501,7 +501,7 @@ class SensorStateClass(StrEnum): UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = { SensorDeviceClass.ATMOSPHERIC_PRESSURE: PressureConverter, - SensorDeviceClass.BLOOD_SUGAR_LEVEL: BloodSugarLevelConverter, + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: BloodSugarLevelConverter, SensorDeviceClass.CONDUCTIVITY: ConductivityConverter, SensorDeviceClass.CURRENT: ElectricCurrentConverter, SensorDeviceClass.DATA_RATE: DataRateConverter, @@ -533,7 +533,7 @@ class SensorStateClass(StrEnum): SensorDeviceClass.AQI: {None}, SensorDeviceClass.ATMOSPHERIC_PRESSURE: set(UnitOfPressure), SensorDeviceClass.BATTERY: {PERCENTAGE}, - SensorDeviceClass.BLOOD_SUGAR_LEVEL: set(UnitOfBloodGlucoseConcentration), + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: set(UnitOfBloodGlucoseConcentration), SensorDeviceClass.CO: {CONCENTRATION_PARTS_PER_MILLION}, SensorDeviceClass.CO2: {CONCENTRATION_PARTS_PER_MILLION}, SensorDeviceClass.CONDUCTIVITY: set(UnitOfConductivity), @@ -609,7 +609,7 @@ class SensorStateClass(StrEnum): SensorDeviceClass.AQI: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.ATMOSPHERIC_PRESSURE: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.BATTERY: {SensorStateClass.MEASUREMENT}, - SensorDeviceClass.BLOOD_SUGAR_LEVEL: {SensorStateClass.MEASUREMENT}, + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CO: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CO2: {SensorStateClass.MEASUREMENT}, SensorDeviceClass.CONDUCTIVITY: {SensorStateClass.MEASUREMENT}, diff --git a/homeassistant/components/sensor/device_condition.py b/homeassistant/components/sensor/device_condition.py index 02cea835c0f524..56ecb36adb3b1d 100644 --- a/homeassistant/components/sensor/device_condition.py +++ b/homeassistant/components/sensor/device_condition.py @@ -37,7 +37,7 @@ CONF_IS_AQI = "is_aqi" CONF_IS_ATMOSPHERIC_PRESSURE = "is_atmospheric_pressure" CONF_IS_BATTERY_LEVEL = "is_battery_level" -CONF_IS_BLOOD_SUGAR_LEVEL = "is_blood_sugar_level" +CONF_IS_BLOOD_GLUCOSE_CONCENTRATION = "is_blood_glucose_concentration" CONF_IS_CO = "is_carbon_monoxide" CONF_IS_CO2 = "is_carbon_dioxide" CONF_IS_CONDUCTIVITY = "is_conductivity" @@ -88,7 +88,9 @@ SensorDeviceClass.AQI: [{CONF_TYPE: CONF_IS_AQI}], SensorDeviceClass.ATMOSPHERIC_PRESSURE: [{CONF_TYPE: CONF_IS_ATMOSPHERIC_PRESSURE}], SensorDeviceClass.BATTERY: [{CONF_TYPE: CONF_IS_BATTERY_LEVEL}], - SensorDeviceClass.BLOOD_SUGAR_LEVEL: [{CONF_TYPE: CONF_IS_BLOOD_SUGAR_LEVEL}], + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: [ + {CONF_TYPE: CONF_IS_BLOOD_GLUCOSE_CONCENTRATION} + ], SensorDeviceClass.CO: [{CONF_TYPE: CONF_IS_CO}], SensorDeviceClass.CO2: [{CONF_TYPE: CONF_IS_CO2}], SensorDeviceClass.CONDUCTIVITY: [{CONF_TYPE: CONF_IS_CONDUCTIVITY}], @@ -153,7 +155,7 @@ CONF_IS_AQI, CONF_IS_ATMOSPHERIC_PRESSURE, CONF_IS_BATTERY_LEVEL, - CONF_IS_BLOOD_SUGAR_LEVEL, + CONF_IS_BLOOD_GLUCOSE_CONCENTRATION, CONF_IS_CO, CONF_IS_CO2, CONF_IS_CONDUCTIVITY, diff --git a/homeassistant/components/sensor/device_trigger.py b/homeassistant/components/sensor/device_trigger.py index 22e0aad9d132a0..ffee10d9f401b4 100644 --- a/homeassistant/components/sensor/device_trigger.py +++ b/homeassistant/components/sensor/device_trigger.py @@ -36,7 +36,7 @@ CONF_AQI = "aqi" CONF_ATMOSPHERIC_PRESSURE = "atmospheric_pressure" CONF_BATTERY_LEVEL = "battery_level" -CONF_BLOOD_SUGAR_LEVEL = "blood_sugar_level" +CONF_BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration" CONF_CO = "carbon_monoxide" CONF_CO2 = "carbon_dioxide" CONF_CONDUCTIVITY = "conductivity" @@ -87,7 +87,9 @@ SensorDeviceClass.AQI: [{CONF_TYPE: CONF_AQI}], SensorDeviceClass.ATMOSPHERIC_PRESSURE: [{CONF_TYPE: CONF_ATMOSPHERIC_PRESSURE}], SensorDeviceClass.BATTERY: [{CONF_TYPE: CONF_BATTERY_LEVEL}], - SensorDeviceClass.BLOOD_SUGAR_LEVEL: [{CONF_TYPE: CONF_BLOOD_SUGAR_LEVEL}], + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: [ + {CONF_TYPE: CONF_BLOOD_GLUCOSE_CONCENTRATION} + ], SensorDeviceClass.CO: [{CONF_TYPE: CONF_CO}], SensorDeviceClass.CO2: [{CONF_TYPE: CONF_CO2}], SensorDeviceClass.CONDUCTIVITY: [{CONF_TYPE: CONF_CONDUCTIVITY}], @@ -153,7 +155,7 @@ CONF_AQI, CONF_ATMOSPHERIC_PRESSURE, CONF_BATTERY_LEVEL, - CONF_BLOOD_SUGAR_LEVEL, + CONF_BLOOD_GLUCOSE_CONCENTRATION, CONF_CO, CONF_CO2, CONF_CONDUCTIVITY, diff --git a/homeassistant/components/sensor/icons.json b/homeassistant/components/sensor/icons.json index a377501905d096..ea4c902e665735 100644 --- a/homeassistant/components/sensor/icons.json +++ b/homeassistant/components/sensor/icons.json @@ -12,7 +12,7 @@ "atmospheric_pressure": { "default": "mdi:thermometer-lines" }, - "blood_sugar_level": { + "blood_glucose_concentration": { "default": "mdi:spoon-sugar" }, "carbon_dioxide": { diff --git a/homeassistant/components/sensor/strings.json b/homeassistant/components/sensor/strings.json index a4d9bebc22365e..64fc1c1eabb9fb 100644 --- a/homeassistant/components/sensor/strings.json +++ b/homeassistant/components/sensor/strings.json @@ -6,7 +6,7 @@ "is_aqi": "Current {entity_name} air quality index", "is_atmospheric_pressure": "Current {entity_name} atmospheric pressure", "is_battery_level": "Current {entity_name} battery level", - "is_blood_sugar_level": "Current {entity_name} blood sugar level", + "is_blood_glucose_concentration": "Current {entity_name} blood sugar level", "is_carbon_monoxide": "Current {entity_name} carbon monoxide concentration level", "is_carbon_dioxide": "Current {entity_name} carbon dioxide concentration level", "is_conductivity": "Current {entity_name} conductivity", @@ -57,7 +57,7 @@ "aqi": "{entity_name} air quality index changes", "atmospheric_pressure": "{entity_name} atmospheric pressure changes", "battery_level": "{entity_name} battery level changes", - "blood_sugar_level": "{entity_name} blood sugar level changes", + "blood_glucose_concentration": "{entity_name} blood sugar level changes", "carbon_monoxide": "{entity_name} carbon monoxide concentration changes", "carbon_dioxide": "{entity_name} carbon dioxide concentration changes", "conductivity": "{entity_name} conductivity changes", @@ -151,7 +151,7 @@ "battery": { "name": "Battery" }, - "blood_sugar_level": { + "blood_glucose_concentration": { "name": "Blood sugar level" }, "carbon_monoxide": { diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index b235345a22b1bd..7a6831cec7e40d 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -177,7 +177,7 @@ class DistanceConverter(BaseUnitConverter): class BloodSugarLevelConverter(BaseUnitConverter): """Utility to convert blood sugar level values.""" - UNIT_CLASS = "blood_sugar_level" + UNIT_CLASS = "blood_glucose_concentration" _UNIT_CONVERSION: dict[str | None, float] = { UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: 18, UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER: 1, From d135acedc1231ed4412c0fe09f465bc689b63108 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 1 Nov 2024 08:07:11 +0000 Subject: [PATCH 6/8] Rename --- homeassistant/components/number/const.py | 2 +- homeassistant/components/recorder/statistics.py | 7 +++++-- homeassistant/components/recorder/websocket_api.py | 6 ++++-- homeassistant/components/sensor/const.py | 6 +++--- homeassistant/components/sensor/strings.json | 2 +- homeassistant/util/unit_conversion.py | 2 +- tests/util/test_unit_conversion.py | 8 ++++---- 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index b07231845c1b3f..a412c2e4db7f49 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -111,7 +111,7 @@ class NumberDeviceClass(StrEnum): """ BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration" - """Blood sugar level. + """Blood glucose concentration. Unit of measurement: `mg/dL`, `mmol/L` """ diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index dda04fdd64b94a..9a66c4542b5e0b 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -28,7 +28,7 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import ( BaseUnitConverter, - BloodSugarLevelConverter, + BloodGlugoseConcentrationConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -129,7 +129,10 @@ STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = { - **{unit: BloodSugarLevelConverter for unit in BloodSugarLevelConverter.VALID_UNITS}, + **{ + unit: BloodGlugoseConcentrationConverter + for unit in BloodGlugoseConcentrationConverter.VALID_UNITS + }, **{unit: ConductivityConverter for unit in ConductivityConverter.VALID_UNITS}, **{unit: ConductivityConverter for unit in ConductivityConverter.VALID_UNITS}, **{unit: DataRateConverter for unit in DataRateConverter.VALID_UNITS}, diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index 7d0591a236b6ba..8b8d1cfb0c6579 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -16,7 +16,7 @@ from homeassistant.helpers.json import json_bytes from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import ( - BloodSugarLevelConverter, + BloodGlugoseConcentrationConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -55,7 +55,9 @@ UNIT_SCHEMA = vol.Schema( { - vol.Optional("blood_sugar_level"): vol.In(BloodSugarLevelConverter.VALID_UNITS), + vol.Optional("blood_glucose_concentration"): vol.In( + BloodGlugoseConcentrationConverter.VALID_UNITS + ), vol.Optional("conductivity"): vol.In(ConductivityConverter.VALID_UNITS), vol.Optional("data_rate"): vol.In(DataRateConverter.VALID_UNITS), vol.Optional("distance"): vol.In(DistanceConverter.VALID_UNITS), diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 1d920f38d574c3..8d9bb4d4403221 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -48,7 +48,7 @@ ) from homeassistant.util.unit_conversion import ( BaseUnitConverter, - BloodSugarLevelConverter, + BloodGlugoseConcentrationConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -130,7 +130,7 @@ class SensorDeviceClass(StrEnum): """ BLOOD_GLUCOSE_CONCENTRATION = "blood_glucose_concentration" - """Blood sugar level. + """Blood glucose concentration. Unit of measurement: `mg/dL`, `mmol/L` """ @@ -501,7 +501,7 @@ class SensorStateClass(StrEnum): UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = { SensorDeviceClass.ATMOSPHERIC_PRESSURE: PressureConverter, - SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: BloodSugarLevelConverter, + SensorDeviceClass.BLOOD_GLUCOSE_CONCENTRATION: BloodGlugoseConcentrationConverter, SensorDeviceClass.CONDUCTIVITY: ConductivityConverter, SensorDeviceClass.CURRENT: ElectricCurrentConverter, SensorDeviceClass.DATA_RATE: DataRateConverter, diff --git a/homeassistant/components/sensor/strings.json b/homeassistant/components/sensor/strings.json index 64fc1c1eabb9fb..318c2f01859b57 100644 --- a/homeassistant/components/sensor/strings.json +++ b/homeassistant/components/sensor/strings.json @@ -152,7 +152,7 @@ "name": "Battery" }, "blood_glucose_concentration": { - "name": "Blood sugar level" + "name": "Blood glucose concentration" }, "carbon_monoxide": { "name": "Carbon monoxide" diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 7a6831cec7e40d..dfffd193c30c37 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -174,7 +174,7 @@ class DistanceConverter(BaseUnitConverter): } -class BloodSugarLevelConverter(BaseUnitConverter): +class BloodGlugoseConcentrationConverter(BaseUnitConverter): """Utility to convert blood sugar level values.""" UNIT_CLASS = "blood_glucose_concentration" diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index cc0f673fe58f16..d17ab39f0679cf 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -33,7 +33,7 @@ from homeassistant.util import unit_conversion from homeassistant.util.unit_conversion import ( BaseUnitConverter, - BloodSugarLevelConverter, + BloodGlugoseConcentrationConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -61,7 +61,7 @@ _ALL_CONVERTERS: dict[type[BaseUnitConverter], list[str | None]] = { converter: sorted(converter.VALID_UNITS, key=lambda x: (x is None, x)) for converter in ( - BloodSugarLevelConverter, + BloodGlugoseConcentrationConverter, ConductivityConverter, DataRateConverter, DistanceConverter, @@ -83,7 +83,7 @@ # Dict containing all converters with a corresponding unit ratio. _GET_UNIT_RATIO: dict[type[BaseUnitConverter], tuple[str | None, str | None, float]] = { - BloodSugarLevelConverter: ( + BloodGlugoseConcentrationConverter: ( UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER, UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER, 18, @@ -138,7 +138,7 @@ _CONVERTED_VALUE: dict[ type[BaseUnitConverter], list[tuple[float, str | None, float, str | None]] ] = { - BloodSugarLevelConverter: [ + BloodGlugoseConcentrationConverter: [ ( 90, UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER, From cf8773db7cab21a463f580f72df7488869eadaf2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 1 Nov 2024 08:10:32 +0000 Subject: [PATCH 7/8] Rename --- homeassistant/components/sensor/strings.json | 4 ++-- homeassistant/util/unit_conversion.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/strings.json b/homeassistant/components/sensor/strings.json index 318c2f01859b57..6d529e72c3b2bb 100644 --- a/homeassistant/components/sensor/strings.json +++ b/homeassistant/components/sensor/strings.json @@ -6,7 +6,7 @@ "is_aqi": "Current {entity_name} air quality index", "is_atmospheric_pressure": "Current {entity_name} atmospheric pressure", "is_battery_level": "Current {entity_name} battery level", - "is_blood_glucose_concentration": "Current {entity_name} blood sugar level", + "is_blood_glucose_concentration": "Current {entity_name} blood glucose concentration", "is_carbon_monoxide": "Current {entity_name} carbon monoxide concentration level", "is_carbon_dioxide": "Current {entity_name} carbon dioxide concentration level", "is_conductivity": "Current {entity_name} conductivity", @@ -57,7 +57,7 @@ "aqi": "{entity_name} air quality index changes", "atmospheric_pressure": "{entity_name} atmospheric pressure changes", "battery_level": "{entity_name} battery level changes", - "blood_glucose_concentration": "{entity_name} blood sugar level changes", + "blood_glucose_concentration": "{entity_name} blood glucose concentration changes", "carbon_monoxide": "{entity_name} carbon monoxide concentration changes", "carbon_dioxide": "{entity_name} carbon dioxide concentration changes", "conductivity": "{entity_name} conductivity changes", diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index dfffd193c30c37..b1c2e4cd309c96 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -175,7 +175,7 @@ class DistanceConverter(BaseUnitConverter): class BloodGlugoseConcentrationConverter(BaseUnitConverter): - """Utility to convert blood sugar level values.""" + """Utility to convert blood glucose concentration values.""" UNIT_CLASS = "blood_glucose_concentration" _UNIT_CONVERSION: dict[str | None, float] = { From 31cfbb8a3e11a870aaef3c06e025ddbaeda6ec7e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:32:06 +0000 Subject: [PATCH 8/8] MILLIMOLE_PER_LITER --- homeassistant/const.py | 2 +- homeassistant/util/unit_conversion.py | 2 +- tests/util/test_unit_conversion.py | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/const.py b/homeassistant/const.py index 41068748fd8ea3..dc20627ed7228b 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1357,7 +1357,7 @@ class UnitOfBloodGlucoseConcentration(StrEnum): """Blood glucose concentration units.""" MILLIGRAMS_PER_DECILITER = "mg/dL" - MILLIMOLS_PER_LITER = "mmol/L" + MILLIMOLE_PER_LITER = "mmol/L" # Speed units diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index b1c2e4cd309c96..b4fe0a4c7f2747 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -180,7 +180,7 @@ class BloodGlugoseConcentrationConverter(BaseUnitConverter): UNIT_CLASS = "blood_glucose_concentration" _UNIT_CONVERSION: dict[str | None, float] = { UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER: 18, - UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER: 1, + UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER: 1, } VALID_UNITS = set(UnitOfBloodGlucoseConcentration) diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index d17ab39f0679cf..65e4aa1e1ba47a 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -85,7 +85,7 @@ _GET_UNIT_RATIO: dict[type[BaseUnitConverter], tuple[str | None, str | None, float]] = { BloodGlugoseConcentrationConverter: ( UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER, - UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER, + UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER, 18, ), ConductivityConverter: ( @@ -143,11 +143,11 @@ 90, UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER, 5, - UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER, + UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER, ), ( 1, - UnitOfBloodGlucoseConcentration.MILLIMOLS_PER_LITER, + UnitOfBloodGlucoseConcentration.MILLIMOLE_PER_LITER, 18, UnitOfBloodGlucoseConcentration.MILLIGRAMS_PER_DECILITER, ),