Skip to content

Commit

Permalink
Fix bad access to UniFi runtime_data when not assigned (home-assistan…
Browse files Browse the repository at this point in the history
…t#121725)

* Fix bad access to runtime_data when not assigned

* Fix review comment

* Clean up if statements
Kane610 authored Jul 10, 2024
1 parent 70f05e5 commit 61111f5
Showing 2 changed files with 45 additions and 7 deletions.
13 changes: 6 additions & 7 deletions homeassistant/components/unifi/config_flow.py
Original file line number Diff line number Diff line change
@@ -164,13 +164,12 @@ async def async_step_site(
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(
39 changes: 39 additions & 0 deletions tests/components/unifi/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 61111f5

Please sign in to comment.