mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix dropouts in history_stats graphs on restart (#73110)
This commit is contained in:
parent
a9ab98fb45
commit
0505c596a5
@ -21,6 +21,7 @@ from homeassistant.const import (
|
||||
TIME_HOURS,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
@ -101,6 +102,9 @@ async def async_setup_platform(
|
||||
|
||||
history_stats = HistoryStats(hass, entity_id, entity_states, start, end, duration)
|
||||
coordinator = HistoryStatsUpdateCoordinator(hass, history_stats, name)
|
||||
await coordinator.async_refresh()
|
||||
if not coordinator.last_update_success:
|
||||
raise PlatformNotReady from coordinator.last_exception
|
||||
async_add_entities([HistoryStatsSensor(coordinator, sensor_type, name)])
|
||||
|
||||
|
||||
@ -152,6 +156,7 @@ class HistoryStatsSensor(HistoryStatsSensorBase):
|
||||
super().__init__(coordinator, name)
|
||||
self._attr_native_unit_of_measurement = UNITS[sensor_type]
|
||||
self._type = sensor_type
|
||||
self._process_update()
|
||||
|
||||
@callback
|
||||
def _process_update(self) -> None:
|
||||
|
@ -9,7 +9,7 @@ import pytest
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.history_stats import DOMAIN
|
||||
from homeassistant.const import SERVICE_RELOAD, STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||
from homeassistant.const import SERVICE_RELOAD, STATE_UNKNOWN
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.helpers.entity_component import async_update_entity
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
@ -50,7 +50,7 @@ class TestHistoryStatsSensor(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
assert state.state == "0.0"
|
||||
|
||||
def test_setup_multiple_states(self):
|
||||
"""Test the history statistics sensor setup for multiple states."""
|
||||
@ -71,7 +71,7 @@ class TestHistoryStatsSensor(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNKNOWN
|
||||
assert state.state == "0.0"
|
||||
|
||||
def test_wrong_duration(self):
|
||||
"""Test when duration value is not a timedelta."""
|
||||
@ -153,12 +153,12 @@ async def test_invalid_date_for_start(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNKNOWN
|
||||
assert hass.states.get("sensor.test") is None
|
||||
next_update_time = dt_util.utcnow() + timedelta(minutes=1)
|
||||
with freeze_time(next_update_time):
|
||||
async_fire_time_changed(hass, next_update_time)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNAVAILABLE
|
||||
assert hass.states.get("sensor.test") is None
|
||||
|
||||
|
||||
async def test_invalid_date_for_end(hass, recorder_mock):
|
||||
@ -178,12 +178,12 @@ async def test_invalid_date_for_end(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNKNOWN
|
||||
assert hass.states.get("sensor.test") is None
|
||||
next_update_time = dt_util.utcnow() + timedelta(minutes=1)
|
||||
with freeze_time(next_update_time):
|
||||
async_fire_time_changed(hass, next_update_time)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNAVAILABLE
|
||||
assert hass.states.get("sensor.test") is None
|
||||
|
||||
|
||||
async def test_invalid_entity_in_template(hass, recorder_mock):
|
||||
@ -203,12 +203,12 @@ async def test_invalid_entity_in_template(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNKNOWN
|
||||
assert hass.states.get("sensor.test") is None
|
||||
next_update_time = dt_util.utcnow() + timedelta(minutes=1)
|
||||
with freeze_time(next_update_time):
|
||||
async_fire_time_changed(hass, next_update_time)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNAVAILABLE
|
||||
assert hass.states.get("sensor.test") is None
|
||||
|
||||
|
||||
async def test_invalid_entity_returning_none_in_template(hass, recorder_mock):
|
||||
@ -228,12 +228,12 @@ async def test_invalid_entity_returning_none_in_template(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNKNOWN
|
||||
assert hass.states.get("sensor.test") is None
|
||||
next_update_time = dt_util.utcnow() + timedelta(minutes=1)
|
||||
with freeze_time(next_update_time):
|
||||
async_fire_time_changed(hass, next_update_time)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.test").state == STATE_UNAVAILABLE
|
||||
assert hass.states.get("sensor.test") is None
|
||||
|
||||
|
||||
async def test_reload(hass, recorder_mock):
|
||||
@ -302,6 +302,10 @@ async def test_measure_multiple(hass, recorder_mock):
|
||||
]
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
@ -347,11 +351,6 @@ async def test_measure_multiple(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
for i in range(1, 5):
|
||||
await async_update_entity(hass, f"sensor.sensor{i}")
|
||||
await hass.async_block_till_done()
|
||||
@ -382,6 +381,10 @@ async def test_measure(hass, recorder_mock):
|
||||
]
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
@ -427,11 +430,6 @@ async def test_measure(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
for i in range(1, 5):
|
||||
await async_update_entity(hass, f"sensor.sensor{i}")
|
||||
await hass.async_block_till_done()
|
||||
@ -463,6 +461,10 @@ async def test_async_on_entire_period(hass, recorder_mock):
|
||||
]
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
@ -508,11 +510,6 @@ async def test_async_on_entire_period(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
for i in range(1, 5):
|
||||
await async_update_entity(hass, f"sensor.on_sensor{i}")
|
||||
await hass.async_block_till_done()
|
||||
@ -1235,6 +1232,10 @@ async def test_measure_from_end_going_backwards(hass, recorder_mock):
|
||||
]
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
), freeze_time(start_time):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
@ -1280,11 +1281,6 @@ async def test_measure_from_end_going_backwards(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
), freeze_time(start_time):
|
||||
for i in range(1, 5):
|
||||
await async_update_entity(hass, f"sensor.sensor{i}")
|
||||
await hass.async_block_till_done()
|
||||
@ -1329,6 +1325,10 @@ async def test_measure_cet(hass, recorder_mock):
|
||||
]
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
@ -1374,11 +1374,6 @@ async def test_measure_cet(hass, recorder_mock):
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.recorder.history.state_changes_during_period",
|
||||
_fake_states,
|
||||
):
|
||||
for i in range(1, 5):
|
||||
await async_update_entity(hass, f"sensor.sensor{i}")
|
||||
await hass.async_block_till_done()
|
||||
|
Loading…
x
Reference in New Issue
Block a user