Skip to content

Verbraucher: nur steuerbare in Prioritätenliste hinzufügen, Prioritätensteuerung-Korrektur - #4354

Open
LKuemmel wants to merge 4 commits into
openWB:masterfrom
LKuemmel:loadmanagement_prio_list
Open

LKuemmel wants to merge 4 commits into
openWB:masterfrom
LKuemmel:loadmanagement_prio_list

Conversation

@LKuemmel

@LKuemmel LKuemmel commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Sourcery

Keep priority control synchronized with configured, controllable vehicles and consumers.

New Features:

  • Validate priority-control entries against currently configured vehicles and controllable consumers, removing obsolete entries and adding missing ones with user notifications.

Bug Fixes:

  • Prevent non-controllable consumers from being added to priority control and update priority entries correctly when consumer usage changes, including retained MQTT messages.

Enhancements:

  • Preserve valid priority groups while cleaning up invalid or empty entries and use the consumer's configured default usage when creating consumers.

Tests:

  • Add coverage for restoring missing vehicle priorities, preserving valid groups, removing obsolete entries, and handling consumer usage defaults.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 5 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="packages/control/counter_all/loadmanagement_prio.py" line_range="26-27" />
<code_context>
+                },
+            }
+
+            self._remove_obsolete_entries(self.data.get.loadmanagement_prios, valid_ids)
+            self._add_missing_entries(self.data.get.loadmanagement_prios, valid_ids)
+
+        except Exception:
</code_context>
<issue_to_address>
**Priority repairs are discarded**

When periodic validation adds or removes a priority entry, `validate_loadmanagement_prios()` changes only the copied priority list without publishing it to `SubData`; the next `copy_data()` reloads the stale list, so callers keep seeing old priorities and the repairs are lost.

Publish the updated priority list to `SubData` whenever validation changes it.

Also at `packages/control/counter_all/loadmanagement_prio.py:43`, `packages/control/counter_all/loadmanagement_prio.py:53`, `packages/main.py:195`.
</issue_to_address>

### Comment 2
<location path="packages/control/counter_all/loadmanagement_prio.py" line_range="37" />
<code_context>
+            if entry["type"] == "group":
+                children = entry["children"]
+                had_children = bool(children)
+                self._remove_obsolete_entries(children)
+                if had_children and not children:
+                    entries.remove(entry)
</code_context>
<issue_to_address>
**Groups stop priority validation**

When the priority list contains a group, `_remove_obsolete_entries()` requires `valid_ids`, but the recursive call omits it and raises `TypeError`; `validate_loadmanagement_prios()` catches the exception and exits before completing cleanup or adding missing entries, leaving the list unsynchronized.

Pass `valid_ids` to the recursive `_remove_obsolete_entries()` call.
</issue_to_address>

### Comment 3
<location path="packages/main.py" line_range="195" />
<code_context>
                 data.data.general_data.grid_protection()
                 data.data.optional_data.ocpp_transfer_meter_values()
                 data.data.counter_all_data.validate_hierarchy()
+                data.data.counter_all_data.validate_loadmanagement_prios()
                 data.data.optional_data.remove_outdated_prices()
             loadvars_.ep_get_prices()
</code_context>
<issue_to_address>
**Transient copy failures erase priority groups**

When a transient copy error leaves either component dictionary incomplete during the five-minute handler, `copy_data()` can leave `ev_data` or `consumer_data` partially populated after logging and swallowing a copy exception; validation then removes entries it cannot see and deletes groups that become empty. When the data recovers, missing entries are appended flat, so users lose their configured priority grouping.

Make data copying atomic or return a success status, and skip priority validation unless the component snapshot completed successfully.
</issue_to_address>

