Remove deprecated use of incorrect UoM with device class in mqtt sensor

This commit is contained in:
G Johansson 2025-06-17 16:37:47 +00:00
parent ed9503324d
commit 7411e54b2c
2 changed files with 5 additions and 46 deletions

View File

@ -138,12 +138,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

View File

@ -21,7 +21,7 @@ from homeassistant.const import (
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant, State, callback
from homeassistant.helpers import device_registry as dr, issue_registry as ir
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import dt as dt_util
@ -71,7 +71,6 @@ from .common import (
from tests.common import (
MockConfigEntry,
async_capture_events,
async_fire_mqtt_message,
async_fire_time_changed,
mock_restore_cache_with_extra_data,
@ -892,48 +891,11 @@ async def test_invalid_unit_of_measurement(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test device_class with invalid unit of measurement."""
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
assert await mqtt_mock_entry()
assert (
"The unit of measurement `ppm` is not valid together with device class `energy`"
"The unit of measurement 'ppm' is not valid together with device class 'energy'"
in caplog.text
)
# A repair issue was logged
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()
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
@pytest.mark.parametrize(