Skip to content

Commit 0952f64

Browse files
test(octopus): pin that the auto-wiring guard tests for content, not presence
automatic_config() records the device set it wired on every call, including calls that wired nothing, so the recorded set is [] rather than None after one run on an account with no devices. The "have we ever wired anything" guard therefore has to test that set for content: an `is not None` check passes from the second call onwards and blanks a manual apps.yaml config that auto-discovery never touched. The existing single-call test could not catch that, so add one that runs automatic_config() repeatedly with no devices. Corrects the comment, which described the guard as an is-not-None check it never was.
1 parent 64d9b3e commit 0952f64

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

apps/predbat/octopus.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,11 @@ def automatic_config(self, tariffs):
11751175
self.set_arg("metric_standing_charge", self.get_entity_name("sensor", tariff + "_standing"))
11761176
devices = self.get_intelligent_devices()
11771177
# Also enter this block when the device set has emptied, so the slot args are cleared rather
1178-
# than left pointing at a device that no longer exists. Only once something has been wired
1179-
# though (intelligent_config_devices is not None): before the first discovery a user's own
1180-
# apps.yaml entries are the only wiring there is, and blanking them would break a manual setup.
1178+
# than left pointing at a device that no longer exists. Only once real devices have actually
1179+
# been wired though - hence the truthiness test rather than an `is not None` one. The set
1180+
# below is recorded on every call, including calls that wired nothing, so it is [] and not
1181+
# None after the first run on an account with no devices; treating that as "previously
1182+
# wired" would blank a user's own apps.yaml entries, which auto-discovery never touched.
11811183
if devices or self.intelligent_config_devices:
11821184
# Suspended devices (e.g. an old/decommissioned charger still linked to the Octopus
11831185
# account) aren't actively charging, so exclude them from the entity lists and from

apps/predbat/tests/test_octopus_misc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,7 @@ def test_octopus_automatic_config_clears_removed_devices(my_predbat):
20532053
Tests:
20542054
- Test 1: Slot args are cleared when the last discovered device disappears
20552055
- Test 2: Manually configured slot args are untouched when no device was ever discovered
2056+
- Test 3: Manually configured slot args survive repeated runs with no devices
20562057
"""
20572058
print("\n**** Running Octopus automatic_config device removal tests ****")
20582059
failed = False
@@ -2093,6 +2094,26 @@ def test_octopus_automatic_config_clears_removed_devices(my_predbat):
20932094
else:
20942095
print("PASS: Manual slot config untouched when no device was ever discovered")
20952096

2097+
# Test 3: automatic_config() records the device set it wired on every call, including the calls
2098+
# where it wired nothing - so after one run with no devices the recorded set is [] rather than
2099+
# None. The "have we ever wired anything" guard therefore has to test that set for content, not
2100+
# merely for being set: an `is not None` check would pass here from the second call onwards and
2101+
# blank a manual apps.yaml config that auto-discovery never touched.
2102+
print("\n*** Test 3: Manual slot config survives repeated runs with no devices ***")
2103+
my_predbat.args["octopus_intelligent_slot"] = "binary_sensor.manually_configured_dispatching"
2104+
api3 = OctopusAPI(my_predbat, key="test-api-key", account_id="test-account", automatic=False)
2105+
api3.intelligent_devices = {}
2106+
2107+
api3.automatic_config(["import"])
2108+
api3.automatic_config(["import"])
2109+
api3.automatic_config(["import"])
2110+
2111+
if my_predbat.args.get("octopus_intelligent_slot") != "binary_sensor.manually_configured_dispatching":
2112+
print(f"ERROR: Repeated runs with no devices blanked the manual slot config, got {my_predbat.args.get('octopus_intelligent_slot')}")
2113+
failed = True
2114+
else:
2115+
print("PASS: Manual slot config survives repeated runs with no devices")
2116+
20962117
my_predbat.args.clear()
20972118
my_predbat.args.update(original_args)
20982119

0 commit comments

Comments
 (0)