Skip to content

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

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

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

Conversation

@drHouse-gif

@drHouse-gif drHouse-gif commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Rebased onto master (d8cb65d, v2.21). This replaces #31, whose branch was based on v2.20 and no longer merged cleanly: v2.21 changed the default_enabled expression right next to the lines this touches, so temperature and humidity channels of sleepy devices now default to enabled. The resolution keeps that new expression and threads it through the reset path, so a channel whose type changed falls back to v2.21's rule rather than the old digitalinput-only one.

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.

Verified on v2.21

tests/test_lifecycle.py: 11 passed, 1 xfailed (the remaining marker belongs to a different proposal). Full suite in combination with the other proposals: 119 passed / 1 failed on aioshelly 12.0.0 and 120 passed / 0 failed on 14.1.0, against a pristine v2.21 baseline of 75/4/1 and 76/4/0. The single 12.0.0 failure, test_timeouts.py::test_freeze_longer_than_reconnect_loop, is pre-existing on unmodified v2.21.

…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 drHouse-gif changed the title Don't inherit a stale Enabled setting when a device's channel layout … Don't inherit a stale Enabled setting when a device's channel layout changes Nov 10, 2026
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