2019-02-13 20:21:14 +00:00
|
|
|
"""Support for testing internet speed via Speedtest.net."""
|
2021-07-22 10:25:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-04-04 06:31:55 +00:00
|
|
|
from datetime import timedelta
|
2022-12-18 09:57:17 +00:00
|
|
|
from functools import partial
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2019-10-15 23:20:59 +00:00
|
|
|
import speedtest
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2021-09-23 18:44:59 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-18 09:57:17 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
EVENT_HOMEASSISTANT_STARTED,
|
|
|
|
Platform,
|
2020-06-10 16:33:48 +00:00
|
|
|
)
|
2022-12-18 09:57:17 +00:00
|
|
|
from homeassistant.core import CoreState, Event, HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-12-18 09:57:17 +00:00
|
|
|
from .const import CONF_MANUAL, DEFAULT_SCAN_INTERVAL, DOMAIN
|
|
|
|
from .coordinator import SpeedTestDataCoordinator
|
|
|
|
|
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2019-02-04 08:47:04 +00:00
|
|
|
|
|
|
|
|
2021-07-22 10:25:54 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-06-10 16:33:48 +00:00
|
|
|
"""Set up the Speedtest.net component."""
|
2022-12-18 09:57:17 +00:00
|
|
|
try:
|
|
|
|
api = await hass.async_add_executor_job(
|
|
|
|
partial(speedtest.Speedtest, secure=True)
|
|
|
|
)
|
|
|
|
coordinator = SpeedTestDataCoordinator(hass, config_entry, api)
|
|
|
|
await hass.async_add_executor_job(coordinator.update_servers)
|
|
|
|
except speedtest.SpeedtestException as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
2020-06-10 16:33:48 +00:00
|
|
|
|
2022-12-18 09:57:17 +00:00
|
|
|
async def _enable_scheduled_speedtests(event: Event | None = None) -> None:
|
2020-07-28 05:57:36 +00:00
|
|
|
"""Activate the data update coordinator."""
|
|
|
|
coordinator.update_interval = timedelta(
|
|
|
|
minutes=config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
|
|
|
)
|
2020-07-06 22:18:56 +00:00
|
|
|
await coordinator.async_refresh()
|
2020-07-28 05:57:36 +00:00
|
|
|
|
2021-07-22 10:25:54 +00:00
|
|
|
if not config_entry.options.get(CONF_MANUAL, False):
|
2020-07-28 05:57:36 +00:00
|
|
|
if hass.state == CoreState.running:
|
|
|
|
await _enable_scheduled_speedtests()
|
|
|
|
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
|
|
|
|
)
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2020-06-10 16:33:48 +00:00
|
|
|
hass.data[DOMAIN] = coordinator
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2019-02-04 08:47:04 +00:00
|
|
|
|
2022-12-18 09:57:17 +00:00
|
|
|
config_entry.async_on_unload(
|
|
|
|
config_entry.add_update_listener(options_updated_listener)
|
|
|
|
)
|
|
|
|
|
2019-02-04 08:47:04 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-07-22 10:25:54 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-06-10 16:33:48 +00:00
|
|
|
"""Unload SpeedTest Entry from config_entry."""
|
2022-12-18 09:57:17 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(
|
2021-04-27 20:10:04 +00:00
|
|
|
config_entry, PLATFORMS
|
2022-12-18 09:57:17 +00:00
|
|
|
):
|
2021-04-27 20:10:04 +00:00
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
return unload_ok
|
2020-06-10 16:33:48 +00:00
|
|
|
|
|
|
|
|
2021-07-22 10:25:54 +00:00
|
|
|
async def options_updated_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2020-06-10 16:33:48 +00:00
|
|
|
"""Handle options update."""
|
2022-12-18 09:57:17 +00:00
|
|
|
coordinator: SpeedTestDataCoordinator = hass.data[DOMAIN]
|
2020-07-13 19:32:22 +00:00
|
|
|
if entry.options[CONF_MANUAL]:
|
2022-12-18 09:57:17 +00:00
|
|
|
coordinator.update_interval = None
|
2020-06-10 16:33:48 +00:00
|
|
|
return
|
2020-07-13 19:32:22 +00:00
|
|
|
|
2022-12-18 09:57:17 +00:00
|
|
|
coordinator.update_interval = timedelta(minutes=entry.options[CONF_SCAN_INTERVAL])
|
|
|
|
await coordinator.async_request_refresh()
|