Fix utility_meter startup (#69064)

This commit is contained in:
Erik Montnemery 2022-04-01 17:28:50 +02:00 committed by GitHub
parent 87100c2517
commit bda997efe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -20,7 +20,6 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR, ENERGY_WATT_HOUR,
EVENT_HOMEASSISTANT_START,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
@ -33,6 +32,7 @@ from homeassistant.helpers.event import (
async_track_state_change_event, async_track_state_change_event,
) )
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.start import async_at_start
from homeassistant.helpers.template import is_number from homeassistant.helpers.template import is_number
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -426,6 +426,10 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
) )
tariff_entity_state = self.hass.states.get(self._tariff_entity) tariff_entity_state = self.hass.states.get(self._tariff_entity)
if not tariff_entity_state:
# The utility meter is not yet added
return
self._change_status(tariff_entity_state.state) self._change_status(tariff_entity_state.state)
return return
@ -439,9 +443,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
self.hass, [self._sensor_source_id], self.async_reading self.hass, [self._sensor_source_id], self.async_reading
) )
self.hass.bus.async_listen_once( async_at_start(self.hass, async_source_tracking)
EVENT_HOMEASSISTANT_START, async_source_tracking
)
@property @property
def name(self): def name(self):

View File

@ -37,7 +37,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import State from homeassistant.core import CoreState, State
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -418,6 +418,9 @@ async def test_device_class(hass, yaml_config, config_entry_configs):
) )
async def test_restore_state(hass, yaml_config, config_entry_config): async def test_restore_state(hass, yaml_config, config_entry_config):
"""Test utility sensor restore state.""" """Test utility sensor restore state."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
last_reset = "2020-12-21T00:00:00.013073+00:00" last_reset = "2020-12-21T00:00:00.013073+00:00"
mock_restore_cache( mock_restore_cache(
hass, hass,
@ -668,6 +671,9 @@ async def test_non_net_consumption(hass, yaml_config, config_entry_config, caplo
) )
async def test_delta_values(hass, yaml_config, config_entry_config, caplog): async def test_delta_values(hass, yaml_config, config_entry_config, caplog):
"""Test utility meter "delta_values" mode.""" """Test utility meter "delta_values" mode."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
now = dt_util.utcnow() now = dt_util.utcnow()
with alter_time(now): with alter_time(now):
if yaml_config: if yaml_config: