added support for home assistant postgres database and convenience function to get state of entity

This commit is contained in:
Brian Bjarke Jensen
2026-01-24 19:48:13 +01:00
parent aaa724164c
commit d59f7d2e2d
20 changed files with 504 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
"""Database table definitions for Home Assistant PostgreSQL database."""
from .event_data import EventData
from .event_types import EventTypes
from .events import Events
from .migration_changes import MigrationChanges
from .recorder_runs import RecorderRuns
from .schema_changes import SchemaChanges
from .state_attributes import StateAttributes
from .states_meta import StatesMeta
from .states import States
from .statistics_meta import StatisticsMeta
from .statistics_runs import StatisticsRuns
from .statistics_short_term import StatisticsShortTerm
from .statistics import Statistics
__all__ = [
"EventData",
"EventTypes",
"Events",
"MigrationChanges",
"RecorderRuns",
"SchemaChanges",
"StateAttributes",
"StatesMeta",
"States",
"StatisticsMeta",
"StatisticsRuns",
"StatisticsShortTerm",
"Statistics",
]
+5
View File
@@ -0,0 +1,5 @@
"""Base declarative class for SQLAlchemy ORM models."""
from sqlalchemy.orm import declarative_base
Base = declarative_base()
+16
View File
@@ -0,0 +1,16 @@
"""SQLAlchemy model for the 'event_data' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, Text
from .base import Base
class EventData(Base):
"""SQLAlchemy model for the 'event_data' table."""
__tablename__ = "event_data"
__table_args__ = {"schema": "public"}
data_id = Column(BigInteger, primary_key=True, autoincrement=True)
hash = Column(BigInteger, nullable=True)
shared_data = Column(Text, nullable=True)
+15
View File
@@ -0,0 +1,15 @@
"""SQLAlchemy model for the 'event_types' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, String
from .base import Base
class EventTypes(Base):
"""SQLAlchemy model for the 'event_types' table."""
__tablename__ = "event_types"
__table_args__ = {"schema": "public"}
event_type_id = Column(BigInteger, primary_key=True, autoincrement=True)
event_type = Column(String(64), nullable=True)
+39
View File
@@ -0,0 +1,39 @@
"""SQLAlchemy model for the 'events' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import (
Column,
BigInteger,
CHAR,
SmallInteger,
TIMESTAMP,
Float,
LargeBinary,
ForeignKey,
)
from .base import Base
class Events(Base):
"""SQLAlchemy model for the 'events' table."""
__tablename__ = "events"
__table_args__ = {"schema": "public"}
event_id = Column(BigInteger, primary_key=True, autoincrement=True)
event_type = Column(CHAR(1), nullable=True)
event_data = Column(CHAR(1), nullable=True)
origin = Column(CHAR(1), nullable=True)
origin_idx = Column(SmallInteger, nullable=True)
time_fired = Column(TIMESTAMP(timezone=True), nullable=True)
time_fired_ts = Column(Float, nullable=True)
context_id = Column(CHAR(1), nullable=True)
context_user_id = Column(CHAR(1), nullable=True)
context_parent_id = Column(CHAR(1), nullable=True)
data_id = Column(BigInteger, ForeignKey("public.event_data.data_id"), nullable=True)
context_id_bin = Column(LargeBinary, nullable=True)
context_user_id_bin = Column(LargeBinary, nullable=True)
context_parent_id_bin = Column(LargeBinary, nullable=True)
event_type_id = Column(
BigInteger, ForeignKey("public.event_types.event_type_id"), nullable=True
)
@@ -0,0 +1,15 @@
"""SQLAlchemy model for the 'migration_changes' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, String, SmallInteger
from .base import Base
class MigrationChanges(Base):
"""SQLAlchemy model for the 'migration_changes' table."""
__tablename__ = "migration_changes"
__table_args__ = {"schema": "public"}
migration_id = Column(String(255), primary_key=True)
version = Column(SmallInteger, nullable=False)
+18
View File
@@ -0,0 +1,18 @@
"""SQLAlchemy model for the 'recorder_runs' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, TIMESTAMP, Boolean
from .base import Base
class RecorderRuns(Base):
"""SQLAlchemy model for the 'recorder_runs' table."""
__tablename__ = "recorder_runs"
__table_args__ = {"schema": "public"}
run_id = Column(BigInteger, primary_key=True, autoincrement=True)
start = Column(TIMESTAMP(timezone=True), nullable=False)
end = Column(TIMESTAMP(timezone=True), nullable=True)
closed_incorrect = Column(Boolean, nullable=False)
created = Column(TIMESTAMP(timezone=True), nullable=False)
+16
View File
@@ -0,0 +1,16 @@
"""SQLAlchemy model for the 'schema_changes' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, Integer, TIMESTAMP
from .base import Base
class SchemaChanges(Base):
"""SQLAlchemy model for the 'schema_changes' table."""
__tablename__ = "schema_changes"
__table_args__ = {"schema": "public"}
change_id = Column(BigInteger, primary_key=True, autoincrement=True)
schema_version = Column(Integer, nullable=True)
changed = Column(TIMESTAMP(timezone=True), nullable=False)
+16
View File
@@ -0,0 +1,16 @@
"""SQLAlchemy model for the 'state_attributes' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, Text
from .base import Base
class StateAttributes(Base):
"""SQLAlchemy model for the 'state_attributes' table."""
__tablename__ = "state_attributes"
__table_args__ = {"schema": "public"}
attributes_id = Column(BigInteger, primary_key=True, autoincrement=True)
hash = Column(BigInteger, nullable=True)
shared_attrs = Column(Text, nullable=True)
+45
View File
@@ -0,0 +1,45 @@
"""SQLAlchemy model for the 'states' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import (
Column,
BigInteger,
CHAR,
String,
SmallInteger,
TIMESTAMP,
Float,
LargeBinary,
ForeignKey,
)
from .base import Base
class States(Base):
"""SQLAlchemy model for the 'states' table."""
__tablename__ = "states"
__table_args__ = {"schema": "public"}
state_id = Column(BigInteger, primary_key=True, autoincrement=True)
entity_id = Column(CHAR(1), nullable=True)
state = Column(String(255), nullable=True)
attributes = Column(CHAR(1), nullable=True)
event_id = Column(SmallInteger, nullable=True)
last_changed = Column(TIMESTAMP(timezone=True), nullable=True)
last_changed_ts = Column(Float, nullable=True)
last_reported_ts = Column(Float, nullable=True)
last_updated = Column(TIMESTAMP(timezone=True), nullable=True)
last_updated_ts = Column(Float, nullable=True)
old_state_id = Column(BigInteger, ForeignKey("public.states.state_id"), nullable=True)
attributes_id = Column(
BigInteger, ForeignKey("public.state_attributes.attributes_id"), nullable=True
)
context_id = Column(CHAR(1), nullable=True)
context_user_id = Column(CHAR(1), nullable=True)
context_parent_id = Column(CHAR(1), nullable=True)
origin_idx = Column(SmallInteger, nullable=True)
context_id_bin = Column(LargeBinary, nullable=True)
context_user_id_bin = Column(LargeBinary, nullable=True)
context_parent_id_bin = Column(LargeBinary, nullable=True)
metadata_id = Column(BigInteger, ForeignKey("public.states_meta.metadata_id"), nullable=True)
+15
View File
@@ -0,0 +1,15 @@
"""SQLAlchemy model for the 'states_meta' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, String
from .base import Base
class StatesMeta(Base):
"""SQLAlchemy model for the 'states_meta' table."""
__tablename__ = "states_meta"
__table_args__ = {"schema": "public"}
metadata_id = Column(BigInteger, primary_key=True, autoincrement=True)
entity_id = Column(String(255), nullable=True)
+29
View File
@@ -0,0 +1,29 @@
"""SQLAlchemy model for the 'statistics' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, TIMESTAMP, Float, ForeignKey
from .base import Base
class Statistics(Base):
"""SQLAlchemy model for the 'statistics' table."""
__tablename__ = "statistics"
__table_args__ = {"schema": "public"}
id = Column(BigInteger, primary_key=True, autoincrement=True)
created = Column(TIMESTAMP(timezone=True), nullable=True)
created_ts = Column(Float, nullable=True)
metadata_id = Column(
BigInteger, ForeignKey("public.statistics_meta.id", ondelete="CASCADE"), nullable=True
)
start = Column(TIMESTAMP(timezone=True), nullable=True)
start_ts = Column(Float, nullable=True)
mean = Column(Float, nullable=True)
mean_weight = Column(Float, nullable=True)
min = Column(Float, nullable=True)
max = Column(Float, nullable=True)
last_reset = Column(TIMESTAMP(timezone=True), nullable=True)
last_reset_ts = Column(Float, nullable=True)
state = Column(Float, nullable=True)
sum = Column(Float, nullable=True)
+22
View File
@@ -0,0 +1,22 @@
"""SQLAlchemy model for the 'statistics_meta' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, String, Boolean, SmallInteger
from .base import Base
class StatisticsMeta(Base):
"""SQLAlchemy model for the 'statistics_meta' table."""
__tablename__ = "statistics_meta"
__table_args__ = {"schema": "public"}
id = Column(BigInteger, primary_key=True, autoincrement=True)
statistic_id = Column(String(255), nullable=True)
source = Column(String(32), nullable=True)
unit_of_measurement = Column(String(255), nullable=True)
unit_class = Column(String(255), nullable=True)
has_mean = Column(Boolean, nullable=True)
has_sum = Column(Boolean, nullable=True)
name = Column(String(255), nullable=True)
mean_type = Column(SmallInteger, nullable=False)
+15
View File
@@ -0,0 +1,15 @@
"""SQLAlchemy model for the 'statistics_runs' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, TIMESTAMP
from .base import Base
class StatisticsRuns(Base):
"""SQLAlchemy model for the 'statistics_runs' table."""
__tablename__ = "statistics_runs"
__table_args__ = {"schema": "public"}
run_id = Column(BigInteger, primary_key=True, autoincrement=True)
start = Column(TIMESTAMP(timezone=True), nullable=False)
@@ -0,0 +1,29 @@
"""SQLAlchemy model for the 'statistics_short_term' table in the Home Assistant PostgreSQL database."""
from sqlalchemy import Column, BigInteger, TIMESTAMP, Float, ForeignKey
from .base import Base
class StatisticsShortTerm(Base):
"""SQLAlchemy model for the 'statistics_short_term' table."""
__tablename__ = "statistics_short_term"
__table_args__ = {"schema": "public"}
id = Column(BigInteger, primary_key=True, autoincrement=True)
created = Column(TIMESTAMP(timezone=True), nullable=True)
created_ts = Column(Float, nullable=True)
metadata_id = Column(
BigInteger, ForeignKey("public.statistics_meta.id", ondelete="CASCADE"), nullable=True
)
start = Column(TIMESTAMP(timezone=True), nullable=True)
start_ts = Column(Float, nullable=True)
mean = Column(Float, nullable=True)
mean_weight = Column(Float, nullable=True)
min = Column(Float, nullable=True)
max = Column(Float, nullable=True)
last_reset = Column(TIMESTAMP(timezone=True), nullable=True)
last_reset_ts = Column(Float, nullable=True)
state = Column(Float, nullable=True)
sum = Column(Float, nullable=True)