Hoist ATTR_LAST_RESET from utility_meter to SensorEntity (#50757)

This commit is contained in:
Erik Montnemery 2021-05-17 13:51:09 +02:00 committed by GitHub
parent a9c73ac264
commit ee4e14e45e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 16 deletions

View file

@ -2,9 +2,9 @@
from __future__ import annotations
from collections.abc import Mapping
from datetime import timedelta
from datetime import datetime, timedelta
import logging
from typing import Any, cast
from typing import Any, cast, final
import voluptuous as vol
@ -36,6 +36,7 @@ from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)
ATTR_LAST_RESET = "last_reset"
ATTR_STATE_CLASS = "state_class"
DOMAIN = "sensor"
@ -100,10 +101,24 @@ class SensorEntity(Entity):
"""Return the state class of this entity, from STATE_CLASSES, if any."""
return None
@property
def last_reset(self) -> datetime | None:
"""Return the time when the sensor was last reset, if any."""
return None
@property
def capability_attributes(self) -> Mapping[str, Any] | None:
"""Return the capability attributes."""
if self.state_class:
return {ATTR_STATE_CLASS: self.state_class}
if state_class := self.state_class:
return {ATTR_STATE_CLASS: state_class}
return None
@final
@property
def state_attributes(self) -> dict[str, Any] | None:
"""Return state attributes."""
if last_reset := self.last_reset:
return {ATTR_LAST_RESET: last_reset}
return None