Skip to content

Add a on_hk_start trigger to measure hk latency#33

Open
nuance wants to merge 1 commit into
rednblkx:mainfrom
nuance:on_hk_start
Open

Add a on_hk_start trigger to measure hk latency#33
nuance wants to merge 1 commit into
rednblkx:mainfrom
nuance:on_hk_start

Conversation

@nuance
Copy link
Copy Markdown

@nuance nuance commented Mar 20, 2025

I was tracking down some latency in a lock I built with this project and found it helpful to add a trigger before the homekit handshake. I imagine this could be generally useful if someone wanted to flash a light or provide some other feedback that the nfc read had completed.

Summary by CodeRabbit

  • New Features
    • Introduced a new automation trigger that activates when HomeKit interactions begin, allowing users to seamlessly integrate start events into their automation workflows.
    • Enhanced event handling for HomeKit, providing improved responsiveness and flexibility in configuring automation actions.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 20, 2025

Walkthrough

The changes extend the HomeKit integration by adding a new trigger for automation events. A new Python trigger class (OnHkStartTrigger) is introduced with corresponding configuration schema updates. In the C++ code, a new HKStartTrigger class with a process() method is added within the HomeKit namespace. The LockEntity class is also modified by adding a member vector to store these triggers and a registration method (register_onhkstart_trigger). This new mechanism allows HomeKit events to trigger custom automation processes through a consistent and integrated framework.

Changes

File(s) Change Summary
components/homekit/__init__.py Introduces OnHkStartTrigger as a subclass of automation.Trigger; updates CONFIG_SCHEMA with cv.Optional("on_hk_start"); and modifies to_code to register the trigger with lock_entity.register_onhkstart_trigger(trigger).
components/homekit/automation.h
components/homekit/automation.cpp
Adds a new class HKStartTrigger within the HomeKit namespace; implements a public method process() similar to existing triggers to handle start events in automation.
components/homekit/lock.h
components/homekit/lock.cpp
Adds a new member variable (triggers_onhk_start_) to store HKStartTrigger pointers and a new method register_onhkstart_trigger in LockEntity; enhances set_nfc_ctx to iterate over the new triggers and call their process() methods when conditions are met.

Sequence Diagram(s)

sequenceDiagram
    participant Config as Automation Config
    participant HomekitPy as HomeKit Python Layer
    participant LockEntity as LockEntity
    participant HKStartTrig as HKStartTrigger

    Config->>HomekitPy: Define OnHkStartTrigger and update CONFIG_SCHEMA
    HomekitPy->>LockEntity: Call register_onhkstart_trigger(trigger)
    LockEntity->>LockEntity: Store HKStartTrigger in triggers_onhk_start_
    Note over LockEntity: When HomeKit start event occurs
    LockEntity->>HKStartTrig: Call process() on each registered trigger
    HKStartTrig-->>LockEntity: Trigger event processed
Loading

Poem

I'm a bunny in the code,
Hopping through each trigger mode.
With a twitch of nose and leaping art,
New HomeKit flows now play their part.
Carrots of code, sweet and smart —
My hops celebrate a brand new start!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b5085d and 3cca10b.

📒 Files selected for processing (5)
  • components/homekit/__init__.py (3 hunks)
  • components/homekit/automation.cpp (1 hunks)
  • components/homekit/automation.h (1 hunks)
  • components/homekit/lock.cpp (2 hunks)
  • components/homekit/lock.h (2 hunks)
🔇 Additional comments (9)
components/homekit/automation.h (1)

19-23: LGTM! The new HKStartTrigger class is well-designed.

The new trigger class follows the same pattern as the existing trigger classes (HKAuthTrigger and HKFailTrigger), providing a consistent interface for triggering HomeKit events.

components/homekit/automation.cpp (1)

13-15: Implementation looks good.

The process() method implementation correctly follows the established pattern, simply triggering the callback with no arguments - consistent with the existing HKFailTrigger implementation.

components/homekit/lock.cpp (2)

259-261: Registration method implementation is correct.

The method correctly adds the provided trigger to the vector, consistent with the existing trigger registration methods.


290-293: Perfect placement for the trigger invocation.

The trigger is fired at the right moment - immediately after confirming HomeKit support but before starting the authentication process. This is ideal for measuring handshake latency or providing user feedback (like flashing a light) as mentioned in the PR description.

components/homekit/lock.h (2)

48-48: Member variable added correctly.

The new vector for storing HKStartTrigger pointers is placed alongside other similar trigger vectors, maintaining consistency in the class structure.


66-66: Method declaration is properly placed.

The new method declaration is correctly placed with the other trigger registration methods, maintaining a logical grouping of related functionality.

components/homekit/__init__.py (3)

31-33: LGTM: New trigger class for HomeKit handshake start

The new OnHkStartTrigger class properly follows the established pattern for HomeKit triggers, inheriting from the automation trigger template with no parameters, similar to the existing OnHkFailTrigger.


74-78: LGTM: Schema validation for the new trigger

The configuration schema for on_hk_start is correctly implemented, following the same pattern as the existing on_hk_success and on_hk_fail triggers. Good job maintaining consistency with the existing code structure.


132-135: LGTM: Implementation for processing the new trigger

The implementation for processing the on_hk_start trigger properly follows the pattern established for the other triggers. The code creates the trigger variable and registers it with the lock entity using the new register_onhkstart_trigger method.

This will enable users to run automations when a HomeKit handshake begins, which aligns well with the PR objective of measuring latency and providing feedback when an NFC read operation is detected.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rednblkx rednblkx force-pushed the main branch 6 times, most recently from 4e3bba5 to e37b956 Compare February 21, 2026 15:54
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