mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Clean up DNS IP integration (#51143)
* Clean up DNS IP integration * Commit missing change oops
This commit is contained in:
@ -24,6 +24,7 @@ homeassistant.components.canary.*
|
||||
homeassistant.components.cover.*
|
||||
homeassistant.components.device_automation.*
|
||||
homeassistant.components.device_tracker.*
|
||||
homeassistant.components.dnsip.*
|
||||
homeassistant.components.dunehd.*
|
||||
homeassistant.components.elgato.*
|
||||
homeassistant.components.fitbit.*
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""Get your own public IP address or that of any host."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
@ -8,7 +10,10 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
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__)
|
||||
|
||||
@ -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."""
|
||||
hostname = config[CONF_HOSTNAME]
|
||||
name = config.get(CONF_NAME)
|
||||
if not name:
|
||||
if hostname == DEFAULT_HOSTNAME:
|
||||
name = DEFAULT_NAME
|
||||
else:
|
||||
name = hostname
|
||||
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):
|
||||
"""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."""
|
||||
|
||||
self.hass = hass
|
||||
self._name = name
|
||||
self._attr_name = name
|
||||
self.hostname = hostname
|
||||
self.resolver = aiodns.DNSResolver()
|
||||
self.resolver.nameservers = [resolver]
|
||||
self.querytype = "AAAA" if ipv6 else "A"
|
||||
self._state = None
|
||||
|
||||
@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
|
||||
|
||||
async def async_update(self):
|
||||
async def async_update(self) -> None:
|
||||
"""Get the current DNS IP address for hostname."""
|
||||
|
||||
try:
|
||||
response = await self.resolver.query(self.hostname, self.querytype)
|
||||
except DNSError as err:
|
||||
_LOGGER.warning("Exception while resolving host: %s", err)
|
||||
response = None
|
||||
|
||||
if response:
|
||||
self._state = response[0].host
|
||||
self._attr_state = response[0].host
|
||||
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_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.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
|
Reference in New Issue
Block a user