Skip to content

Commit d01758c

Browse files
authored
Ensure mqtt sensor has a valid native unit of measurement (home-assistant#146722)
1 parent 5487bfe commit d01758c

File tree

3 files changed

+7
-78
lines changed

3 files changed

+7
-78
lines changed

homeassistant/components/mqtt/sensor.py

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from homeassistant.helpers import config_validation as cv
3636
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
3737
from homeassistant.helpers.event import async_call_later
38-
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
3938
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
4039
from homeassistant.helpers.typing import ConfigType, VolSchemaType
4140
from homeassistant.util import dt as dt_util
@@ -48,7 +47,6 @@
4847
CONF_OPTIONS,
4948
CONF_STATE_TOPIC,
5049
CONF_SUGGESTED_DISPLAY_PRECISION,
51-
DOMAIN,
5250
PAYLOAD_NONE,
5351
)
5452
from .entity import MqttAvailabilityMixin, MqttEntity, async_setup_entity_entry_helper
@@ -138,12 +136,9 @@ def validate_sensor_state_and_device_class_config(config: ConfigType) -> ConfigT
138136
device_class in DEVICE_CLASS_UNITS
139137
and unit_of_measurement not in DEVICE_CLASS_UNITS[device_class]
140138
):
141-
_LOGGER.warning(
142-
"The unit of measurement `%s` is not valid "
143-
"together with device class `%s`. "
144-
"this will stop working in HA Core 2025.7.0",
145-
unit_of_measurement,
146-
device_class,
139+
raise vol.Invalid(
140+
f"The unit of measurement `{unit_of_measurement}` is not valid "
141+
f"together with device class `{device_class}`",
147142
)
148143

149144
return config
@@ -194,40 +189,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
194189
None
195190
)
196191

197-
@callback
198-
def async_check_uom(self) -> None:
199-
"""Check if the unit of measurement is valid with the device class."""
200-
if (
201-
self._discovery_data is not None
202-
or self.device_class is None
203-
or self.native_unit_of_measurement is None
204-
):
205-
return
206-
if (
207-
self.device_class in DEVICE_CLASS_UNITS
208-
and self.native_unit_of_measurement
209-
not in DEVICE_CLASS_UNITS[self.device_class]
210-
):
211-
async_create_issue(
212-
self.hass,
213-
DOMAIN,
214-
self.entity_id,
215-
issue_domain=sensor.DOMAIN,
216-
is_fixable=False,
217-
severity=IssueSeverity.WARNING,
218-
learn_more_url=URL_DOCS_SUPPORTED_SENSOR_UOM,
219-
translation_placeholders={
220-
"uom": self.native_unit_of_measurement,
221-
"device_class": self.device_class.value,
222-
"entity_id": self.entity_id,
223-
},
224-
translation_key="invalid_unit_of_measurement",
225-
breaks_in_ha_version="2025.7.0",
226-
)
227-
228192
async def mqtt_async_added_to_hass(self) -> None:
229193
"""Restore state for entities with expire_after set."""
230-
self.async_check_uom()
231194
last_state: State | None
232195
last_sensor_data: SensorExtraStoredData | None
233196
if (

homeassistant/components/mqtt/strings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
"invalid_platform_config": {
44
"title": "Invalid config found for MQTT {domain} item",
55
"description": "Home Assistant detected an invalid config for a manually configured item.\n\nPlatform domain: **{domain}**\nConfiguration file: **{config_file}**\nNear line: **{line}**\nConfiguration found:\n```yaml\n{config}\n```\nError: **{error}**.\n\nMake sure the configuration is valid and [reload](/developer-tools/yaml) the manually configured MQTT items or restart Home Assistant to fix this issue."
6-
},
7-
"invalid_unit_of_measurement": {
8-
"title": "Sensor with invalid unit of measurement",
9-
"description": "Manual configured Sensor entity **{entity_id}** has a configured unit of measurement **{uom}** which is not valid with configured device class **{device_class}**. Make sure a valid unit of measurement is configured or remove the device class, and [reload](/developer-tools/yaml) the manually configured MQTT items or restart Home Assistant to fix this issue."
106
}
117
},
128
"config": {

tests/components/mqtt/test_sensor.py

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -898,42 +898,12 @@ async def test_invalid_unit_of_measurement(
898898
"The unit of measurement `ppm` is not valid together with device class `energy`"
899899
in caplog.text
900900
)
901-
# A repair issue was logged
901+
# A repair issue was logged for the failing YAML config
902902
assert len(events) == 1
903-
assert events[0].data["issue_id"] == "sensor.test"
904-
# Assert the sensor works
905-
async_fire_mqtt_message(hass, "test-topic", "100")
906-
await hass.async_block_till_done()
903+
assert events[0].data["domain"] == mqtt.DOMAIN
904+
# Assert the sensor is not created
907905
state = hass.states.get("sensor.test")
908-
assert state is not None
909-
assert state.state == "100"
910-
911-
caplog.clear()
912-
913-
discovery_payload = {
914-
"name": "bla",
915-
"state_topic": "test-topic2",
916-
"device_class": "temperature",
917-
"unit_of_measurement": "C",
918-
}
919-
# Now discover an other invalid sensor
920-
async_fire_mqtt_message(
921-
hass, "homeassistant/sensor/bla/config", json.dumps(discovery_payload)
922-
)
923-
await hass.async_block_till_done()
924-
assert (
925-
"The unit of measurement `C` is not valid together with device class `temperature`"
926-
in caplog.text
927-
)
928-
# Assert the sensor works
929-
async_fire_mqtt_message(hass, "test-topic2", "21")
930-
await hass.async_block_till_done()
931-
state = hass.states.get("sensor.bla")
932-
assert state is not None
933-
assert state.state == "21"
934-
935-
# No new issue was registered for the discovered entity
936-
assert len(events) == 1
906+
assert state is None
937907

938908

939909
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)