Fix light: restore last brightness on turn-on instead of staying dark - #9
Open
danielbrowne wants to merge 1 commit into
Open
Fix light: restore last brightness on turn-on instead of staying dark#9danielbrowne wants to merge 1 commit into
danielbrowne wants to merge 1 commit into
Conversation
The light entity models 'off' as brightness 0 (the device has no separate lamp power). However async_turn_on only called set_brightness/set_color when an explicit value was passed, so a bare toggle-on (light card tap or the more-info dialog toggle) turned the device power on but left brightness at 0 -- so is_on stayed False and the light could not be switched back on from the UI. Cycling the power switch didn't help (it only toggles device power, not brightness); only setting an explicit brightness/color (e.g. from the Hatch app) recovered it. Store the brightness on turn-off and restore it on a value-less turn-on (falling back to full brightness when unknown), so the on/off toggle behaves like a normal dimmable light. Existing behavior for explicit brightness/color is unchanged. Adds tests covering off->on round-trip, default fallback, and the no-clobber-when-already-lit case.
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.
Problem
The light's on/off toggle is effectively one-way: you can turn the light off, but a plain toggle won't turn it back on. From the UI (light card tap, or the more-info dialog toggle) the light goes dark and then won't come back — and cycling the power switch doesn't help either. The only way to recover is to set an explicit brightness/color (e.g. from the Hatch app).
Root cause
The device has no separate lamp power, so the integration models light "off" as
brightness = 0. Butasync_turn_ononly callsset_brightness/set_colorwhen an explicit value is passed in:A bare
turn_on()(no attributes) therefore powers the device on but leaves brightness at0, sois_onstaysFalseand the light appears stuck off. The power switch only toggles device power (SI), not brightness, so it can't rescue it.Fix
async_turn_offbefore zeroing it.async_turn_on, when no brightness is supplied and the light is currently at 0, restore the remembered brightness (falling back to full brightness when unknown).Behaviour with an explicit brightness or color is unchanged; a bare toggle now round-trips off→on like a normal dimmable light. No new dependencies.
Tests
Adds coverage to
tests/test_light.py:test_async_turn_off_remembers_brightnesstest_async_turn_on_restores_last_brightnesstest_async_turn_on_defaults_full_when_unknowntest_async_turn_on_no_restore_when_already_lit(doesn't clobber current brightness)test_turn_off_then_on_round_trips_brightnessAll existing light tests remain valid (the restore path only triggers when current brightness is 0).
Notes
Verified against a real 1st-gen Hatch Rest.