Raise HomeAssistantError when nextdns actions fail on invalid API key (#184060)

This commit is contained in:
Maciej Bieniek
2026-10-02 18:00:03 +02:00
committed by GitHub
parent 13c881913e
commit 4a3de60388
6 changed files with 82 additions and 23 deletions
+8 -1
View File
@@ -62,5 +62,12 @@ class NextDnsButton(NextDnsEntity, ButtonEntity):
"error": repr(err),
},
) from err
except InvalidApiKeyError:
except InvalidApiKeyError as err:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="auth_error",
translation_placeholders={
"entry": self.coordinator.config_entry.title,
},
) from err
@@ -30,9 +30,7 @@ rules:
unique-config-entry: done
# Silver
action-exceptions:
status: exempt
comment: The integration does not register services.
action-exceptions: done
config-entry-unloading: done
docs-configuration-parameters:
status: exempt
@@ -426,6 +426,9 @@
"method_error": {
"message": "An error occurred while calling the NextDNS API method for {entity}: {error}"
},
"set_setting_error": {
"message": "An error occurred while changing the setting for {entity}"
},
"update_error": {
"message": "An error occurred while retrieving data from the NextDNS API for {entry}: {error}"
}
+17 -5
View File
@@ -622,10 +622,22 @@ class NextDnsSwitch(NextDnsEntity, SwitchEntity):
"error": repr(err),
},
) from err
except InvalidApiKeyError:
except InvalidApiKeyError as err:
self.coordinator.config_entry.async_start_reauth(self.hass)
return
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="auth_error",
translation_placeholders={
"entry": self.coordinator.config_entry.title,
},
) from err
if result:
self._attr_is_on = new_state
self.async_write_ha_state()
if not result:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_setting_error",
translation_placeholders={"entity": self.entity_id},
)
self._attr_is_on = new_state
self.async_write_ha_state()
+11 -7
View File
@@ -97,17 +97,21 @@ async def test_button_auth_error(
mock_config_entry: MockConfigEntry,
mock_nextdns_client: AsyncMock,
) -> None:
"""Tests that the press action starts re-auth flow."""
"""Tests that the press action raises an error and starts re-auth flow."""
await init_integration(hass, mock_config_entry)
mock_nextdns_client.clear_logs.side_effect = InvalidApiKeyError
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.fake_profile_clear_logs"},
blocking=True,
)
with pytest.raises(
HomeAssistantError,
match="Authentication failed for NextDNS, please update your API key",
):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.fake_profile_clear_logs"},
blocking=True,
)
assert mock_config_entry.state is ConfigEntryState.LOADED
+42 -7
View File
@@ -183,17 +183,21 @@ async def test_switch_auth_error(
mock_config_entry: MockConfigEntry,
mock_nextdns_client: AsyncMock,
) -> None:
"""Tests that the turn on/off action starts re-auth flow."""
"""Tests that the turn on/off action raises an error and starts re-auth flow."""
await init_integration(hass, mock_config_entry)
mock_nextdns_client.set_setting.side_effect = InvalidApiKeyError
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
blocking=True,
)
with pytest.raises(
HomeAssistantError,
match="Authentication failed for NextDNS, please update your API key",
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
blocking=True,
)
assert mock_config_entry.state is ConfigEntryState.LOADED
@@ -207,3 +211,34 @@ async def test_switch_auth_error(
assert "context" in flow
assert flow["context"].get("source") == SOURCE_REAUTH
assert flow["context"].get("entry_id") == mock_config_entry.entry_id
async def test_switch_set_setting_failure(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_nextdns_client: AsyncMock,
) -> None:
"""Tests that the turn on action raises an error when the API reports failure."""
await init_integration(hass, mock_config_entry)
mock_nextdns_client.set_setting.return_value = False
state = hass.states.get("switch.fake_profile_block_page")
assert state
assert state.state == STATE_OFF
with pytest.raises(
HomeAssistantError,
match="An error occurred while changing the setting for"
" switch.fake_profile_block_page",
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.fake_profile_block_page"},
blocking=True,
)
state = hass.states.get("switch.fake_profile_block_page")
assert state
assert state.state == STATE_OFF