2021-02-24 11:04:38 +00:00
|
|
|
"""The Mullvad VPN integration."""
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import async_timeout
|
|
|
|
from mullvad_api import MullvadAPI
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-06 03:06:35 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-02-24 11:04:38 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers import update_coordinator
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
2021-12-06 03:06:35 +00:00
|
|
|
PLATFORMS = [Platform.BINARY_SENSOR]
|
2021-02-24 11:04:38 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:28:14 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Set up Mullvad VPN integration."""
|
|
|
|
|
|
|
|
async def async_get_mullvad_api_data():
|
2021-11-04 15:07:50 +00:00
|
|
|
async with async_timeout.timeout(10):
|
2021-02-24 11:04:38 +00:00
|
|
|
api = await hass.async_add_executor_job(MullvadAPI)
|
|
|
|
return api.data
|
|
|
|
|
2021-02-24 12:43:44 +00:00
|
|
|
coordinator = update_coordinator.DataUpdateCoordinator(
|
2021-02-24 11:04:38 +00:00
|
|
|
hass,
|
|
|
|
logging.getLogger(__name__),
|
|
|
|
name=DOMAIN,
|
|
|
|
update_method=async_get_mullvad_api_data,
|
|
|
|
update_interval=timedelta(minutes=1),
|
|
|
|
)
|
2021-03-29 22:51:39 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2021-02-24 12:43:44 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN] = coordinator
|
2021-02-24 11:04:38 +00:00
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2021-02-24 11:04:38 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-24 11:04:38 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 16:49:13 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-02-24 11:04:38 +00:00
|
|
|
if unload_ok:
|
|
|
|
del hass.data[DOMAIN]
|
|
|
|
|
|
|
|
return unload_ok
|