ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld6002b.cpp
Go to the documentation of this file.
1#include "ld6002b.h"
2#include "esphome/core/log.h"
3#include <cinttypes>
4#include <cstring>
5
6namespace esphome::ld6002b {
7
8ESPHOME_LOG_TAG(TAG, "ld6002b");
9
10static constexpr uint16_t TYPE_REPORT_LOW_POWER = 0x0A12;
11static constexpr uint16_t TYPE_REPORT_WORK_MODE = 0x0A14;
12
13static constexpr uint32_t CMD_LOW_POWER_ON = 0x16;
14static constexpr uint32_t CMD_LOW_POWER_OFF = 0x17;
15static constexpr uint32_t CMD_GET_LOW_POWER = 0x18;
16
18 this->LD600XComponent::setup();
19 if (this->is_failed())
20 return;
21 if (this->wakeup_pin_ != nullptr) {
22 this->wakeup_pin_->setup();
23 this->wakeup_pin_->digital_write(true);
24 }
25}
26
28#ifdef USE_SWITCH
29 bool want_low_power = this->low_power_switch_ != nullptr;
30 if (want_low_power) {
31 // The query below confirms what the module took; until it lands, the work mode
32 // fallback runs on the restored value.
33 this->low_power_switch_->restore(false);
34 }
35#else
36 bool want_low_power = false;
37#endif
38#ifdef USE_TEXT_SENSOR
39 want_low_power = want_low_power || this->work_mode_text_sensor_ != nullptr;
40#endif
41 if (want_low_power) {
42 this->send_control_command_(CMD_GET_LOW_POWER);
43 }
44}
45
47 ESP_LOGCONFIG(TAG, " Auto wake: %s", this->auto_wake_ ? LOG_STR_LITERAL("true") : LOG_STR_LITERAL("false"));
48 if (this->wakeup_pin_ != nullptr) {
49 LOG_PIN(" Wake-up Pin: ", this->wakeup_pin_);
50 ESP_LOGCONFIG(TAG, " Wake Pulse: %" PRIu32 "ms", this->wakeup_pulse_ms_);
51 }
52#ifdef USE_SWITCH
53 LOG_SWITCH(" ", "Low Power", this->low_power_switch_);
54#endif
55}
56
58#ifdef USE_TEXT_SENSOR
59 // The work mode fallback reads presence off this stream. It weighs no target
60 // presence at all while the stream is off.
61 return this->work_mode_text_sensor_ != nullptr;
62#else
63 return false;
64#endif
65}
66
67bool LD6002BComponent::handle_model_report(uint16_t type, const uint8_t *data, uint16_t len) {
68 switch (type) {
69 case TYPE_REPORT_LOW_POWER:
70 this->handle_low_power_report_(data, len);
71 return true;
72 case TYPE_REPORT_WORK_MODE:
73 this->handle_work_mode_report_(data, len);
74 return true;
75 default:
76 return false;
77 }
78}
79
80void LD6002BComponent::handle_low_power_report_(const uint8_t *data, uint16_t len) {
81 if (len < 1)
82 return;
83 bool enabled = data[0] != 0;
84 this->low_power_enabled_ = enabled;
85 this->low_power_reported_ = true;
86#ifdef USE_SWITCH
87 PUBLISH_STATE(this->low_power_switch_, enabled);
88#endif
90}
91
92void LD6002BComponent::handle_work_mode_report_(const uint8_t *data, uint16_t len) {
93 if (len < 1)
94 return;
95 // Zero is the unattended half of this transition. Read outside the text sensor's
96 // ifdef because the area sensors do not need one configured to have gone stale.
97 const bool low_power = (data[0] == 0);
98#ifdef USE_TEXT_SENSOR
99 if (this->work_mode_text_sensor_ != nullptr) {
100 this->work_mode_reported_ = true;
101 this->publish_work_mode_(low_power);
102 }
103#endif
104 // Protocol V1.2 section 2.1.17: this message is sent only on the transition
105 // between the unattended low-power mode and normal operation, so a zero is the
106 // module stating that nobody is in any area. Not while a target is still being
107 // tracked, though: the reset_unattended command is undocumented on whether it
108 // forces this report, and where two statements from the module disagree the live
109 // one wins.
110 if (low_power && !this->target_presence_any_) {
111 this->clear_area_presence_();
112 }
113}
114
116#ifdef USE_TEXT_SENSOR
117 if (this->work_mode_text_sensor_ == nullptr || this->work_mode_reported_ || !this->low_power_reported_) {
118 return;
119 }
120 // Target presence is only meaningful while the stream that maintains it runs.
121 // Area presence keeps its own report, so it still counts with the target stream
122 // off and low power alone decides only when neither half has anything to say.
123 const bool presence = (this->target_display_enabled_ && this->target_presence_any_) || this->area_presence_any_;
124 this->publish_work_mode_(this->low_power_enabled_ && !presence);
125#endif
126}
127
129#ifdef USE_TEXT_SENSOR
130 if (this->work_mode_text_sensor_ == nullptr) {
131 return;
132 }
133 if (this->work_mode_low_power_.next(low_power)) {
134 this->work_mode_text_sensor_->publish_state(low_power ? "low_power" : "normal");
135 }
136#endif
137}
138
139bool LD6002BComponent::defer_frame_write(uint16_t type, const uint8_t *data, uint8_t len, bool track) {
140 // Anonymous timeouts never replace each other; with a pulse already pending the module is waking anyway.
141 if (!this->auto_wake_ || this->wakeup_pin_ == nullptr || this->wake_pulse_pending_)
142 return false;
143 // Snapshot the payload: the deferred write must not depend on state a completing command changes.
144 if (len > 0 && data != nullptr) {
145 std::memcpy(this->wake_scratch_.data(), data, len);
146 }
147 // A button pulse must not raise the pin in the middle of this one.
149 this->wake_pulse_pending_ = true;
150 this->wakeup_pin_->digital_write(false);
151 const uint8_t generation = this->send_generation_;
152 this->set_timeout(this->wakeup_pulse_ms_, [this, type, len, track, generation]() {
153 this->wakeup_pin_->digital_write(true);
154 this->wake_pulse_pending_ = false;
155 // Anonymous timeouts are never cancelled, so a tracked pulse whose command has since been
156 // retired must not transmit: the frame would land after its successor and be booked to it.
157 if (track && generation != this->send_generation_) {
158 return;
159 }
160 this->write_frame_(type, (len > 0) ? this->wake_scratch_.data() : nullptr, len, track);
161 });
162 return true;
163}
164
166 // A command's own pulse raises the pin and writes after it, so ride along instead of
167 // claiming the flag: claiming it would send that command down the immediate-write path
168 // with the pin still low.
169 if (this->wakeup_pin_ == nullptr || this->wake_pulse_pending_)
170 return;
171 this->wakeup_pin_->digital_write(false);
173 [this]() { this->wakeup_pin_->digital_write(true); });
174}
175
176#ifdef USE_SWITCH
178 if (entity != this->low_power_switch_)
179 return this->LD600XComponent::set_switch_state(entity, state);
180 if (!this->send_control_command_(state ? CMD_LOW_POWER_ON : CMD_LOW_POWER_OFF))
181 return false;
183 this->low_power_reported_ = true;
185 return true;
186}
187#endif
188
189#ifdef USE_BUTTON
191 if (kind == BUTTON_WAKE) {
192 this->wake_();
193 } else {
194 this->LD600XComponent::press_button(kind);
195 }
196}
197#endif
198
199} // namespace esphome::ld6002b
bool is_failed() const
Definition component.h:287
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:2135
virtual void setup()=0
virtual void digital_write(bool value)=0
bool defer_frame_write(uint16_t type, const uint8_t *data, uint8_t len, bool track) override
Definition ld6002b.cpp:139
std::array< uint8_t, ld600x::CMD_MAX_DATA_LEN > wake_scratch_
Definition ld6002b.h:51
static constexpr uint32_t WAKE_BUTTON_TIMEOUT_ID
Definition ld6002b.h:46
bool handle_model_report(uint16_t type, const uint8_t *data, uint16_t len) override
Definition ld6002b.cpp:67
void handle_low_power_report_(const uint8_t *data, uint16_t len)
Definition ld6002b.cpp:80
void publish_work_mode_(bool low_power)
Definition ld6002b.cpp:128
Deduplicator< bool > work_mode_low_power_
Definition ld6002b.h:57
void handle_work_mode_report_(const uint8_t *data, uint16_t len)
Definition ld6002b.cpp:92
SUB_SWITCH(low_power) bool set_switch_state(switch_ void press_button(uint8_t kind) override
Definition ld6002b.cpp:190
bool model_wants_target_stream() const override
Definition ld6002b.cpp:57
virtual bool set_switch_state(switch_::Switch *entity, bool state)
Definition ld600x.cpp:1391
void write_frame_(uint16_t type, const uint8_t *data, uint8_t len, bool track)
Definition ld600x.cpp:924
bool send_control_command_(uint32_t command)
Definition ld600x.cpp:943
Base class for all switches.
Definition switch.h:43
uint8_t type
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "ld6002b")
const void size_t len
Definition hal.h:68
static void uint32_t