2929from homeassistant .util import dt as dt_util
3030from homeassistant .util .collection import chunked_or_all
3131
32- from ..const import LAST_REPORTED_SCHEMA_VERSION , MAX_IDS_FOR_INDEXED_GROUP_BY
32+ from ..const import MAX_IDS_FOR_INDEXED_GROUP_BY
3333from ..db_schema import (
3434 SHARED_ATTR_OR_LEGACY_ATTRIBUTES ,
3535 StateAttributes ,
@@ -388,10 +388,9 @@ def _state_changed_during_period_stmt(
388388 limit : int | None ,
389389 include_start_time_state : bool ,
390390 run_start_ts : float | None ,
391- include_last_reported : bool ,
392391) -> Select | CompoundSelect :
393392 stmt = (
394- _stmt_and_join_attributes (no_attributes , False , include_last_reported )
393+ _stmt_and_join_attributes (no_attributes , False , True )
395394 .filter (
396395 (
397396 (States .last_changed_ts == States .last_updated_ts )
@@ -424,22 +423,22 @@ def _state_changed_during_period_stmt(
424423 single_metadata_id ,
425424 no_attributes ,
426425 False ,
427- include_last_reported ,
426+ True ,
428427 ).subquery (),
429428 no_attributes ,
430429 False ,
431- include_last_reported ,
430+ True ,
432431 ),
433432 _select_from_subquery (
434433 stmt .subquery (),
435434 no_attributes ,
436435 False ,
437- include_last_reported ,
436+ True ,
438437 ),
439438 ).subquery (),
440439 no_attributes ,
441440 False ,
442- include_last_reported ,
441+ True ,
443442 )
444443
445444
@@ -454,9 +453,6 @@ def state_changes_during_period(
454453 include_start_time_state : bool = True ,
455454) -> dict [str , list [State ]]:
456455 """Return states changes during UTC period start_time - end_time."""
457- has_last_reported = (
458- get_instance (hass ).schema_version >= LAST_REPORTED_SCHEMA_VERSION
459- )
460456 if not entity_id :
461457 raise ValueError ("entity_id must be provided" )
462458 entity_ids = [entity_id .lower ()]
@@ -489,14 +485,12 @@ def state_changes_during_period(
489485 limit ,
490486 include_start_time_state ,
491487 oldest_ts ,
492- has_last_reported ,
493488 ),
494489 track_on = [
495490 bool (end_time_ts ),
496491 no_attributes ,
497492 bool (limit ),
498493 include_start_time_state ,
499- has_last_reported ,
500494 ],
501495 )
502496 return cast (
@@ -543,10 +537,10 @@ def _get_last_state_changes_single_stmt(metadata_id: int) -> Select:
543537
544538
545539def _get_last_state_changes_multiple_stmt (
546- number_of_states : int , metadata_id : int , include_last_reported : bool
540+ number_of_states : int , metadata_id : int
547541) -> Select :
548542 return (
549- _stmt_and_join_attributes (False , False , include_last_reported )
543+ _stmt_and_join_attributes (False , False , True )
550544 .where (
551545 States .state_id
552546 == (
@@ -568,9 +562,6 @@ def get_last_state_changes(
568562 hass : HomeAssistant , number_of_states : int , entity_id : str
569563) -> dict [str , list [State ]]:
570564 """Return the last number_of_states."""
571- has_last_reported = (
572- get_instance (hass ).schema_version >= LAST_REPORTED_SCHEMA_VERSION
573- )
574565 entity_id_lower = entity_id .lower ()
575566 entity_ids = [entity_id_lower ]
576567
@@ -595,9 +586,8 @@ def get_last_state_changes(
595586 else :
596587 stmt = lambda_stmt (
597588 lambda : _get_last_state_changes_multiple_stmt (
598- number_of_states , metadata_id , has_last_reported
589+ number_of_states , metadata_id
599590 ),
600- track_on = [has_last_reported ],
601591 )
602592 states = list (execute_stmt_lambda_element (session , stmt , orm_rows = False ))
603593 return cast (
0 commit comments