mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Prevent speedtest from blocking startup or causing other intergations to fail setup (#38305)
When speedtest starts up, it would saturate the network interface and cause other integrations to randomly fail to setup. We now wait to do the first speed test until after the started event is fired.
This commit is contained in:
parent
f06ae1fa95
commit
77b6f8c9f2
@ -6,7 +6,12 @@ import speedtest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_SCAN_INTERVAL
|
||||
from homeassistant.const import (
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_SCAN_INTERVAL,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
)
|
||||
from homeassistant.core import CoreState
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
@ -70,10 +75,25 @@ async def async_setup_entry(hass, config_entry):
|
||||
coordinator = SpeedTestDataCoordinator(hass, config_entry)
|
||||
await coordinator.async_setup()
|
||||
|
||||
if not config_entry.options[CONF_MANUAL]:
|
||||
async def _enable_scheduled_speedtests(*_):
|
||||
"""Activate the data update coordinator."""
|
||||
coordinator.update_interval = timedelta(
|
||||
minutes=config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
)
|
||||
await coordinator.async_refresh()
|
||||
if not coordinator.last_update_success:
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
if not config_entry.options[CONF_MANUAL]:
|
||||
if hass.state == CoreState.running:
|
||||
await _enable_scheduled_speedtests()
|
||||
if not coordinator.last_update_success:
|
||||
raise ConfigEntryNotReady
|
||||
else:
|
||||
# Running a speed test during startup can prevent
|
||||
# integrations from being able to setup because it
|
||||
# can saturate the network interface.
|
||||
hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STARTED, _enable_scheduled_speedtests
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = coordinator
|
||||
|
||||
@ -107,12 +127,6 @@ class SpeedTestDataCoordinator(DataUpdateCoordinator):
|
||||
super().__init__(
|
||||
self.hass, _LOGGER, name=DOMAIN, update_method=self.async_update,
|
||||
)
|
||||
if not self.config_entry.options.get(CONF_MANUAL):
|
||||
self.update_interval = timedelta(
|
||||
minutes=self.config_entry.options.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
)
|
||||
|
||||
def update_servers(self):
|
||||
"""Update list of test servers."""
|
||||
|
@ -12,7 +12,6 @@ from .const import (
|
||||
ATTR_SERVER_ID,
|
||||
ATTR_SERVER_NAME,
|
||||
ATTRIBUTION,
|
||||
CONF_MANUAL,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
ICON,
|
||||
@ -97,10 +96,9 @@ class SpeedtestSensor(RestoreEntity):
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity which will be added."""
|
||||
await super().async_added_to_hass()
|
||||
if self.coordinator.config_entry.options[CONF_MANUAL]:
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
self._state = state.state
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
self._state = state.state
|
||||
|
||||
@callback
|
||||
def update():
|
||||
|
Loading…
x
Reference in New Issue
Block a user