ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4
7
8#include "dfrobot_sen0395.h"
9
11
12template<typename... Ts>
13class DfrobotSen0395SettingsAction final : public Action<Ts...>, public Parented<DfrobotSen0395Component> {
14 public:
15 TEMPLATABLE_VALUE(int8_t, factory_reset)
16 TEMPLATABLE_VALUE(int8_t, start_after_power_on)
17 TEMPLATABLE_VALUE(int8_t, turn_on_led)
18 TEMPLATABLE_VALUE(int8_t, presence_via_uart)
19 TEMPLATABLE_VALUE(int8_t, sensitivity)
20 TEMPLATABLE_VALUE(float, delay_after_detect)
21 TEMPLATABLE_VALUE(float, delay_after_disappear)
22 TEMPLATABLE_VALUE(float, det_min1)
23 TEMPLATABLE_VALUE(float, det_max1)
24 TEMPLATABLE_VALUE(float, det_min2)
25 TEMPLATABLE_VALUE(float, det_max2)
26 TEMPLATABLE_VALUE(float, det_min3)
27 TEMPLATABLE_VALUE(float, det_max3)
28 TEMPLATABLE_VALUE(float, det_min4)
29 TEMPLATABLE_VALUE(float, det_max4)
30
31 void play(const Ts &...x) override {
32 // Worst case: stop, factory reset, six settings, save, start
33 if (!this->parent_->has_queue_room(10))
34 return;
35 this->parent_->enqueue(CommandType::COMMAND_TYPE_POWER, false);
36 if (this->factory_reset_.has_value() && this->factory_reset_.value(x...) == true) {
37 this->parent_->enqueue(CommandType::COMMAND_TYPE_FACTORY_RESET);
38 }
39 if (this->det_min1_.has_value() && this->det_max1_.has_value()) {
40 // Each segment is read once, so the guard and the command see the same values.
41 const float min1 = this->det_min1_.value(x...);
42 if (min1 >= 0) {
43 const float max1 = this->det_max1_.value(x...);
44 if (max1 >= 0) {
45 this->parent_->enqueue_det_range_cfg(min1, max1, this->det_min2_.value_or(x..., -1),
46 this->det_max2_.value_or(x..., -1), this->det_min3_.value_or(x..., -1),
47 this->det_max3_.value_or(x..., -1), this->det_min4_.value_or(x..., -1),
48 this->det_max4_.value_or(x..., -1));
49 }
50 }
51 }
52 if (this->delay_after_detect_.has_value() && this->delay_after_disappear_.has_value()) {
53 float detect = this->delay_after_detect_.value(x...);
54 float disappear = this->delay_after_disappear_.value(x...);
55 if (detect >= 0 && disappear >= 0) {
56 this->parent_->enqueue_set_latency(detect, disappear);
57 }
58 }
59 if (this->start_after_power_on_.has_value()) {
60 int8_t val = this->start_after_power_on_.value(x...);
61 if (val >= 0) {
62 this->parent_->enqueue(CommandType::COMMAND_TYPE_SENSOR_CFG_START, val);
63 }
64 }
65 if (this->turn_on_led_.has_value()) {
66 int8_t val = this->turn_on_led_.value(x...);
67 if (val >= 0) {
68 this->parent_->enqueue(CommandType::COMMAND_TYPE_LED_MODE, val);
69 }
70 }
71 if (this->presence_via_uart_.has_value()) {
72 int8_t val = this->presence_via_uart_.value(x...);
73 if (val >= 0) {
74 this->parent_->enqueue(CommandType::COMMAND_TYPE_UART_OUTPUT, val);
75 }
76 }
77 if (this->sensitivity_.has_value()) {
78 int8_t val = this->sensitivity_.value(x...);
79 if (val >= 0) {
80 this->parent_->enqueue(CommandType::COMMAND_TYPE_SENSITIVITY, std::min<int8_t>(val, 9));
81 }
82 }
83 this->parent_->enqueue(CommandType::COMMAND_TYPE_SAVE_CFG);
84 this->parent_->enqueue(CommandType::COMMAND_TYPE_POWER, true);
85 }
86};
87
88} // namespace esphome::dfrobot_sen0395
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:2160
TEMPLATABLE_VALUE(int8_t, factory_reset) TEMPLATABLE_VALUE(int8_t
if(written< 0)
Definition helpers.h:1342
uint16_t x
Definition tt21100.cpp:5