Skip to content

Fix light: restore last brightness on turn-on instead of staying dark - #9

Open
danielbrowne wants to merge 1 commit into
jcgoette:mainfrom
danielbrowne:fix/light-restore-brightness
Open

Fix light: restore last brightness on turn-on instead of staying dark#9
danielbrowne wants to merge 1 commit into
jcgoette:mainfrom
danielbrowne:fix/light-restore-brightness

Conversation

@danielbrowne

Copy link
Copy Markdown

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. But async_turn_on only calls set_brightness/set_color when an explicit value is passed in:

if brightness:
    await self._hatch_rest_device.set_brightness(brightness)
if rgb:
    await self._hatch_rest_device.set_color(*rgb)

A bare turn_on() (no attributes) therefore powers the device on but leaves brightness at 0, so is_on stays False and the light appears stuck off. The power switch only toggles device power (SI), not brightness, so it can't rescue it.

Fix

  • Remember the brightness in async_turn_off before zeroing it.
  • In 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_brightness
  • test_async_turn_on_restores_last_brightness
  • test_async_turn_on_defaults_full_when_unknown
  • test_async_turn_on_no_restore_when_already_lit (doesn't clobber current brightness)
  • test_turn_off_then_on_round_trips_brightness

All existing light tests remain valid (the restore path only triggers when current brightness is 0).

Notes

Verified against a real 1st-gen Hatch Rest.

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.
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