…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.
Problem
The
Enabledsetting is keyed by channel index, not by channel type — deliberately, perthe comment in
_add_device: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
Enabledthe user had set for the oldswitch and is started automatically, even though they never enabled an EM1 meter. The
index the device no longer uses keeps its
Enabledas well, so the same thing happens againif the device later grows back to that many channels.
This is the defect already recorded as
xfail(strict=True)ontest_capability_change_while_offline.Why the existing mechanism does not cover it
CAPABILITIES_CHANGEDalready handles this for a device that is still connected, viastop_and_disable_all_channels. That helper walks the/Devices/<serial>/<n>/EnabledD-Bus items and acts on the ones whose value is
1:But when a device is removed those items are invalidated to
None(_remove_devicesetsEnabled,TypeandNametoNonefor every index). After the driver has given upreconnecting 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
Enabledwas last agreed for, in a setting alongside it:and fall back to the default for the new type when it differs. Indices the device no
longer uses have their
Enabledcleared; channel indices are contiguous, so the scan stopsat the first index that was never set up.
The D-Bus
/Devices/<serial>/<n>/Typeitem cannot serve this purpose, for the same reasonas 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_enabledrather than hard-coding0keeps the Smoke/Flood behaviourintact: a
digitalinputchannel still defaults to enabled.Respecting
rein/Password_protectedThat branch changes
discovery.pyheavily (+394 lines), including two hunks inside_add_deviceitself. This change was deliberately placed in the channel-setup loop, whichfalls between those hunks and is untouched by it.
Verified: this branch merges cleanly onto
origin/rein/Password_protected. If you wouldrather it waited for that branch to land first, that is fine — it is independent of it
either way.
Tests
Removed the
xfailmarker ontest_capability_change_while_offline, per the convention inCLAUDE.md. It passes now and fails on the unpatched driver (verified by restoringmaster'sdiscovery.pyand re-running).The test covers both halves:
assert_channels_disabled(..., 2)checks the index that changedtype and the index that disappeared, and the final
holdconfirms the new EM1 channelstays disabled rather than coming up on its own.
xfailcount intests/test_lifecycle.pygoes from 2 to 1; the remainingtest_channel_count_change_within_reconnect_loopmarker is a separate defect addressed in#30.
Compatibility
Adds one persisted setting per channel (
.../Type). No D-Bus path added, removed orchanged. 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:
Disclosure: a pre-existing failure on aioshelly 12.0.0
tests/test_timeouts.py::test_freeze_longer_than_reconnect_loopfails on aioshelly12.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.