2022-06-07 12:41:43 +00:00
|
|
|
"""Models for SQLAlchemy."""
|
2022-05-24 13:34:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
from collections.abc import Callable
|
2022-05-24 13:34:46 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import logging
|
2022-07-20 02:02:03 +00:00
|
|
|
from typing import Any, TypeVar, cast
|
2022-05-24 13:34:46 +00:00
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
import ciso8601
|
2022-05-24 13:34:46 +00:00
|
|
|
from fnvhash import fnv1a_32
|
|
|
|
from sqlalchemy import (
|
2022-06-07 12:41:43 +00:00
|
|
|
JSON,
|
2022-05-24 13:34:46 +00:00
|
|
|
BigInteger,
|
|
|
|
Boolean,
|
|
|
|
Column,
|
|
|
|
DateTime,
|
|
|
|
Float,
|
|
|
|
ForeignKey,
|
|
|
|
Identity,
|
|
|
|
Index,
|
|
|
|
Integer,
|
|
|
|
SmallInteger,
|
|
|
|
String,
|
|
|
|
Text,
|
|
|
|
distinct,
|
2022-06-07 12:41:43 +00:00
|
|
|
type_coerce,
|
2022-05-24 13:34:46 +00:00
|
|
|
)
|
2022-06-07 12:41:43 +00:00
|
|
|
from sqlalchemy.dialects import mysql, oracle, postgresql, sqlite
|
2022-05-24 13:34:46 +00:00
|
|
|
from sqlalchemy.ext.declarative import declared_attr
|
2022-06-07 12:41:43 +00:00
|
|
|
from sqlalchemy.orm import aliased, declarative_base, relationship
|
2022-05-24 13:34:46 +00:00
|
|
|
from sqlalchemy.orm.session import Session
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
MAX_LENGTH_EVENT_CONTEXT_ID,
|
|
|
|
MAX_LENGTH_EVENT_EVENT_TYPE,
|
|
|
|
MAX_LENGTH_EVENT_ORIGIN,
|
|
|
|
MAX_LENGTH_STATE_ENTITY_ID,
|
|
|
|
MAX_LENGTH_STATE_STATE,
|
|
|
|
)
|
|
|
|
from homeassistant.core import Context, Event, EventOrigin, State, split_entity_id
|
2022-06-23 12:32:26 +00:00
|
|
|
from homeassistant.helpers.json import (
|
|
|
|
JSON_DECODE_EXCEPTIONS,
|
|
|
|
JSON_DUMP,
|
|
|
|
json_bytes,
|
|
|
|
json_loads,
|
|
|
|
)
|
2022-05-24 13:34:46 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
2022-06-22 19:59:51 +00:00
|
|
|
from .const import ALL_DOMAIN_EXCLUDE_ATTRS
|
2022-06-07 12:41:43 +00:00
|
|
|
from .models import StatisticData, StatisticMetaData, process_timestamp
|
|
|
|
|
2022-05-24 13:34:46 +00:00
|
|
|
# SQLAlchemy Schema
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
Base = declarative_base()
|
|
|
|
|
2022-09-15 16:01:24 +00:00
|
|
|
SCHEMA_VERSION = 30
|
2022-05-24 13:34:46 +00:00
|
|
|
|
2022-07-20 02:02:03 +00:00
|
|
|
_StatisticsBaseSelfT = TypeVar("_StatisticsBaseSelfT", bound="StatisticsBase")
|
|
|
|
|
2022-05-24 13:34:46 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
TABLE_EVENTS = "events"
|
|
|
|
TABLE_EVENT_DATA = "event_data"
|
|
|
|
TABLE_STATES = "states"
|
|
|
|
TABLE_STATE_ATTRIBUTES = "state_attributes"
|
|
|
|
TABLE_RECORDER_RUNS = "recorder_runs"
|
|
|
|
TABLE_SCHEMA_CHANGES = "schema_changes"
|
|
|
|
TABLE_STATISTICS = "statistics"
|
|
|
|
TABLE_STATISTICS_META = "statistics_meta"
|
|
|
|
TABLE_STATISTICS_RUNS = "statistics_runs"
|
|
|
|
TABLE_STATISTICS_SHORT_TERM = "statistics_short_term"
|
|
|
|
|
|
|
|
ALL_TABLES = [
|
|
|
|
TABLE_STATES,
|
|
|
|
TABLE_STATE_ATTRIBUTES,
|
|
|
|
TABLE_EVENTS,
|
|
|
|
TABLE_EVENT_DATA,
|
|
|
|
TABLE_RECORDER_RUNS,
|
|
|
|
TABLE_SCHEMA_CHANGES,
|
|
|
|
TABLE_STATISTICS,
|
|
|
|
TABLE_STATISTICS_META,
|
|
|
|
TABLE_STATISTICS_RUNS,
|
|
|
|
TABLE_STATISTICS_SHORT_TERM,
|
|
|
|
]
|
|
|
|
|
|
|
|
TABLES_TO_CHECK = [
|
|
|
|
TABLE_STATES,
|
|
|
|
TABLE_EVENTS,
|
|
|
|
TABLE_RECORDER_RUNS,
|
|
|
|
TABLE_SCHEMA_CHANGES,
|
|
|
|
]
|
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
LAST_UPDATED_INDEX = "ix_states_last_updated"
|
|
|
|
ENTITY_ID_LAST_UPDATED_INDEX = "ix_states_entity_id_last_updated"
|
|
|
|
EVENTS_CONTEXT_ID_INDEX = "ix_events_context_id"
|
|
|
|
STATES_CONTEXT_ID_INDEX = "ix_states_context_id"
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
class FAST_PYSQLITE_DATETIME(sqlite.DATETIME): # type: ignore[misc]
|
|
|
|
"""Use ciso8601 to parse datetimes instead of sqlalchemy built-in regex."""
|
2022-05-24 13:34:46 +00:00
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
def result_processor(self, dialect, coltype): # type: ignore[no-untyped-def]
|
|
|
|
"""Offload the datetime parsing to ciso8601."""
|
|
|
|
return lambda value: None if value is None else ciso8601.parse_datetime(value)
|
|
|
|
|
|
|
|
|
2022-09-09 06:06:14 +00:00
|
|
|
JSON_VARIANT_CAST = Text().with_variant(
|
2022-06-07 12:41:43 +00:00
|
|
|
postgresql.JSON(none_as_null=True), "postgresql"
|
|
|
|
)
|
2022-09-09 06:06:14 +00:00
|
|
|
JSONB_VARIANT_CAST = Text().with_variant(
|
2022-06-07 12:41:43 +00:00
|
|
|
postgresql.JSONB(none_as_null=True), "postgresql"
|
|
|
|
)
|
|
|
|
DATETIME_TYPE = (
|
|
|
|
DateTime(timezone=True)
|
|
|
|
.with_variant(mysql.DATETIME(timezone=True, fsp=6), "mysql")
|
|
|
|
.with_variant(FAST_PYSQLITE_DATETIME(), "sqlite")
|
2022-05-24 13:34:46 +00:00
|
|
|
)
|
|
|
|
DOUBLE_TYPE = (
|
|
|
|
Float()
|
|
|
|
.with_variant(mysql.DOUBLE(asdecimal=False), "mysql")
|
|
|
|
.with_variant(oracle.DOUBLE_PRECISION(), "oracle")
|
|
|
|
.with_variant(postgresql.DOUBLE_PRECISION(), "postgresql")
|
|
|
|
)
|
2022-06-07 12:41:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JSONLiteral(JSON): # type: ignore[misc]
|
|
|
|
"""Teach SA how to literalize json."""
|
|
|
|
|
|
|
|
def literal_processor(self, dialect: str) -> Callable[[Any], str]:
|
|
|
|
"""Processor to convert a value to JSON."""
|
|
|
|
|
|
|
|
def process(value: Any) -> str:
|
|
|
|
"""Dump json."""
|
2022-06-22 19:59:51 +00:00
|
|
|
return JSON_DUMP(value)
|
2022-06-07 12:41:43 +00:00
|
|
|
|
|
|
|
return process
|
|
|
|
|
|
|
|
|
2022-05-24 13:34:46 +00:00
|
|
|
EVENT_ORIGIN_ORDER = [EventOrigin.local, EventOrigin.remote]
|
|
|
|
EVENT_ORIGIN_TO_IDX = {origin: idx for idx, origin in enumerate(EVENT_ORIGIN_ORDER)}
|
|
|
|
|
|
|
|
|
|
|
|
class Events(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Event history data."""
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
# Used for fetching events at a specific time
|
|
|
|
# see logbook
|
|
|
|
Index("ix_events_event_type_time_fired", "event_type", "time_fired"),
|
|
|
|
{"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_EVENTS
|
2022-06-07 12:41:43 +00:00
|
|
|
event_id = Column(Integer, Identity(), primary_key=True)
|
2022-05-24 13:34:46 +00:00
|
|
|
event_type = Column(String(MAX_LENGTH_EVENT_EVENT_TYPE))
|
|
|
|
event_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
2022-06-07 12:41:43 +00:00
|
|
|
origin = Column(String(MAX_LENGTH_EVENT_ORIGIN)) # no longer used for new rows
|
2022-05-24 13:34:46 +00:00
|
|
|
origin_idx = Column(SmallInteger)
|
|
|
|
time_fired = Column(DATETIME_TYPE, index=True)
|
|
|
|
context_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID), index=True)
|
|
|
|
context_user_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID))
|
|
|
|
context_parent_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID))
|
|
|
|
data_id = Column(Integer, ForeignKey("event_data.data_id"), index=True)
|
|
|
|
event_data_rel = relationship("EventData")
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.Events("
|
|
|
|
f"id={self.event_id}, type='{self.event_type}', "
|
|
|
|
f"origin_idx='{self.origin_idx}', time_fired='{self.time_fired}'"
|
|
|
|
f", data_id={self.data_id})>"
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_event(event: Event) -> Events:
|
|
|
|
"""Create an event database object from a native event."""
|
|
|
|
return Events(
|
|
|
|
event_type=event.event_type,
|
|
|
|
event_data=None,
|
|
|
|
origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin),
|
|
|
|
time_fired=event.time_fired,
|
|
|
|
context_id=event.context.id,
|
|
|
|
context_user_id=event.context.user_id,
|
|
|
|
context_parent_id=event.context.parent_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
def to_native(self, validate_entity_id: bool = True) -> Event | None:
|
|
|
|
"""Convert to a native HA Event."""
|
|
|
|
context = Context(
|
|
|
|
id=self.context_id,
|
|
|
|
user_id=self.context_user_id,
|
|
|
|
parent_id=self.context_parent_id,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
return Event(
|
|
|
|
self.event_type,
|
2022-06-23 12:32:26 +00:00
|
|
|
json_loads(self.event_data) if self.event_data else {},
|
2022-05-24 13:34:46 +00:00
|
|
|
EventOrigin(self.origin)
|
|
|
|
if self.origin
|
|
|
|
else EVENT_ORIGIN_ORDER[self.origin_idx],
|
|
|
|
process_timestamp(self.time_fired),
|
|
|
|
context=context,
|
|
|
|
)
|
2022-06-23 12:32:26 +00:00
|
|
|
except JSON_DECODE_EXCEPTIONS:
|
|
|
|
# When json_loads fails
|
2022-05-24 13:34:46 +00:00
|
|
|
_LOGGER.exception("Error converting to event: %s", self)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class EventData(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Event data history."""
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
{"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_EVENT_DATA
|
|
|
|
data_id = Column(Integer, Identity(), primary_key=True)
|
|
|
|
hash = Column(BigInteger, index=True)
|
|
|
|
# Note that this is not named attributes to avoid confusion with the states table
|
|
|
|
shared_data = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.EventData("
|
|
|
|
f"id={self.data_id}, hash='{self.hash}', data='{self.shared_data}'"
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_event(event: Event) -> EventData:
|
|
|
|
"""Create object from an event."""
|
2022-06-22 19:59:51 +00:00
|
|
|
shared_data = json_bytes(event.data)
|
2022-05-24 13:34:46 +00:00
|
|
|
return EventData(
|
2022-06-22 19:59:51 +00:00
|
|
|
shared_data=shared_data.decode("utf-8"),
|
|
|
|
hash=EventData.hash_shared_data_bytes(shared_data),
|
2022-05-24 13:34:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-06-22 19:59:51 +00:00
|
|
|
def shared_data_bytes_from_event(event: Event) -> bytes:
|
|
|
|
"""Create shared_data from an event."""
|
|
|
|
return json_bytes(event.data)
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-06-22 19:59:51 +00:00
|
|
|
def hash_shared_data_bytes(shared_data_bytes: bytes) -> int:
|
2022-05-24 13:34:46 +00:00
|
|
|
"""Return the hash of json encoded shared data."""
|
2022-06-22 19:59:51 +00:00
|
|
|
return cast(int, fnv1a_32(shared_data_bytes))
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
def to_native(self) -> dict[str, Any]:
|
|
|
|
"""Convert to an HA state object."""
|
|
|
|
try:
|
2022-06-23 12:32:26 +00:00
|
|
|
return cast(dict[str, Any], json_loads(self.shared_data))
|
|
|
|
except JSON_DECODE_EXCEPTIONS:
|
2022-05-24 13:34:46 +00:00
|
|
|
_LOGGER.exception("Error converting row to event data: %s", self)
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class States(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""State change history."""
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
# Used for fetching the state of entities at a specific time
|
|
|
|
# (get_states in history.py)
|
2022-06-07 12:41:43 +00:00
|
|
|
Index(ENTITY_ID_LAST_UPDATED_INDEX, "entity_id", "last_updated"),
|
2022-05-24 13:34:46 +00:00
|
|
|
{"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_STATES
|
|
|
|
state_id = Column(Integer, Identity(), primary_key=True)
|
|
|
|
entity_id = Column(String(MAX_LENGTH_STATE_ENTITY_ID))
|
|
|
|
state = Column(String(MAX_LENGTH_STATE_STATE))
|
2022-06-07 12:41:43 +00:00
|
|
|
attributes = Column(
|
|
|
|
Text().with_variant(mysql.LONGTEXT, "mysql")
|
|
|
|
) # no longer used for new rows
|
|
|
|
event_id = Column( # no longer used for new rows
|
2022-05-24 13:34:46 +00:00
|
|
|
Integer, ForeignKey("events.event_id", ondelete="CASCADE"), index=True
|
|
|
|
)
|
2022-06-07 12:41:43 +00:00
|
|
|
last_changed = Column(DATETIME_TYPE)
|
2022-05-24 13:34:46 +00:00
|
|
|
last_updated = Column(DATETIME_TYPE, default=dt_util.utcnow, index=True)
|
|
|
|
old_state_id = Column(Integer, ForeignKey("states.state_id"), index=True)
|
|
|
|
attributes_id = Column(
|
|
|
|
Integer, ForeignKey("state_attributes.attributes_id"), index=True
|
|
|
|
)
|
|
|
|
context_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID), index=True)
|
|
|
|
context_user_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID))
|
|
|
|
context_parent_id = Column(String(MAX_LENGTH_EVENT_CONTEXT_ID))
|
|
|
|
origin_idx = Column(SmallInteger) # 0 is local, 1 is remote
|
|
|
|
old_state = relationship("States", remote_side=[state_id])
|
|
|
|
state_attributes = relationship("StateAttributes")
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.States("
|
|
|
|
f"id={self.state_id}, entity_id='{self.entity_id}', "
|
|
|
|
f"state='{self.state}', event_id='{self.event_id}', "
|
|
|
|
f"last_updated='{self.last_updated.isoformat(sep=' ', timespec='seconds')}', "
|
|
|
|
f"old_state_id={self.old_state_id}, attributes_id={self.attributes_id}"
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_event(event: Event) -> States:
|
|
|
|
"""Create object from a state_changed event."""
|
|
|
|
entity_id = event.data["entity_id"]
|
|
|
|
state: State | None = event.data.get("new_state")
|
|
|
|
dbstate = States(
|
|
|
|
entity_id=entity_id,
|
|
|
|
attributes=None,
|
|
|
|
context_id=event.context.id,
|
|
|
|
context_user_id=event.context.user_id,
|
|
|
|
context_parent_id=event.context.parent_id,
|
|
|
|
origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin),
|
|
|
|
)
|
|
|
|
|
|
|
|
# None state means the state was removed from the state machine
|
|
|
|
if state is None:
|
|
|
|
dbstate.state = ""
|
|
|
|
dbstate.last_updated = event.time_fired
|
2022-06-07 12:41:43 +00:00
|
|
|
dbstate.last_changed = None
|
|
|
|
return dbstate
|
|
|
|
|
|
|
|
dbstate.state = state.state
|
|
|
|
dbstate.last_updated = state.last_updated
|
|
|
|
if state.last_updated == state.last_changed:
|
|
|
|
dbstate.last_changed = None
|
2022-05-24 13:34:46 +00:00
|
|
|
else:
|
|
|
|
dbstate.last_changed = state.last_changed
|
|
|
|
|
|
|
|
return dbstate
|
|
|
|
|
|
|
|
def to_native(self, validate_entity_id: bool = True) -> State | None:
|
|
|
|
"""Convert to an HA state object."""
|
|
|
|
context = Context(
|
|
|
|
id=self.context_id,
|
|
|
|
user_id=self.context_user_id,
|
|
|
|
parent_id=self.context_parent_id,
|
|
|
|
)
|
|
|
|
try:
|
2022-06-23 12:32:26 +00:00
|
|
|
attrs = json_loads(self.attributes) if self.attributes else {}
|
|
|
|
except JSON_DECODE_EXCEPTIONS:
|
|
|
|
# When json_loads fails
|
2022-05-24 13:34:46 +00:00
|
|
|
_LOGGER.exception("Error converting row to state: %s", self)
|
|
|
|
return None
|
2022-06-07 12:41:43 +00:00
|
|
|
if self.last_changed is None or self.last_changed == self.last_updated:
|
|
|
|
last_changed = last_updated = process_timestamp(self.last_updated)
|
|
|
|
else:
|
|
|
|
last_updated = process_timestamp(self.last_updated)
|
|
|
|
last_changed = process_timestamp(self.last_changed)
|
|
|
|
return State(
|
|
|
|
self.entity_id,
|
|
|
|
self.state,
|
|
|
|
# Join the state_attributes table on attributes_id to get the attributes
|
|
|
|
# for newer states
|
|
|
|
attrs,
|
|
|
|
last_changed,
|
|
|
|
last_updated,
|
|
|
|
context=context,
|
|
|
|
validate_entity_id=validate_entity_id,
|
|
|
|
)
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StateAttributes(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""State attribute change history."""
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
{"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_STATE_ATTRIBUTES
|
|
|
|
attributes_id = Column(Integer, Identity(), primary_key=True)
|
|
|
|
hash = Column(BigInteger, index=True)
|
|
|
|
# Note that this is not named attributes to avoid confusion with the states table
|
|
|
|
shared_attrs = Column(Text().with_variant(mysql.LONGTEXT, "mysql"))
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.StateAttributes("
|
|
|
|
f"id={self.attributes_id}, hash='{self.hash}', attributes='{self.shared_attrs}'"
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_event(event: Event) -> StateAttributes:
|
|
|
|
"""Create object from a state_changed event."""
|
|
|
|
state: State | None = event.data.get("new_state")
|
|
|
|
# None state means the state was removed from the state machine
|
2022-06-22 19:59:51 +00:00
|
|
|
attr_bytes = b"{}" if state is None else json_bytes(state.attributes)
|
|
|
|
dbstate = StateAttributes(shared_attrs=attr_bytes.decode("utf-8"))
|
|
|
|
dbstate.hash = StateAttributes.hash_shared_attrs_bytes(attr_bytes)
|
2022-05-24 13:34:46 +00:00
|
|
|
return dbstate
|
|
|
|
|
|
|
|
@staticmethod
|
2022-06-22 19:59:51 +00:00
|
|
|
def shared_attrs_bytes_from_event(
|
2022-05-24 13:34:46 +00:00
|
|
|
event: Event, exclude_attrs_by_domain: dict[str, set[str]]
|
2022-06-22 19:59:51 +00:00
|
|
|
) -> bytes:
|
2022-05-24 13:34:46 +00:00
|
|
|
"""Create shared_attrs from a state_changed event."""
|
|
|
|
state: State | None = event.data.get("new_state")
|
|
|
|
# None state means the state was removed from the state machine
|
|
|
|
if state is None:
|
2022-06-22 19:59:51 +00:00
|
|
|
return b"{}"
|
2022-05-24 13:34:46 +00:00
|
|
|
domain = split_entity_id(state.entity_id)[0]
|
|
|
|
exclude_attrs = (
|
|
|
|
exclude_attrs_by_domain.get(domain, set()) | ALL_DOMAIN_EXCLUDE_ATTRS
|
|
|
|
)
|
2022-06-22 19:59:51 +00:00
|
|
|
return json_bytes(
|
2022-05-24 13:34:46 +00:00
|
|
|
{k: v for k, v in state.attributes.items() if k not in exclude_attrs}
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-06-22 19:59:51 +00:00
|
|
|
def hash_shared_attrs_bytes(shared_attrs_bytes: bytes) -> int:
|
2022-06-23 12:32:26 +00:00
|
|
|
"""Return the hash of json encoded shared attributes."""
|
2022-06-22 19:59:51 +00:00
|
|
|
return cast(int, fnv1a_32(shared_attrs_bytes))
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
def to_native(self) -> dict[str, Any]:
|
|
|
|
"""Convert to an HA state object."""
|
|
|
|
try:
|
2022-06-23 12:32:26 +00:00
|
|
|
return cast(dict[str, Any], json_loads(self.shared_attrs))
|
|
|
|
except JSON_DECODE_EXCEPTIONS:
|
|
|
|
# When json_loads fails
|
2022-05-24 13:34:46 +00:00
|
|
|
_LOGGER.exception("Error converting row to state attributes: %s", self)
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class StatisticsBase:
|
|
|
|
"""Statistics base class."""
|
|
|
|
|
|
|
|
id = Column(Integer, Identity(), primary_key=True)
|
|
|
|
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
|
|
|
|
|
|
|
@declared_attr # type: ignore[misc]
|
|
|
|
def metadata_id(self) -> Column:
|
|
|
|
"""Define the metadata_id column for sub classes."""
|
|
|
|
return Column(
|
|
|
|
Integer,
|
|
|
|
ForeignKey(f"{TABLE_STATISTICS_META}.id", ondelete="CASCADE"),
|
|
|
|
index=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
start = Column(DATETIME_TYPE, index=True)
|
|
|
|
mean = Column(DOUBLE_TYPE)
|
|
|
|
min = Column(DOUBLE_TYPE)
|
|
|
|
max = Column(DOUBLE_TYPE)
|
|
|
|
last_reset = Column(DATETIME_TYPE)
|
|
|
|
state = Column(DOUBLE_TYPE)
|
|
|
|
sum = Column(DOUBLE_TYPE)
|
|
|
|
|
|
|
|
@classmethod
|
2022-07-20 02:02:03 +00:00
|
|
|
def from_stats(
|
|
|
|
cls: type[_StatisticsBaseSelfT], metadata_id: int, stats: StatisticData
|
|
|
|
) -> _StatisticsBaseSelfT:
|
2022-05-24 13:34:46 +00:00
|
|
|
"""Create object from a statistics."""
|
|
|
|
return cls( # type: ignore[call-arg,misc]
|
|
|
|
metadata_id=metadata_id,
|
|
|
|
**stats,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Statistics(Base, StatisticsBase): # type: ignore[misc,valid-type]
|
|
|
|
"""Long term statistics."""
|
|
|
|
|
|
|
|
duration = timedelta(hours=1)
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
# Used for fetching statistics for a certain entity at a specific time
|
|
|
|
Index("ix_statistics_statistic_id_start", "metadata_id", "start", unique=True),
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_STATISTICS
|
|
|
|
|
|
|
|
|
|
|
|
class StatisticsShortTerm(Base, StatisticsBase): # type: ignore[misc,valid-type]
|
|
|
|
"""Short term statistics."""
|
|
|
|
|
|
|
|
duration = timedelta(minutes=5)
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
# Used for fetching statistics for a certain entity at a specific time
|
|
|
|
Index(
|
|
|
|
"ix_statistics_short_term_statistic_id_start",
|
|
|
|
"metadata_id",
|
|
|
|
"start",
|
|
|
|
unique=True,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_STATISTICS_SHORT_TERM
|
|
|
|
|
|
|
|
|
|
|
|
class StatisticsMeta(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Statistics meta data."""
|
|
|
|
|
|
|
|
__table_args__ = (
|
|
|
|
{"mysql_default_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
|
|
|
|
)
|
|
|
|
__tablename__ = TABLE_STATISTICS_META
|
|
|
|
id = Column(Integer, Identity(), primary_key=True)
|
2022-06-07 12:41:43 +00:00
|
|
|
statistic_id = Column(String(255), index=True, unique=True)
|
2022-05-24 13:34:46 +00:00
|
|
|
source = Column(String(32))
|
|
|
|
unit_of_measurement = Column(String(255))
|
|
|
|
has_mean = Column(Boolean)
|
|
|
|
has_sum = Column(Boolean)
|
|
|
|
name = Column(String(255))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_meta(meta: StatisticMetaData) -> StatisticsMeta:
|
|
|
|
"""Create object from meta data."""
|
|
|
|
return StatisticsMeta(**meta)
|
|
|
|
|
|
|
|
|
|
|
|
class RecorderRuns(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Representation of recorder run."""
|
|
|
|
|
|
|
|
__table_args__ = (Index("ix_recorder_runs_start_end", "start", "end"),)
|
|
|
|
__tablename__ = TABLE_RECORDER_RUNS
|
|
|
|
run_id = Column(Integer, Identity(), primary_key=True)
|
2022-10-19 08:26:17 +00:00
|
|
|
start = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
|
|
|
end = Column(DATETIME_TYPE)
|
2022-05-24 13:34:46 +00:00
|
|
|
closed_incorrect = Column(Boolean, default=False)
|
2022-10-19 08:26:17 +00:00
|
|
|
created = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
end = (
|
|
|
|
f"'{self.end.isoformat(sep=' ', timespec='seconds')}'" if self.end else None
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
f"<recorder.RecorderRuns("
|
|
|
|
f"id={self.run_id}, start='{self.start.isoformat(sep=' ', timespec='seconds')}', "
|
|
|
|
f"end={end}, closed_incorrect={self.closed_incorrect}, "
|
|
|
|
f"created='{self.created.isoformat(sep=' ', timespec='seconds')}'"
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
def entity_ids(self, point_in_time: datetime | None = None) -> list[str]:
|
|
|
|
"""Return the entity ids that existed in this run.
|
|
|
|
|
|
|
|
Specify point_in_time if you want to know which existed at that point
|
|
|
|
in time inside the run.
|
|
|
|
"""
|
|
|
|
session = Session.object_session(self)
|
|
|
|
|
|
|
|
assert session is not None, "RecorderRuns need to be persisted"
|
|
|
|
|
|
|
|
query = session.query(distinct(States.entity_id)).filter(
|
|
|
|
States.last_updated >= self.start
|
|
|
|
)
|
|
|
|
|
|
|
|
if point_in_time is not None:
|
|
|
|
query = query.filter(States.last_updated < point_in_time)
|
|
|
|
elif self.end is not None:
|
|
|
|
query = query.filter(States.last_updated < self.end)
|
|
|
|
|
|
|
|
return [row[0] for row in query]
|
|
|
|
|
|
|
|
def to_native(self, validate_entity_id: bool = True) -> RecorderRuns:
|
|
|
|
"""Return self, native format is this model."""
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class SchemaChanges(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Representation of schema version changes."""
|
|
|
|
|
|
|
|
__tablename__ = TABLE_SCHEMA_CHANGES
|
|
|
|
change_id = Column(Integer, Identity(), primary_key=True)
|
|
|
|
schema_version = Column(Integer)
|
2022-10-19 08:26:17 +00:00
|
|
|
changed = Column(DATETIME_TYPE, default=dt_util.utcnow)
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.SchemaChanges("
|
|
|
|
f"id={self.change_id}, schema_version={self.schema_version}, "
|
|
|
|
f"changed='{self.changed.isoformat(sep=' ', timespec='seconds')}'"
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class StatisticsRuns(Base): # type: ignore[misc,valid-type]
|
|
|
|
"""Representation of statistics run."""
|
|
|
|
|
|
|
|
__tablename__ = TABLE_STATISTICS_RUNS
|
|
|
|
run_id = Column(Integer, Identity(), primary_key=True)
|
2022-10-19 08:26:17 +00:00
|
|
|
start = Column(DATETIME_TYPE, index=True)
|
2022-05-24 13:34:46 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""Return string representation of instance for debugging."""
|
|
|
|
return (
|
|
|
|
f"<recorder.StatisticsRuns("
|
|
|
|
f"id={self.run_id}, start='{self.start.isoformat(sep=' ', timespec='seconds')}', "
|
|
|
|
f")>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-07 12:41:43 +00:00
|
|
|
EVENT_DATA_JSON = type_coerce(
|
2022-09-09 06:06:14 +00:00
|
|
|
EventData.shared_data.cast(JSONB_VARIANT_CAST), JSONLiteral(none_as_null=True)
|
2022-06-07 12:41:43 +00:00
|
|
|
)
|
|
|
|
OLD_FORMAT_EVENT_DATA_JSON = type_coerce(
|
2022-09-09 06:06:14 +00:00
|
|
|
Events.event_data.cast(JSONB_VARIANT_CAST), JSONLiteral(none_as_null=True)
|
2022-06-07 12:41:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
SHARED_ATTRS_JSON = type_coerce(
|
2022-09-09 06:06:14 +00:00
|
|
|
StateAttributes.shared_attrs.cast(JSON_VARIANT_CAST), JSON(none_as_null=True)
|
2022-06-07 12:41:43 +00:00
|
|
|
)
|
|
|
|
OLD_FORMAT_ATTRS_JSON = type_coerce(
|
2022-09-09 06:06:14 +00:00
|
|
|
States.attributes.cast(JSON_VARIANT_CAST), JSON(none_as_null=True)
|
2022-06-07 12:41:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ENTITY_ID_IN_EVENT: Column = EVENT_DATA_JSON["entity_id"]
|
|
|
|
OLD_ENTITY_ID_IN_EVENT: Column = OLD_FORMAT_EVENT_DATA_JSON["entity_id"]
|
|
|
|
DEVICE_ID_IN_EVENT: Column = EVENT_DATA_JSON["device_id"]
|
|
|
|
OLD_STATE = aliased(States, name="old_state")
|