core/homeassistant/components/mullvad/__init__.py

49 lines
1.3 KiB
Python
Raw Normal View History

"""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
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]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""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):
api = await hass.async_add_executor_job(MullvadAPI)
return api.data
coordinator = update_coordinator.DataUpdateCoordinator(
hass,
logging.getLogger(__name__),
name=DOMAIN,
update_method=async_get_mullvad_api_data,
update_interval=timedelta(minutes=1),
)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN] = coordinator
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
del hass.data[DOMAIN]
return unload_ok