diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index 4bd7d280ec..baa091a925 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -26,6 +26,27 @@ void LD2450Component::setup() { ESP_LOGCONFIG(TAG, "Mac Address : %s", const_cast(this->mac_.c_str())); ESP_LOGCONFIG(TAG, "Firmware Version : %s", const_cast(this->version_.c_str())); ESP_LOGCONFIG(TAG, "HLK-LD2450 setup complete"); +#ifdef USE_API + ESP_LOGCONFIG(TAG, "Registering services"); + CustomAPIDevice::register_service(&::esphome::ld2450::LD2450Component::on_set_radar_zone_, "set_radar_zone", + { + "zone_type", + "zone1_x1", + "zone1_y1", + "zone1_x2", + "zone1_y2", + "zone2_x1", + "zone2_y1", + "zone2_x2", + "zone2_y2", + "zone3_x1", + "zone3_y1", + "zone3_x2", + "zone3_y2", + }); + CustomAPIDevice::register_service(&::esphome::ld2450::LD2450Component::on_reset_radar_zone_, "reset_radar_zone"); + ESP_LOGCONFIG(TAG, "Services registration complete"); +#endif } void LD2450Component::dump_config() { @@ -109,6 +130,34 @@ void LD2450Component::loop() { } } +// Service reset_radar_zone +void LD2450Component::on_reset_radar_zone_() { + this->zone_type_ = 0; + for (auto &i : zone_config_) { + i.x1 = 0; + i.y1 = 0; + i.x2 = 0; + i.y2 = 0; + } + this->send_set_zone_command_(); +} + +// Service set_radar_zone +void LD2450Component::on_set_radar_zone_(int zone_type, int zone1_x1, int zone1_y1, int zone1_x2, int zone1_y2, + int zone2_x1, int zone2_y1, int zone2_x2, int zone2_y2, int zone3_x1, + int zone3_y1, int zone3_x2, int zone3_y2) { + this->zone_type_ = zone_type; + int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1, + zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2}; + for (int i = 0; i < MAX_ZONES; i++) { + zone_config_[i].x1 = zone_parameters[i * 4]; + zone_config_[i].y1 = zone_parameters[i * 4 + 1]; + zone_config_[i].x2 = zone_parameters[i * 4 + 2]; + zone_config_[i].y2 = zone_parameters[i * 4 + 3]; + } + this->send_set_zone_command_(); +} + // Set Zone on LD2450 Sensor void LD2450Component::send_set_zone_command_() { uint8_t cmd_value[26] = {}; diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index 15d02f0865..3a1bc725d2 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -23,6 +23,9 @@ #ifdef USE_SELECT #include "esphome/components/select/select.h" #endif +#ifdef USE_API +#include "esphome/components/api/custom_api_device.h" +#endif #ifdef USE_TEXT_SENSOR #include "esphome/components/text_sensor/text_sensor.h" #endif @@ -110,7 +113,11 @@ enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 }; enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 }; +#ifdef USE_API +class LD2450Component : public Component, public uart::UARTDevice, public esphome::api::CustomAPIDevice { +#else class LD2450Component : public Component, public uart::UARTDevice { +#endif #ifdef USE_SENSOR SUB_SENSOR(target_count) SUB_SENSOR(still_target_count) diff --git a/tests/test1.1.yaml b/tests/test1.1.yaml index fa97da8eed..252713021c 100644 --- a/tests/test1.1.yaml +++ b/tests/test1.1.yaml @@ -43,6 +43,8 @@ uart: parity: NONE stop_bits: 1 +api: + ld2450: id: ld2450_radar uart_id: uart_ld2450