2019-02-13 20:21:14 +00:00
|
|
|
"""Support for testing internet speed via Fast.com."""
|
2021-07-05 08:19:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-04 14:20:14 +00:00
|
|
|
import logging
|
2019-02-02 05:50:22 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2023-11-21 08:59:34 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2023-12-11 21:28:04 +00:00
|
|
|
from homeassistant.const import CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_STARTED
|
|
|
|
from homeassistant.core import CoreState, Event, HomeAssistant
|
2019-12-04 14:20:14 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-07-05 08:19:37 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
from .const import CONF_MANUAL, DEFAULT_INTERVAL, DOMAIN, PLATFORMS
|
|
|
|
from .coordinator import FastdotcomDataUpdateCoordindator
|
2019-02-02 05:50:22 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_INTERVAL): vol.All(
|
|
|
|
cv.time_period, cv.positive_timedelta
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_MANUAL, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-02-02 05:50:22 +00:00
|
|
|
|
|
|
|
|
2023-11-21 08:59:34 +00:00
|
|
|
async def async_setup_platform(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
|
|
"""Set up the Fast.com component. (deprecated)."""
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config[DOMAIN],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2023-12-11 21:28:04 +00:00
|
|
|
"""Set up Fast.com from a config entry."""
|
|
|
|
coordinator = FastdotcomDataUpdateCoordindator(hass)
|
2019-02-04 06:43:59 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
async def _request_refresh(event: Event) -> None:
|
|
|
|
"""Request a refresh."""
|
|
|
|
await coordinator.async_request_refresh()
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
if hass.state == CoreState.running:
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
else:
|
|
|
|
# Don't start the speedtest when HA is starting up
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _request_refresh)
|
2019-02-02 05:50:22 +00:00
|
|
|
|
2023-12-11 21:28:04 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
2023-11-21 08:59:34 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(
|
|
|
|
entry,
|
|
|
|
PLATFORMS,
|
2022-01-10 09:30:47 +00:00
|
|
|
)
|
2019-02-02 05:50:22 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-11-21 08:59:34 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload Fast.com config entry."""
|
2023-12-11 21:28:04 +00:00
|
|
|
hass.services.async_remove(DOMAIN, "speedtest")
|
2023-11-21 08:59:34 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2023-12-11 21:28:04 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2023-11-21 08:59:34 +00:00
|
|
|
return unload_ok
|