[cpap] Add Last Pump Action text sensor for HA logbook#33
Conversation
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
WalkthroughAdds a new public text sensor Changes
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")
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 | 🟠 MajorGuard
pump_stoppedevent emission to avoid false logbook entries from rejected starts.ESPHome executes
on_turn_offwhenever the switch is turned off. The rejected-start branch at Line 185 callsswitch.turn_off: pump_control, which triggerson_turn_offand unconditionally emitspump_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
📒 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.
|
merging to beta - confirmed working! |
Version: 26.3.2.1
What does this implement/fix?
Replaces the incorrect
homeassistant.actionlogbook 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_stoppedemission is guarded by checkingpump_start_time != 0so 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
Checklist / Checklijst:
If user-visible functionality or configuration variables are added/modified: