mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Use device class duration for history_stats sensors (#70720)
This commit is contained in:
parent
a5fa40180c
commit
1c3203abda
@ -72,19 +72,6 @@ def async_calculate_period(
|
||||
return start, end
|
||||
|
||||
|
||||
def pretty_duration(hours: float) -> str:
|
||||
"""Format a duration in days, hours, minutes, seconds."""
|
||||
seconds = int(3600 * hours)
|
||||
days, seconds = divmod(seconds, 86400)
|
||||
hours, seconds = divmod(seconds, 3600)
|
||||
minutes, seconds = divmod(seconds, 60)
|
||||
if days > 0:
|
||||
return "%dd %dh %dm" % (days, hours, minutes)
|
||||
if hours > 0:
|
||||
return "%dh %dm" % (hours, minutes)
|
||||
return "%dm" % minutes
|
||||
|
||||
|
||||
def pretty_ratio(
|
||||
value: float, period: tuple[datetime.datetime, datetime.datetime]
|
||||
) -> float:
|
||||
|
@ -6,7 +6,12 @@ import datetime
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
@ -26,7 +31,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from . import DOMAIN, PLATFORMS
|
||||
from .coordinator import HistoryStatsUpdateCoordinator
|
||||
from .data import HistoryStats
|
||||
from .helpers import pretty_duration, pretty_ratio
|
||||
from .helpers import pretty_ratio
|
||||
|
||||
CONF_START = "start"
|
||||
CONF_END = "end"
|
||||
@ -46,8 +51,6 @@ UNITS: dict[str, str] = {
|
||||
}
|
||||
ICON = "mdi:chart-line"
|
||||
|
||||
ATTR_VALUE = "value"
|
||||
|
||||
|
||||
def exactly_two_period_keys(conf):
|
||||
"""Ensure exactly 2 of CONF_PERIOD_KEYS are provided."""
|
||||
@ -136,6 +139,9 @@ class HistoryStatsSensorBase(
|
||||
class HistoryStatsSensor(HistoryStatsSensorBase):
|
||||
"""A HistoryStats sensor."""
|
||||
|
||||
_attr_device_class = SensorDeviceClass.DURATION
|
||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: HistoryStatsUpdateCoordinator,
|
||||
@ -153,7 +159,6 @@ class HistoryStatsSensor(HistoryStatsSensorBase):
|
||||
state = self.coordinator.data
|
||||
if state is None or state.hours_matched is None:
|
||||
self._attr_native_value = None
|
||||
self._attr_extra_state_attributes = {}
|
||||
return
|
||||
|
||||
if self._type == CONF_TYPE_TIME:
|
||||
@ -162,6 +167,3 @@ class HistoryStatsSensor(HistoryStatsSensorBase):
|
||||
self._attr_native_value = pretty_ratio(state.hours_matched, state.period)
|
||||
elif self._type == CONF_TYPE_COUNT:
|
||||
self._attr_native_value = state.changes_to_match_state
|
||||
self._attr_extra_state_attributes = {
|
||||
ATTR_VALUE: pretty_duration(state.hours_matched)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user