Repository navigation
Don't inherit a stale Enabled setting when a device's channel layout changes - #39
Open
drHouse-gif wants to merge 1 commit into
Open
drHouse-gif wants to merge 1 commit into
drHouse-gif wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rebased onto
master(d8cb65d, v2.21). This replaces #31, whose branch was based on v2.20 and no longer merged cleanly: v2.21 changed thedefault_enabledexpression 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 olddigitalinput-only one.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.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.