Remove unused code in RecorderRuns.entity_ids (#89526)
parent
01e1221443
commit
52cea16f74
|
@ -25,14 +25,11 @@ from sqlalchemy import (
|
|||
SmallInteger,
|
||||
String,
|
||||
Text,
|
||||
distinct,
|
||||
type_coerce,
|
||||
)
|
||||
from sqlalchemy.dialects import mysql, oracle, postgresql, sqlite
|
||||
from sqlalchemy.engine.interfaces import Dialect
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, aliased, mapped_column, relationship
|
||||
from sqlalchemy.orm.query import RowReturningQuery
|
||||
from sqlalchemy.orm.session import Session
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant.const import (
|
||||
|
@ -699,27 +696,6 @@ class RecorderRuns(Base):
|
|||
f" created='{self.created.isoformat(sep=' ', timespec='seconds')}')>"
|
||||
)
|
||||
|
||||
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: RowReturningQuery[tuple[str]] = session.query(distinct(States.entity_id))
|
||||
|
||||
query = query.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) -> Self:
|
||||
"""Return self, native format is this model."""
|
||||
return self
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
"""Managers for each table."""
|
|
@ -4,15 +4,11 @@ from unittest.mock import PropertyMock
|
|||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
from homeassistant.components.recorder.const import SupportedDialect
|
||||
from homeassistant.components.recorder.db_schema import (
|
||||
Base,
|
||||
EventData,
|
||||
Events,
|
||||
RecorderRuns,
|
||||
StateAttributes,
|
||||
States,
|
||||
)
|
||||
|
@ -151,78 +147,6 @@ def test_from_event_to_delete_state() -> None:
|
|||
assert db_state.last_updated_ts == event.time_fired.timestamp()
|
||||
|
||||
|
||||
def test_entity_ids(recorder_db_url: str) -> None:
|
||||
"""Test if entity ids helper method works."""
|
||||
if recorder_db_url.startswith("mysql://"):
|
||||
# Dropping the database after this test will fail on MySQL
|
||||
# because it will create an InnoDB deadlock.
|
||||
return
|
||||
engine = create_engine(recorder_db_url)
|
||||
Base.metadata.create_all(engine)
|
||||
session_factory = sessionmaker(bind=engine)
|
||||
|
||||
session = scoped_session(session_factory)
|
||||
session.query(Events).delete()
|
||||
session.query(States).delete()
|
||||
session.query(RecorderRuns).delete()
|
||||
|
||||
run = RecorderRuns(
|
||||
start=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC),
|
||||
end=datetime(2016, 7, 9, 23, 0, 0, tzinfo=dt.UTC),
|
||||
closed_incorrect=False,
|
||||
created=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC),
|
||||
)
|
||||
|
||||
session.add(run)
|
||||
session.commit()
|
||||
|
||||
before_run = datetime(2016, 7, 9, 8, 0, 0, tzinfo=dt.UTC)
|
||||
in_run = datetime(2016, 7, 9, 13, 0, 0, tzinfo=dt.UTC)
|
||||
in_run2 = datetime(2016, 7, 9, 15, 0, 0, tzinfo=dt.UTC)
|
||||
in_run3 = datetime(2016, 7, 9, 18, 0, 0, tzinfo=dt.UTC)
|
||||
after_run = datetime(2016, 7, 9, 23, 30, 0, tzinfo=dt.UTC)
|
||||
|
||||
assert run.to_native() == run
|
||||
assert run.entity_ids() == []
|
||||
|
||||
session.add(
|
||||
States(
|
||||
entity_id="sensor.temperature",
|
||||
state="20",
|
||||
last_changed=before_run,
|
||||
last_updated=before_run,
|
||||
)
|
||||
)
|
||||
session.add(
|
||||
States(
|
||||
entity_id="sensor.sound",
|
||||
state="10",
|
||||
last_changed=after_run,
|
||||
last_updated=after_run,
|
||||
)
|
||||
)
|
||||
|
||||
session.add(
|
||||
States(
|
||||
entity_id="sensor.humidity",
|
||||
state="76",
|
||||
last_changed=in_run,
|
||||
last_updated=in_run,
|
||||
)
|
||||
)
|
||||
session.add(
|
||||
States(
|
||||
entity_id="sensor.lux",
|
||||
state="5",
|
||||
last_changed=in_run3,
|
||||
last_updated=in_run3,
|
||||
)
|
||||
)
|
||||
|
||||
assert sorted(run.entity_ids()) == ["sensor.humidity", "sensor.lux"]
|
||||
assert run.entity_ids(in_run2) == ["sensor.humidity"]
|
||||
|
||||
|
||||
def test_states_from_native_invalid_entity_id() -> None:
|
||||
"""Test loading a state from an invalid entity ID."""
|
||||
state = States()
|
||||
|
|
Loading…
Reference in New Issue