2021-09-13 16:18:21 +00:00
|
|
|
"""Support for Netgear routers."""
|
2022-02-03 11:28:04 +00:00
|
|
|
from datetime import timedelta
|
2022-01-18 19:02:43 +00:00
|
|
|
import logging
|
|
|
|
|
2021-09-13 16:18:21 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-18 19:02:43 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL
|
2021-09-13 16:18:21 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
2022-02-03 11:28:04 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2021-09-13 16:18:21 +00:00
|
|
|
|
2022-02-03 11:28:04 +00:00
|
|
|
from .const import DOMAIN, KEY_COORDINATOR, KEY_ROUTER, PLATFORMS
|
2021-09-13 16:18:21 +00:00
|
|
|
from .errors import CannotLoginException
|
|
|
|
from .router import NetgearRouter
|
|
|
|
|
2022-01-18 19:02:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-02-03 11:28:04 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
|
|
|
|
2021-09-13 16:18:21 +00:00
|
|
|
|
2021-11-06 18:48:02 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-09-13 16:18:21 +00:00
|
|
|
"""Set up Netgear component."""
|
|
|
|
router = NetgearRouter(hass, entry)
|
|
|
|
try:
|
2022-02-03 11:28:04 +00:00
|
|
|
if not await router.async_setup():
|
|
|
|
raise ConfigEntryNotReady
|
2021-09-13 16:18:21 +00:00
|
|
|
except CannotLoginException as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
|
|
|
|
2022-01-18 19:02:43 +00:00
|
|
|
port = entry.data.get(CONF_PORT)
|
|
|
|
ssl = entry.data.get(CONF_SSL)
|
|
|
|
if port != router.port or ssl != router.ssl:
|
2022-01-19 11:11:23 +00:00
|
|
|
data = {**entry.data, CONF_PORT: router.port, CONF_SSL: router.ssl}
|
2022-01-18 19:02:43 +00:00
|
|
|
hass.config_entries.async_update_entry(entry, data=data)
|
|
|
|
_LOGGER.info(
|
2022-01-19 11:11:23 +00:00
|
|
|
"Netgear port-SSL combination updated from (%i, %r) to (%i, %r), "
|
|
|
|
"this should only occur after a firmware update",
|
2022-01-18 19:02:43 +00:00
|
|
|
port,
|
|
|
|
ssl,
|
|
|
|
router.port,
|
|
|
|
router.ssl,
|
|
|
|
)
|
|
|
|
|
2021-09-13 16:18:21 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
|
|
|
|
entry.async_on_unload(entry.add_update_listener(update_listener))
|
|
|
|
|
2021-10-27 22:58:14 +00:00
|
|
|
device_registry = dr.async_get(hass)
|
2021-09-13 16:18:21 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=entry.entry_id,
|
|
|
|
identifiers={(DOMAIN, entry.unique_id)},
|
|
|
|
manufacturer="Netgear",
|
|
|
|
name=router.device_name,
|
|
|
|
model=router.model,
|
|
|
|
sw_version=router.firmware_version,
|
2021-12-18 11:34:16 +00:00
|
|
|
configuration_url=f"http://{entry.data[CONF_HOST]}/",
|
2021-09-13 16:18:21 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 11:28:04 +00:00
|
|
|
async def async_update_data() -> bool:
|
|
|
|
"""Fetch data from the router."""
|
|
|
|
data = await router.async_update_device_trackers()
|
|
|
|
return data
|
|
|
|
|
|
|
|
# Create update coordinator
|
|
|
|
coordinator = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=router.device_name,
|
|
|
|
update_method=async_update_data,
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
KEY_ROUTER: router,
|
|
|
|
KEY_COORDINATOR: coordinator,
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:18:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-11-06 18:48:02 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-09-13 16:18:21 +00:00
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
|
|
|
|
|
|
if unload_ok:
|
2022-02-03 11:28:04 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2021-09-13 16:18:21 +00:00
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|