mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Clean up DNS IP integration (#51143)
* Clean up DNS IP integration * Commit missing change oops
This commit is contained in:
parent
eb2b60434c
commit
d9eb1d85a2
@ -24,6 +24,7 @@ homeassistant.components.canary.*
|
|||||||
homeassistant.components.cover.*
|
homeassistant.components.cover.*
|
||||||
homeassistant.components.device_automation.*
|
homeassistant.components.device_automation.*
|
||||||
homeassistant.components.device_tracker.*
|
homeassistant.components.device_tracker.*
|
||||||
|
homeassistant.components.dnsip.*
|
||||||
homeassistant.components.dunehd.*
|
homeassistant.components.dunehd.*
|
||||||
homeassistant.components.elgato.*
|
homeassistant.components.elgato.*
|
||||||
homeassistant.components.fitbit.*
|
homeassistant.components.fitbit.*
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Get your own public IP address or that of any host."""
|
"""Get your own public IP address or that of any host."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -8,7 +10,10 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -36,57 +41,44 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
async def async_setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: ConfigType,
|
||||||
|
async_add_devices: AddEntitiesCallback,
|
||||||
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
|
) -> None:
|
||||||
"""Set up the DNS IP sensor."""
|
"""Set up the DNS IP sensor."""
|
||||||
hostname = config[CONF_HOSTNAME]
|
hostname = config[CONF_HOSTNAME]
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
if not name:
|
|
||||||
if hostname == DEFAULT_HOSTNAME:
|
|
||||||
name = DEFAULT_NAME
|
|
||||||
else:
|
|
||||||
name = hostname
|
|
||||||
ipv6 = config[CONF_IPV6]
|
ipv6 = config[CONF_IPV6]
|
||||||
if ipv6:
|
|
||||||
resolver = config[CONF_RESOLVER_IPV6]
|
|
||||||
else:
|
|
||||||
resolver = config[CONF_RESOLVER]
|
|
||||||
|
|
||||||
async_add_devices([WanIpSensor(hass, name, hostname, resolver, ipv6)], True)
|
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)
|
||||||
|
|
||||||
|
|
||||||
class WanIpSensor(SensorEntity):
|
class WanIpSensor(SensorEntity):
|
||||||
"""Implementation of a DNS IP sensor."""
|
"""Implementation of a DNS IP sensor."""
|
||||||
|
|
||||||
def __init__(self, hass, name, hostname, resolver, ipv6):
|
def __init__(self, name: str, hostname: str, resolver: str, ipv6: bool) -> None:
|
||||||
"""Initialize the DNS IP sensor."""
|
"""Initialize the DNS IP sensor."""
|
||||||
|
self._attr_name = name
|
||||||
self.hass = hass
|
|
||||||
self._name = name
|
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.resolver = aiodns.DNSResolver()
|
self.resolver = aiodns.DNSResolver()
|
||||||
self.resolver.nameservers = [resolver]
|
self.resolver.nameservers = [resolver]
|
||||||
self.querytype = "AAAA" if ipv6 else "A"
|
self.querytype = "AAAA" if ipv6 else "A"
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
async def async_update(self) -> None:
|
||||||
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
|
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Get the current DNS IP address for hostname."""
|
"""Get the current DNS IP address for hostname."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = await self.resolver.query(self.hostname, self.querytype)
|
response = await self.resolver.query(self.hostname, self.querytype)
|
||||||
except DNSError as err:
|
except DNSError as err:
|
||||||
_LOGGER.warning("Exception while resolving host: %s", err)
|
_LOGGER.warning("Exception while resolving host: %s", err)
|
||||||
response = None
|
response = None
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
self._state = response[0].host
|
self._attr_state = response[0].host
|
||||||
else:
|
else:
|
||||||
self._state = None
|
self._attr_state = None
|
||||||
|
11
mypy.ini
11
mypy.ini
@ -275,6 +275,17 @@ no_implicit_optional = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = true
|
warn_unreachable = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.components.dnsip.*]
|
||||||
|
check_untyped_defs = true
|
||||||
|
disallow_incomplete_defs = true
|
||||||
|
disallow_subclassing_any = true
|
||||||
|
disallow_untyped_calls = true
|
||||||
|
disallow_untyped_decorators = true
|
||||||
|
disallow_untyped_defs = true
|
||||||
|
no_implicit_optional = true
|
||||||
|
warn_return_any = true
|
||||||
|
warn_unreachable = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.dunehd.*]
|
[mypy-homeassistant.components.dunehd.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user