Skip to content

Commit 3d147df

Browse files
authored
Match current PR in esphome (#4)
* Lots of cleanup PR submitted to ESPHome. These are the first round of requested changes. Lots of logging changes move to static inline functions model is now required. This allowed runtime validation to be removed in favor of compile time validation. make actions consistent. * pylint suggestion, move to mode in {set} * Use static_cast instead * Fix model logic error * Test by defining enum with type * Revert "Test by defining enum with type" This reverts commit e683819. * Keep the 5% increase but add 1ms * Match current PR in ESPHome * Remove BMP581 and TSL2591 components * Match ESPHome PR * Correcting id * Still trying * Update binary_sensor.py * Fix syntax error * More syntax errors * Formatting * CLANG formatting cleanup * Correct mode values * Use std::move in version setter methods * Addin Restart and Factory Reset buttons and version text_sensors * Fix errors * Update dfrobot_c4001.h * Fix more errors * Fix missing reset button * Fix hardware_version config variable * Try again * Increase command queue depth and logging cleanup * Add Restart Action * Fix check string after reset command * Change config_load() for Factory Reset and Restart commands * Support Blue LED control * Update tfmini.cpp * Update dfrobot_c4001.cpp * Use const reference for version setters Updated set_software_version and set_hardware_version to take const std::string& instead of std::string, improving efficiency by avoiding unnecessary string copies. * Fix clang-format formatting error * Fix compile error when using any text_sensor Remove orphaned motion binary_sensor Correct reported occupancy when VERBOSE logging is enabled Remove state is changed logic around sensor and binary_sensor reporting. Makes initial updates quicker Corrected LED state not written to flash * README changes due to new buttons and actions in dfrobot_c4001 component * Fix syntax errors * Remove debug code * Add notes about ESPHome PRs
1 parent 81541be commit 3d147df

File tree

23 files changed

+2055
-2363
lines changed

23 files changed

+2055
-2363
lines changed

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,15 @@ Using the STUSB4500 ESPHome component requires the following steps:
188188
[00:00:19][C][stusb4500:116]: NVM matches settings
189189
[00:00:19][C][stusb4500:130]: PDO3 negotiated 12.00V @ 3.00A, 36.00W
190190
```
191-
4. The STUSB4500 comonent does check that the NVM is indeed different before flashing it but it is prudent to remove the ```flash_nvm: true``` after it is clear the STUSB4500 is working as configured.
191+
4. The STUSB4500 component does check that the NVM is indeed different before flashing it but it is prudent to remove the ```flash_nvm: true``` after it is clear the STUSB4500 is working as configured.
192192

193193

194194
## ESPHome C4001 External Component
195195

196+
> [!NOTE]
197+
> This component is in the process of being added to ESPHome. ESPHome [PR#9327](https://github.com/esphome/esphome/pull/9327).
198+
199+
196200
The DFRobot C4001 (SEN0609 or SEN0610) is a millimeter-wave presence detector. The C4001 millimeter-wave presence sensor has the advantage of being able to detect both static and moving objects. It also has a relatively strong anti-interference ability, making it less susceptible to factors such as temperature changes, variations in ambient light, and environmental noise. Whether a person is sitting, sleeping, or in motion, the sensor can quickly detect their presence.
197201

198202
<p align="center">
@@ -267,16 +271,26 @@ switch:
267271
led_enable:
268272
name: Enable LED
269273

274+
text_sensor:
275+
- platform: dfrobot_c4001
276+
dfrobot_c4001_id: mmwave_sensor
277+
software_version:
278+
name: Software Version
279+
hardware_version:
280+
name: Hardware Version
281+
270282
```
271283

272284
### Configuration Variables
273285
+ **mode** (*Required*, enumeration): This sets the operation mode of the sensor. Options are ```PRESENCE``` and ```SPEED_AND_DISTANCE```.
274286

275287
### Buttons
276288
+ **config_save** (*Optional*): When you click this button the current configuration will be saved. Keep in mind that these are writes to flash and there is a limited number of time you can do this before the flash wears out. All Options from [Button Component](https://esphome.io/components/button/index.html#base-button-configuration).
289+
+ **factory_reset** (*Optional*): Clicking this button will perform a factory reset of the module and all configuration values will go back to default. All Options from [Button Component](https://esphome.io/components/button/index.html#base-button-configuration).
290+
+ **restart** (*Optional*): When you click this button the module will restart and all configuration values will remains as previously set. All Options from [Button Component](https://esphome.io/components/button/index.html#base-button-configuration).
277291

278292
### Binary Sensors
279-
+ **config_changed** (*Optional*): When ```true`` the current sensor configuration has been changed but not saved to the sensor. All Options from [Binary Sensor Component](https://esphome.io/components/binary_sensor/#base-binary-sensor-configuration).
293+
+ **config_changed** (*Optional*): When ```true``` the current sensor configuration has been changed but not saved to the sensor. All Options from [Binary Sensor Component](https://esphome.io/components/binary_sensor/#base-binary-sensor-configuration).
280294
+ **occupancy** (*Optional*): In ```PRESENCE``` mode this indicates presence. In ```SPEED_AND_DISTANCE``` mode this indicates a target is being tracked. All Options from [Binary Sensor Component](https://esphome.io/components/binary_sensor/#base-binary-sensor-configuration).
281295

282296
### Numbers
@@ -299,10 +313,15 @@ switch:
299313
+ **led_enable** (*Optional*): When turned on the green LED will flash when the sensor has been started. The blue LED cannot be disabled with this command. All Options from [Switch Component](https://esphome.io/components/switch/index.html#base-switch-configuration).
300314
+ **micro_motion_enable** (*Optional*): Turns on micro motion mode. Available only in ```SPEED_AND_DISTANCE``` mode. All Options from [Switch Component](https://esphome.io/components/switch/index.html#base-switch-configuration).
301315

316+
### Sensors
317+
+ **target_distance** (*Optional*): When **occupancy** binary sensor is ```true``` this sensor indicates distance to target in meters (m). When **occupancy** binary sensor is ```false``` this sensor switches to 0.0 indicating invalid data. Available only in ```SPEED_AND_DISTANCE``` mode. All Options from [Sensor Component](https://esphome.io/components/sensor/index.html#base-sensor-configuration).
318+
+ **target_speed** (*Optional*): When **occupancy** binary sensor is ```true``` this sensor indicates target speed in meters per second (m/s). When **occupancy** binary sensor is ```false``` this sensor switches to 0.0 indicating invalid data. Available only in ```SPEED_AND_DISTANCE``` mode. All Options from [Sensor Component](https://esphome.io/components/sensor/index.html#base-sensor-configuration).
319+
302320
### Actions
303-
+ **dfrobot_c4001.factor_reset** Will perform a factory reset of the module and all configuration values will go back to default. The module will restart with these defaults. Keep in mind that these are writes to flash and there is a limited number of time you can do this before the flash wears out. This is much easier to do with a lambda that accidentally performs a factory reset every second.
321+
+ **dfrobot_c4001.factory_reset** Will perform a factory reset of the module and all configuration values will go back to default. The module will restart with these defaults. Keep in mind that these are writes to flash and there is a limited number of time you can do this before the flash wears out. This is much easier to do with a lambda that accidentally performs a factory reset every second.
304322

305323
Example using automations...
324+
306325
```yaml
307326
button:
308327
- platform: template
@@ -318,6 +337,25 @@ Example in lambdas...
318337
id(mmwave_sensor).factory_reset();
319338
```
320339

340+
+ **dfrobot_c4001.restart** Will restart the module and all configuration values will remains as previously set.
341+
342+
Example using automations...
343+
344+
```yaml
345+
button:
346+
- platform: template
347+
name: Restart
348+
on_press:
349+
- dfrobot_c4001.restart: mmwave_sensor
350+
entity_category: CONFIG
351+
352+
```
353+
Example in lambdas...
354+
```
355+
- lambda: |-
356+
id(mmwave_sensor).restart();
357+
```
358+
321359
## ESPHome LD2450 External Component
322360

323361
This custom component adds some functionality to the built-in LD2450 Component. Only the differences are listed below.
@@ -349,6 +387,11 @@ number:
349387

350388
## SEN5X External Component
351389

390+
391+
> [!NOTE]
392+
> A Pull Request to change the sen5x component in ESPHome is in progress. ESPHome [PR#9254](https://github.com/esphome/esphome/pull/9254).
393+
394+
352395
This external component adds SEN60, SEN63C, SEN65, SEN66 and SEN68 support to the built-in sen5x component. This component extends [PR #8318](https://github.com/esphome/esphome/pull/8318). Only the differences from sen5x component are listed below.
353396

354397
Temperature compensation is not working for the SEN6x models. Still waiting on the Sensirion Application Note.

components/dfrobot_c4001/__init__.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
import esphome.codegen as cg
2-
import esphome.config_validation as cv
31
from esphome import automation
42
from esphome.automation import maybe_simple_id
3+
import esphome.codegen as cg
54
from esphome.components import uart
6-
7-
from esphome.const import (
8-
CONF_FACTORY_RESET,
9-
CONF_ID,
10-
CONF_MODE,
11-
CONF_SENSITIVITY,
12-
)
13-
5+
import esphome.config_validation as cv
6+
from esphome.const import CONF_ID, CONF_MODE
147

158
DEPENDENCIES = ["uart"]
169

1710
CODEOWNERS = ["@mikelawrence"]
1811
MULTI_CONF = True
1912

20-
CONF_DFROBOT_C4001_HUB_ID = "dfrobot_c4001_id"
13+
CONF_DFROBOT_C4001_ID = "dfrobot_c4001_id"
2114
MODE = ""
2215

2316
dfrobot_c4001_ns = cg.esphome_ns.namespace("dfrobot_c4001")
24-
DFRobotC4001Hub = dfrobot_c4001_ns.class_("DFRobotC4001Hub", cg.Component, uart.UARTDevice)
17+
DFRobotC4001Hub = dfrobot_c4001_ns.class_(
18+
"DFRobotC4001Hub", cg.Component, uart.UARTDevice
19+
)
2520
ModeConfig = dfrobot_c4001_ns.enum("ModeConfig")
2621

2722
CONF_MODE_SELECTS = {
28-
"PRESENCE": ModeConfig.Mode_Presence,
29-
"SPEED_AND_DISTANCE": ModeConfig.Mode_Speed_and_Distance,
23+
"PRESENCE": ModeConfig.MODE_PRESENCE,
24+
"SPEED_AND_DISTANCE": ModeConfig.MODE_SPEED_AND_DISTANCE,
3025
}
3126

3227

@@ -35,10 +30,13 @@
3530
"DFRobotC4001FactoryResetAction", automation.Action
3631
)
3732

33+
DFRobotC4001RestartAction = dfrobot_c4001_ns.class_(
34+
"DFRobotC4001RestartAction", automation.Action
35+
)
3836

3937
HUB_CHILD_SCHEMA = cv.Schema(
4038
{
41-
cv.GenerateID(CONF_DFROBOT_C4001_HUB_ID): cv.use_id(DFRobotC4001Hub),
39+
cv.GenerateID(CONF_DFROBOT_C4001_ID): cv.use_id(DFRobotC4001Hub),
4240
}
4341
)
4442

@@ -80,7 +78,22 @@ async def to_code(config):
8078
}
8179
),
8280
)
83-
async def dfrobot_c4001_reset_to_code(config, action_id, template_arg, args):
81+
async def dfrobot_c4001_factory_reset_to_code(config, action_id, template_arg, args):
82+
var = cg.new_Pvariable(action_id, template_arg)
83+
await cg.register_parented(var, config[CONF_ID])
84+
return var
85+
86+
87+
@automation.register_action(
88+
"dfrobot_c4001.restart",
89+
DFRobotC4001RestartAction,
90+
maybe_simple_id(
91+
{
92+
cv.GenerateID(): cv.use_id(DFRobotC4001Hub),
93+
}
94+
),
95+
)
96+
async def dfrobot_c4001_restart_to_code(config, action_id, template_arg, args):
8497
var = cg.new_Pvariable(action_id, template_arg)
8598
await cg.register_parented(var, config[CONF_ID])
8699
return var

components/dfrobot_c4001/automation.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55

66
#include "dfrobot_c4001.h"
77

8-
namespace esphome
9-
{
10-
namespace dfrobot_c4001
11-
{
12-
template <typename... Ts>
13-
class DFRobotC4001FactoryResetAction : public Action<Ts...>, public Parented<DFRobotC4001Hub>
14-
{
15-
public:
16-
void play(Ts... x)
17-
{
18-
this->parent_->enqueue(make_unique<PowerCommand>(false));
19-
this->parent_->enqueue(make_unique<FactoryResetCommand>());
20-
}
21-
};
22-
} // namespace dfrobot_c4001
23-
} // namespace esphome
8+
namespace esphome {
9+
namespace dfrobot_c4001 {
10+
template<typename... Ts> class DFRobotC4001FactoryResetAction : public Action<Ts...>, public Parented<DFRobotC4001Hub> {
11+
public:
12+
void play(Ts... x) { this->parent_->factory_reset(); }
13+
};
14+
template<typename... Ts> class DFRobotC4001RestartAction : public Action<Ts...>, public Parented<DFRobotC4001Hub> {
15+
public:
16+
void play(Ts... x) { this->parent_->restart(); }
17+
};
18+
} // namespace dfrobot_c4001
19+
} // namespace esphome

components/dfrobot_c4001/binary_sensor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import esphome.codegen as cg
2-
import esphome.config_validation as cv
32
from esphome.components import binary_sensor
3+
import esphome.config_validation as cv
44
from esphome.const import (
5-
DEVICE_CLASS_MOTION,
65
DEVICE_CLASS_OCCUPANCY,
76
DEVICE_CLASS_UPDATE,
8-
ENTITY_CATEGORY_CONFIG,
97
ENTITY_CATEGORY_DIAGNOSTIC,
108
)
119

12-
from . import HUB_CHILD_SCHEMA, CONF_DFROBOT_C4001_HUB_ID
10+
from . import CONF_DFROBOT_C4001_ID, HUB_CHILD_SCHEMA
1311

1412
DEPENDENCIES = ["dfrobot_c4001"]
1513

@@ -35,7 +33,7 @@
3533

3634

3735
async def to_code(config):
38-
sens0609_hub = await cg.get_variable(config[CONF_DFROBOT_C4001_HUB_ID])
36+
sens0609_hub = await cg.get_variable(config[CONF_DFROBOT_C4001_ID])
3937
if occupancy := config.get(CONF_OCCUPANCY):
4038
bs = await binary_sensor.new_binary_sensor(occupancy)
4139
cg.add(sens0609_hub.set_occupancy_binary_sensor(bs))
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import esphome.codegen as cg
2-
import esphome.config_validation as cv
32
from esphome.components import button
3+
import esphome.config_validation as cv
44
from esphome.const import (
55
CONF_FACTORY_RESET,
66
CONF_RESTART,
77
DEVICE_CLASS_UPDATE,
8+
DEVICE_CLASS_RESTART,
89
ENTITY_CATEGORY_CONFIG,
9-
ENTITY_CATEGORY_DIAGNOSTIC,
1010
ICON_RESTART,
1111
ICON_RESTART_ALERT,
1212
)
1313

14-
from .. import HUB_CHILD_SCHEMA, CONF_DFROBOT_C4001_HUB_ID, dfrobot_c4001_ns
15-
14+
from .. import CONF_DFROBOT_C4001_ID, HUB_CHILD_SCHEMA, dfrobot_c4001_ns
1615

1716
CONF_CONFIG_SAVE = "config_save"
1817

1918
ConfigSaveButton = dfrobot_c4001_ns.class_("ConfigSaveButton", button.Button)
19+
FactoryResetButton = dfrobot_c4001_ns.class_("FactoryResetButton", button.Button)
20+
RestartButton = dfrobot_c4001_ns.class_("RestartButton", button.Button)
2021

2122
CONFIG_SCHEMA = (
2223
cv.Schema(
@@ -25,7 +26,18 @@
2526
ConfigSaveButton,
2627
device_class=DEVICE_CLASS_UPDATE,
2728
entity_category=ENTITY_CATEGORY_CONFIG,
28-
# icon=ICON_RESTART_ALERT,
29+
),
30+
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
31+
FactoryResetButton,
32+
device_class=DEVICE_CLASS_RESTART,
33+
entity_category=ENTITY_CATEGORY_CONFIG,
34+
icon=ICON_RESTART_ALERT,
35+
),
36+
cv.Optional(CONF_RESTART): button.button_schema(
37+
RestartButton,
38+
device_class=DEVICE_CLASS_RESTART,
39+
entity_category=ENTITY_CATEGORY_CONFIG,
40+
icon=ICON_RESTART,
2941
),
3042
}
3143
)
@@ -35,8 +47,16 @@
3547

3648

3749
async def to_code(config):
38-
sens0609_hub = await cg.get_variable(config[CONF_DFROBOT_C4001_HUB_ID])
50+
sens0609_hub = await cg.get_variable(config[CONF_DFROBOT_C4001_ID])
3951
if config_save := config.get(CONF_CONFIG_SAVE):
4052
b = await button.new_button(config_save)
41-
await cg.register_parented(b, config[CONF_DFROBOT_C4001_HUB_ID])
53+
await cg.register_parented(b, config[CONF_DFROBOT_C4001_ID])
4254
cg.add(sens0609_hub.set_config_save_button(b))
55+
if factory_reset := config.get(CONF_FACTORY_RESET):
56+
b = await button.new_button(factory_reset)
57+
await cg.register_parented(b, config[CONF_DFROBOT_C4001_ID])
58+
cg.add(sens0609_hub.set_factory_reset_button(b))
59+
if restart := config.get(CONF_RESTART):
60+
b = await button.new_button(restart)
61+
await cg.register_parented(b, config[CONF_DFROBOT_C4001_ID])
62+
cg.add(sens0609_hub.set_restart_button(b))
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "button.h"
22

3-
namespace esphome
4-
{
5-
namespace dfrobot_c4001
6-
{
7-
void ConfigSaveButton::press_action() { this->parent_->config_save(); }
8-
} // namespace dfrobot_c4001
9-
} // namespace esphome
3+
namespace esphome {
4+
namespace dfrobot_c4001 {
5+
void ConfigSaveButton::press_action() { this->parent_->config_save(); }
6+
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
7+
void RestartButton::press_action() { this->parent_->restart(); }
8+
} // namespace dfrobot_c4001
9+
} // namespace esphome

components/dfrobot_c4001/button/button.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
#include "esphome/components/button/button.h"
44
#include "../dfrobot_c4001.h"
55

6-
namespace esphome
7-
{
8-
namespace dfrobot_c4001
9-
{
6+
namespace esphome {
7+
namespace dfrobot_c4001 {
108

11-
class ConfigSaveButton : public button::Button, public Parented<DFRobotC4001Hub>
12-
{
13-
protected:
14-
void press_action() override;
15-
};
9+
class ConfigSaveButton : public button::Button, public Parented<DFRobotC4001Hub> {
10+
protected:
11+
void press_action() override;
12+
};
1613

17-
} // namespace dfrobot_c4001
18-
} // namespace esphome
14+
class FactoryResetButton : public button::Button, public Parented<DFRobotC4001Hub> {
15+
protected:
16+
void press_action() override;
17+
};
18+
19+
class RestartButton : public button::Button, public Parented<DFRobotC4001Hub> {
20+
protected:
21+
void press_action() override;
22+
};
23+
24+
} // namespace dfrobot_c4001
25+
} // namespace esphome

0 commit comments

Comments
 (0)