Skip to content

Conversation

@96liuzhixin
Copy link

What does this implement/fix?

Add support for the DFRobot C4001 motion sensor as an external component in ESPHome.
This includes a main component (c4001Component) and a binary sensor (C4001BinarySensor) for presence detection.
It allows users to configure the sensor via YAML and exposes motion detection to Home Assistant.

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):

  • esphome/esphome-docs#XXX # 对应文档 PR

Test Environment

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

Example entry for config.yaml:

uart:
  id: uart_bus  
  tx_pin: GPIO1
  rx_pin: GPIO3
  baud_rate: 9600

external_components:
  - source:
      type: git
      url: https://github.com/96liuzhixin/esphome
      ref: dev
    components:
      dfrobot_c4001

dfrobot_c4001:
  id: my_c4001
  uart_id: uart_bus

select:
  - platform: dfrobot_c4001
    operating_mode:
      name: "mode select"

number:
  - platform: dfrobot_c4001
    max_range:
      name: "Max detection distance"
    trig_range:
      name: "trigger range"
    keep_sensitivity:
      name: "keep_sensitivity"
    trig_sensitivity:
      name: "trig_sensitivity"
    confirm_delay:
      name: "confirm_delay"
    disappear_delay:
      name: "disappear_delay"
    threshold_factor:
      name: "threshold_factor"
    min_range:
      name: "Min detection distance"

switch:
  - platform: dfrobot_c4001
    motion_switch:
      name: "Motion Switch"

sensor:
  - platform: dfrobot_c4001
    c4001_id: my_c4001
    speed:
      name: "Speed"
      id: c4001_speed
    distance:
      name: "Distance"
      id: c4001_distance

binary_sensor:
  - platform: dfrobot_c4001
    exist_state:
      name: presence

@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#10675
    components: [dfrobot_c4001]
    refresh: 1h

(Added by the PR bot)

esphome[bot]
esphome bot previously requested changes Sep 11, 2025
Copy link

@esphome esphome bot 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 Size

This PR is too large with 1683 line changes (excluding tests). Please consider breaking it down into smaller, focused PRs to make review easier and reduce the risk of conflicts.

For guidance on breaking down large PRs, see: https://developers.esphome.io/contributing/submitting-your-work/#how-to-approach-large-submissions


👥 Code Ownership

Hey there @96liuzhixin,
Thanks for submitting this pull request! Can you add yourself as a codeowner for this integration? This way we can notify you if a bug report for this integration is reported.

In __init__.py of the integration, please add:

CODEOWNERS = ["@96liuzhixin"]

And run script/build_codeowners.py

@esphome
Copy link

esphome bot commented Sep 11, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@esphome esphome bot added the too-big label Sep 11, 2025
@esphome esphome bot marked this pull request as draft September 11, 2025 09:26
@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.43%. Comparing base (11d0d4d) to head (b5f08cf).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #10675      +/-   ##
==========================================
- Coverage   72.46%   72.43%   -0.03%     
==========================================
  Files          53       53              
  Lines       11152    11152              
  Branches     1510     1510              
==========================================
- Hits         8081     8078       -3     
- Misses       2676     2678       +2     
- Partials      395      396       +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.

@esphome esphome bot added has-tests and removed needs-tests labels Sep 12, 2025
@96liuzhixin 96liuzhixin marked this pull request as ready for review September 12, 2025 02:51
@esphome esphome bot dismissed their stale review September 12, 2025 02:51

Stale

esphome[bot]
esphome bot previously requested changes Sep 12, 2025
Copy link

@esphome esphome bot 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 Size

This PR is too large with 1589 line changes (excluding tests). Please consider breaking it down into smaller, focused PRs to make review easier and reduce the risk of conflicts.

For guidance on breaking down large PRs, see: https://developers.esphome.io/contributing/submitting-your-work/#how-to-approach-large-submissions

@esphome esphome bot marked this pull request as draft September 12, 2025 02:52
@96liuzhixin
Copy link
Author

I have attached the document. This is the PR link. esphome/esphome-docs#5349

@mikelawrence
Copy link
Contributor

mikelawrence commented Sep 12, 2025

I already have a DFRobot C4001 PR #9810 in progress here and it's already broken up into 4 parts to meets their 1000 line requirements and it's fairly complete including LED control. Take a look and let me know what you think.

@96liuzhixin
Copy link
Author

We are the official developers of DF and we understand the sensor’s characteristics better:

According to our testing and the official documentation, the OUT LED and RUN LED have no control functionality.

We tested their library and found that it cannot obtain the sensor’s exact configuration; in practice, the library also fails to perform this function.

Our implementation is more user-friendly for data acquisition.

Their library shows this error: “[E][component:145]: dfrobot_c4001 is marked FAILED: Too many errors.”

We recommend using our library instead, and we will continue to update and address any issues that arise in the future.
We also sincerely appreciate the library you have provided.

@mikelawrence
Copy link
Contributor

mikelawrence commented Sep 15, 2025

We are the official developers of DF and we understand the sensor’s characteristics better:

According to our testing and the official documentation, the OUT LED and RUN LED have no control functionality.

We tested their library and found that it cannot obtain the sensor’s exact configuration; in practice, the library also fails to perform this function.

Our implementation is more user-friendly for data acquisition.

Their library shows this error: “[E][component:145]: dfrobot_c4001 is marked FAILED: Too many errors.”

We recommend using our library instead, and we will continue to update and address any issues that arise in the future. We also sincerely appreciate the library you have provided.

I should have looked at your github page. It's great when manufacturers get involved and support projects like this. It's good for your business and it's good for us the users. A lot of companies can't or won't do this. Thank you very much!

