-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add Cloudflare DNS component. #15388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6857f3a
Add Cloudflare DNS component
ludeeus 1744d22
Removed man
ludeeus aa0a0df
Update .coveragerc
ludeeus aba5ad0
Update cloudflare.py
ludeeus dcc098d
Update cloudflare.py
ludeeus a502eeb
Changed records to be required
ludeeus c8cd57b
Fix typos, update order and other minor changes
fabaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing is checking this return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may be a side effect of me usually doing bash, where you should return a value from a funtion/method to tell the parent script that you are done.
This is not needed in Python?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not needed. It also makes the code confusing. Only return something if it's going to be used. On the other hand, if a function should return something, it should always return something. Return values should be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okei, I will add a PR to remove that line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#15402