Skip to content

[cpap] Add Last Pump Action text sensor for HA logbook#33

Merged
bharvey88 merged 3 commits intobetafrom
fix/pump-activity-event
Mar 11, 2026
Merged

[cpap] Add Last Pump Action text sensor for HA logbook#33
bharvey88 merged 3 commits intobetafrom
fix/pump-activity-event

Conversation

@bharvey88
Copy link
Contributor

@bharvey88 bharvey88 commented Mar 10, 2026

Version: 26.3.2.1

What does this implement/fix?

Replaces the incorrect homeassistant.action logbook approach from #32 with a Last Pump Action template text sensor whose state changes appear verbatim in the HA logbook — no API actions required on the device.

The text sensor state is updated at three points:

  • "Auto Refill Triggered" — CPAP auto refill + invert water logic triggers the pump via input sensor going dry
  • "Pump Started" — pump_control turns on and conditions are met
  • "Pump Stopped" — pump_control turns off after a successful start (guarded: not emitted on rejected starts)

The pump_stopped emission is guarded by checking pump_start_time != 0 so that false logbook entries are not created when a start is rejected due to water conditions not being met.

Fixes the incorrect implementation in #32.

Types of changes

  • Bugfix (fixed change that fixes an issue)
  • New feature (thanks!)
  • Breaking change (repair/feature that breaks existing functionality)
  • Dependency Update - Does not publish
  • Other - Does not publish
  • Website of github readme file update - Does not publish
  • Github workflows - Does not publish

Checklist / Checklijst:

  • The code change has been tested and works locally
  • The code change has not yet been tested

If user-visible functionality or configuration variables are added/modified:

  • Added/updated documentation for the web page

Replaces the homeassistant.action logbook.log approach from #32 with an
ESPHome event entity, which surfaces natively in the HA logbook without
requiring API actions to be enabled on the device.

Fires three event types:
- auto_refill_triggered: CPAP auto refill + invert water logic triggers pump
- pump_started: pump_control turns on and conditions are met
- pump_stopped: pump_control turns off
@coderabbitai
Copy link

coderabbitai bot commented Mar 10, 2026

Walkthrough

Adds a new public text sensor last_pump_action and updates pump start, pump stop, and auto-refill flows to publish human-readable states ("Pump Started", "Pump Stopped", "Auto Refill Triggered") to that sensor in Integrations/ESPHome/Core.yaml.

Changes

Cohort / File(s) Summary
ESPHome Core
Integrations/ESPHome/Core.yaml
Added a template text_sensor last_pump_action (name "Last Pump Action", icon mdi:pump). On pump start, publish "Pump Started"; on pump stop, conditionally publish "Pump Stopped" if pump_start_time was non-zero; on fluid_input_sensor on_release (auto-refill), publish "Auto Refill Triggered" and continue pumpUntilFull.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant PumpLogic as Pump Logic (ESPHome)
    participant TextSensor as last_pump_action (ESPHome entity)
    participant HA as Home Assistant / Consumers

    PumpLogic->>TextSensor: publish "Pump Started"
    TextSensor-->>HA: state updated ("Pump Started")
    PumpLogic->>TextSensor: publish "Pump Stopped" (if pump_start_time != 0)
    TextSensor-->>HA: state updated ("Pump Stopped")
    PumpLogic->>TextSensor: publish "Auto Refill Triggered" (on fluid_input_sensor on_release)
    TextSensor-->>HA: state updated ("Auto Refill Triggered")
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • TrevorSchirmer

Poem

🐰
I hop where the soft pump messages sing,
"Started", "Stopped", and "Auto Refill" they bring.
A tick of a sensor, a bright little tale,
I nibble the bits as the signals set sail. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding a Last Pump Action text sensor to the ESPHome Core configuration for Home Assistant logbook integration.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/pump-activity-event

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

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

Copy link

@coderabbitai coderabbitai bot left a comment

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

⚠️ Outside diff range comments (1)
Integrations/ESPHome/Core.yaml (1)

186-194: ⚠️ Potential issue | 🟠 Major

Guard pump_stopped event emission to avoid false logbook entries from rejected starts.

ESPHome executes on_turn_off whenever the switch is turned off. The rejected-start branch at Line 185 calls switch.turn_off: pump_control, which triggers on_turn_off and unconditionally emits pump_stopped. This creates false pump activity entries in Home Assistant logbook for failed start attempts.

Guard the event emission by checking pump_start_time != 0, which is only set during successful starts:

Proposed fix
     on_turn_off:
       then:
-        - lambda: |-
-            id(pump_start_time) = 0;
-            id(safety_alert_active) = false;
-        - logger.log: "Pump turned off"
-        - event.trigger:
-            id: pump_activity_event
-            event_type: pump_stopped
+        - if:
+            condition:
+              lambda: 'return id(pump_start_time) != 0;'
+            then:
+              - logger.log: "Pump turned off"
+              - event.trigger:
+                  id: pump_activity_event
+                  event_type: pump_stopped
+        - lambda: |-
+            id(pump_start_time) = 0;
+            id(safety_alert_active) = false;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Integrations/ESPHome/Core.yaml` around lines 186 - 194, The on_turn_off
handler unconditionally emits the pump_stopped event even when a start was
rejected (switch.turn_off: pump_control), causing false logbook entries; update
the on_turn_off sequence to guard emission by checking id(pump_start_time) != 0
before triggering id: pump_activity_event with event_type: pump_stopped and only
reset id(pump_start_time) and id(safety_alert_active) after this check so that
pump_stopped is only logged for successful starts.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@Integrations/ESPHome/Core.yaml`:
- Around line 186-194: The on_turn_off handler unconditionally emits the
pump_stopped event even when a start was rejected (switch.turn_off:
pump_control), causing false logbook entries; update the on_turn_off sequence to
guard emission by checking id(pump_start_time) != 0 before triggering id:
pump_activity_event with event_type: pump_stopped and only reset
id(pump_start_time) and id(safety_alert_active) after this check so that
pump_stopped is only logged for successful starts.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8d89eed4-647a-42b7-86a0-e5680f7494ab

📥 Commits

Reviewing files that changed from the base of the PR and between bd58d14 and ab51496.

📒 Files selected for processing (1)
  • Integrations/ESPHome/Core.yaml

pump_start_time is only set when the pump actually starts successfully.
Checking != 0 before logging and emitting pump_stopped prevents false
logbook entries when a start is rejected and switch.turn_off is called
from the else branch of on_turn_on.
…tries

ESPHome event entities show only a generic 'detected an event' string in
the HA logbook. Replaces the Pump Activity event entity with a Last Pump
Action text sensor whose state changes (Auto Refill Triggered, Pump
Started, Pump Stopped) appear verbatim in the HA logbook.
@bharvey88 bharvey88 changed the title [cpap] Add Pump Activity event entity for HA logbook [cpap] Add Last Pump Action text sensor for HA logbook Mar 10, 2026
@bharvey88
Copy link
Contributor Author

merging to beta - confirmed working!

@bharvey88 bharvey88 merged commit 0cabc9c into beta Mar 11, 2026
8 checks passed
@bharvey88 bharvey88 deleted the fix/pump-activity-event branch March 11, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants