2019-04-03 15:40:03 +00:00
|
|
|
"""Get your own public IP address or that of any host."""
|
2021-05-27 12:10:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
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
|
|
|
|
2019-10-15 12:26:04 +00:00
|
|
|
import aiodns
|
|
|
|
from aiodns.error import DNSError
|
2017-02-27 10:45:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-05-07 19:32:05 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-05-27 12:10:28 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2017-02-27 10:45:32 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-05-27 12:10:28 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOSTNAME = "hostname"
|
|
|
|
CONF_IPV6 = "ipv6"
|
|
|
|
CONF_RESOLVER = "resolver"
|
|
|
|
CONF_RESOLVER_IPV6 = "resolver_ipv6"
|
2017-02-27 10:45:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOSTNAME = "myip.opendns.com"
|
2019-05-07 19:32:05 +00:00
|
|
|
DEFAULT_IPV6 = False
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "myip"
|
|
|
|
DEFAULT_RESOLVER = "208.67.222.222"
|
|
|
|
DEFAULT_RESOLVER_IPV6 = "2620:0:ccc::2"
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=120)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
|
2021-05-27 12:10:28 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_devices: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the DNS IP sensor."""
|
2020-04-15 12:10:07 +00:00
|
|
|
hostname = config[CONF_HOSTNAME]
|
2018-08-26 19:27:03 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2020-04-15 12:10:07 +00:00
|
|
|
ipv6 = config[CONF_IPV6]
|
2017-02-27 10:45:32 +00:00
|
|
|
|
2021-05-27 12:10:28 +00:00
|
|
|
if not name:
|
|
|
|
name = DEFAULT_NAME if hostname == DEFAULT_HOSTNAME else hostname
|
|
|
|
resolver = config[CONF_RESOLVER_IPV6] if ipv6 else config[CONF_RESOLVER]
|
|
|
|
|
|
|
|
async_add_devices([WanIpSensor(name, hostname, resolver, ipv6)], True)
|
2017-02-27 10:45:32 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class WanIpSensor(SensorEntity):
|
2017-02-27 10:45:32 +00:00
|
|
|
"""Implementation of a DNS IP sensor."""
|
|
|
|
|
2021-05-27 12:10:28 +00:00
|
|
|
def __init__(self, name: str, hostname: str, resolver: str, ipv6: bool) -> None:
|
2019-05-07 19:32:05 +00:00
|
|
|
"""Initialize the DNS IP sensor."""
|
2021-05-27 12:10:28 +00:00
|
|
|
self._attr_name = name
|
2018-08-26 19:27:03 +00:00
|
|
|
self.hostname = hostname
|
2019-05-23 04:09:59 +00:00
|
|
|
self.resolver = aiodns.DNSResolver()
|
2017-02-27 10:45:32 +00:00
|
|
|
self.resolver.nameservers = [resolver]
|
2019-07-31 19:25:30 +00:00
|
|
|
self.querytype = "AAAA" if ipv6 else "A"
|
2017-02-27 10:45:32 +00:00
|
|
|
|
2021-05-27 12:10:28 +00:00
|
|
|
async def async_update(self) -> None:
|
2017-02-27 10:45:32 +00:00
|
|
|
"""Get the current DNS IP address for hostname."""
|
2018-10-12 07:30:35 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +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
|
2021-05-27 12:10:28 +00:00
|
|
|
|
2017-02-27 10:45:32 +00:00
|
|
|
if response:
|
2021-08-11 16:57:12 +00:00
|
|
|
self._attr_native_value = response[0].host
|
2017-02-27 10:45:32 +00:00
|
|
|
else:
|
2021-08-11 16:57:12 +00:00
|
|
|
self._attr_native_value = None
|