mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 04:55:48 +08:00
[ld2450] add on_data callback (#13601)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
|
from esphome import automation
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import uart
|
from esphome.components import uart
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_ID, CONF_THROTTLE
|
from esphome.const import CONF_ID, CONF_ON_DATA, CONF_THROTTLE, CONF_TRIGGER_ID
|
||||||
|
|
||||||
AUTO_LOAD = ["ld24xx"]
|
AUTO_LOAD = ["ld24xx"]
|
||||||
DEPENDENCIES = ["uart"]
|
DEPENDENCIES = ["uart"]
|
||||||
@@ -11,6 +12,8 @@ MULTI_CONF = True
|
|||||||
ld2450_ns = cg.esphome_ns.namespace("ld2450")
|
ld2450_ns = cg.esphome_ns.namespace("ld2450")
|
||||||
LD2450Component = ld2450_ns.class_("LD2450Component", cg.Component, uart.UARTDevice)
|
LD2450Component = ld2450_ns.class_("LD2450Component", cg.Component, uart.UARTDevice)
|
||||||
|
|
||||||
|
LD2450DataTrigger = ld2450_ns.class_("LD2450DataTrigger", automation.Trigger.template())
|
||||||
|
|
||||||
CONF_LD2450_ID = "ld2450_id"
|
CONF_LD2450_ID = "ld2450_id"
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
@@ -20,6 +23,11 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
cv.Optional(CONF_THROTTLE): cv.invalid(
|
cv.Optional(CONF_THROTTLE): cv.invalid(
|
||||||
f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead"
|
f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead"
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_ON_DATA): automation.validate_automation(
|
||||||
|
{
|
||||||
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LD2450DataTrigger),
|
||||||
|
}
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(uart.UART_DEVICE_SCHEMA)
|
.extend(uart.UART_DEVICE_SCHEMA)
|
||||||
@@ -45,3 +53,6 @@ async def to_code(config):
|
|||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await uart.register_uart_device(var, config)
|
await uart.register_uart_device(var, config)
|
||||||
|
for conf in config.get(CONF_ON_DATA, []):
|
||||||
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||||
|
await automation.build_automation(trigger, [], conf)
|
||||||
|
|||||||
@@ -413,6 +413,10 @@ void LD2450Component::restart_and_read_all_info() {
|
|||||||
this->set_timeout(1500, [this]() { this->read_all_info(); });
|
this->set_timeout(1500, [this]() { this->read_all_info(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LD2450Component::add_on_data_callback(std::function<void()> &&callback) {
|
||||||
|
this->data_callback_.add(std::move(callback));
|
||||||
|
}
|
||||||
|
|
||||||
// Send command with values to LD2450
|
// Send command with values to LD2450
|
||||||
void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
|
void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
|
||||||
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
|
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
|
||||||
@@ -613,6 +617,8 @@ void LD2450Component::handle_periodic_data_() {
|
|||||||
this->still_presence_millis_ = App.get_loop_component_start_time();
|
this->still_presence_millis_ = App.get_loop_component_start_time();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
this->data_callback_.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LD2450Component::handle_ack_data_() {
|
bool LD2450Component::handle_ack_data_() {
|
||||||
|
|||||||
@@ -141,6 +141,9 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
|||||||
int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
|
int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
|
||||||
int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
|
int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
|
||||||
|
|
||||||
|
/// Add a callback that will be called after each successfully processed periodic data frame.
|
||||||
|
void add_on_data_callback(std::function<void()> &&callback);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
|
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
|
||||||
void set_config_mode_(bool enable);
|
void set_config_mode_(bool enable);
|
||||||
@@ -190,6 +193,15 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
|||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
std::array<text_sensor::TextSensor *, 3> direction_text_sensors_{};
|
std::array<text_sensor::TextSensor *, 3> direction_text_sensors_{};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
LazyCallbackManager<void()> data_callback_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LD2450DataTrigger : public Trigger<> {
|
||||||
|
public:
|
||||||
|
explicit LD2450DataTrigger(LD2450Component *parent) {
|
||||||
|
parent->add_on_data_callback([this]() { this->trigger(); });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esphome::ld2450
|
} // namespace esphome::ld2450
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
ld2450:
|
ld2450:
|
||||||
- id: ld2450_radar
|
- id: ld2450_radar
|
||||||
|
on_data:
|
||||||
|
then:
|
||||||
|
- logger.log: "LD2450 Radar Data Received"
|
||||||
|
|
||||||
button:
|
button:
|
||||||
- platform: ld2450
|
- platform: ld2450
|
||||||
|
|||||||
Reference in New Issue
Block a user