From e7a5b3a2deded061bb6e999fba7e8d7d6b54e418 Mon Sep 17 00:00:00 2001 From: GuilleGF Date: Fri, 14 May 2021 12:47:16 +0200 Subject: [PATCH 1/2] feat: add timeout and config interval --- README.md | 5 ++- custom_components/ovh/__init__.py | 54 +++++++++++++++-------------- custom_components/ovh/manifest.json | 2 +- hacs.json | 3 +- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 83ff412..9b37351 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ To use the integration in your installation, add the following to your `configur | `domain` | yes | string | The subdomain you are modifying the DNS configuration for | | `username` | yes | string | The DynHost username | | `password` | yes | string | Password for the DynHost username | +| `password` | yes | string | Password for the DynHost username | +| `scan_interval` | no | time | How often to call the update service. (default: 10 minutes) | + #### Basic Example: ```yaml @@ -20,4 +23,4 @@ ovh: username: YOUR_USERNAME password: YOUR_PASSWORD ``` -Based on the official [No-IP.com](https://github.com/home-assistant/core/tree/dev/homeassistant/components/no_ip) integration. Thanks to the creators! +Based on the official [No-IP.com](https://github.com/home-assistant/core/tree/dev/homeassistant/components/no_ip) and [Mythic Beasts](https://github.com/home-assistant/core/blob/dev/homeassistant/components/mythicbeastsdns) integrations. Thanks to the creators! diff --git a/custom_components/ovh/__init__.py b/custom_components/ovh/__init__.py index 302e81b..489bcb3 100644 --- a/custom_components/ovh/__init__.py +++ b/custom_components/ovh/__init__.py @@ -4,25 +4,28 @@ import logging import aiohttp -from aiohttp.hdrs import USER_AGENT from aiohttp import BasicAuth import async_timeout import voluptuous as vol -from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME -from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE +from homeassistant.const import ( + CONF_DOMAIN, + CONF_PASSWORD, + CONF_USERNAME, + CONF_SCAN_INTERVAL +) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.event import async_track_time_interval _LOGGER = logging.getLogger(__name__) DOMAIN = "ovh" -# We should set a dedicated address for the user agent. -EMAIL = "hello@home-assistant.io" - -INTERVAL = timedelta(minutes=5) +DEFAULT_INTERVAL = timedelta(minutes=10) -DEFAULT_TIMEOUT = 10 +TIMEOUT = 10 +UPDATE_URL = "https://www.ovh.com/nic/update" OVH_ERRORS = { "nohost": "Hostname supplied does not exist under specified account", @@ -32,9 +35,6 @@ "abuse": "Username is blocked due to abuse", } -UPDATE_URL = "https://www.ovh.com/nic/update" -HA_USER_AGENT = f"{SERVER_SOFTWARE} {EMAIL}" - CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.Schema( @@ -42,50 +42,52 @@ vol.Required(CONF_DOMAIN): cv.string, vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_PASSWORD): cv.string, - vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, + vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_INTERVAL): vol.All( + cv.time_period, cv.positive_timedelta + ), } ) }, extra=vol.ALLOW_EXTRA, ) - async def async_setup(hass, config): """Initialize the OVH component.""" - domain = config[DOMAIN].get(CONF_DOMAIN) - user = config[DOMAIN].get(CONF_USERNAME) - password = config[DOMAIN].get(CONF_PASSWORD) - timeout = config[DOMAIN].get(CONF_TIMEOUT) + conf = config[DOMAIN] + domain = conf.get(CONF_DOMAIN) + user = conf.get(CONF_USERNAME) + password = conf.get(CONF_PASSWORD) + interval = conf.get(CONF_SCAN_INTERVAL) - session = hass.helpers.aiohttp_client.async_get_clientsession() + session = async_get_clientsession(hass) - result = await _update_ovh(hass, session, domain, user, password, timeout) + result = await _update_ovh(hass, session, domain, user, password) if not result: return False async def update_domain_interval(now): """Update the OVH entry.""" - await _update_ovh(hass, session, domain, user, password, timeout) + await _update_ovh(hass, session, domain, user, password) - hass.helpers.event.async_track_time_interval(update_domain_interval, INTERVAL) + async_track_time_interval(hass, update_domain_interval, interval) return True -async def _update_ovh(hass, session, domain, user, password, timeout): +async def _update_ovh(hass, session, domain, user, password): """Update OVH.""" - url = UPDATE_URL params = {"system": "dyndns", "hostname": domain} - headers = {USER_AGENT: HA_USER_AGENT} authentication = BasicAuth(user, password) try: - with async_timeout.timeout(timeout): - resp = await session.get(url, params=params, headers=headers, auth=authentication) + with async_timeout.timeout(TIMEOUT): + resp = await session.get(UPDATE_URL, params=params, auth=authentication) body = await resp.text() if body.startswith("good") or body.startswith("nochg"): + _LOGGER.info("Updating OVH for domain: %s", domain) + return True _LOGGER.warning("Updating OVH failed: %s => %s", domain, OVH_ERRORS[body.strip()]) diff --git a/custom_components/ovh/manifest.json b/custom_components/ovh/manifest.json index 071643e..38b5b16 100644 --- a/custom_components/ovh/manifest.json +++ b/custom_components/ovh/manifest.json @@ -5,5 +5,5 @@ "issue_tracker": "https://github.com/GuilleGF/hassio-ovh/issues", "codeowners": ["@GuilleGF"], "iot_class": "cloud_polling", - "version": "1.1.0" + "version": "2.0.0" } diff --git a/hacs.json b/hacs.json index 3eba72f..a74e523 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,4 @@ { "name": "OVH DynHost", - "render_readme": true, - "iot_class": "cloud_push" + "render_readme": true } From ec06085b0a252c3402f3099d89a3f8148ac44fb5 Mon Sep 17 00:00:00 2001 From: GuilleGF Date: Fri, 14 May 2021 13:09:59 +0200 Subject: [PATCH 2/2] fix: readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9b37351..728c1ed 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ To use the integration in your installation, add the following to your `configur | `domain` | yes | string | The subdomain you are modifying the DNS configuration for | | `username` | yes | string | The DynHost username | | `password` | yes | string | Password for the DynHost username | -| `password` | yes | string | Password for the DynHost username | | `scan_interval` | no | time | How often to call the update service. (default: 10 minutes) | #### Basic Example: