Home Assistant Unofficial Reference 2024.12.1
statistics.py
Go to the documentation of this file.
1 """Models for statistics in the Recorder."""
2 
3 from __future__ import annotations
4 
5 from datetime import datetime, timedelta
6 from typing import Literal, TypedDict
7 
8 
9 class StatisticResult(TypedDict):
10  """Statistic result data class.
11 
12  Allows multiple datapoints for the same statistic_id.
13  """
14 
15  meta: StatisticMetaData
16  stat: StatisticData
17 
18 
19 class StatisticDataTimestampBase(TypedDict):
20  """Mandatory fields for statistic data class with a timestamp."""
21 
22  start_ts: float
23 
24 
25 class StatisticDataBase(TypedDict):
26  """Mandatory fields for statistic data class."""
27 
28  start: datetime
29 
30 
31 class StatisticMixIn(TypedDict, total=False):
32  """Mandatory fields for statistic data class."""
33 
34  state: float
35  sum: float
36  min: float
37  max: float
38  mean: float
39 
40 
42  """Statistic data class."""
43 
44  last_reset: datetime | None
45 
46 
48  """Statistic data class with a timestamp."""
49 
50  last_reset_ts: float | None
51 
52 
53 class StatisticMetaData(TypedDict):
54  """Statistic meta data class."""
55 
56  has_mean: bool
57  has_sum: bool
58  name: str | None
59  source: str
60  statistic_id: str
61  unit_of_measurement: str | None
62 
63 
64 class CalendarStatisticPeriod(TypedDict, total=False):
65  """Statistic period definition."""
66 
67  period: Literal["hour", "day", "week", "month", "year"]
68  offset: int
69 
70 
71 class FixedStatisticPeriod(TypedDict, total=False):
72  """Statistic period definition."""
73 
74  end_time: datetime
75  start_time: datetime
76 
77 
78 class RollingWindowStatisticPeriod(TypedDict, total=False):
79  """Statistic period definition."""
80 
81  duration: timedelta
82  offset: timedelta
83 
84 
85 class StatisticPeriod(TypedDict, total=False):
86  """Statistic period definition."""
87 
88  calendar: CalendarStatisticPeriod
89  fixed_period: FixedStatisticPeriod
90  rolling_window: RollingWindowStatisticPeriod