Add action exceptions to Duck DNS (#160331)

This commit is contained in:
Manu
2026-01-06 08:38:22 +01:00
committed by GitHub
parent 5f8f3c961a
commit 53ed344fe0
3 changed files with 63 additions and 9 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
"""Integrate with DuckDNS."""
"""Duck DNS integration."""
from __future__ import annotations
+24 -7
View File
@@ -2,11 +2,12 @@
from __future__ import annotations
from aiohttp import ClientError
import voluptuous as vol
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_DOMAIN
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import ConfigEntrySelector
@@ -62,9 +63,25 @@ async def update_domain_service(call: ServiceCall) -> None:
session = async_get_clientsession(call.hass)
await update_duckdns(
session,
entry.data[CONF_DOMAIN],
entry.data[CONF_ACCESS_TOKEN],
txt=call.data.get(ATTR_TXT),
)
try:
if not await update_duckdns(
session,
entry.data[CONF_DOMAIN],
entry.data[CONF_ACCESS_TOKEN],
txt=call.data.get(ATTR_TXT),
):
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="update_failed",
translation_placeholders={
CONF_DOMAIN: entry.data[CONF_DOMAIN],
},
)
except ClientError as e:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
translation_placeholders={
CONF_DOMAIN: entry.data[CONF_DOMAIN],
},
) from e
+38 -1
View File
@@ -18,7 +18,7 @@ from homeassistant.components.duckdns.helpers import UPDATE_URL
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.util.dt import utcnow
from .conftest import TEST_SUBDOMAIN, TEST_TOKEN
@@ -202,6 +202,43 @@ async def test_service_exceptions(
)
@pytest.mark.parametrize(
("side_effect", "exception_msg"),
[
(
False,
"Updating Duck DNS domain homeassistant failed",
),
(
ClientError,
"Updating Duck DNS domain homeassistant failed due to a connection error",
),
],
)
@pytest.mark.usefixtures("setup_duckdns")
async def test_service_request_exception(
hass: HomeAssistant,
config_entry: MockConfigEntry,
side_effect: Exception | bool,
exception_msg: str,
) -> None:
"""Test service request exception."""
with (
patch(
"homeassistant.components.duckdns.services.update_duckdns",
side_effect=[side_effect],
),
pytest.raises(HomeAssistantError, match=exception_msg),
):
await hass.services.async_call(
DOMAIN,
SERVICE_SET_TXT,
{ATTR_CONFIG_ENTRY: config_entry.entry_id},
blocking=True,
)
@pytest.mark.usefixtures("setup_duckdns")
async def test_service_select_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker