diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index 689be1100f6..8d81df6431f 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -383,15 +383,9 @@ class SensorEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): ): if not self._invalid_suggested_unit_of_measurement_reported: self._invalid_suggested_unit_of_measurement_reported = True - report_issue = self._suggest_report_issue() - # This should raise in Home Assistant Core 2024.5 - _LOGGER.warning( - ( - "%s sets an invalid suggested_unit_of_measurement. Please %s. " - "This warning will become an error in Home Assistant Core 2024.5" - ), - type(self), - report_issue, + raise ValueError( + f"Entity {type(self)} suggest an incorrect " + f"unit of measurement: {suggested_unit_of_measurement}." ) return False diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 0aa0ff3de85..126e327f364 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -4,7 +4,6 @@ from __future__ import annotations from datetime import UTC, date, datetime from decimal import Decimal -import logging from types import ModuleType from typing import Any @@ -2634,25 +2633,13 @@ async def test_suggested_unit_guard_invalid_unit( assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}}) await hass.async_block_till_done() - # Unit of measurement should be native one - state = hass.states.get(entity.entity_id) - assert int(state.state) == state_value - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit + assert not hass.states.get("sensor.invalid") + assert not entity_registry.async_get("sensor.invalid") - # Assert the suggested unit is ignored and not stored in the entity registry - entry = entity_registry.async_get(entity.entity_id) - assert entry.unit_of_measurement == native_unit - assert entry.options == {} assert ( - "homeassistant.components.sensor", - logging.WARNING, - ( - " sets an" - " invalid suggested_unit_of_measurement. Please create a bug report at " - "https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test%22." - " This warning will become an error in Home Assistant Core 2024.5" - ), - ) in caplog.record_tuples + "Entity suggest an incorrect unit of measurement: invalid_unit" + in caplog.text + ) @pytest.mark.parametrize(