Warn if unit_of_measurement is set on instances of SensorEntityDescription (#54867)
* Add class BaseEntityDescription without unit_of_measurement * Refactor according to review comments * Tweak * Fix offending integrations * Fix offending integrations
This commit is contained in:
parent
e134246cbd
commit
2fa07777cd
6 changed files with 57 additions and 29 deletions
|
@ -5,6 +5,7 @@ from collections.abc import Mapping
|
|||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
import inspect
|
||||
import logging
|
||||
from typing import Any, Final, cast, final
|
||||
|
||||
|
@ -128,9 +129,31 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
class SensorEntityDescription(EntityDescription):
|
||||
"""A class that describes sensor entities."""
|
||||
|
||||
state_class: str | None = None
|
||||
last_reset: datetime | None = None # Deprecated, to be removed in 2021.11
|
||||
native_unit_of_measurement: str | None = None
|
||||
state_class: 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):
|
||||
|
@ -220,11 +243,6 @@ class SensorEntity(Entity):
|
|||
and self._attr_unit_of_measurement is not None
|
||||
):
|
||||
return self._attr_unit_of_measurement
|
||||
if (
|
||||
hasattr(self, "entity_description")
|
||||
and self.entity_description.unit_of_measurement is not None
|
||||
):
|
||||
return self.entity_description.unit_of_measurement
|
||||
|
||||
native_unit_of_measurement = self.native_unit_of_measurement
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue