mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
DNS IP implement retry (#105675)
* DNS IP implement retry * Review comments
This commit is contained in:
parent
f22d6a4279
commit
bcf75795c2
@ -23,6 +23,8 @@ from .const import (
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
DEFAULT_RETRIES = 2
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=120)
|
||||
@ -67,6 +69,7 @@ class WanIpSensor(SensorEntity):
|
||||
self.resolver = aiodns.DNSResolver()
|
||||
self.resolver.nameservers = [resolver]
|
||||
self.querytype = "AAAA" if ipv6 else "A"
|
||||
self._retries = DEFAULT_RETRIES
|
||||
self._attr_extra_state_attributes = {
|
||||
"Resolver": resolver,
|
||||
"Querytype": self.querytype,
|
||||
@ -90,5 +93,8 @@ class WanIpSensor(SensorEntity):
|
||||
if response:
|
||||
self._attr_native_value = response[0].host
|
||||
self._attr_available = True
|
||||
self._retries = DEFAULT_RETRIES
|
||||
elif self._retries > 0:
|
||||
self._retries -= 1
|
||||
else:
|
||||
self._attr_available = False
|
||||
|
@ -5,6 +5,7 @@ from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from aiodns.error import DNSError
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
|
||||
from homeassistant.components.dnsip.const import (
|
||||
CONF_HOSTNAME,
|
||||
@ -14,10 +15,10 @@ from homeassistant.components.dnsip.const import (
|
||||
CONF_RESOLVER_IPV6,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.components.dnsip.sensor import SCAN_INTERVAL
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import RetrieveDNS
|
||||
|
||||
@ -58,7 +59,9 @@ async def test_sensor(hass: HomeAssistant) -> None:
|
||||
assert state2.state == "1.2.3.4"
|
||||
|
||||
|
||||
async def test_sensor_no_response(hass: HomeAssistant) -> None:
|
||||
async def test_sensor_no_response(
|
||||
hass: HomeAssistant, freezer: FrozenDateTimeFactory
|
||||
) -> None:
|
||||
"""Test the DNS IP sensor with DNS error."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
@ -95,10 +98,18 @@ async def test_sensor_no_response(hass: HomeAssistant) -> None:
|
||||
"homeassistant.components.dnsip.sensor.aiodns.DNSResolver",
|
||||
return_value=dns_mock,
|
||||
):
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
dt_util.utcnow() + timedelta(minutes=10),
|
||||
)
|
||||
freezer.tick(timedelta(seconds=SCAN_INTERVAL.seconds))
|
||||
async_fire_time_changed(hass)
|
||||
freezer.tick(timedelta(seconds=SCAN_INTERVAL.seconds))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Allows 2 retries before going unavailable
|
||||
state = hass.states.get("sensor.home_assistant_io")
|
||||
assert state.state == "1.2.3.4"
|
||||
|
||||
freezer.tick(timedelta(seconds=SCAN_INTERVAL.seconds))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.home_assistant_io")
|
||||
|
Loading…
x
Reference in New Issue
Block a user