2019-02-13 20:21:14 +00:00
|
|
|
"""Update the IP addresses of your Cloudflare DNS records."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-07-09 21:11:54 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-10-18 19:32:14 +00:00
|
|
|
from pycfdns import CloudflareUpdater
|
2020-10-21 14:09:00 +00:00
|
|
|
from pycfdns.exceptions import (
|
|
|
|
CloudflareAuthenticationException,
|
|
|
|
CloudflareConnectionException,
|
|
|
|
CloudflareException,
|
|
|
|
)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-05 05:56:50 +00:00
|
|
|
from homeassistant.const import CONF_API_TOKEN, CONF_ZONE
|
2020-10-21 14:09:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2018-07-09 21:11:54 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-10-21 14:09:00 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_RECORDS,
|
|
|
|
DATA_UNDO_UPDATE_INTERVAL,
|
|
|
|
DEFAULT_UPDATE_INTERVAL,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_UPDATE_RECORDS,
|
|
|
|
)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-05 05:56:50 +00:00
|
|
|
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Cloudflare from a config entry."""
|
|
|
|
cfupdate = CloudflareUpdater(
|
|
|
|
async_get_clientsession(hass),
|
|
|
|
entry.data[CONF_API_TOKEN],
|
|
|
|
entry.data[CONF_ZONE],
|
|
|
|
entry.data[CONF_RECORDS],
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
zone_id = await cfupdate.get_zone_id()
|
|
|
|
except CloudflareAuthenticationException:
|
|
|
|
_LOGGER.error("API access forbidden. Please reauthenticate")
|
|
|
|
return False
|
|
|
|
except CloudflareConnectionException as error:
|
|
|
|
raise ConfigEntryNotReady from error
|
|
|
|
|
|
|
|
async def update_records(now):
|
2018-07-09 21:11:54 +00:00
|
|
|
"""Set up recurring update."""
|
2020-10-21 14:09:00 +00:00
|
|
|
try:
|
|
|
|
await _async_update_cloudflare(cfupdate, zone_id)
|
|
|
|
except CloudflareException as error:
|
|
|
|
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
async def update_records_service(call):
|
2018-07-09 21:11:54 +00:00
|
|
|
"""Set up service for manual trigger."""
|
2020-10-21 14:09:00 +00:00
|
|
|
try:
|
|
|
|
await _async_update_cloudflare(cfupdate, zone_id)
|
|
|
|
except CloudflareException as error:
|
|
|
|
_LOGGER.error("Error updating zone %s: %s", entry.data[CONF_ZONE], error)
|
|
|
|
|
|
|
|
update_interval = timedelta(minutes=DEFAULT_UPDATE_INTERVAL)
|
|
|
|
undo_interval = async_track_time_interval(hass, update_records, update_interval)
|
|
|
|
|
2021-05-05 05:56:50 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-10-21 14:09:00 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
|
|
DATA_UNDO_UPDATE_INTERVAL: undo_interval,
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_UPDATE_RECORDS, update_records_service)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload Cloudflare config entry."""
|
|
|
|
hass.data[DOMAIN][entry.entry_id][DATA_UNDO_UPDATE_INTERVAL]()
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
return True
|
2018-07-09 21:11:54 +00:00
|
|
|
|
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
async def _async_update_cloudflare(cfupdate: CloudflareUpdater, zone_id: str):
|
|
|
|
_LOGGER.debug("Starting update for zone %s", cfupdate.zone)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
records = await cfupdate.get_record_info(zone_id)
|
|
|
|
_LOGGER.debug("Records: %s", records)
|
2018-07-09 21:11:54 +00:00
|
|
|
|
2020-10-21 14:09:00 +00:00
|
|
|
await cfupdate.update_records(zone_id, records)
|
|
|
|
_LOGGER.debug("Update for zone %s is complete", cfupdate.zone)
|