From d319440508aec68cc68551a7b1c9a4d099466bbc Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sat, 27 Jun 2026 23:27:15 +0200 Subject: [PATCH] Migrate openweathermap to UnitOfDensity / UnitOfRatio enums (#175021) --- .../components/openweathermap/sensor.py | 22 +++++----- .../openweathermap/snapshots/test_sensor.ambr | 44 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/openweathermap/sensor.py b/homeassistant/components/openweathermap/sensor.py index af3339696b3c..10d61a45f5c0 100644 --- a/homeassistant/components/openweathermap/sensor.py +++ b/homeassistant/components/openweathermap/sensor.py @@ -9,12 +9,12 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.const import ( - CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, DEGREE, - PERCENTAGE, UV_INDEX, + UnitOfDensity, UnitOfLength, UnitOfPressure, + UnitOfRatio, UnitOfSpeed, UnitOfTemperature, UnitOfVolumetricFlux, @@ -107,7 +107,7 @@ WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( ), SensorEntityDescription( key=ATTR_API_HUMIDITY, - native_unit_of_measurement=PERCENTAGE, + native_unit_of_measurement=UnitOfRatio.PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, state_class=SensorStateClass.MEASUREMENT, ), @@ -121,7 +121,7 @@ WEATHER_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key=ATTR_API_CLOUDS, translation_key=ATTR_API_CLOUDS, - native_unit_of_measurement=PERCENTAGE, + native_unit_of_measurement=UnitOfRatio.PERCENTAGE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( @@ -174,43 +174,43 @@ AIRPOLLUTION_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_CO, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.CO, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_NO, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.NITROGEN_MONOXIDE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_NO2, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.NITROGEN_DIOXIDE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_O3, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.OZONE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_SO2, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.SULPHUR_DIOXIDE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_PM2_5, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM25, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key=ATTR_API_AIRPOLLUTION_PM10, - native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, + native_unit_of_measurement=UnitOfDensity.MICROGRAMS_PER_CUBIC_METER, device_class=SensorDeviceClass.PM10, state_class=SensorStateClass.MEASUREMENT, ), diff --git a/tests/components/openweathermap/snapshots/test_sensor.ambr b/tests/components/openweathermap/snapshots/test_sensor.ambr index 45ff321d2127..d4c07e43cf0b 100644 --- a/tests/components/openweathermap/snapshots/test_sensor.ambr +++ b/tests/components/openweathermap/snapshots/test_sensor.ambr @@ -90,7 +90,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-co', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_carbon_monoxide-state] @@ -100,7 +100,7 @@ : 'carbon_monoxide', : 'OpenWeatherMap Carbon monoxide', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_carbon_monoxide', @@ -146,7 +146,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-no2', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_nitrogen_dioxide-state] @@ -156,7 +156,7 @@ : 'nitrogen_dioxide', : 'OpenWeatherMap Nitrogen dioxide', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_nitrogen_dioxide', @@ -202,7 +202,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-no', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_nitrogen_monoxide-state] @@ -212,7 +212,7 @@ : 'nitrogen_monoxide', : 'OpenWeatherMap Nitrogen monoxide', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_nitrogen_monoxide', @@ -258,7 +258,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-o3', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_ozone-state] @@ -268,7 +268,7 @@ : 'ozone', : 'OpenWeatherMap Ozone', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_ozone', @@ -314,7 +314,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-pm10', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_pm10-state] @@ -324,7 +324,7 @@ : 'pm10', : 'OpenWeatherMap PM10', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_pm10', @@ -370,7 +370,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-pm2_5', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_pm2_5-state] @@ -380,7 +380,7 @@ : 'pm25', : 'OpenWeatherMap PM2.5', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_pm2_5', @@ -426,7 +426,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-so2', - 'unit_of_measurement': 'μg/m³', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_sulphur_dioxide-state] @@ -436,7 +436,7 @@ : 'sulphur_dioxide', : 'OpenWeatherMap Sulphur dioxide', : , - : 'μg/m³', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_sulphur_dioxide', @@ -541,7 +541,7 @@ 'supported_features': 0, 'translation_key': 'clouds', 'unique_id': '12.34-56.78-clouds', - 'unit_of_measurement': '%', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[current][sensor.openweathermap_cloud_coverage-state] @@ -550,7 +550,7 @@ : 'Data provided by OpenWeatherMap', : 'OpenWeatherMap Cloud coverage', : , - : '%', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_cloud_coverage', @@ -706,7 +706,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-humidity', - 'unit_of_measurement': '%', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[current][sensor.openweathermap_humidity-state] @@ -716,7 +716,7 @@ : 'humidity', : 'OpenWeatherMap Humidity', : , - : '%', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_humidity', @@ -1504,7 +1504,7 @@ 'supported_features': 0, 'translation_key': 'clouds', 'unique_id': '12.34-56.78-clouds', - 'unit_of_measurement': '%', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[v3.0][sensor.openweathermap_cloud_coverage-state] @@ -1513,7 +1513,7 @@ : 'Data provided by OpenWeatherMap', : 'OpenWeatherMap Cloud coverage', : , - : '%', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_cloud_coverage', @@ -1669,7 +1669,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-humidity', - 'unit_of_measurement': '%', + 'unit_of_measurement': , }) # --- # name: test_sensor_states[v3.0][sensor.openweathermap_humidity-state] @@ -1679,7 +1679,7 @@ : 'humidity', : 'OpenWeatherMap Humidity', : , - : '%', + : , }), 'context': , 'entity_id': 'sensor.openweathermap_humidity',