Include initial state in history_stats count (#71952)
parent
8f7f3f328e
commit
c74b241949
|
@ -19,7 +19,7 @@ class HistoryStatsState:
|
|||
"""The current stats of the history stats."""
|
||||
|
||||
hours_matched: float | None
|
||||
changes_to_match_state: int | None
|
||||
match_count: int | None
|
||||
period: tuple[datetime.datetime, datetime.datetime]
|
||||
|
||||
|
||||
|
@ -121,14 +121,12 @@ class HistoryStats:
|
|||
self._state = HistoryStatsState(None, None, self._period)
|
||||
return self._state
|
||||
|
||||
hours_matched, changes_to_match_state = self._async_compute_hours_and_changes(
|
||||
hours_matched, match_count = self._async_compute_hours_and_changes(
|
||||
now_timestamp,
|
||||
current_period_start_timestamp,
|
||||
current_period_end_timestamp,
|
||||
)
|
||||
self._state = HistoryStatsState(
|
||||
hours_matched, changes_to_match_state, self._period
|
||||
)
|
||||
self._state = HistoryStatsState(hours_matched, match_count, self._period)
|
||||
return self._state
|
||||
|
||||
def _update_from_database(
|
||||
|
@ -156,7 +154,7 @@ class HistoryStats:
|
|||
)
|
||||
last_state_change_timestamp = start_timestamp
|
||||
elapsed = 0.0
|
||||
changes_to_match_state = 0
|
||||
match_count = 1 if previous_state_matches else 0
|
||||
|
||||
# Make calculations
|
||||
for item in self._history_current_period:
|
||||
|
@ -166,7 +164,7 @@ class HistoryStats:
|
|||
if previous_state_matches:
|
||||
elapsed += state_change_timestamp - last_state_change_timestamp
|
||||
elif current_state_matches:
|
||||
changes_to_match_state += 1
|
||||
match_count += 1
|
||||
|
||||
previous_state_matches = current_state_matches
|
||||
last_state_change_timestamp = state_change_timestamp
|
||||
|
@ -178,4 +176,4 @@ class HistoryStats:
|
|||
|
||||
# Save value in hours
|
||||
hours_matched = elapsed / 3600
|
||||
return hours_matched, changes_to_match_state
|
||||
return hours_matched, match_count
|
||||
|
|
|
@ -166,4 +166,4 @@ class HistoryStatsSensor(HistoryStatsSensorBase):
|
|||
elif self._type == CONF_TYPE_RATIO:
|
||||
self._attr_native_value = pretty_ratio(state.hours_matched, state.period)
|
||||
elif self._type == CONF_TYPE_COUNT:
|
||||
self._attr_native_value = state.changes_to_match_state
|
||||
self._attr_native_value = state.match_count
|
||||
|
|
|
@ -438,7 +438,7 @@ async def test_measure(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "83.3"
|
||||
|
||||
|
||||
|
@ -519,7 +519,7 @@ async def test_async_on_entire_period(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.on_sensor1").state == "1.0"
|
||||
assert hass.states.get("sensor.on_sensor2").state == "1.0"
|
||||
assert hass.states.get("sensor.on_sensor3").state == "0"
|
||||
assert hass.states.get("sensor.on_sensor3").state == "1"
|
||||
assert hass.states.get("sensor.on_sensor4").state == "100.0"
|
||||
|
||||
|
||||
|
@ -886,7 +886,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.0"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.0"
|
||||
assert hass.states.get("sensor.sensor3").state == "0"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor4").state == "0.0"
|
||||
|
||||
one_hour_in = start_time + timedelta(minutes=60)
|
||||
|
@ -896,7 +896,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "1.0"
|
||||
assert hass.states.get("sensor.sensor2").state == "1.0"
|
||||
assert hass.states.get("sensor.sensor3").state == "0"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor4").state == "50.0"
|
||||
|
||||
turn_off_time = start_time + timedelta(minutes=90)
|
||||
|
@ -908,7 +908,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor2").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor3").state == "0"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor4").state == "75.0"
|
||||
|
||||
turn_back_on_time = start_time + timedelta(minutes=105)
|
||||
|
@ -918,7 +918,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor2").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor3").state == "0"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor4").state == "75.0"
|
||||
|
||||
with freeze_time(turn_back_on_time):
|
||||
|
@ -927,7 +927,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor2").state == "1.5"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "75.0"
|
||||
|
||||
end_time = start_time + timedelta(minutes=120)
|
||||
|
@ -937,7 +937,7 @@ async def test_async_start_from_history_and_switch_to_watching_state_changes_mul
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "1.75"
|
||||
assert hass.states.get("sensor.sensor2").state == "1.75"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "87.5"
|
||||
|
||||
|
||||
|
@ -1198,7 +1198,7 @@ async def test_measure_sliding_window(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "41.7"
|
||||
|
||||
past_next_update = start_time + timedelta(minutes=30)
|
||||
|
@ -1211,7 +1211,7 @@ async def test_measure_sliding_window(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "41.7"
|
||||
|
||||
|
||||
|
@ -1291,7 +1291,7 @@ async def test_measure_from_end_going_backwards(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "83.3"
|
||||
|
||||
past_next_update = start_time + timedelta(minutes=30)
|
||||
|
@ -1304,7 +1304,7 @@ async def test_measure_from_end_going_backwards(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "83.3"
|
||||
|
||||
|
||||
|
@ -1385,7 +1385,7 @@ async def test_measure_cet(hass, recorder_mock):
|
|||
|
||||
assert hass.states.get("sensor.sensor1").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor2").state == "0.83"
|
||||
assert hass.states.get("sensor.sensor3").state == "1"
|
||||
assert hass.states.get("sensor.sensor3").state == "2"
|
||||
assert hass.states.get("sensor.sensor4").state == "83.3"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue