Skip to content

Commit 102debb

Browse files
authored
Merge pull request #243 from binarydev/fix-238
Fix deprecation warning, broken reconfiguration, and obsolete dependency.
2 parents 312ca9f + b6137b8 commit 102debb

File tree

5 files changed

+95
-16
lines changed

5 files changed

+95
-16
lines changed

custom_components/generac/config_flow.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,38 @@ def _extract_email_from_cookie(self, cookie_str):
4545
except Exception:
4646
return None
4747

48-
async def async_step_reconfigure(
49-
self, user_input=None
50-
): # pylint: disable=unused-argument
51-
"""Manage the options."""
52-
return await self.async_step_user(user_input)
48+
async def async_step_reconfigure(self, user_input=None):
49+
"""Handle reconfiguration."""
50+
errors = {}
51+
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
52+
53+
if user_input is not None:
54+
session_cookie = user_input.get(CONF_SESSION_COOKIE, "")
55+
error = await self._test_credentials(
56+
"",
57+
"",
58+
session_cookie,
59+
)
60+
if error is None:
61+
return self.async_update_reload_and_abort(
62+
entry,
63+
data={**entry.data, **user_input},
64+
reason="Reconfigure Successful",
65+
)
66+
errors["base"] = error
67+
68+
return self.async_show_form(
69+
step_id="reconfigure",
70+
data_schema=vol.Schema(
71+
{
72+
vol.Required(
73+
CONF_SESSION_COOKIE,
74+
default=entry.data.get(CONF_SESSION_COOKIE),
75+
): str,
76+
}
77+
),
78+
errors=errors,
79+
)
5380

5481
async def async_step_user(self, user_input=None):
5582
"""Handle a flow initialized by the user."""
@@ -71,9 +98,7 @@ async def async_step_user(self, user_input=None):
7198
unique_id = self._extract_email_from_cookie(session_cookie) or "generac"
7299

73100
await self.async_set_unique_id(unique_id)
74-
# We don't want to abort if the unique ID is already configured
75-
# HA does the right thing and will reconfigure the existing entry
76-
# self._abort_if_unique_id_configured()
101+
self._abort_if_unique_id_configured()
77102

78103
return self.async_create_entry(title=unique_id, data=user_input)
79104
else:
@@ -122,7 +147,6 @@ class GeneracOptionsFlowHandler(config_entries.OptionsFlow):
122147

123148
def __init__(self, config_entry):
124149
"""Initialize HACS options flow."""
125-
self.config_entry = config_entry
126150
self.options = dict(config_entry.options)
127151

128152
async def async_step_init(self, user_input=None): # pylint: disable=unused-argument

custom_components/generac/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"documentation": "https://github.com/binarydev/ha-generac",
88
"iot_class": "cloud_polling",
99
"issue_tracker": "https://github.com/binarydev/ha-generac/issues",
10-
"requirements": ["beautifulsoup4==4.12.2", "dacite==1.8.1"],
10+
"requirements": ["dacite==1.8.1"],
1111
"version": "0.4.0"
1212
}

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
beautifulsoup4==4.13.4
21
dacite==1.9.2
32
homeassistant==2025.8.3
43
voluptuous==0.15.2

tests/test_config_flow.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,69 @@ async def test_options_flow(hass: HomeAssistant) -> None:
169169
@pytest.mark.asyncio
170170
async def test_reconfigure_flow(hass: HomeAssistant) -> None:
171171
"""Test the reconfigure flow."""
172-
entry = MockConfigEntry(domain=DOMAIN, data={}, options={})
172+
entry = MockConfigEntry(
173+
domain=DOMAIN, data={"session_cookie": "old_cookie"}, options={}
174+
)
173175
entry.add_to_hass(hass)
174176

175-
await hass.config_entries.async_setup(entry.entry_id)
176-
await hass.async_block_till_done()
177+
with patch("custom_components.generac.async_setup_entry", return_value=True):
178+
await hass.config_entries.async_setup(entry.entry_id)
179+
await hass.async_block_till_done()
177180

178181
result = await hass.config_entries.flow.async_init(
179182
DOMAIN,
180183
context={
181-
"source": config_entries.SOURCE_RECONFIGURE,
184+
"source": "reconfigure",
182185
"entry_id": entry.entry_id,
183186
},
184187
)
185188

186189
assert result["type"] == "form"
187-
assert result["step_id"] == "user"
190+
assert result["step_id"] == "reconfigure"
191+
192+
with patch(
193+
"custom_components.generac.config_flow.GeneracApiClient.async_get_data",
194+
return_value=True,
195+
), patch("custom_components.generac.async_setup_entry", return_value=True), patch(
196+
"custom_components.generac.async_unload_entry", return_value=True
197+
):
198+
result2 = await hass.config_entries.flow.async_configure(
199+
result["flow_id"],
200+
{
201+
"session_cookie": "new_cookie",
202+
},
203+
)
204+
await hass.async_block_till_done()
205+
206+
assert result2["type"] == "abort"
207+
assert result2["reason"] == "Reconfigure Successful"
208+
assert entry.data["session_cookie"] == "new_cookie"
209+
210+
211+
async def test_duplicate_entry(hass: HomeAssistant) -> None:
212+
"""Test duplicate entry is handled."""
213+
entry = MockConfigEntry(
214+
domain=DOMAIN,
215+
unique_id="binarydev@testing.com",
216+
data={"session_cookie": "existing"},
217+
)
218+
entry.add_to_hass(hass)
219+
220+
await setup.async_setup_component(hass, "persistent_notification", {})
221+
result = await hass.config_entries.flow.async_init(
222+
DOMAIN, context={"source": config_entries.SOURCE_USER}
223+
)
224+
225+
with patch(
226+
"custom_components.generac.config_flow.GeneracApiClient.async_get_data",
227+
return_value=True,
228+
):
229+
result2 = await hass.config_entries.flow.async_configure(
230+
result["flow_id"],
231+
{
232+
"session_cookie": "MobileLinkClientCookie=%7B%0D%0A%20%20%22signInName%22%3A%20%22binarydev%40testing.com%22%0D%0A%7D",
233+
},
234+
)
235+
236+
assert result2["type"] == "abort"
237+
assert result2["reason"] == "already_configured"

0 commit comments

Comments
 (0)