1 """Update the IP addresses of your Route53 DNS records."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from http
import HTTPStatus
11 import voluptuous
as vol
19 _LOGGER = logging.getLogger(__name__)
21 CONF_ACCESS_KEY_ID =
"aws_access_key_id"
22 CONF_SECRET_ACCESS_KEY =
"aws_secret_access_key"
23 CONF_RECORDS =
"records"
30 CONFIG_SCHEMA = vol.Schema(
34 vol.Required(CONF_ACCESS_KEY_ID): cv.string,
35 vol.Required(CONF_DOMAIN): cv.string,
36 vol.Required(CONF_RECORDS): vol.All(cv.ensure_list, [cv.string]),
37 vol.Required(CONF_SECRET_ACCESS_KEY): cv.string,
38 vol.Required(CONF_ZONE): cv.string,
39 vol.Optional(CONF_TTL, default=DEFAULT_TTL): cv.positive_int,
43 extra=vol.ALLOW_EXTRA,
47 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
48 """Set up the Route53 component."""
49 domain = config[DOMAIN][CONF_DOMAIN]
50 records = config[DOMAIN][CONF_RECORDS]
51 zone = config[DOMAIN][CONF_ZONE]
52 aws_access_key_id = config[DOMAIN][CONF_ACCESS_KEY_ID]
53 aws_secret_access_key = config[DOMAIN][CONF_SECRET_ACCESS_KEY]
54 ttl = config[DOMAIN][CONF_TTL]
56 def update_records_interval(now):
57 """Set up recurring update."""
59 aws_access_key_id, aws_secret_access_key, zone, domain, records, ttl
62 def update_records_service(call: ServiceCall) ->
None:
63 """Set up service for manual trigger."""
65 aws_access_key_id, aws_secret_access_key, zone, domain, records, ttl
70 hass.services.register(DOMAIN,
"update_records", update_records_service)
77 return f
"{record}.{domain}"
81 aws_access_key_id: str,
82 aws_secret_access_key: str,
88 _LOGGER.debug(
"Starting update for zone %s", zone)
90 client = boto3.client(
92 aws_access_key_id=aws_access_key_id,
93 aws_secret_access_key=aws_secret_access_key,
98 ipaddress = requests.get(
"https://api.ipify.org/", timeout=5).text
100 except requests.RequestException:
101 _LOGGER.warning(
"Unable to reach the ipify service")
105 for record
in records:
106 _LOGGER.debug(
"Processing record: %s", record)
111 "ResourceRecordSet": {
115 "ResourceRecords": [{
"Value": ipaddress}],
120 _LOGGER.debug(
"Submitting the following changes to Route53")
121 _LOGGER.debug(changes)
123 response = client.change_resource_record_sets(
124 HostedZoneId=zone, ChangeBatch={
"Changes": changes}
126 _LOGGER.debug(
"Response is %s", response)
128 if response[
"ResponseMetadata"][
"HTTPStatusCode"] != HTTPStatus.OK:
129 _LOGGER.warning(response)
def _update_route53(str aws_access_key_id, str aws_secret_access_key, str zone, str domain, list[str] records, int ttl)
bool setup(HomeAssistant hass, ConfigType config)
def _get_fqdn(record, domain)