-
-
Notifications
You must be signed in to change notification settings - Fork 100
Fix config flow to persist cpids data in HA config entries #1712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,15 +167,19 @@ async def async_step_cp_user( | |
**user_input, | ||
CONF_NUM_CONNECTORS: self._detected_num_connectors, | ||
} | ||
self._data[CONF_CPIDS].append({self._cp_id: cp_data}) | ||
cpids_list = self._data.get(CONF_CPIDS, []).copy() | ||
cpids_list.append({self._cp_id: cp_data}) | ||
self._data = {**self._data, CONF_CPIDS: cpids_list} | ||
|
||
if user_input[CONF_MONITORED_VARIABLES_AUTOCONFIG]: | ||
self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = ( | ||
DEFAULT_MONITORED_VARIABLES | ||
) | ||
return self.async_update_reload_and_abort( | ||
self._entry, | ||
data_updates=self._data, | ||
self.hass.config_entries.async_update_entry( | ||
self._entry, data=self._data | ||
) | ||
return self.async_abort(reason="Added/Updated charge point") | ||
|
||
Comment on lines
174
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainWrite to the correct CP entry and ensure runtime picks up changes (reload).
- if user_input[CONF_MONITORED_VARIABLES_AUTOCONFIG]:
- self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = (
- DEFAULT_MONITORED_VARIABLES
- )
- self.hass.config_entries.async_update_entry(
- self._entry, data=self._data
- )
- return self.async_abort(reason="Added/Updated charge point")
+ if user_input[CONF_MONITORED_VARIABLES_AUTOCONFIG]:
+ self._data[CONF_CPIDS][cp_idx][self._cp_id][CONF_MONITORED_VARIABLES] = (
+ DEFAULT_MONITORED_VARIABLES
+ )
+ # Option A (preferred if available):
+ return self.async_update_reload_and_abort(
+ entry=self._entry,
+ data=self._data,
+ reason="cp_added_or_updated",
+ )
+ # Option B (fallback):
+ # self.hass.config_entries.async_update_entry(self._entry, data=self._data)
+ # await self.hass.config_entries.async_reload(self._entry.entry_id)
+ # return self.async_abort(reason="cp_added_or_updated") To confirm whether 🏁 Script executed: #!/bin/bash
rg -n "async_update_reload_and_abort|async_reload\(" -g '!**/venv/**' -C2 Length of output: 148 🏁 Script executed: #!/bin/bash
# Search for async_update_reload_and_abort and async_reload usages in the codebase
rg -n "async_update_reload_and_abort" -C2 .
rg -n "async_reload\(" -C2 . Length of output: 2445 🏁 Script executed: #!/bin/bash
# Search for cp_idx occurrences and context in config_flow.py
rg -n "cp_idx" -C3 custom_components/ocpp/config_flow.py Length of output: 0 Compute correct CP index and reload entry after update (config_flow.py 174–182)
🤖 Prompt for AI Agents
|
||
else: | ||
return await self.async_step_measurands() | ||
|
||
|
@@ -201,10 +205,11 @@ async def async_step_measurands(self, user_input=None): | |
self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = ( | ||
self._measurands | ||
) | ||
return self.async_update_reload_and_abort( | ||
self._entry, | ||
data_updates=self._data, | ||
|
||
self.hass.config_entries.async_update_entry( | ||
self._entry, data=self._data | ||
) | ||
return self.async_abort(reason="Added/Updated charge point") | ||
|
||
return self.async_show_form( | ||
step_id="measurands", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Prevent duplicate CPIDs and fix CPID mismatch (discovery vs. user input).
Good switch to immutable update, but we always append and we key by
self._cp_id
while duplicate-checking usesuser_input[CONF_CPID]
(nested field not covered by_async_abort_entries_match
). This can create duplicate CPIDs and ignores a potential mismatch between discoveredcp_id
and the value entered in the form.user_input[CONF_CPID] == self._cp_id
or ignore the form’s CPID to avoid drift.Apply:
Optional (enforce discovery CPID):
Also applies to: 162-169
🤖 Prompt for AI Agents