### Comment 4
<location path="packages/control/counter_all/loadmanagement_prio.py" line_range="19-21" />
<code_context>
+    def validate_loadmanagement_prios(self: LoadmanagementPrioProtocol) -> None:
+        try:
+            valid_ids = {
+                ComponentType.VEHICLE: {vehicle.num for vehicle in data.data.ev_data.values()},
+                ComponentType.CONSUMER: {
+                    consumer.num for consumer in data.data.consumer_data.values()
+                    if consumer.data.usage.type not in NOT_CONTROLLED
+                },
</code_context>
<issue_to_address>
**New priorities get arbitrary order**

When multiple valid IDs are missing from the list, `_add_missing_entries` iterates these sets and appends each missing item to the priority list, so newly discovered vehicles and consumers receive an order unrelated to their source order; that list order determines charging priority, so users can see unexpected prioritization.

Preserve a defined order for IDs (for example, source order or sorted IDs) when adding missing entries.
</issue_to_address>

### Comment 5
<location path="packages/control/counter_all/loadmanagement_prio.py" line_range="36-39" />
<code_context>
+        for entry in entries.copy():
+            if entry["type"] == "group":
+                children = entry["children"]
+                had_children = bool(children)
+                self._remove_obsolete_entries(children)
+                if had_children and not children:
+                    entries.remove(entry)
+            else:
+                for type_name, ids in valid_ids.items():
</code_context>
<issue_to_address>
**Empty priority groups remain**

When the priority list contains an empty group before validation, or a nested empty group, `_remove_obsolete_entries` skips removal when a group starts empty, so pre-existing empty groups—and empty nested groups—remain in the priority list after validation and are still shown to callers.

Remove a group whenever its children are empty after recursive cleanup, including when it was empty before validation.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +26 to +27
self._remove_obsolete_entries(self.data.get.loadmanagement_prios, valid_ids)
self._add_missing_entries(self.data.get.loadmanagement_prios, valid_ids)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Priority repairs are discarded

When periodic validation adds or removes a priority entry, validate_loadmanagement_prios() changes only the copied priority list without publishing it to SubData; the next copy_data() reloads the stale list, so callers keep seeing old priorities and the repairs are lost.

Publish the updated priority list to SubData whenever validation changes it.

Also at packages/control/counter_all/loadmanagement_prio.py:43, packages/control/counter_all/loadmanagement_prio.py:53, packages/main.py:195.

Prompt for AI agents
In `packages/control/counter_all/loadmanagement_prio.py` at lines 26-27:

**Priority repairs are discarded**

When periodic validation adds or removes a priority entry, `validate_loadmanagement_prios()` changes only the copied priority list without publishing it to `SubData`; the next `copy_data()` reloads the stale list, so callers keep seeing old priorities and the repairs are lost.

Publish the updated priority list to `SubData` whenever validation changes it.

Also at `packages/control/counter_all/loadmanagement_prio.py:43`, `packages/control/counter_all/loadmanagement_prio.py:53`, `packages/main.py:195`.

Comment thread packages/main.py
data.data.general_data.grid_protection()
data.data.optional_data.ocpp_transfer_meter_values()
data.data.counter_all_data.validate_hierarchy()
data.data.counter_all_data.validate_loadmanagement_prios()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Transient copy failures erase priority groups

When a transient copy error leaves either component dictionary incomplete during the five-minute handler, copy_data() can leave ev_data or consumer_data partially populated after logging and swallowing a copy exception; validation then removes entries it cannot see and deletes groups that become empty. When the data recovers, missing entries are appended flat, so users lose their configured priority grouping.

Make data copying atomic or return a success status, and skip priority validation unless the component snapshot completed successfully.

Prompt for AI agents
In `packages/main.py` at line 195:

**Transient copy failures erase priority groups**

When a transient copy error leaves either component dictionary incomplete during the five-minute handler, `copy_data()` can leave `ev_data` or `consumer_data` partially populated after logging and swallowing a copy exception; validation then removes entries it cannot see and deletes groups that become empty. When the data recovers, missing entries are appended flat, so users lose their configured priority grouping.

Make data copying atomic or return a success status, and skip priority validation unless the component snapshot completed successfully.

Comment on lines +19 to +21
ComponentType.VEHICLE: {vehicle.num for vehicle in data.data.ev_data.values()},
ComponentType.CONSUMER: {
consumer.num for consumer in data.data.consumer_data.values()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · New priorities get arbitrary order

When multiple valid IDs are missing from the list, _add_missing_entries iterates these sets and appends each missing item to the priority list, so newly discovered vehicles and consumers receive an order unrelated to their source order; that list order determines charging priority, so users can see unexpected prioritization.

Preserve a defined order for IDs (for example, source order or sorted IDs) when adding missing entries.

Prompt for AI agents
In `packages/control/counter_all/loadmanagement_prio.py` at lines 19-21:

**New priorities get arbitrary order**

When multiple valid IDs are missing from the list, `_add_missing_entries` iterates these sets and appends each missing item to the priority list, so newly discovered vehicles and consumers receive an order unrelated to their source order; that list order determines charging priority, so users can see unexpected prioritization.

Preserve a defined order for IDs (for example, source order or sorted IDs) when adding missing entries.

Comment on lines +36 to +39
had_children = bool(children)
self._remove_obsolete_entries(children)
if had_children and not children:
entries.remove(entry)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Empty priority groups remain

When the priority list contains an empty group before validation, or a nested empty group, _remove_obsolete_entries skips removal when a group starts empty, so pre-existing empty groups—and empty nested groups—remain in the priority list after validation and are still shown to callers.

Remove a group whenever its children are empty after recursive cleanup, including when it was empty before validation.

Prompt for AI agents
In `packages/control/counter_all/loadmanagement_prio.py` at lines 36-39:

**Empty priority groups remain**

When the priority list contains an empty group before validation, or a nested empty group, `_remove_obsolete_entries` skips removal when a group starts empty, so pre-existing empty groups—and empty nested groups—remain in the priority list after validation and are still shown to callers.

Remove a group whenever its children are empty after recursive cleanup, including when it was empty before validation.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active moderate and critical findings remain in the priority-list cleanup and ordering logic, leaving obsolete groups and making restored priorities nondeterministic.

2 open issues
What changed in this PR

This PR synchronizes load-management priorities with currently valid and controllable vehicles and consumers, including automatic cleanup and restoration during the five-minute control cycle.

Changes:

  • Validates priorities periodically and removes obsolete or non-controllable entries.
  • Restores missing entries for valid controllable vehicles and consumers.
  • Corrects consumer creation handling and adds coverage for priority synchronization and usage types.
File Description
packages/​main.py Runs load-management priority validation during the five-minute control cycle.
packages/​helpermodules/​command.py Uses the consumer's default usage type when updating priorities during creation.
packages/​helpermodules/​command_test.py Tests priority behavior for all consumer usage types.
packages/​control/​counter_all/​loadmanagement_prio.py Implements priority validation, cleanup, restoration, and consumer usage updates.
packages/​control/​counter_all/​loadmanagement_prio_test.py Adds test coverage for priority synchronization, cleanup, grouping, and consumer priority updates.
packages/​control/​counter_all/​counter_all_data.py Extends the priority-management protocol with validation helper methods.

Review effort: Lite

Comment on lines +52 to +55
for type_name, ids in valid_ids.items():
for id in ids:
if not self._has_loadmanagement_prio_item(type_name, id, entries):
self.add_loadmanagement_prio_item(type_name, id)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reihenfolge muss der Kunde sowieso nach seinen Wünschen anpassen, er bekommt eine Meldung dazu.

Comment on lines +37 to +40
had_children = bool(children)
self._remove_obsolete_loadmanagement_prio_entries(children, valid_ids)
if had_children and not children:
entries.remove(entry)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gewollt, könnte ein Platzhalter sein.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants