diff --git a/homeassistant/components/mqtt/sensor.py b/homeassistant/components/mqtt/sensor.py index d7ac3d9474a..067dfcd8154 100644 --- a/homeassistant/components/mqtt/sensor.py +++ b/homeassistant/components/mqtt/sensor.py @@ -128,15 +128,14 @@ def validate_sensor_state_and_device_class_config(config: ConfigType) -> ConfigT f"together with state class '{state_class}'" ) - if (device_class := config.get(CONF_DEVICE_CLASS)) is None or ( - unit_of_measurement := config.get(CONF_UNIT_OF_MEASUREMENT) - ) in EQUIVALENT_UNITS: - unit_of_measurement = EQUIVALENT_UNITS[unit_of_measurement] - config[CONF_UNIT_OF_MEASUREMENT] = unit_of_measurement + if (unit_of_measurement := config.get(CONF_UNIT_OF_MEASUREMENT)) is None: + return config - if ( - device_class := config.get(CONF_DEVICE_CLASS) - ) is None or unit_of_measurement is None: + config[CONF_UNIT_OF_MEASUREMENT] = EQUIVALENT_UNITS.get( + unit_of_measurement, unit_of_measurement + ) + + if (device_class := config.get(CONF_DEVICE_CLASS)) is None: return config if ( diff --git a/tests/components/openweathermap/snapshots/test_sensor.ambr b/tests/components/openweathermap/snapshots/test_sensor.ambr index 11a1feb721f..955af9dcf39 100644 --- a/tests/components/openweathermap/snapshots/test_sensor.ambr +++ b/tests/components/openweathermap/snapshots/test_sensor.ambr @@ -140,7 +140,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-no2', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_nitrogen_dioxide-state] @@ -150,7 +150,7 @@ 'device_class': 'nitrogen_dioxide', 'friendly_name': 'openweathermap Nitrogen dioxide', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_nitrogen_dioxide', @@ -194,7 +194,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-no', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_nitrogen_monoxide-state] @@ -204,7 +204,7 @@ 'device_class': 'nitrogen_monoxide', 'friendly_name': 'openweathermap Nitrogen monoxide', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_nitrogen_monoxide', @@ -248,7 +248,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-o3', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_ozone-state] @@ -258,7 +258,7 @@ 'device_class': 'ozone', 'friendly_name': 'openweathermap Ozone', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_ozone', @@ -302,7 +302,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-pm10', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_pm10-state] @@ -312,7 +312,7 @@ 'device_class': 'pm10', 'friendly_name': 'openweathermap PM10', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_pm10', @@ -356,7 +356,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-pm2_5', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_pm2_5-state] @@ -366,7 +366,7 @@ 'device_class': 'pm25', 'friendly_name': 'openweathermap PM2.5', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_pm2_5', @@ -410,7 +410,7 @@ 'supported_features': 0, 'translation_key': None, 'unique_id': '12.34-56.78-so2', - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }) # --- # name: test_sensor_states[air_pollution][sensor.openweathermap_sulphur_dioxide-state] @@ -420,7 +420,7 @@ 'device_class': 'sulphur_dioxide', 'friendly_name': 'openweathermap Sulphur dioxide', 'state_class': , - 'unit_of_measurement': 'µg/m³', + 'unit_of_measurement': 'μg/m³', }), 'context': , 'entity_id': 'sensor.openweathermap_sulphur_dioxide',