Add Cloudflare DNS component. (#15388)

* Add Cloudflare DNS component

* Removed man

* Update .coveragerc

* Update cloudflare.py

* Update cloudflare.py

* Changed records to be required

* Fix typos, update order and other minor changes
pull/15341/merge
Joakim Sørensen 2018-07-09 23:11:54 +02:00 committed by Fabian Affolter
parent e62bb299ff
commit c5a2ffbcb9
3 changed files with 83 additions and 0 deletions

View File

@ -64,6 +64,8 @@ omit =
homeassistant/components/cast/*
homeassistant/components/*/cast.py
homeassistant/components/cloudflare.py
homeassistant/components/comfoconnect.py
homeassistant/components/*/comfoconnect.py

View File

@ -0,0 +1,78 @@
"""
Update the IP addresses of your Cloudflare DNS records.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/cloudflare/
"""
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.const import CONF_API_KEY, CONF_EMAIL, CONF_ZONE
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_time_interval
REQUIREMENTS = ['pycfdns==0.0.1']
_LOGGER = logging.getLogger(__name__)
CONF_RECORDS = 'records'
DOMAIN = 'cloudflare'
INTERVAL = timedelta(minutes=60)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_EMAIL): cv.string,
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_ZONE): cv.string,
vol.Required(CONF_RECORDS): vol.All(cv.ensure_list, [cv.string]),
})
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up the Cloudflare component."""
from pycfdns import CloudflareUpdater
cfupdate = CloudflareUpdater()
email = config[DOMAIN][CONF_EMAIL]
key = config[DOMAIN][CONF_API_KEY]
zone = config[DOMAIN][CONF_ZONE]
records = config[DOMAIN][CONF_RECORDS]
def update_records_interval(now):
"""Set up recurring update."""
_update_cloudflare(cfupdate, email, key, zone, records)
def update_records_service(now):
"""Set up service for manual trigger."""
_update_cloudflare(cfupdate, email, key, zone, records)
track_time_interval(hass, update_records_interval, INTERVAL)
hass.services.register(
DOMAIN, 'update_records', update_records_service)
return True
def _update_cloudflare(cfupdate, email, key, zone, records):
"""Update DNS records for a given zone."""
_LOGGER.debug("Starting update for zone %s", zone)
headers = cfupdate.set_header(email, key)
_LOGGER.debug("Header data defined as: %s", headers)
zoneid = cfupdate.get_zoneID(headers, zone)
_LOGGER.debug("Zone ID is set to: %s", zoneid)
update_records = cfupdate.get_recordInfo(headers, zoneid, zone, records)
_LOGGER.debug("Records: %s", update_records)
result = cfupdate.update_records(headers, zoneid, update_records)
_LOGGER.debug("Update for zone %s is complete", zone)
if result is not True:
_LOGGER.warning(result)
return True

View File

@ -758,6 +758,9 @@ pyblackbird==0.5
# homeassistant.components.neato
pybotvac==0.0.7
# homeassistant.components.cloudflare
pycfdns==0.0.1
# homeassistant.components.media_player.channels
pychannels==1.0.0