I'll be eager to test when ready!

@96liuzhixin 96liuzhixin marked this pull request as ready for review October 10, 2025 05:42
@esphome esphome bot dismissed their stale review October 10, 2025 05:42

Stale

Copy link

@esphome esphome bot 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 Size

This PR is too large with 1589 line changes (excluding tests). Please consider breaking it down into smaller, focused PRs to make review easier and reduce the risk of conflicts.

For guidance on breaking down large PRs, see: https://developers.esphome.io/contributing/submitting-your-work/#how-to-approach-large-submissions

@esphome esphome bot marked this pull request as draft November 18, 2025 09:12
@github-actions
Copy link
Contributor

Memory Impact Analysis

Components: dfrobot_c4001
Platform: esp32-idf

Metric Target Branch This PR Change
RAM 12,036 bytes 12,220 bytes 📈 🚨 +184 bytes (+1.53%)
Flash 225,859 bytes 261,875 bytes 📈 🚨 +36,016 bytes (+15.95%)
📊 Component Memory Breakdown
Component Target Flash PR Flash Change
libc 51,876 bytes 58,395 bytes 📈 +6,519 bytes (+12.57%)
[esphome]dfrobot_c4001 0 bytes 5,142 bytes 📈 🔸 +5,142 bytes (0.00%)
gpio_driver 915 bytes 5,173 bytes 📈 +4,258 bytes (+465.36%)
uart_driver 8,003 bytes 11,157 bytes 📈 +3,154 bytes (+39.41%)
app_framework 397 bytes 2,425 bytes 📈 +2,028 bytes (+510.83%)
[esphome]uart 0 bytes 1,727 bytes 📈 🔸 +1,727 bytes (0.00%)
[esphome]core 6,458 bytes 7,440 bytes 📈 🚨 +982 bytes (+15.21%)
rtc 6,241 bytes 7,168 bytes 📈 +927 bytes (+14.85%)
cpp_runtime 1,556 bytes 2,430 bytes 📈 +874 bytes (+56.17%)
[esphome]select 0 bytes 629 bytes 📈 🔸 +629 bytes (0.00%)
[esphome]esp32 1,467 bytes 2,058 bytes 📈 🚨 +591 bytes (+40.29%)
[esphome]sensor 0 bytes 389 bytes 📈 🔸 +389 bytes (0.00%)
[esphome]switch 0 bytes 200 bytes 📈 🔸 +200 bytes (0.00%)
math_lib 1,409 bytes 1,579 bytes 📈 +170 bytes (+12.07%)
[esphome]number 0 bytes 164 bytes 📈 🔸 +164 bytes (0.00%)
crypto_math 1,458 bytes 1,616 bytes 📈 +158 bytes (+10.84%)
other 1,636 bytes 1,775 bytes 📈 +139 bytes (+8.50%)
memory_alloc 255 bytes 383 bytes 📈 +128 bytes (+50.20%)
[esphome]binary_sensor 0 bytes 124 bytes 📈 🔸 +124 bytes (0.00%)
freertos 14,415 bytes 14,492 bytes 📈 +77 bytes (+0.53%)
... ... ... (4 more components not shown)
🔍 Symbol-Level Changes (click to expand)

Changed Symbols

Symbol Target Size PR Size Change
setup() 222 bytes 2,250 bytes 📈 +2,028 bytes (+913.51%)
esphome::App 176 bytes 268 bytes 📈 +92 bytes (+52.27%)
[global constructor for: esphome::Application::register_component_(esphome::Component*)] 148 bytes 194 bytes 📈 +46 bytes (+31.08%)
__func__$13 13 bytes 24 bytes 📈 +11 bytes (+84.62%)
__func__$21 20 bytes 30 bytes 📈 +10 bytes (+50.00%)
__func__$28 22 bytes 18 bytes 📉 -4 bytes (-18.18%)
__func__$9 22 bytes 26 bytes 📈 +4 bytes (+18.18%)
__func__$11 22 bytes 25 bytes 📈 +3 bytes (+13.64%)
__func__$27 23 bytes 25 bytes 📈 +2 bytes (+8.70%)
__FUNCTION__$25 18 bytes 16 bytes 📉 -2 bytes (-11.11%)
__FUNCTION__$14 18 bytes 20 bytes 📈 +2 bytes (+11.11%)

New Symbols (top 15)

Symbol Size
_strtod_l 3,278 bytes
__gethex 1,093 bytes
uart_set_pin 739 bytes
esphome::dfrobot_c4001::C4001Component::update_config_param() 534 bytes
uart_wait_tx_done 414 bytes
uart_hal_set_mode 403 bytes
esphome::uart::IDFUARTComponent::load_settings(bool) 402 bytes
esphome::dfrobot_c4001::parse_dfdmd(std::__cxx11::basic_string<char, std::char_traits, std:...esphome::dfrobot_c4001::parse_dfdmd(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)
365 bytes
__hexnan 347 bytes
gpio_config 311 bytes
esphome::dfrobot_c4001::C4001Component::get_data() 307 bytes
esphome::dfrobot_c4001::extract_two_numbers_after_keyword(std::__cxx11::basic_string<char, std::c...esphome::dfrobot_c4001::extract_two_numbers_after_keyword(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, float*, float*)
295 bytes
esphome::select::Select::publish_state(unsigned int) 275 bytes
uart_read_bytes 267 bytes
esphome::dfrobot_c4001::parse_dfhpd(std::__cxx11::basic_string<char, std::char_traits, std:...esphome::dfrobot_c4001::parse_dfhpd(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)
255 bytes
300 more new symbols... Total: 25,860 bytes

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants