From 0c18fe35e5dc751bfc80bac74130aa2fffb36a08 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 10 Jun 2025 09:50:31 +0200 Subject: [PATCH] Migrate cloudflare to use runtime data (#146429) --- .../components/cloudflare/__init__.py | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/cloudflare/__init__.py b/homeassistant/components/cloudflare/__init__.py index f8fbac396a6..9a05cf48c59 100644 --- a/homeassistant/components/cloudflare/__init__.py +++ b/homeassistant/components/cloudflare/__init__.py @@ -3,7 +3,8 @@ from __future__ import annotations import asyncio -from datetime import timedelta +from dataclasses import dataclass +from datetime import datetime, timedelta import logging import socket @@ -26,8 +27,18 @@ from .const import CONF_RECORDS, DEFAULT_UPDATE_INTERVAL, DOMAIN, SERVICE_UPDATE _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.""" session = async_get_clientsession(hass) client = pycfdns.Client( @@ -45,12 +56,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: except pycfdns.ComunicationException as 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.""" try: - await _async_update_cloudflare( - hass, client, dns_zone, entry.data[CONF_RECORDS] - ) + await _async_update_cloudflare(hass, entry) except ( pycfdns.AuthenticationException, pycfdns.ComunicationException, @@ -60,9 +71,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def update_records_service(call: ServiceCall) -> None: """Set up service for manual trigger.""" try: - await _async_update_cloudflare( - hass, client, dns_zone, entry.data[CONF_RECORDS] - ) + await _async_update_cloudflare(hass, entry) except ( pycfdns.AuthenticationException, pycfdns.ComunicationException, @@ -79,7 +88,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: 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.""" return True @@ -87,10 +96,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def _async_update_cloudflare( hass: HomeAssistant, - client: pycfdns.Client, - dns_zone: pycfdns.ZoneModel, - target_records: list[str], + entry: CloudflareConfigEntry, ) -> 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"]) records = await client.list_dns_records(zone_id=dns_zone["id"], type="A")