Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnsip: errors #136941

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions homeassistant/components/dnsip/config_flow.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
from __future__ import annotations

import asyncio
import contextlib
import logging
from typing import Any

import aiodns
@@ -31,11 +31,11 @@
DEFAULT_HOSTNAME,
DEFAULT_NAME,
DEFAULT_PORT,
DEFAULT_RESOLVER,
DEFAULT_RESOLVER_IPV6,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOSTNAME, default=DEFAULT_HOSTNAME): cv.string,
@@ -54,24 +54,30 @@

async def async_validate_hostname(
hostname: str,
resolver_ipv4: str,
resolver_ipv6: str,
resolver_ipv4: str | None,
resolver_ipv6: str | None,
port: int,
port_ipv6: int,
) -> dict[str, bool]:
"""Validate hostname."""

async def async_check(
hostname: str, resolver: str, qtype: str, port: int = 53
hostname: str, resolver: str | None, qtype: str, port: int = 53
) -> bool:
"""Return if able to resolve hostname."""
result = False
with contextlib.suppress(DNSError):
result = bool(
await aiodns.DNSResolver(
nameservers=[resolver], udp_port=port, tcp_port=port
).query(hostname, qtype)
)
args: dict[str, Any] = {
"udp_port": port,
"tcp_port": port,
}
if resolver is not None:
args["nameservers"] = [resolver]

try:
result = bool(await aiodns.DNSResolver(**args).query(hostname, qtype))
except DNSError as err:
_LOGGER.warning("Exception while resolving host: %s", err)
result = False

return result

result: dict[str, bool] = {}
@@ -113,8 +119,8 @@ async def async_step_user(
if user_input:
hostname = user_input[CONF_HOSTNAME]
name = DEFAULT_NAME if hostname == DEFAULT_HOSTNAME else hostname
resolver = user_input.get(CONF_RESOLVER, DEFAULT_RESOLVER)
resolver_ipv6 = user_input.get(CONF_RESOLVER_IPV6, DEFAULT_RESOLVER_IPV6)
resolver = user_input.get(CONF_RESOLVER)
resolver_ipv6 = user_input.get(CONF_RESOLVER_IPV6)
port = user_input.get(CONF_PORT, DEFAULT_PORT)
port_ipv6 = user_input.get(CONF_PORT_IPV6, DEFAULT_PORT)

@@ -174,9 +180,9 @@ async def async_step_init(
"""Manage the options."""
errors = {}
if user_input is not None:
resolver = user_input.get(CONF_RESOLVER, DEFAULT_RESOLVER)
resolver = user_input.get(CONF_RESOLVER)
port = user_input.get(CONF_PORT, DEFAULT_PORT)
resolver_ipv6 = user_input.get(CONF_RESOLVER_IPV6, DEFAULT_RESOLVER_IPV6)
resolver_ipv6 = user_input.get(CONF_RESOLVER_IPV6)
port_ipv6 = user_input.get(CONF_PORT_IPV6, DEFAULT_PORT)
validate = await async_validate_hostname(
self.config_entry.data[CONF_HOSTNAME],
2 changes: 0 additions & 2 deletions homeassistant/components/dnsip/const.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,4 @@
DEFAULT_HOSTNAME = "myip.opendns.com"
DEFAULT_IPV6 = False
DEFAULT_NAME = "myip"
DEFAULT_RESOLVER = "208.67.222.222"
DEFAULT_PORT = 53
DEFAULT_RESOLVER_IPV6 = "2620:119:53::53"
6 changes: 4 additions & 2 deletions homeassistant/components/dnsip/sensor.py
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ def __init__(
self,
name: str,
hostname: str,
resolver: str,
resolver: str | None,
ipv6: bool,
port: int,
) -> None:
@@ -86,7 +86,9 @@ def __init__(
self._attr_unique_id = f"{hostname}_{ipv6}"
self.hostname = hostname
self.resolver = aiodns.DNSResolver(tcp_port=port, udp_port=port)
self.resolver.nameservers = [resolver]
if resolver is not None:
self.resolver.nameservers = [resolver]
# otherwise default resolver is used
self.querytype = "AAAA" if ipv6 else "A"
self._retries = DEFAULT_RETRIES
self._attr_extra_state_attributes = {