1 """Update the IP addresses of your Cloudflare DNS records."""
3 from __future__
import annotations
6 from datetime
import timedelta
16 ConfigEntryAuthFailed,
25 from .const
import CONF_RECORDS, DEFAULT_UPDATE_INTERVAL, DOMAIN, SERVICE_UPDATE_RECORDS
27 _LOGGER = logging.getLogger(__name__)
31 """Set up Cloudflare from a config entry."""
33 client = pycfdns.Client(
34 api_token=entry.data[CONF_API_TOKEN],
35 client_session=session,
39 dns_zones = await client.list_zones()
41 zone
for zone
in dns_zones
if zone[
"name"] == entry.data[CONF_ZONE]
43 except pycfdns.AuthenticationException
as error:
44 raise ConfigEntryAuthFailed
from error
45 except pycfdns.ComunicationException
as error:
46 raise ConfigEntryNotReady
from error
48 async
def update_records(now):
49 """Set up recurring update."""
52 hass, client, dns_zone, entry.data[CONF_RECORDS]
55 pycfdns.AuthenticationException,
56 pycfdns.ComunicationException,
58 _LOGGER.error(
"Error updating zone %s: %s", entry.data[CONF_ZONE], error)
60 async
def update_records_service(call: ServiceCall) ->
None:
61 """Set up service for manual trigger."""
64 hass, client, dns_zone, entry.data[CONF_RECORDS]
67 pycfdns.AuthenticationException,
68 pycfdns.ComunicationException,
70 _LOGGER.error(
"Error updating zone %s: %s", entry.data[CONF_ZONE], error)
72 update_interval =
timedelta(minutes=DEFAULT_UPDATE_INTERVAL)
73 entry.async_on_unload(
77 hass.data.setdefault(DOMAIN, {})
78 hass.data[DOMAIN][entry.entry_id] = {}
80 hass.services.async_register(DOMAIN, SERVICE_UPDATE_RECORDS, update_records_service)
86 """Unload Cloudflare config entry."""
87 hass.data[DOMAIN].pop(entry.entry_id)
94 client: pycfdns.Client,
95 dns_zone: pycfdns.ZoneModel,
96 target_records: list[str],
98 _LOGGER.debug(
"Starting update for zone %s", dns_zone[
"name"])
100 records = await client.list_dns_records(zone_id=dns_zone[
"id"], type=
"A")
101 _LOGGER.debug(
"Records: %s", records)
111 for record
in records
112 if record[
"name"]
in target_records
and record[
"content"] != location_info.ip
115 if len(filtered_records) == 0:
116 _LOGGER.debug(
"All target records are up to date")
119 await asyncio.gather(
121 client.update_dns_record(
122 zone_id=dns_zone[
"id"],
123 record_id=record[
"id"],
124 record_content=location_info.ip,
125 record_name=record[
"name"],
126 record_type=record[
"type"],
127 record_proxied=record[
"proxied"],
129 for record
in filtered_records
133 _LOGGER.debug(
"Update for zone %s is complete", dns_zone[
"name"])
None _async_update_cloudflare(HomeAssistant hass, pycfdns.Client client, pycfdns.ZoneModel dns_zone, list[str] target_records)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)
LocationInfo|None async_detect_location_info(aiohttp.ClientSession session)
bool is_ipv4_address(str address)