Make host optional (#10063)
* Make host optional * Update test to reflect code changes
This commit is contained in:
parent
e8a701ffd0
commit
42e59b465e
2 changed files with 33 additions and 24 deletions
|
@ -1,46 +1,55 @@
|
||||||
"""Integrate with NamecheapDNS."""
|
"""
|
||||||
|
Integrate with namecheap DNS services.
|
||||||
|
|
||||||
|
For more details about this component, please refer to the documentation at
|
||||||
|
https://home-assistant.io/components/namecheapdns/
|
||||||
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_ACCESS_TOKEN, CONF_DOMAIN
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_DOMAIN
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
DOMAIN = 'namecheapdns'
|
|
||||||
UPDATE_URL = 'https://dynamicdns.park-your-domain.com/update'
|
|
||||||
INTERVAL = timedelta(minutes=5)
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DOMAIN = 'namecheapdns'
|
||||||
|
|
||||||
|
INTERVAL = timedelta(minutes=5)
|
||||||
|
|
||||||
|
UPDATE_URL = 'https://dynamicdns.park-your-domain.com/update'
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
|
||||||
vol.Required(CONF_DOMAIN): cv.string,
|
vol.Required(CONF_DOMAIN): cv.string,
|
||||||
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
|
vol.Optional(CONF_HOST, default='@'): cv.string,
|
||||||
})
|
})
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup(hass, config):
|
def async_setup(hass, config):
|
||||||
"""Initialize the NamecheapDNS component."""
|
"""Initialize the namecheap DNS component."""
|
||||||
host = config[DOMAIN][CONF_HOST]
|
host = config[DOMAIN][CONF_HOST]
|
||||||
domain = config[DOMAIN][CONF_DOMAIN]
|
domain = config[DOMAIN][CONF_DOMAIN]
|
||||||
token = config[DOMAIN][CONF_ACCESS_TOKEN]
|
password = config[DOMAIN][CONF_PASSWORD]
|
||||||
|
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
|
|
||||||
result = yield from _update_namecheapdns(session, host, domain, token)
|
result = yield from _update_namecheapdns(session, host, domain, password)
|
||||||
|
|
||||||
if not result:
|
if not result:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def update_domain_interval(now):
|
def update_domain_interval(now):
|
||||||
"""Update the NamecheapDNS entry."""
|
"""Update the namecheap DNS entry."""
|
||||||
yield from _update_namecheapdns(session, host, domain, token)
|
yield from _update_namecheapdns(session, host, domain, password)
|
||||||
|
|
||||||
async_track_time_interval(hass, update_domain_interval, INTERVAL)
|
async_track_time_interval(hass, update_domain_interval, INTERVAL)
|
||||||
|
|
||||||
|
@ -48,14 +57,14 @@ def async_setup(hass, config):
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _update_namecheapdns(session, host, domain, token):
|
def _update_namecheapdns(session, host, domain, password):
|
||||||
"""Update NamecheapDNS."""
|
"""Update namecheap DNS entry."""
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'host': host,
|
'host': host,
|
||||||
'domain': domain,
|
'domain': domain,
|
||||||
'password': token,
|
'password': password,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = yield from session.get(UPDATE_URL, params=params)
|
resp = yield from session.get(UPDATE_URL, params=params)
|
||||||
|
@ -64,7 +73,7 @@ def _update_namecheapdns(session, host, domain, token):
|
||||||
err_count = root.find('ErrCount').text
|
err_count = root.find('ErrCount').text
|
||||||
|
|
||||||
if int(err_count) != 0:
|
if int(err_count) != 0:
|
||||||
_LOGGER.warning('Updating Namecheap domain %s failed', domain)
|
_LOGGER.warning("Updating namecheap domain failed: %s", domain)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -12,7 +12,7 @@ from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
HOST = 'test'
|
HOST = 'test'
|
||||||
DOMAIN = 'bla'
|
DOMAIN = 'bla'
|
||||||
TOKEN = 'abcdefgh'
|
PASSWORD = 'abcdefgh'
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -21,7 +21,7 @@ def setup_namecheapdns(hass, aioclient_mock):
|
||||||
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'password': TOKEN
|
'password': PASSWORD,
|
||||||
}, text='<interface-response><ErrCount>0</ErrCount></interface-response>')
|
}, text='<interface-response><ErrCount>0</ErrCount></interface-response>')
|
||||||
|
|
||||||
hass.loop.run_until_complete(async_setup_component(
|
hass.loop.run_until_complete(async_setup_component(
|
||||||
|
@ -29,7 +29,7 @@ def setup_namecheapdns(hass, aioclient_mock):
|
||||||
'namecheapdns': {
|
'namecheapdns': {
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'access_token': TOKEN
|
'password': PASSWORD,
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -40,14 +40,14 @@ def test_setup(hass, aioclient_mock):
|
||||||
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'password': TOKEN
|
'password': PASSWORD
|
||||||
}, text='<interface-response><ErrCount>0</ErrCount></interface-response>')
|
}, text='<interface-response><ErrCount>0</ErrCount></interface-response>')
|
||||||
|
|
||||||
result = yield from async_setup_component(hass, namecheapdns.DOMAIN, {
|
result = yield from async_setup_component(hass, namecheapdns.DOMAIN, {
|
||||||
'namecheapdns': {
|
'namecheapdns': {
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'access_token': TOKEN
|
'password': PASSWORD,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
assert result
|
assert result
|
||||||
|
@ -64,14 +64,14 @@ def test_setup_fails_if_update_fails(hass, aioclient_mock):
|
||||||
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
aioclient_mock.get(namecheapdns.UPDATE_URL, params={
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'password': TOKEN
|
'password': PASSWORD,
|
||||||
}, text='<interface-response><ErrCount>1</ErrCount></interface-response>')
|
}, text='<interface-response><ErrCount>1</ErrCount></interface-response>')
|
||||||
|
|
||||||
result = yield from async_setup_component(hass, namecheapdns.DOMAIN, {
|
result = yield from async_setup_component(hass, namecheapdns.DOMAIN, {
|
||||||
'namecheapdns': {
|
'namecheapdns': {
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'domain': DOMAIN,
|
'domain': DOMAIN,
|
||||||
'access_token': TOKEN
|
'password': PASSWORD,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
assert not result
|
assert not result
|
||||||
|
|
Loading…
Add table
Reference in a new issue