Add SensorEntityDescription class (#53357)

This commit is contained in:
Franck Nijhof 2021-07-26 22:00:43 +02:00 committed by GitHub
parent 88cffc86bb
commit ee452d415d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 293 additions and 247 deletions

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from abc import ABC
import asyncio
from collections.abc import Awaitable, Iterable, Mapping, MutableMapping
from dataclasses import dataclass
from datetime import datetime, timedelta
import functools as ft
import logging
@ -178,6 +179,21 @@ class DeviceInfo(TypedDict, total=False):
default_model: str
@dataclass
class EntityDescription:
"""An class that describes Home Assistant entities."""
# This is the key identifier for this entity
key: str
device_class: str | None = None
entity_registry_enabled_default: bool = True
force_update: bool = False
icon: str | None = None
name: str | None = None
unit_of_measurement: str | None = None
class Entity(ABC):
"""An abstract class for Home Assistant entities."""
@ -194,6 +210,9 @@ class Entity(ABC):
# Owning platform instance. Will be set by EntityPlatform
platform: EntityPlatform | None = None
# Entity description instance for this Entity
entity_description: EntityDescription
# If we reported if this entity was slow
_slow_reported = False
@ -223,19 +242,19 @@ class Entity(ABC):
_attr_assumed_state: bool = False
_attr_available: bool = True
_attr_context_recent_time: timedelta = timedelta(seconds=5)
_attr_device_class: str | None = None
_attr_device_class: str | None
_attr_device_info: DeviceInfo | None = None
_attr_entity_picture: str | None = None
_attr_entity_registry_enabled_default: bool = True
_attr_entity_registry_enabled_default: bool
_attr_extra_state_attributes: MutableMapping[str, Any] | None = None
_attr_force_update: bool = False
_attr_icon: str | None = None
_attr_name: str | None = None
_attr_force_update: bool
_attr_icon: str | None
_attr_name: str | None
_attr_should_poll: bool = True
_attr_state: StateType = STATE_UNKNOWN
_attr_supported_features: int | None = None
_attr_unique_id: str | None = None
_attr_unit_of_measurement: str | None = None
_attr_unit_of_measurement: str | None
@property
def should_poll(self) -> bool:
@ -253,7 +272,11 @@ class Entity(ABC):
@property
def name(self) -> str | None:
"""Return the name of the entity."""
return self._attr_name
if hasattr(self, "_attr_name"):
return self._attr_name
if hasattr(self, "entity_description"):
return self.entity_description.name
return None
@property
def state(self) -> StateType:
@ -309,17 +332,29 @@ class Entity(ABC):
@property
def device_class(self) -> str | None:
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._attr_device_class
if hasattr(self, "_attr_device_class"):
return self._attr_device_class
if hasattr(self, "entity_description"):
return self.entity_description.device_class
return None
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of this entity, if any."""
return self._attr_unit_of_measurement
if hasattr(self, "_attr_unit_of_measurement"):
return self._attr_unit_of_measurement
if hasattr(self, "entity_description"):
return self.entity_description.unit_of_measurement
return None
@property
def icon(self) -> str | None:
"""Return the icon to use in the frontend, if any."""
return self._attr_icon
if hasattr(self, "_attr_icon"):
return self._attr_icon
if hasattr(self, "entity_description"):
return self.entity_description.icon
return None
@property
def entity_picture(self) -> str | None:
@ -343,7 +378,11 @@ class Entity(ABC):
If True, a state change will be triggered anytime the state property is
updated, not just when the value changes.
"""
return self._attr_force_update
if hasattr(self, "_attr_force_update"):
return self._attr_force_update
if hasattr(self, "entity_description"):
return self.entity_description.force_update
return False
@property
def supported_features(self) -> int | None:
@ -358,7 +397,11 @@ class Entity(ABC):
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._attr_entity_registry_enabled_default
if hasattr(self, "_attr_entity_registry_enabled_default"):
return self._attr_entity_registry_enabled_default
if hasattr(self, "entity_description"):
return self.entity_description.entity_registry_enabled_default
return True
# DO NOT OVERWRITE
# These properties and methods are either managed by Home Assistant or they