mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix exception during history_stats startup (#16932)
* Fix exception during history_stats startup * Do not track changes during startup * Ignore args
This commit is contained in:
parent
3ddad83a84
commit
caaf4f5694
@ -10,6 +10,7 @@ import math
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.core import callback
|
||||||
from homeassistant.components import history
|
from homeassistant.components import history
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@ -19,7 +20,7 @@ from homeassistant.const import (
|
|||||||
EVENT_HOMEASSISTANT_START)
|
EVENT_HOMEASSISTANT_START)
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import async_track_state_change
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -94,8 +95,6 @@ class HistoryStatsSensor(Entity):
|
|||||||
self, hass, entity_id, entity_state, start, end, duration,
|
self, hass, entity_id, entity_state, start, end, duration,
|
||||||
sensor_type, name):
|
sensor_type, name):
|
||||||
"""Initialize the HistoryStats sensor."""
|
"""Initialize the HistoryStats sensor."""
|
||||||
self._hass = hass
|
|
||||||
|
|
||||||
self._entity_id = entity_id
|
self._entity_id = entity_id
|
||||||
self._entity_state = entity_state
|
self._entity_state = entity_state
|
||||||
self._duration = duration
|
self._duration = duration
|
||||||
@ -109,15 +108,19 @@ class HistoryStatsSensor(Entity):
|
|||||||
self.value = None
|
self.value = None
|
||||||
self.count = None
|
self.count = None
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def start_refresh(*args):
|
||||||
|
"""Register state tracking."""
|
||||||
|
@callback
|
||||||
def force_refresh(*args):
|
def force_refresh(*args):
|
||||||
"""Force the component to refresh."""
|
"""Force the component to refresh."""
|
||||||
self.schedule_update_ha_state(True)
|
self.async_schedule_update_ha_state(True)
|
||||||
|
|
||||||
# Update value when home assistant starts
|
force_refresh()
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, force_refresh)
|
async_track_state_change(self.hass, self._entity_id, force_refresh)
|
||||||
|
|
||||||
# Update value when tracked entity changes its state
|
# Delay first refresh to keep startup fast
|
||||||
track_state_change(hass, entity_id, force_refresh)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_refresh)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user