8000
Skip to content

[api] Add conditional compilation for Home Assistant service subscriptions - #9900

Merged
bdraco merged 2 commits into
devfrom
api_ha_services
Jul 28, 2025
Merged

[api] Add conditional compilation for Home Assistant service subscriptions#9900
bdraco merged 2 commits into
devfrom
api_ha_services

Conversation

@bdraco
@bdraco bdraco commented Jul 26, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR adds conditional compilation for Home Assistant service subscriptions in the API component, similar to #9898 which did the same for Home Assistant state subscriptions.

This optimization reduces binary size and memory usage for devices that don't use Home Assistant service calls (e.g., homeassistant.service, homeassistant.event, or the homeassistant number/switch components).

Key changes:

  • Adds USE_API_HOMEASSISTANT_SERVICES compilation flag
  • Service-related code is only compiled when needed
  • Automatically enabled when using:
    • homeassistant.service or homeassistant.event actions
    • homeassistant platform for number or switch components
  • Can be manually enabled via api: homeassistant_services: true

Breaking change for external components:
External components using CustomAPIDevice::call_homeassistant_service() or CustomAPIDevice::fire_homeassistant_event() will need to either:

  1. Add cg.add_define("USE_API_HOMEASSISTANT_SERVICES") in their Python code
  2. Have users manually enable it in their YAML config

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-docs with documentation (if applicable):

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# Manual enable for external components
api:
  homeassistant_services: true

# Auto-enabled by using homeassistant.service
button:
  - platform: template
    name: "Call HA Service"
    on_press:
      - homeassistant.service:
          service: persistent_notification.create
          data:
            title: "ESPHome"
            message: "Hello from ESPHome!"

# Auto-enabled by using homeassistant number/switch
number:
  - platform: homeassistant
    entity_id: input_number.test
    name: "HA Number"

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@github-actions
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#9900
    components: [api, homeassistant]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Jul 26, 2025
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.09%. Comparing base (d54db47) to head (1cf5822).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #9900   +/-   ##
=======================================
  Coverage   60.09%   60.09%           
=======================================
  Files          51       51           
  Lines       10303    10303           
  Branches     1372     1372           
=======================================
  Hits         6192     6192           
  Misses       3755     3755           
  Partials      356      356           

☔ 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.

bdraco added a commit to esphome/aioesphomeapi that referenced this pull request Jul 26, 2025
@bdraco
bdraco marked this pull request as ready for review July 26, 2025 09:28
Copilot AI review requested due to automatic review settings July 26, 2025 09:28
@bdraco
bdraco requested review from a team and OttoWinter as code owners July 26, 2025 09:28
@github-actions
Copy link
Copy Markdown
Contributor

👋 Hi there! This PR modifies 15 file(s) with codeowners.

@OttoWinter, @landonr, @Links2004 - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

Copilot AI 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.

Pull Request Overview

This PR adds conditional compilation for Home Assistant service subscriptions in the API component to reduce binary size and memory usage for devices that don't use Hom 8000 e Assistant service calls. It introduces the USE_API_HOMEASSISTANT_SERVICES compilation flag that is automatically enabled when using relevant actions or platforms, or can be manually enabled via configuration.

  • Adds conditional compilation guards around Home Assistant service-related code
  • Automatically enables the feature when using homeassistant.service, homeassistant.event, or homeassistant number/switch platforms
  • Provides manual configuration option api: homeassistant_services: true

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
esphome/core/defines.h Adds the new USE_API_HOMEASSISTANT_SERVICES define
esphome/components/homeassistant/switch/init.py Auto-enables the define for homeassistant switch platform
esphome/components/homeassistant/number/init.py Auto-enables the define for homeassistant number platform
esphome/components/api/homeassistant_service.h Wraps service-related classes with conditional compilation
esphome/components/api/custom_api_device.h Guards service call methods with conditional compilation
esphome/components/api/api_server.h Guards service call method declaration
esphome/components/api/api_server.cpp Guards service call method implementation
esphome/components/api/api_pb2_service.h Guards service subscription methods and handlers
esphome/components/api/api_pb2_service.cpp Guards service subscription message handling
esphome/components/api/api_pb2_dump.cpp Guards service message dump methods
esphome/components/api/api_pb2.h Guards service-related protobuf message classes
esphome/components/api/api_pb2.cpp Guards service message encoding/calculation methods
esphome/components/api/api_connection.h Guards service call methods and subscription handling
esphome/components/api/api.proto Adds ifdef option to service-related protobuf messages
esphome/components/api/init.py Adds configuration option and auto-enables for service/event actions

@kbx81 kbx81 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks @bdraco 🍻

@bdraco
bdraco commented Jul 28, 2025
Copy link
Copy Markdown
Member Author

Thanks

@bdraco
bdraco merged commit 05f6d01 into dev Jul 28, 2025
32 checks passed
@bdraco
bdraco deleted the api_ha_services branch July 28, 2025 04:35
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

0