mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Ensure mqtt sensor has a valid native unit of measurement (#146722)
This commit is contained in:
parent
5487bfe1d9
commit
d01758cea8
@ -35,7 +35,6 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
|
||||
from homeassistant.helpers.typing import ConfigType, VolSchemaType
|
||||
from homeassistant.util import dt as dt_util
|
||||
@ -48,7 +47,6 @@ from .const import (
|
||||
CONF_OPTIONS,
|
||||
CONF_STATE_TOPIC,
|
||||
CONF_SUGGESTED_DISPLAY_PRECISION,
|
||||
DOMAIN,
|
||||
PAYLOAD_NONE,
|
||||
)
|
||||
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
|
||||
device_class in DEVICE_CLASS_UNITS
|
||||
and unit_of_measurement not in DEVICE_CLASS_UNITS[device_class]
|
||||
):
|
||||
_LOGGER.warning(
|
||||
"The unit of measurement `%s` is not valid "
|
||||
"together with device class `%s`. "
|
||||
"this will stop working in HA Core 2025.7.0",
|
||||
unit_of_measurement,
|
||||
device_class,
|
||||
raise vol.Invalid(
|
||||
f"The unit of measurement `{unit_of_measurement}` is not valid "
|
||||
f"together with device class `{device_class}`",
|
||||
)
|
||||
|
||||
return config
|
||||
@ -194,40 +189,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
||||
None
|
||||
)
|
||||
|
||||
@callback
|
||||
def async_check_uom(self) -> None:
|
||||
"""Check if the unit of measurement is valid with the device class."""
|
||||
if (
|
||||
self._discovery_data is not None
|
||||
or self.device_class is None
|
||||
or self.native_unit_of_measurement is None
|
||||
):
|
||||
return
|
||||
if (
|
||||
self.device_class in DEVICE_CLASS_UNITS
|
||||
and self.native_unit_of_measurement
|
||||
not in DEVICE_CLASS_UNITS[self.device_class]
|
||||
):
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
self.entity_id,
|
||||
issue_domain=sensor.DOMAIN,
|
||||
is_fixable=False,
|
||||
severity=IssueSeverity.WARNING,
|
||||
learn_more_url=URL_DOCS_SUPPORTED_SENSOR_UOM,
|
||||
translation_placeholders={
|
||||
"uom": self.native_unit_of_measurement,
|
||||
"device_class": self.device_class.value,
|
||||
"entity_id": self.entity_id,
|
||||
},
|
||||
translation_key="invalid_unit_of_measurement",
|
||||
breaks_in_ha_version="2025.7.0",
|
||||
)
|
||||
|
||||
async def mqtt_async_added_to_hass(self) -> None:
|
||||
"""Restore state for entities with expire_after set."""
|
||||
self.async_check_uom()
|
||||
last_state: State | None
|
||||
last_sensor_data: SensorExtraStoredData | None
|
||||
if (
|
||||
|
@ -3,10 +3,6 @@
|
||||
"invalid_platform_config": {
|
||||
"title": "Invalid config found for MQTT {domain} item",
|
||||
"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."
|
||||
},
|
||||
"invalid_unit_of_measurement": {
|
||||
"title": "Sensor with invalid unit of measurement",
|
||||
"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."
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
|
@ -898,42 +898,12 @@ async def test_invalid_unit_of_measurement(
|
||||
"The unit of measurement `ppm` is not valid together with device class `energy`"
|
||||
in caplog.text
|
||||
)
|
||||
# A repair issue was logged
|
||||
# A repair issue was logged for the failing YAML config
|
||||
assert len(events) == 1
|
||||
assert events[0].data["issue_id"] == "sensor.test"
|
||||
# Assert the sensor works
|
||||
async_fire_mqtt_message(hass, "test-topic", "100")
|
||||
await hass.async_block_till_done()
|
||||
assert events[0].data["domain"] == mqtt.DOMAIN
|
||||
# Assert the sensor is not created
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state is not None
|
||||
assert state.state == "100"
|
||||
|
||||
caplog.clear()
|
||||
|
||||
discovery_payload = {
|
||||
"name": "bla",
|
||||
"state_topic": "test-topic2",
|
||||
"device_class": "temperature",
|
||||
"unit_of_measurement": "C",
|
||||
}
|
||||
# Now discover an other invalid sensor
|
||||
async_fire_mqtt_message(
|
||||
hass, "homeassistant/sensor/bla/config", json.dumps(discovery_payload)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
"The unit of measurement `C` is not valid together with device class `temperature`"
|
||||
in caplog.text
|
||||
)
|
||||
# Assert the sensor works
|
||||
async_fire_mqtt_message(hass, "test-topic2", "21")
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("sensor.bla")
|
||||
assert state is not None
|
||||
assert state.state == "21"
|
||||
|
||||
# No new issue was registered for the discovered entity
|
||||
assert len(events) == 1
|
||||
assert state is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
Loading…
x
Reference in New Issue
Block a user