Skip to content

Don't inherit a stale Enabled setting when a device's channel layout changes - #31

Closed
drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:fix/stale-enabled-on-layout-change
Closed

drHouse-gif wants to merge 1 commit into
victronenergy:masterfrom
drHouse-gif:fix/stale-enabled-on-layout-change

Conversation

@drHouse-gif

Copy link
Copy Markdown
Collaborator

Problem

The Enabled setting is keyed by channel index, not by channel type — deliberately, per
the comment in _add_device:

Don't encode the channel type in the setting path to remain compatible with older versions.

So when a device comes back with a different layout — two switches replaced by a single EM1
meter — the new channel at index 1 inherits the Enabled the user had set for the old
switch and is started automatically, even though they never enabled an EM1 meter. The
index the device no longer uses keeps its Enabled as well, so the same thing happens again
if the device later grows back to that many channels.

This is the defect already recorded as xfail(strict=True) on
test_capability_change_while_offline.

Why the existing mechanism does not cover it

CAPABILITIES_CHANGED already handles this for a device that is still connected, via
stop_and_disable_all_channels. That helper walks the /Devices/<serial>/<n>/Enabled
D-Bus items and acts on the ones whose value is 1:

while (enabled_item := self.service.get_item(f'/Devices/{serial}/{i}/Enabled')) is not None:
    if enabled_item.value == 1: ...

But when a device is removed those items are invalidated to None (_remove_device sets
Enabled, Type and Name to None for every index). After the driver has given up
reconnecting there is therefore nothing left for it to act on, which is exactly the window
this defect lives in.

Solution

Remember the channel type each Enabled was last agreed for, in a setting alongside it:

/Settings/Devices/shelly_<serial>/<n>/Type

and fall back to the default for the new type when it differs. Indices the device no
longer uses have their Enabled cleared; channel indices are contiguous, so the scan stops
at the first index that was never set up.

The D-Bus /Devices/<serial>/<n>/Type item cannot serve this purpose, for the same reason
as above: it is invalidated on removal, so it cannot distinguish a device returning
unchanged from one returning with a different layout. Resetting on every return would
discard the user's choice after any long outage.

Upgrade behaviour

An empty stored type means "this index has not been set up before", so existing
installations keep whatever they have enabled today — nothing is reset on the first run
after an upgrade, and the new setting simply starts recording from then on.

Resetting to default_enabled rather than hard-coding 0 keeps the Smoke/Flood behaviour
intact: a digitalinput channel still defaults to enabled.

Respecting rein/Password_protected

That branch changes discovery.py heavily (+394 lines), including two hunks inside
_add_device itself. This change was deliberately placed in the channel-setup loop, which
falls between those hunks and is untouched by it.

Verified: this branch merges cleanly onto origin/rein/Password_protected. If you would
rather it waited for that branch to land first, that is fine — it is independent of it
either way.

Tests

Removed the xfail marker on test_capability_change_while_offline, per the convention in
CLAUDE.md. It passes now and fails on the unpatched driver (verified by restoring
master's discovery.py and re-running).

The test covers both halves: assert_channels_disabled(..., 2) checks the index that changed
type and the index that disappeared, and the final hold confirms the new EM1 channel
stays disabled rather than coming up on its own.

xfail count in tests/test_lifecycle.py goes from 2 to 1; the remaining
test_channel_count_change_within_reconnect_loop marker is a separate defect addressed in
#30.

Compatibility

Adds one persisted setting per channel (.../Type). No D-Bus path added, removed or
changed. No new dependency. No public signature changed. Devices whose layout is stable take
the same path as before, since the reset only fires when the stored type differs.

Verified on Python 3.12.16 against both:

aioshelly Result
14.1.0 (what CI installs, unpinned) 59 passed, 3 xfailed
12.0.0 (what the Venus OS recipe ships) 58 passed, 3 xfailed, 1 failed

Disclosure: a pre-existing failure on aioshelly 12.0.0

tests/test_timeouts.py::test_freeze_longer_than_reconnect_loop fails on aioshelly
12.0.0 and passes on 14.1.0. Not caused by this change — it reproduces on unmodified
master. Same note as in #26, #27, #28, #29 and #30.

No hardware was used — all results are against mock/mock_shelly_server.py.

…changes

The Enabled setting is keyed by channel index rather than by channel type, to
stay compatible with older versions. So when a device comes back with a
different layout - two switches replaced by a single EM1 meter - the new channel
at index 1 inherits the Enabled the user had set for the old switch, and is
started even though they never enabled an EM1 meter. The index the device no
longer uses keeps its Enabled too, so the same thing happens again if the device
later grows back to that many channels.

The capabilities_changed event already handles this for a device that is still
connected, by way of stop_and_disable_all_channels. That does not help here: it
walks the /Devices/<serial>/<n>/Enabled dbus items, and those are invalidated
when a device is removed, so after the driver gave up reconnecting there is
nothing left for it to act on.

Remember the channel type each Enabled was last agreed for, in a setting
alongside it, and fall back to the default for the new type when it differs. The
dbus Type item cannot serve this purpose for the same reason as above: it is
invalidated on removal, so it cannot distinguish a device returning unchanged
from one returning with a different layout. Indices the device no longer uses
have their Enabled cleared; channel indices are contiguous, so the scan stops at
the first index that was never set up.

An empty stored type means the index has not been set up before, so existing
installations keep whatever they have enabled today and nothing is reset on the
first run after an upgrade.

Removes the xfail marker on test_capability_change_while_offline, which now
passes and fails on the unpatched driver.
@drHouse-gif

Copy link
Copy Markdown
Collaborator Author

Correction to the "Respecting rein/Password_protected" section above

Two statements in the description are wrong and I want to retract them before they cost anyone time.

1. "Verified: this branch merges cleanly onto origin/rein/Password_protected."

It does not. My check was faulty: the git checkout -b that was meant to put me on the password branch failed because that branch name was already taken by an earlier experiment, I had suppressed stderr, and so the git merge that followed ran on this branch into itself and duly reported success.

Re-run properly, from a confirmed checkout of origin/rein/Password_protected at 0b704c5:

  • the merge conflicts (exit code 1)
  • exactly one conflicted file, discovery.py, with one conflict region

2. "This change was deliberately placed in the channel-setup loop, which falls between those hunks and is untouched by it."

Only half true. The per-index Type setting and the reset do sit inside the channel-setup loop, which your branch does indeed leave alone. But the orphan-index scan is after that loop, at the end of _add_device — and that is exactly where your new methods land, which is the conflict.

What the conflict actually is

Both sides insert at the same point, and the two insertions have nothing in common:

  • yours: _get_device_password, _auth_failure_status, _check_authentication_changed, _on_password_changed
  • mine: the scan that clears the Enabled of channel indices the device no longer uses

So resolving it is keeping both blocks, in either order. It is a textual collision, not a semantic one.

What I have not verified

I have not run the test suite against the resolved combination, so I am not claiming it works — only that the conflict is a single both-sides insertion. If this PR is useful to you I will do that and report the numbers, or simply rebase onto whichever of the two lands first. Your call.

Everything else in the description is unaffected: the behaviour, the reasoning for a persisted type rather than the invalidated D-Bus item, and the test results quoted against master.

@drHouse-gif

Copy link
Copy Markdown
Collaborator Author

Closing in favour of #39, which is the same change rebased onto v2.21. This branch was based on v2.20 and no longer merges cleanly: v2.21 changed the default_enabled expression next to the lines this touches, and #39 keeps the new expression and threads it through the reset path.

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.

1 participant