Skip to content

Add deep-sleep functionality with button wake support - #109

Merged
JanPetterMG merged 13 commits into
mainfrom
feat/deep-sleep
Sep 22, 2026
Merged

JanPetterMG merged 13 commits into
mainfrom
feat/deep-sleep

Conversation

@JanPetterMG

Copy link
Copy Markdown
Contributor

Implement deep-sleep mode that allows waking the device using buttons. This includes enhancements to the button handler and integration with the Home Assistant for power control. The DeskService now supports a power action to initiate deep sleep.

@JanPetterMG JanPetterMG added the enhancement New feature or request label Sep 21, 2026
@JanPetterMG JanPetterMG added this to the v1.0.0 milestone Sep 21, 2026
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: e64c72c2-3906-4ed0-94d3-3a96129ced4e

📥 Commits

Reviewing files that changed from the base of the PR and between ebd3407 and 0a4fbff.

📒 Files selected for processing (2)
  • src/esp/ButtonHandler.cpp
  • src/esp/DeskService.cpp
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/esp/DeskService.cpp
  • src/esp/ButtonHandler.cpp

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


Summary by CodeRabbit

  • New Features

    • Added a Home Assistant “Power off” control, disabled by default.
    • Activating the control disconnects the device and puts it into deep sleep.
    • Supported button inputs can wake the device from deep sleep.
    • The device maintains its output-enabled state while sleeping.
  • Documentation

    • Documented the “Power off” control in the Home Assistant configuration table.

Walkthrough

The ESP32 now exposes a disabled-by-default Home Assistant “Power off” button. The command enables GPIO holds and enters deep sleep. DeskService releases holds during startup, and ButtonHandler updates deep-sleep wake-up selection.

Changes

Power-off deep-sleep control

Layer / File(s) Summary
Power-off action path
src/esp/HomeAssistantHandler.cpp, src/esp/DeskService.cpp, README.md
Home Assistant adds a disabled-by-default “Power off” button that sends the power action. The action enables GPIO holds before entering deep sleep. The README documents the control.
Deep-sleep output retention
src/esp/DeskService.cpp
DeskService releases the output-enable GPIO and deep-sleep holds before opening NVS and reading the persisted state.
Simulation-pin wake configuration
src/esp/ButtonHandler.cpp
ButtonHandler::begin() prioritizes combined GPIO wake-up when supported and uses the ESP32 ext0/ext1 path as a fallback. The wake-up branches no longer configure pull-ups or GPIO holds.

Sequence Diagram(s)

sequenceDiagram
  participant HomeAssistantHandler
  participant CommandTopic
  participant DeskService
  participant ESP32
  HomeAssistantHandler->>CommandTopic: Publish {"action":"power"}
  CommandTopic->>DeskService: Deliver power action
  DeskService->>ESP32: Enable GPIO holds and enter deep sleep
Loading

Priority: ⬇️ Low

Change: Feature

Merge Risk: 🟡 Moderate · up to 0a4fb

After waking from deep sleep, the device may report an output state that does not match the physical output. This should be resolved before merging.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary changes: deep-sleep functionality and button-based wake support.
Description check ✅ Passed The description accurately covers the button handler changes, Home Assistant power control, and DeskService power action.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
✨ Simplify code
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit taps the power command,
Deep sleep follows as planned.
GPIO holds keep outputs tight,
Wake pins wait through the night.
NVS starts with holds released,
The desk rests, its state preserved.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 97b8a528-74cf-4246-9bf0-0122a001d3b8

📥 Commits

Reviewing files that changed from the base of the PR and between eed71d5 and e14046f.

📒 Files selected for processing (4)
  • README.md
  • src/esp/ButtonHandler.cpp
  • src/esp/DeskService.cpp
  • src/esp/HomeAssistantHandler.cpp

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

📜 Review details
🔇 Additional comments (3)
src/esp/HomeAssistantHandler.cpp (1)

283-294: LGTM!

src/esp/DeskService.cpp (1)

367-372: LGTM!

README.md (1)

255-255: LGTM!

Comment thread src/esp/ButtonHandler.cpp Outdated
The AVR holds the buttons in HIGH state, so there's no need for pull resistors during deep sleep, except for the logic level shifter (`OE`).

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 766436a6-3240-42a9-b272-f34e70a2c0be

📥 Commits

Reviewing files that changed from the base of the PR and between e14046f and bc7d11a.

📒 Files selected for processing (2)
  • src/esp/ButtonHandler.cpp
  • src/esp/DeskService.cpp

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

📜 Review details
🔇 Additional comments (1)
src/esp/ButtonHandler.cpp (1)

16-23: LGTM!

Comment thread src/esp/DeskService.cpp Outdated
@JanPetterMG

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Release the GPIO holds before restoring PIN_OE. · DeskService.cpp:40

src/esp/DeskService.cpp:40
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Release the GPIO holds before restoring PIN_OE.

After deep-sleep wake, gpio_hold_en() can still lock PIN_OE. On supported targets, gpio_deep_sleep_hold_en() can keep that hold active. Therefore, the digitalWrite() at Line 40 can be ignored while enable is updated from NVS. This can leave software state different from the physical OE level.

Disable both holds before the write, using the same guard and sequence as setOutputEnable().

Proposed fix
         if (nvs_get_u8(handle, "oe", &_enable) == ESP_OK)
         {
             enable = static_cast<bool>(_enable);
+#if SOC_GPIO_SUPPORT_HOLD_IO_IN_DSLP && !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
+            gpio_deep_sleep_hold_dis();
+#endif
+            gpio_hold_dis(static_cast<gpio_num_t>(PIN_OE));
             digitalWrite(PIN_OE, enable ? HIGH : LOW);
         }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: e417c66c-9b80-47b8-815f-2362a24262af

📥 Commits

Reviewing files that changed from the base of the PR and between 8119b50 and d484878.

📒 Files selected for processing (1)
  • src/esp/DeskService.cpp

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

📜 Review details
🔇 Additional comments (1)
src/esp/DeskService.cpp (1)

371-376: LGTM!

Also applies to: 437-445

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 7324734f-e21a-4be9-a39f-637f99dfcdc0

📥 Commits

Reviewing files that changed from the base of the PR and between d484878 and d8f94e2.

📒 Files selected for processing (1)
  • src/esp/DeskService.cpp

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.

Comment thread src/esp/DeskService.cpp Outdated
@coderabbitai

coderabbitai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

🤖 Completed: Generate docstrings for PR #109View PR #113

@JanPetterMG
JanPetterMG marked this pull request as ready for review September 22, 2026 12:16
Copilot AI lite review requested due to automatic review settings September 22, 2026 12:16
@chatgpt-codex-connector

This comment was marked as off-topic.

This comment was marked as off-topic.

@JanPetterMG
JanPetterMG added this pull request to the merge queue Sep 22, 2026
Merged via the queue into main with commit e583304 Sep 22, 2026
23 checks passed
@JanPetterMG
JanPetterMG deleted the feat/deep-sleep branch September 22, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request esp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants