From 87100c2517af268a9fb738befe29da25e6f256bb Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 1 Apr 2022 17:22:19 +0200 Subject: [PATCH] Drop deprecated support for unit_of_measurement from sensor (#69061) --- homeassistant/components/sensor/__init__.py | 29 --------------------- tests/components/sensor/test_init.py | 11 +------- 2 files changed, 1 insertion(+), 39 deletions(-) diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index b9cb3d94796..56d4ae884fe 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -5,7 +5,6 @@ from collections.abc import Callable, Mapping from contextlib import suppress from dataclasses import dataclass from datetime import date, datetime, timedelta, timezone -import inspect import logging from math import floor, log10 from typing import Any, Final, cast, final @@ -253,27 +252,6 @@ class SensorEntityDescription(EntityDescription): state_class: SensorStateClass | str | None = None unit_of_measurement: None = None # Type override, use native_unit_of_measurement - def __post_init__(self) -> None: - """Post initialisation processing.""" - if self.unit_of_measurement: - caller = inspect.stack()[2] # type: ignore[unreachable] - module = inspect.getmodule(caller[0]) - if "custom_components" in module.__file__: - report_issue = "report it to the custom component author." - else: - report_issue = ( - "create a bug report at " - "https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue" - ) - _LOGGER.warning( - "%s is setting 'unit_of_measurement' on an instance of " - "SensorEntityDescription, this is not valid and will be unsupported " - "from Home Assistant 2021.11. Please %s", - module.__name__, - report_issue, - ) - self.native_unit_of_measurement = self.unit_of_measurement - class SensorEntity(Entity): """Base class for sensor entities.""" @@ -387,13 +365,6 @@ class SensorEntity(Entity): if self._sensor_option_unit_of_measurement: return self._sensor_option_unit_of_measurement - # Support for _attr_unit_of_measurement will be removed in Home Assistant 2021.11 - if ( - hasattr(self, "_attr_unit_of_measurement") - and self._attr_unit_of_measurement is not None - ): - return self._attr_unit_of_measurement # type: ignore[unreachable] - native_unit_of_measurement = self.native_unit_of_measurement if native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT): diff --git a/tests/components/sensor/test_init.py b/tests/components/sensor/test_init.py index 950c737375b..544e85b04ef 100644 --- a/tests/components/sensor/test_init.py +++ b/tests/components/sensor/test_init.py @@ -4,7 +4,7 @@ from datetime import date, datetime, timezone import pytest from pytest import approx -from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription +from homeassistant.components.sensor import SensorDeviceClass from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, PRESSURE_HPA, @@ -116,15 +116,6 @@ async def test_deprecated_last_reset( assert "last_reset" not in state.attributes -async def test_deprecated_unit_of_measurement(hass, caplog, enable_custom_integrations): - """Test warning on deprecated unit_of_measurement.""" - SensorEntityDescription("catsensor", unit_of_measurement="cats") - assert ( - "tests.components.sensor.test_init is setting 'unit_of_measurement' on an " - "instance of SensorEntityDescription" - ) in caplog.text - - async def test_datetime_conversion(hass, caplog, enable_custom_integrations): """Test conversion of datetime.""" test_timestamp = datetime(2017, 12, 19, 18, 29, 42, tzinfo=timezone.utc)