1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Use service helper to retrieve config entry in Duck DNS integration (#162879)

This commit is contained in:
Manu
2026-02-13 00:00:23 +01:00
committed by GitHub
parent d10f5cc9ea
commit e6de37cc69
2 changed files with 19 additions and 22 deletions

View File

@@ -8,7 +8,7 @@ import voluptuous as vol
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_DOMAIN
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, service
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import ConfigEntrySelector
@@ -47,13 +47,9 @@ def get_config_entry(
translation_domain=DOMAIN,
translation_key="entry_not_selected",
)
return entries[0]
if not (entry := hass.config_entries.async_get_entry(entry_id)):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="entry_not_found",
)
return entry
entry_id = entries[0].entry_id
return service.async_get_config_entry(hass, DOMAIN, entry_id)
async def update_domain_service(call: ServiceCall) -> None:

View File

@@ -172,8 +172,8 @@ async def test_service_clear_txt(
@pytest.mark.parametrize(
("payload", "exception_msg"),
[
({ATTR_CONFIG_ENTRY: "1234"}, "Duck DNS integration entry not found"),
(None, "Duck DNS integration entry not selected"),
({ATTR_CONFIG_ENTRY: "1234"}, "service_config_entry_not_found"),
(None, "entry_not_selected"),
],
)
@pytest.mark.usefixtures("setup_duckdns")
@@ -193,26 +193,21 @@ async def test_service_exceptions(
entry_id="67890",
).add_to_hass(hass)
with pytest.raises(ServiceValidationError, match=exception_msg):
with pytest.raises(ServiceValidationError) as e:
await hass.services.async_call(
DOMAIN,
SERVICE_SET_TXT,
payload,
blocking=True,
)
assert e.value.translation_key == exception_msg
@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",
),
(False, "update_failed"),
(ClientError, "connection_error"),
],
)
@pytest.mark.usefixtures("setup_duckdns")
@@ -229,7 +224,7 @@ async def test_service_request_exception(
"homeassistant.components.duckdns.services.update_duckdns",
side_effect=[side_effect],
),
pytest.raises(HomeAssistantError, match=exception_msg),
pytest.raises(HomeAssistantError) as e,
):
await hass.services.async_call(
DOMAIN,
@@ -237,6 +232,7 @@ async def test_service_request_exception(
{ATTR_CONFIG_ENTRY: config_entry.entry_id},
blocking=True,
)
assert e.value.translation_key == exception_msg
@pytest.mark.usefixtures("setup_duckdns")
@@ -244,7 +240,7 @@ async def test_service_select_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test config entry selection."""
MockConfigEntry(
config_entry = MockConfigEntry(
domain=DOMAIN,
title=f"{TEST_SUBDOMAIN}.duckdns.org",
data={
@@ -252,7 +248,12 @@ async def test_service_select_entry(
CONF_ACCESS_TOKEN: TEST_TOKEN,
},
entry_id="67890",
).add_to_hass(hass)
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
# Empty the fixture mock requests
aioclient_mock.clear_requests()