Add SensorEntityDescription class (#53357)
This commit is contained in:
parent
88cffc86bb
commit
ee452d415d
6 changed files with 293 additions and 247 deletions
|
@ -2,6 +2,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Any, Final, cast, final
|
||||
|
@ -31,7 +32,7 @@ from homeassistant.helpers.config_validation import ( # noqa: F401
|
|||
PLATFORM_SCHEMA,
|
||||
PLATFORM_SCHEMA_BASE,
|
||||
)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
|
@ -95,21 +96,38 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return await component.async_unload_entry(entry)
|
||||
|
||||
|
||||
@dataclass
|
||||
class SensorEntityDescription(EntityDescription):
|
||||
"""An class that describes sensor entities."""
|
||||
|
||||
state_class: str | None = None
|
||||
last_reset: datetime | None = None
|
||||
|
||||
|
||||
class SensorEntity(Entity):
|
||||
"""Base class for sensor entities."""
|
||||
|
||||
_attr_state_class: str | None = None
|
||||
_attr_last_reset: datetime | None = None
|
||||
entity_description: SensorEntityDescription
|
||||
_attr_state_class: str | None
|
||||
_attr_last_reset: datetime | None
|
||||
|
||||
@property
|
||||
def state_class(self) -> str | None:
|
||||
"""Return the state class of this entity, from STATE_CLASSES, if any."""
|
||||
return self._attr_state_class
|
||||
if hasattr(self, "_attr_state_class"):
|
||||
return self._attr_state_class
|
||||
if hasattr(self, "entity_description"):
|
||||
return self.entity_description.state_class
|
||||
return None
|
||||
|
||||
@property
|
||||
def last_reset(self) -> datetime | None:
|
||||
"""Return the time when the sensor was last reset, if any."""
|
||||
return self._attr_last_reset
|
||||
if hasattr(self, "_attr_last_reset"):
|
||||
return self._attr_last_reset
|
||||
if hasattr(self, "entity_description"):
|
||||
return self.entity_description.last_reset
|
||||
return None
|
||||
|
||||
@property
|
||||
def capability_attributes(self) -> Mapping[str, Any] | None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue