Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't access States.last_reported_ts before it's added #114333

Merged
merged 3 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/recorder/const.py
Expand Up @@ -53,6 +53,7 @@
CONTEXT_ID_AS_BINARY_SCHEMA_VERSION = 36
EVENT_TYPE_IDS_SCHEMA_VERSION = 37
STATES_META_SCHEMA_VERSION = 38
LAST_REPORTED_SCHEMA_VERSION = 43

LEGACY_STATES_EVENT_ID_INDEX_SCHEMA_VERSION = 28

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/recorder/core.py
Expand Up @@ -47,6 +47,7 @@
DOMAIN,
ESTIMATED_QUEUE_ITEM_SIZE,
KEEPALIVE_TIME,
LAST_REPORTED_SCHEMA_VERSION,
LEGACY_STATES_EVENT_ID_INDEX_SCHEMA_VERSION,
MARIADB_PYMYSQL_URL_PREFIX,
MARIADB_URL_PREFIX,
Expand Down Expand Up @@ -1203,7 +1204,7 @@ def _commit_event_session(self) -> None:
if (
pending_last_reported
:= self.states_manager.get_pending_last_reported_timestamp()
):
) and self.schema_version >= LAST_REPORTED_SCHEMA_VERSION:
with session.no_autoflush:
session.execute(
update(States),
Expand Down
27 changes: 19 additions & 8 deletions homeassistant/components/recorder/history/modern.py
Expand Up @@ -27,6 +27,7 @@
import homeassistant.util.dt as dt_util

from ... import recorder
from ..const import LAST_REPORTED_SCHEMA_VERSION
from ..db_schema import SHARED_ATTR_OR_LEGACY_ATTRIBUTES, StateAttributes, States
from ..filters import Filters
from ..models import (
Expand Down Expand Up @@ -327,9 +328,10 @@ def _state_changed_during_period_stmt(
limit: int | None,
include_start_time_state: bool,
run_start_ts: float | None,
include_last_reported: bool,
) -> Select | CompoundSelect:
stmt = (
_stmt_and_join_attributes(no_attributes, False, True)
_stmt_and_join_attributes(no_attributes, False, include_last_reported)
.filter(
(
(States.last_changed_ts == States.last_updated_ts)
Expand Down Expand Up @@ -361,22 +363,22 @@ def _state_changed_during_period_stmt(
single_metadata_id,
no_attributes,
False,
True,
include_last_reported,
).subquery(),
no_attributes,
False,
True,
include_last_reported,
),
_select_from_subquery(
stmt.subquery(),
no_attributes,
False,
True,
include_last_reported,
),
).subquery(),
no_attributes,
False,
True,
include_last_reported,
)


Expand All @@ -391,6 +393,9 @@ def state_changes_during_period(
include_start_time_state: bool = True,
) -> MutableMapping[str, list[State]]:
"""Return states changes during UTC period start_time - end_time."""
has_last_reported = (
recorder.get_instance(hass).schema_version >= LAST_REPORTED_SCHEMA_VERSION
)
if not entity_id:
raise ValueError("entity_id must be provided")
entity_ids = [entity_id.lower()]
Expand Down Expand Up @@ -423,12 +428,14 @@ def state_changes_during_period(
limit,
include_start_time_state,
run_start_ts,
has_last_reported,
),
track_on=[
bool(end_time_ts),
no_attributes,
bool(limit),
include_start_time_state,
has_last_reported,
],
)
return cast(
Expand Down Expand Up @@ -475,10 +482,10 @@ def _get_last_state_changes_single_stmt(metadata_id: int) -> Select:


def _get_last_state_changes_multiple_stmt(
number_of_states: int, metadata_id: int
number_of_states: int, metadata_id: int, include_last_reported: bool
) -> Select:
return (
_stmt_and_join_attributes(False, False, True)
_stmt_and_join_attributes(False, False, include_last_reported)
.where(
States.state_id
== (
Expand All @@ -500,6 +507,9 @@ def get_last_state_changes(
hass: HomeAssistant, number_of_states: int, entity_id: str
) -> MutableMapping[str, list[State]]:
"""Return the last number_of_states."""
has_last_reported = (
recorder.get_instance(hass).schema_version >= LAST_REPORTED_SCHEMA_VERSION
)
entity_id_lower = entity_id.lower()
entity_ids = [entity_id_lower]

Expand All @@ -524,8 +534,9 @@ def get_last_state_changes(
else:
stmt = lambda_stmt(
lambda: _get_last_state_changes_multiple_stmt(
number_of_states, metadata_id
number_of_states, metadata_id, has_last_reported
),
track_on=[has_last_reported],
)
states = list(execute_stmt_lambda_element(session, stmt, orm_rows=False))
return cast(
Expand Down