Allow setting an entity's name by its device class (#90767)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2023-05-09 18:55:55 +02:00 committed by GitHub
parent 337b59ba23
commit 67c1051305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 141 additions and 35 deletions

View file

@ -32,6 +32,7 @@ from homeassistant.const import (
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DEVICE_CLASS_NAME
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify
from homeassistant.util.unit_conversion import DistanceConverter, SpeedConverter
@ -349,6 +350,10 @@ class BaseTomorrowioSensorEntity(TomorrowioEntity, SensorEntity):
"""Initialize Tomorrow.io Sensor Entity."""
super().__init__(config_entry, coordinator, api_version)
self.entity_description = description
# It's not possible to do string manipulations on DEVICE_CLASS_NAME
# the assert satisfies the type checker and will catch attempts
# to use DEVICE_CLASS_NAME in the entity descriptions.
assert description.name is not DEVICE_CLASS_NAME
self._attr_name = f"{self._config_entry.data[CONF_NAME]} - {description.name}"
self._attr_unique_id = (
f"{self._config_entry.unique_id}_{slugify(description.name)}"