2019-04-03 15:40:03 +00:00
|
|
|
"""Get your own public IP address or that of any host."""
|
2017-02-27 10:45:32 +00:00
|
|
|
from datetime import timedelta
|
2019-05-07 19:32:05 +00:00
|
|
|
import logging
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-05-07 19:32:05 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2017-02-27 10:45:32 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONF_HOSTNAME = 'hostname'
|
2019-05-07 19:32:05 +00:00
|
|
|
CONF_IPV6 = 'ipv6'
|
2017-02-27 10:45:32 +00:00
|
|
|
CONF_RESOLVER = 'resolver'
|
|
|
|
CONF_RESOLVER_IPV6 = 'resolver_ipv6'
|
|
|
|
|
|
|
|
DEFAULT_HOSTNAME = 'myip.opendns.com'
|
2019-05-07 19:32:05 +00:00
|
|
|
DEFAULT_IPV6 = False
|
|
|
|
DEFAULT_NAME = 'myip'
|
2017-02-27 10:45:32 +00:00
|
|
|
DEFAULT_RESOLVER = '208.67.222.222'
|
|
|
|
DEFAULT_RESOLVER_IPV6 = '2620:0:ccc::2'
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=120)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2018-08-26 19:27:03 +00:00
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
2017-02-27 10:45:32 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2019-05-07 19:32:05 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the DNS IP sensor."""
|
2017-02-27 10:45:32 +00:00
|
|
|
hostname = config.get(CONF_HOSTNAME)
|
2018-08-26 19:27:03 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
if not name:
|
|
|
|
if hostname == DEFAULT_HOSTNAME:
|
|
|
|
name = DEFAULT_NAME
|
|
|
|
else:
|
|
|
|
name = hostname
|
2017-02-27 10:45:32 +00:00
|
|
|
ipv6 = config.get(CONF_IPV6)
|
|
|
|
if ipv6:
|
|
|
|
resolver = config.get(CONF_RESOLVER_IPV6)
|
|
|
|
else:
|
|
|
|
resolver = config.get(CONF_RESOLVER)
|
|
|
|
|
2018-08-26 19:27:03 +00:00
|
|
|
async_add_devices([WanIpSensor(
|
|
|
|
hass, name, hostname, resolver, ipv6)], True)
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WanIpSensor(Entity):
|
|
|
|
"""Implementation of a DNS IP sensor."""
|
|
|
|
|
2018-08-26 19:27:03 +00:00
|
|
|
def __init__(self, hass, name, hostname, resolver, ipv6):
|
2019-05-07 19:32:05 +00:00
|
|
|
"""Initialize the DNS IP sensor."""
|
2017-02-27 10:45:32 +00:00
|
|
|
import aiodns
|
2019-05-07 19:32:05 +00:00
|
|
|
|
2017-02-27 10:45:32 +00:00
|
|
|
self.hass = hass
|
2018-08-26 19:27:03 +00:00
|
|
|
self._name = name
|
|
|
|
self.hostname = hostname
|
2017-02-27 10:45:32 +00:00
|
|
|
self.resolver = aiodns.DNSResolver(loop=self.hass.loop)
|
|
|
|
self.resolver.nameservers = [resolver]
|
|
|
|
self.querytype = 'AAAA' if ipv6 else 'A'
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_update(self):
|
2017-02-27 10:45:32 +00:00
|
|
|
"""Get the current DNS IP address for hostname."""
|
2018-10-12 07:30:35 +00:00
|
|
|
from aiodns.error import DNSError
|
2019-05-07 19:32:05 +00:00
|
|
|
|
2018-10-12 07:30:35 +00:00
|
|
|
try:
|
2019-05-07 19:32:05 +00:00
|
|
|
response = await self.resolver.query(
|
|
|
|
self.hostname, self.querytype)
|
2018-10-12 07:30:35 +00:00
|
|
|
except DNSError as err:
|
|
|
|
_LOGGER.warning("Exception while resolving host: %s", err)
|
|
|
|
response = None
|
2017-02-27 10:45:32 +00:00
|
|
|
if response:
|
|
|
|
self._state = response[0].host
|
|
|
|
else:
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|