mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Move NextDNS data update coordinators to the coordinator module (#115919)
Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
parent
09ae8b9f52
commit
354e8e92f3
@ -4,31 +4,15 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
|
||||||
from typing import TypeVar
|
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ClientConnectorError
|
from aiohttp.client_exceptions import ClientConnectorError
|
||||||
from nextdns import (
|
from nextdns import ApiError, NextDns
|
||||||
AnalyticsDnssec,
|
|
||||||
AnalyticsEncryption,
|
|
||||||
AnalyticsIpVersions,
|
|
||||||
AnalyticsProtocols,
|
|
||||||
AnalyticsStatus,
|
|
||||||
ApiError,
|
|
||||||
ConnectionStatus,
|
|
||||||
InvalidApiKeyError,
|
|
||||||
NextDns,
|
|
||||||
Settings,
|
|
||||||
)
|
|
||||||
from nextdns.model import NextDnsData
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_API_KEY, Platform
|
from homeassistant.const import CONF_API_KEY, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_CONNECTION,
|
ATTR_CONNECTION,
|
||||||
@ -44,104 +28,16 @@ from .const import (
|
|||||||
UPDATE_INTERVAL_CONNECTION,
|
UPDATE_INTERVAL_CONNECTION,
|
||||||
UPDATE_INTERVAL_SETTINGS,
|
UPDATE_INTERVAL_SETTINGS,
|
||||||
)
|
)
|
||||||
|
from .coordinator import (
|
||||||
CoordinatorDataT = TypeVar("CoordinatorDataT", bound=NextDnsData)
|
NextDnsConnectionUpdateCoordinator,
|
||||||
|
NextDnsDnssecUpdateCoordinator,
|
||||||
|
NextDnsEncryptionUpdateCoordinator,
|
||||||
class NextDnsUpdateCoordinator(DataUpdateCoordinator[CoordinatorDataT]): # pylint: disable=hass-enforce-coordinator-module
|
NextDnsIpVersionsUpdateCoordinator,
|
||||||
"""Class to manage fetching NextDNS data API."""
|
NextDnsProtocolsUpdateCoordinator,
|
||||||
|
NextDnsSettingsUpdateCoordinator,
|
||||||
def __init__(
|
NextDnsStatusUpdateCoordinator,
|
||||||
self,
|
NextDnsUpdateCoordinator,
|
||||||
hass: HomeAssistant,
|
)
|
||||||
nextdns: NextDns,
|
|
||||||
profile_id: str,
|
|
||||||
update_interval: timedelta,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize."""
|
|
||||||
self.nextdns = nextdns
|
|
||||||
self.profile_id = profile_id
|
|
||||||
self.profile_name = nextdns.get_profile_name(profile_id)
|
|
||||||
self.device_info = DeviceInfo(
|
|
||||||
configuration_url=f"https://my.nextdns.io/{profile_id}/setup",
|
|
||||||
entry_type=DeviceEntryType.SERVICE,
|
|
||||||
identifiers={(DOMAIN, str(profile_id))},
|
|
||||||
manufacturer="NextDNS Inc.",
|
|
||||||
name=self.profile_name,
|
|
||||||
)
|
|
||||||
|
|
||||||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)
|
|
||||||
|
|
||||||
async def _async_update_data(self) -> CoordinatorDataT:
|
|
||||||
"""Update data via internal method."""
|
|
||||||
try:
|
|
||||||
async with asyncio.timeout(10):
|
|
||||||
return await self._async_update_data_internal()
|
|
||||||
except (ApiError, ClientConnectorError, InvalidApiKeyError) as err:
|
|
||||||
raise UpdateFailed(err) from err
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> CoordinatorDataT:
|
|
||||||
"""Update data via library."""
|
|
||||||
raise NotImplementedError("Update method not implemented")
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsStatusUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsStatus]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS analytics status data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> AnalyticsStatus:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_analytics_status(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsDnssecUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsDnssec]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS analytics Dnssec data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> AnalyticsDnssec:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_analytics_dnssec(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsEncryptionUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsEncryption]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS analytics encryption data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> AnalyticsEncryption:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_analytics_encryption(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsIpVersionsUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsIpVersions]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS analytics IP versions data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> AnalyticsIpVersions:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_analytics_ip_versions(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsProtocolsUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsProtocols]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS analytics protocols data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> AnalyticsProtocols:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_analytics_protocols(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsSettingsUpdateCoordinator(NextDnsUpdateCoordinator[Settings]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS connection data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> Settings:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.get_settings(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
class NextDnsConnectionUpdateCoordinator(NextDnsUpdateCoordinator[ConnectionStatus]): # pylint: disable=hass-enforce-coordinator-module
|
|
||||||
"""Class to manage fetching NextDNS connection data from API."""
|
|
||||||
|
|
||||||
async def _async_update_data_internal(self) -> ConnectionStatus:
|
|
||||||
"""Update data via library."""
|
|
||||||
return await self.nextdns.connection_status(self.profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, Platform.SWITCH]
|
||||||
COORDINATORS: list[tuple[str, type[NextDnsUpdateCoordinator], timedelta]] = [
|
COORDINATORS: list[tuple[str, type[NextDnsUpdateCoordinator], timedelta]] = [
|
||||||
|
@ -19,8 +19,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import CoordinatorDataT, NextDnsConnectionUpdateCoordinator
|
|
||||||
from .const import ATTR_CONNECTION, DOMAIN
|
from .const import ATTR_CONNECTION, DOMAIN
|
||||||
|
from .coordinator import CoordinatorDataT, NextDnsConnectionUpdateCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import NextDnsStatusUpdateCoordinator
|
|
||||||
from .const import ATTR_STATUS, DOMAIN
|
from .const import ATTR_STATUS, DOMAIN
|
||||||
|
from .coordinator import NextDnsStatusUpdateCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
124
homeassistant/components/nextdns/coordinator.py
Normal file
124
homeassistant/components/nextdns/coordinator.py
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
"""NextDns coordinator."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
|
from aiohttp.client_exceptions import ClientConnectorError
|
||||||
|
from nextdns import (
|
||||||
|
AnalyticsDnssec,
|
||||||
|
AnalyticsEncryption,
|
||||||
|
AnalyticsIpVersions,
|
||||||
|
AnalyticsProtocols,
|
||||||
|
AnalyticsStatus,
|
||||||
|
ApiError,
|
||||||
|
ConnectionStatus,
|
||||||
|
InvalidApiKeyError,
|
||||||
|
NextDns,
|
||||||
|
Settings,
|
||||||
|
)
|
||||||
|
from nextdns.model import NextDnsData
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
CoordinatorDataT = TypeVar("CoordinatorDataT", bound=NextDnsData)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsUpdateCoordinator(DataUpdateCoordinator[CoordinatorDataT]):
|
||||||
|
"""Class to manage fetching NextDNS data API."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
nextdns: NextDns,
|
||||||
|
profile_id: str,
|
||||||
|
update_interval: timedelta,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize."""
|
||||||
|
self.nextdns = nextdns
|
||||||
|
self.profile_id = profile_id
|
||||||
|
self.profile_name = nextdns.get_profile_name(profile_id)
|
||||||
|
self.device_info = DeviceInfo(
|
||||||
|
configuration_url=f"https://my.nextdns.io/{profile_id}/setup",
|
||||||
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
|
identifiers={(DOMAIN, str(profile_id))},
|
||||||
|
manufacturer="NextDNS Inc.",
|
||||||
|
name=self.profile_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> CoordinatorDataT:
|
||||||
|
"""Update data via internal method."""
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(10):
|
||||||
|
return await self._async_update_data_internal()
|
||||||
|
except (ApiError, ClientConnectorError, InvalidApiKeyError) as err:
|
||||||
|
raise UpdateFailed(err) from err
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> CoordinatorDataT:
|
||||||
|
"""Update data via library."""
|
||||||
|
raise NotImplementedError("Update method not implemented")
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsStatusUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsStatus]):
|
||||||
|
"""Class to manage fetching NextDNS analytics status data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> AnalyticsStatus:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_analytics_status(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsDnssecUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsDnssec]):
|
||||||
|
"""Class to manage fetching NextDNS analytics Dnssec data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> AnalyticsDnssec:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_analytics_dnssec(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsEncryptionUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsEncryption]):
|
||||||
|
"""Class to manage fetching NextDNS analytics encryption data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> AnalyticsEncryption:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_analytics_encryption(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsIpVersionsUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsIpVersions]):
|
||||||
|
"""Class to manage fetching NextDNS analytics IP versions data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> AnalyticsIpVersions:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_analytics_ip_versions(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsProtocolsUpdateCoordinator(NextDnsUpdateCoordinator[AnalyticsProtocols]):
|
||||||
|
"""Class to manage fetching NextDNS analytics protocols data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> AnalyticsProtocols:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_analytics_protocols(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsSettingsUpdateCoordinator(NextDnsUpdateCoordinator[Settings]):
|
||||||
|
"""Class to manage fetching NextDNS connection data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> Settings:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.get_settings(self.profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
class NextDnsConnectionUpdateCoordinator(NextDnsUpdateCoordinator[ConnectionStatus]):
|
||||||
|
"""Class to manage fetching NextDNS connection data from API."""
|
||||||
|
|
||||||
|
async def _async_update_data_internal(self) -> ConnectionStatus:
|
||||||
|
"""Update data via library."""
|
||||||
|
return await self.nextdns.connection_status(self.profile_id)
|
@ -26,7 +26,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import CoordinatorDataT, NextDnsUpdateCoordinator
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_DNSSEC,
|
ATTR_DNSSEC,
|
||||||
ATTR_ENCRYPTION,
|
ATTR_ENCRYPTION,
|
||||||
@ -35,6 +34,7 @@ from .const import (
|
|||||||
ATTR_STATUS,
|
ATTR_STATUS,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
from .coordinator import CoordinatorDataT, NextDnsUpdateCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ from homeassistant.exceptions import HomeAssistantError
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import CoordinatorDataT, NextDnsSettingsUpdateCoordinator
|
|
||||||
from .const import ATTR_SETTINGS, DOMAIN
|
from .const import ATTR_SETTINGS, DOMAIN
|
||||||
|
from .coordinator import CoordinatorDataT, NextDnsSettingsUpdateCoordinator
|
||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user