Drop deprecated support for unit_of_measurement from sensor (#69061)

pull/69089/head
Erik Montnemery 2022-04-01 17:22:19 +02:00 committed by GitHub
parent 9b21a48048
commit 87100c2517
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 39 deletions

View File

@ -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):

View File

@ -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)