Fix bad access to UniFi runtime_data when not assigned (#121725)

* Fix bad access to runtime_data when not assigned

* Fix review comment

* Clean up if statements
This commit is contained in:
Robert Svensson 2024-07-10 23:53:11 +02:00 committed by GitHub
parent 70f05e5f13
commit 61111f5d71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 7 deletions

View File

@ -164,13 +164,12 @@ class UnifiFlowHandler(ConfigFlow, domain=UNIFI_DOMAIN):
config_entry = self.reauth_config_entry
abort_reason = "reauth_successful"
if (
config_entry is not None
and config_entry.state is not ConfigEntryState.NOT_LOADED
):
hub = config_entry.runtime_data
if hub and hub.available:
if config_entry:
if (
config_entry.state is ConfigEntryState.LOADED
and (hub := config_entry.runtime_data)
and hub.available
):
return self.async_abort(reason="already_configured")
return self.async_update_reload_and_abort(

View File

@ -1,5 +1,6 @@
"""Test UniFi Network config flow."""
from collections.abc import Callable
import socket
from unittest.mock import PropertyMock, patch
@ -338,6 +339,44 @@ async def test_reauth_flow_update_configuration(
assert config_entry.data[CONF_PASSWORD] == "new_pass"
async def test_reauth_flow_update_configuration_on_not_loaded_entry(
hass: HomeAssistant, config_entry_factory: Callable[[], ConfigEntry]
) -> None:
"""Verify reauth flow can update hub configuration on a not loaded entry."""
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError):
config_entry = await config_entry_factory()
result = await hass.config_entries.flow.async_init(
UNIFI_DOMAIN,
context={
"source": SOURCE_REAUTH,
"unique_id": config_entry.unique_id,
"entry_id": config_entry.entry_id,
},
data=config_entry.data,
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "1.2.3.4",
CONF_USERNAME: "new_name",
CONF_PASSWORD: "new_pass",
CONF_PORT: 1234,
CONF_VERIFY_SSL: True,
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reauth_successful"
assert config_entry.data[CONF_HOST] == "1.2.3.4"
assert config_entry.data[CONF_USERNAME] == "new_name"
assert config_entry.data[CONF_PASSWORD] == "new_pass"
@pytest.mark.parametrize("client_payload", [CLIENTS])
@pytest.mark.parametrize("device_payload", [DEVICES])
@pytest.mark.parametrize("wlan_payload", [WLANS])