From e6c88c05adfb60ed810053f950a0f62260e3c17b Mon Sep 17 00:00:00 2001 From: Daniel Perna Date: Mon, 27 Feb 2017 11:45:32 +0100 Subject: [PATCH] [sensor.dnsip] New Sensor: DNS IP (#6214) * Added DNS IP sensor * Removed unused import * Added coverage * fixed flake * Applied suggested changes * Removed debug code * Switched to aiodns * Raised scan interval * Updating state with entity creation * Lint * Updated requirements_all --- .coveragerc | 1 + homeassistant/components/sensor/dnsip.py | 86 ++++++++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 90 insertions(+) create mode 100644 homeassistant/components/sensor/dnsip.py diff --git a/.coveragerc b/.coveragerc index 3f5ba7a35de..ae71a10f73d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -312,6 +312,7 @@ omit = homeassistant/components/sensor/darksky.py homeassistant/components/sensor/deutsche_bahn.py homeassistant/components/sensor/dht.py + homeassistant/components/sensor/dnsip.py homeassistant/components/sensor/dovado.py homeassistant/components/sensor/dte_energy_bridge.py homeassistant/components/sensor/ebox.py diff --git a/homeassistant/components/sensor/dnsip.py b/homeassistant/components/sensor/dnsip.py new file mode 100644 index 00000000000..2807dbc2c58 --- /dev/null +++ b/homeassistant/components/sensor/dnsip.py @@ -0,0 +1,86 @@ +""" +Get your own public IP address or that of any host. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.dnsip/ +""" +import asyncio +import logging +from datetime import timedelta + +import voluptuous as vol + +from homeassistant.const import STATE_UNKNOWN +from homeassistant.components.sensor import PLATFORM_SCHEMA +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity import Entity + +REQUIREMENTS = ['aiodns==1.1.1'] + +_LOGGER = logging.getLogger(__name__) + +CONF_HOSTNAME = 'hostname' +CONF_RESOLVER = 'resolver' +CONF_RESOLVER_IPV6 = 'resolver_ipv6' +CONF_IPV6 = 'ipv6' + +DEFAULT_HOSTNAME = 'myip.opendns.com' +DEFAULT_RESOLVER = '208.67.222.222' +DEFAULT_RESOLVER_IPV6 = '2620:0:ccc::2' +DEFAULT_IPV6 = False + +SCAN_INTERVAL = timedelta(seconds=120) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_HOSTNAME, default=DEFAULT_HOSTNAME): cv.string, + vol.Optional(CONF_RESOLVER, default=DEFAULT_RESOLVER): cv.string, + vol.Optional(CONF_RESOLVER_IPV6, default=DEFAULT_RESOLVER_IPV6): cv.string, + vol.Optional(CONF_IPV6, default=DEFAULT_IPV6): cv.boolean, +}) + + +@asyncio.coroutine +def async_setup_platform(hass, config, async_add_devices, discovery_info=None): + """Setup the DNS IP sensor.""" + hostname = config.get(CONF_HOSTNAME) + ipv6 = config.get(CONF_IPV6) + if ipv6: + resolver = config.get(CONF_RESOLVER_IPV6) + else: + resolver = config.get(CONF_RESOLVER) + + yield from async_add_devices([WanIpSensor( + hass, hostname, resolver, ipv6)], True) + + +class WanIpSensor(Entity): + """Implementation of a DNS IP sensor.""" + + def __init__(self, hass, hostname, resolver, ipv6): + """Initialize the sensor.""" + import aiodns + self.hass = hass + self._name = hostname + self.resolver = aiodns.DNSResolver(loop=self.hass.loop) + self.resolver.nameservers = [resolver] + self.querytype = 'AAAA' if ipv6 else 'A' + self._state = STATE_UNKNOWN + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def state(self): + """Return the current DNS IP address for hostname.""" + return self._state + + @asyncio.coroutine + def async_update(self): + """Get the current DNS IP address for hostname.""" + response = yield from self.resolver.query(self._name, self.querytype) + if response: + self._state = response[0].host + else: + self._state = STATE_UNKNOWN diff --git a/requirements_all.txt b/requirements_all.txt index c13b7952944..37022e6efd0 100755 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -33,6 +33,9 @@ SoCo==0.12 # homeassistant.components.notify.twitter TwitterAPI==2.4.4 +# homeassistant.components.sensor.dnsip +aiodns==1.1.1 + # homeassistant.components.emulated_hue # homeassistant.components.http aiohttp_cors==0.5.0