mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Migrate cloudflare to use runtime data (#146429)
This commit is contained in:
parent
6a23ad96ca
commit
0c18fe35e5
@ -3,7 +3,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
@ -26,8 +27,18 @@ from .const import CONF_RECORDS, DEFAULT_UPDATE_INTERVAL, DOMAIN, SERVICE_UPDATE
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type CloudflareConfigEntry = ConfigEntry[CloudflareRuntimeData]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
@dataclass
|
||||||
|
class CloudflareRuntimeData:
|
||||||
|
"""Runtime data for Cloudflare config entry."""
|
||||||
|
|
||||||
|
client: pycfdns.Client
|
||||||
|
dns_zone: pycfdns.ZoneModel
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: CloudflareConfigEntry) -> bool:
|
||||||
"""Set up Cloudflare from a config entry."""
|
"""Set up Cloudflare from a config entry."""
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
client = pycfdns.Client(
|
client = pycfdns.Client(
|
||||||
@ -45,12 +56,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except pycfdns.ComunicationException as error:
|
except pycfdns.ComunicationException as error:
|
||||||
raise ConfigEntryNotReady from error
|
raise ConfigEntryNotReady from error
|
||||||
|
|
||||||
async def update_records(now):
|
entry.runtime_data = CloudflareRuntimeData(client, dns_zone)
|
||||||
|
|
||||||
|
async def update_records(now: datetime) -> None:
|
||||||
"""Set up recurring update."""
|
"""Set up recurring update."""
|
||||||
try:
|
try:
|
||||||
await _async_update_cloudflare(
|
await _async_update_cloudflare(hass, entry)
|
||||||
hass, client, dns_zone, entry.data[CONF_RECORDS]
|
|
||||||
)
|
|
||||||
except (
|
except (
|
||||||
pycfdns.AuthenticationException,
|
pycfdns.AuthenticationException,
|
||||||
pycfdns.ComunicationException,
|
pycfdns.ComunicationException,
|
||||||
@ -60,9 +71,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
async def update_records_service(call: ServiceCall) -> None:
|
async def update_records_service(call: ServiceCall) -> None:
|
||||||
"""Set up service for manual trigger."""
|
"""Set up service for manual trigger."""
|
||||||
try:
|
try:
|
||||||
await _async_update_cloudflare(
|
await _async_update_cloudflare(hass, entry)
|
||||||
hass, client, dns_zone, entry.data[CONF_RECORDS]
|
|
||||||
)
|
|
||||||
except (
|
except (
|
||||||
pycfdns.AuthenticationException,
|
pycfdns.AuthenticationException,
|
||||||
pycfdns.ComunicationException,
|
pycfdns.ComunicationException,
|
||||||
@ -79,7 +88,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: CloudflareConfigEntry) -> bool:
|
||||||
"""Unload Cloudflare config entry."""
|
"""Unload Cloudflare config entry."""
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -87,10 +96,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def _async_update_cloudflare(
|
async def _async_update_cloudflare(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
client: pycfdns.Client,
|
entry: CloudflareConfigEntry,
|
||||||
dns_zone: pycfdns.ZoneModel,
|
|
||||||
target_records: list[str],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
client = entry.runtime_data.client
|
||||||
|
dns_zone = entry.runtime_data.dns_zone
|
||||||
|
target_records: list[str] = entry.data[CONF_RECORDS]
|
||||||
|
|
||||||
_LOGGER.debug("Starting update for zone %s", dns_zone["name"])
|
_LOGGER.debug("Starting update for zone %s", dns_zone["name"])
|
||||||
|
|
||||||
records = await client.list_dns_records(zone_id=dns_zone["id"], type="A")
|
records = await client.list_dns_records(zone_id=dns_zone["id"], type="A")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user