Delay utility_meter until HA has started (#91017)

* increase information for end user

* only warn after home assistant has started

* delay utility_meter until HA has startED
This commit is contained in:
Diogo Gomes 2023-04-08 15:36:34 +01:00 committed by GitHub
parent 6c7f2167ff
commit fe393c84e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 19 deletions

View file

@ -35,7 +35,7 @@ from homeassistant.helpers.event import (
async_track_point_in_time,
async_track_state_change_event,
)
from homeassistant.helpers.start import async_at_start
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.template import is_number
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify
@ -410,8 +410,11 @@ class UtilityMeterSensor(RestoreSensor):
if (old_state_val := self._validate_state(old_state)) is not None:
return new_state_val - old_state_val
_LOGGER.warning(
"Invalid state (%s > %s)",
"%s received an invalid state change coming from %s (%s > %s)",
self.name,
self._sensor_source_id,
old_state.state if old_state else None,
new_state_val,
)
@ -423,8 +426,14 @@ class UtilityMeterSensor(RestoreSensor):
old_state: State | None = event.data.get("old_state")
new_state: State = event.data.get("new_state") # type: ignore[assignment] # a state change event always has a new state
# First check if the new_state is valid (see discussion in PR #88446)
if (new_state_val := self._validate_state(new_state)) is None:
_LOGGER.warning("Invalid state %s", new_state.state)
_LOGGER.warning(
"%s received an invalid new state from %s : %s",
self.name,
self._sensor_source_id,
new_state.state,
)
return
if self._state is None:
@ -597,7 +606,7 @@ class UtilityMeterSensor(RestoreSensor):
self.hass, [self._sensor_source_id], self.async_reading
)
self.async_on_remove(async_at_start(self.hass, async_source_tracking))
self.async_on_remove(async_at_started(self.hass, async_source_tracking))
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""