Skip to content

[pulse_counter] Fix build failure when use_pcnt is false#14111

Merged
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-pulse-counter-pcnt-include
Feb 19, 2026
Merged

[pulse_counter] Fix build failure when use_pcnt is false#14111
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-pulse-counter-pcnt-include

Conversation

@swoboda1337
Copy link
Member

What does this implement/fix?

Fix a compilation error when using pulse_counter with use_pcnt: false on ESP32 with ESP-IDF framework:

fatal error: driver/pulse_cnt.h: No such file or directory

Root cause: The header pulse_counter_sensor.h unconditionally includes <driver/pulse_cnt.h> whenever SOC_PCNT_SUPPORTED is defined (a hardware capability check from soc/soc_caps.h). But when use_pcnt: false, the Python codegen doesn't call include_builtin_idf_component("esp_driver_pcnt"), and this IDF component is excluded by default — so the header file doesn't exist in the build.

Fix: Add a __has_include(<driver/pulse_cnt.h>) guard so the include only happens when the PCNT driver is actually available. When it's not, HAS_PCNT stays undefined and BasicPulseCounterStorage (software pulse counting) is used instead.

This also fixes the same latent issue for hlw8012, which includes pulse_counter_sensor.h.

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)
  • Developer breaking change (an API change that could break external components)
  • 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):

  • N/A

Test Environment

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

Example entry for config.yaml:

# This config now compiles correctly on ESP-IDF:
sensor:
  - platform: pulse_counter
    use_pcnt: false
    pin: 17
    name: "power"
    update_interval: 180s

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:

Copilot AI review requested due to automatic review settings February 19, 2026 17:04
@github-actions
Copy link
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#14111
    components: [pulse_counter]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.08%. Comparing base (5304750) to head (d0468d6).
⚠️ Report is 8 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #14111      +/-   ##
==========================================
- Coverage   74.11%   74.08%   -0.03%     
==========================================
  Files          55       55              
  Lines       11590    11590              
  Branches     1578     1578              
==========================================
- Hits         8590     8587       -3     
- Misses       2598     2600       +2     
- Partials      402      403       +1     

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

@swoboda1337 swoboda1337 marked this pull request as ready for review February 19, 2026 17:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an ESP32 ESP-IDF build failure in pulse_counter when use_pcnt: false by preventing an unconditional include of the PCNT driver header when that header isn’t available in the build.

Changes:

  • Gate inclusion of <driver/pulse_cnt.h> behind __has_include(...) so HAS_PCNT is only defined when the header is actually present.
  • Ensure the software pulse counting storage path is used when the PCNT driver isn’t available.

@bdraco
Copy link
Member

bdraco commented Feb 19, 2026

Maybe an esp32 test with use_pcnt: false?

@swoboda1337 swoboda1337 force-pushed the fix-pulse-counter-pcnt-include branch from 66296b8 to 1cb0954 Compare February 19, 2026 17:09
@esphome esphome bot added the has-tests label Feb 19, 2026
The pulse_counter header unconditionally includes <driver/pulse_cnt.h>
when SOC_PCNT_SUPPORTED is defined by the hardware capabilities. But
when use_pcnt: false, the esp_driver_pcnt IDF component is not included
in the build (it's excluded by default), so the header is missing.

Add a __has_include guard so the header is only included when the PCNT
driver is actually available. This makes HAS_PCNT correctly undefined
when use_pcnt: false, falling back to BasicPulseCounterStorage.

Fixes esphome#14110

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@swoboda1337 swoboda1337 force-pushed the fix-pulse-counter-pcnt-include branch from 1cb0954 to d0468d6 Compare February 19, 2026 17:10
@swoboda1337
Copy link
Member Author

Maybe an esp32 test with use_pcnt: false?

Was already adding it :)

@swoboda1337
Copy link
Member Author

Thanks

@swoboda1337 swoboda1337 enabled auto-merge (squash) February 19, 2026 17:15
@swoboda1337 swoboda1337 merged commit 9aa1798 into esphome:dev Feb 19, 2026
27 checks passed
swoboda1337 added a commit that referenced this pull request Feb 20, 2026
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@swoboda1337 swoboda1337 mentioned this pull request Feb 20, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 21, 2026
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.

can't compile: driver/pulse_cnt.h: No such file or directory

4 participants