mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 00:27:19 +00:00
Remove deprecated use of incorrect UoM with device class in mqtt sensor
This commit is contained in:
parent
ed9503324d
commit
7411e54b2c
@ -138,12 +138,9 @@ def validate_sensor_state_and_device_class_config(config: ConfigType) -> ConfigT
|
|||||||
device_class in DEVICE_CLASS_UNITS
|
device_class in DEVICE_CLASS_UNITS
|
||||||
and unit_of_measurement not in DEVICE_CLASS_UNITS[device_class]
|
and unit_of_measurement not in DEVICE_CLASS_UNITS[device_class]
|
||||||
):
|
):
|
||||||
_LOGGER.warning(
|
raise vol.Invalid(
|
||||||
"The unit of measurement `%s` is not valid "
|
f"The unit of measurement '{unit_of_measurement}' is not valid "
|
||||||
"together with device class `%s`. "
|
f"together with device class '{device_class}'"
|
||||||
"this will stop working in HA Core 2025.7.0",
|
|
||||||
unit_of_measurement,
|
|
||||||
device_class,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
@ -21,7 +21,7 @@ from homeassistant.const import (
|
|||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, State, callback
|
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.helpers.typing import ConfigType
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
@ -71,7 +71,6 @@ from .common import (
|
|||||||
|
|
||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
async_capture_events,
|
|
||||||
async_fire_mqtt_message,
|
async_fire_mqtt_message,
|
||||||
async_fire_time_changed,
|
async_fire_time_changed,
|
||||||
mock_restore_cache_with_extra_data,
|
mock_restore_cache_with_extra_data,
|
||||||
@ -892,48 +891,11 @@ async def test_invalid_unit_of_measurement(
|
|||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test device_class with invalid unit of measurement."""
|
"""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 await mqtt_mock_entry()
|
||||||
assert (
|
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
|
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(
|
@pytest.mark.parametrize(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user