From 6a5f9faa33ed4511b2ed99919f012c5563bf9783 Mon Sep 17 00:00:00 2001 From: Pascal de Ladurantaye Date: Fri, 2 Nov 2018 23:41:26 -0400 Subject: [PATCH] Add optional ttl config to route53 component (#18135) * Add optional ttl config to route53 component * linting :) --- homeassistant/components/route53.py | 32 ++++++++++++++++++++++++----- homeassistant/const.py | 1 + 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/route53.py b/homeassistant/components/route53.py index f88a15b72b8..4c35983feed 100644 --- a/homeassistant/components/route53.py +++ b/homeassistant/components/route53.py @@ -6,10 +6,11 @@ https://home-assistant.io/components/route53/ """ from datetime import timedelta import logging +from typing import List import voluptuous as vol -from homeassistant.const import CONF_DOMAIN, CONF_ZONE +from homeassistant.const import CONF_DOMAIN, CONF_TTL, CONF_ZONE import homeassistant.helpers.config_validation as cv from homeassistant.helpers.event import track_time_interval @@ -24,6 +25,7 @@ CONF_RECORDS = 'records' DOMAIN = 'route53' INTERVAL = timedelta(minutes=60) +DEFAULT_TTL = 300 CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ @@ -32,6 +34,7 @@ CONFIG_SCHEMA = vol.Schema({ vol.Required(CONF_RECORDS): vol.All(cv.ensure_list, [cv.string]), vol.Required(CONF_SECRET_ACCESS_KEY): cv.string, vol.Required(CONF_ZONE): cv.string, + vol.Optional(CONF_TTL, default=DEFAULT_TTL): cv.positive_int, }) }, extra=vol.ALLOW_EXTRA) @@ -43,16 +46,29 @@ def setup(hass, config): zone = config[DOMAIN][CONF_ZONE] aws_access_key_id = config[DOMAIN][CONF_ACCESS_KEY_ID] aws_secret_access_key = config[DOMAIN][CONF_SECRET_ACCESS_KEY] + ttl = config[DOMAIN][CONF_TTL] def update_records_interval(now): """Set up recurring update.""" _update_route53( - aws_access_key_id, aws_secret_access_key, zone, domain, records) + aws_access_key_id, + aws_secret_access_key, + zone, + domain, + records, + ttl + ) def update_records_service(now): """Set up service for manual trigger.""" _update_route53( - aws_access_key_id, aws_secret_access_key, zone, domain, records) + aws_access_key_id, + aws_secret_access_key, + zone, + domain, + records, + ttl + ) track_time_interval(hass, update_records_interval, INTERVAL) @@ -61,7 +77,13 @@ def setup(hass, config): def _update_route53( - aws_access_key_id, aws_secret_access_key, zone, domain, records): + aws_access_key_id: str, + aws_secret_access_key: str, + zone: str, + domain: str, + records: List[str], + ttl: int, +): import boto3 from ipify import get_ip from ipify import exceptions @@ -95,7 +117,7 @@ def _update_route53( 'ResourceRecordSet': { 'Name': '{}.{}'.format(record, domain), 'Type': 'A', - 'TTL': 300, + 'TTL': ttl, 'ResourceRecords': [ {'Value': ipaddress}, ], diff --git a/homeassistant/const.py b/homeassistant/const.py index 9dbb4d2e53c..ffbba575a14 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -140,6 +140,7 @@ CONF_TIME_ZONE = 'time_zone' CONF_TIMEOUT = 'timeout' CONF_TOKEN = 'token' CONF_TRIGGER_TIME = 'trigger_time' +CONF_TTL = 'ttl' CONF_TYPE = 'type' CONF_UNIT_OF_MEASUREMENT = 'unit_of_measurement' CONF_UNIT_SYSTEM = 'unit_system'