Add support for external statistics (#56607)
* Support external statistics * Update tests * Lint * Adjust code after rebase * Separate external statistic_id with :, add name to metadata * Adjust tests * Simplify get_metadata_with_session * Address review comments * Allow updating external statistics * Validate input * Adjust tests after rebase * Pylint * Adjust tests * Improve test coverage
This commit is contained in:
parent
2f346a8048
commit
f594bc353b
10 changed files with 634 additions and 111 deletions
|
@ -1,7 +1,6 @@
|
|||
"""Models for SQLAlchemy."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
import logging
|
||||
|
@ -41,7 +40,7 @@ import homeassistant.util.dt as dt_util
|
|||
# pylint: disable=invalid-name
|
||||
Base = declarative_base()
|
||||
|
||||
SCHEMA_VERSION = 22
|
||||
SCHEMA_VERSION = 23
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -231,7 +230,7 @@ class StatisticResult(TypedDict):
|
|||
"""
|
||||
|
||||
meta: StatisticMetaData
|
||||
stat: Iterable[StatisticData]
|
||||
stat: StatisticData
|
||||
|
||||
|
||||
class StatisticDataBase(TypedDict):
|
||||
|
@ -310,10 +309,12 @@ class StatisticsShortTerm(Base, StatisticsBase): # type: ignore
|
|||
class StatisticMetaData(TypedDict):
|
||||
"""Statistic meta data class."""
|
||||
|
||||
statistic_id: str
|
||||
unit_of_measurement: str | None
|
||||
has_mean: bool
|
||||
has_sum: bool
|
||||
name: str | None
|
||||
source: str
|
||||
statistic_id: str
|
||||
unit_of_measurement: str | None
|
||||
|
||||
|
||||
class StatisticsMeta(Base): # type: ignore
|
||||
|
@ -329,23 +330,12 @@ class StatisticsMeta(Base): # type: ignore
|
|||
unit_of_measurement = Column(String(255))
|
||||
has_mean = Column(Boolean)
|
||||
has_sum = Column(Boolean)
|
||||
name = Column(String(255))
|
||||
|
||||
@staticmethod
|
||||
def from_meta(
|
||||
source: str,
|
||||
statistic_id: str,
|
||||
unit_of_measurement: str | None,
|
||||
has_mean: bool,
|
||||
has_sum: bool,
|
||||
) -> StatisticsMeta:
|
||||
def from_meta(meta: StatisticMetaData) -> StatisticsMeta:
|
||||
"""Create object from meta data."""
|
||||
return StatisticsMeta(
|
||||
source=source,
|
||||
statistic_id=statistic_id,
|
||||
unit_of_measurement=unit_of_measurement,
|
||||
has_mean=has_mean,
|
||||
has_sum=has_sum,
|
||||
)
|
||||
return StatisticsMeta(**meta)
|
||||
|
||||
|
||||
class RecorderRuns(Base): # type: ignore
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue