Fix utility_meter startup ()

pull/69089/head
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
homeassistant/components/utility_meter
tests/components/utility_meter

View File

@ -20,7 +20,6 @@ from homeassistant.const import (
CONF_NAME,
ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR,
EVENT_HOMEASSISTANT_START,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
@ -33,6 +32,7 @@ from homeassistant.helpers.event import (
async_track_state_change_event,
)
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.typing import ConfigType, DiscoveryInfoType
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)
if not tariff_entity_state:
# The utility meter is not yet added
return
self._change_status(tariff_entity_state.state)
return
@ -439,9 +443,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
self.hass, [self._sensor_source_id], self.async_reading
)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, async_source_tracking
)
async_at_start(self.hass, async_source_tracking)
@property
def name(self):

View File

@ -37,7 +37,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import State
from homeassistant.core import CoreState, State
from homeassistant.setup import async_setup_component
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):
"""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"
mock_restore_cache(
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):
"""Test utility meter "delta_values" mode."""
# Home assistant is not runnit yet
hass.state = CoreState.not_running
now = dt_util.utcnow()
with alter_time(now):
if yaml_config: