Skip to content

Reduce connection latency by proactively sending time response during handshake #1314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Aug 12, 2025

What does this implement/fix?

This PR optimizes the connection process by proactively sending a GetTimeResponse during the initial handshake, eliminating an unnecessary round-trip that was slowing down the first command after reconnection.

Previously, the connection flow included:

  1. Send Hello/Connect request
  2. Receive Hello/Connect response
  3. Receive GetTimeRequest from device
  4. Send GetTimeResponse back
  5. Connection ready for commands

Now with this optimization:

  1. Send Hello/Connect request + GetTimeResponse (in same packet)
  2. Receive Hello/Connect response
  3. Device receives time proactively, skips GetTimeRequest
  4. Connection ready for commands (faster!)

This reduces connection latency by ~0.81 seconds as noted in the issue, which is particularly beneficial during reconnects when Home Assistant needs to quickly restore control of devices.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome (if applicable):

  • Not applicable - no api.proto changes required

Checklist:

  • The code change is tested and works locally.
  • If api.proto was modified, a linked pull request has been made to esphome with the same changes.
  • Tests have been added to verify that the new code works (under tests/ folder).

Additional Notes

  • Devices without Home Assistant time enabled will simply ignore the proactive GetTimeResponse, making this optimization safe for all deployments
  • The time response is sent in the same packet as other handshake messages, so there's virtually no overhead
  • The implementation tracks whether we've sent the initial time response to avoid duplicate responses
  • An additional benefit is that sending the time response during initial handshake (when the connection isn't under pressure) means the device receives more accurate time, as there's no risk of delays that could occur if we waited for the device to request it during a busy reconnection period

Copy link

codecov bot commented Aug 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (70ceaf4) to head (56df9d6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1314   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           20        20           
  Lines         3109      3117    +8     
=========================================
+ Hits          3109      3117    +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

codspeed-hq bot commented Aug 12, 2025

CodSpeed Performance Report

Merging #1314 will not alter performance

Comparing time_opt (56df9d6) with main (70ceaf4)1

Summary

✅ 11 untouched benchmarks

Footnotes

  1. No successful run was found on main (8a4c81c) during the generation of this report, so 70ceaf4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@bdraco
Copy link
Member Author

bdraco commented Aug 12, 2025

maybe better to include it in the hello... need to think about it

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.

Time Request during reconnect slows down first command
1 participant