From 15d3f171793ef8f18e7727b65d01fe98ca9c1966 Mon Sep 17 00:00:00 2001 From: Hareesh M U Date: Thu, 9 Nov 2023 08:25:23 +0000 Subject: [PATCH] Add Zone still and moving target sensors --- esphome/components/ld2450/ld2450.cpp | 59 +++++++++++++++++++++++----- esphome/components/ld2450/ld2450.h | 7 +++- esphome/components/ld2450/sensor.py | 19 +++++++-- tests/test1.1.yaml | 18 +++++++-- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index 21cf622f43..b2961cfa97 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -91,6 +91,12 @@ void LD2450Component::dump_config() { for (sensor::Sensor *s : this->zone_target_count_sensors_) { LOG_SENSOR(" ", "NthZoneTargetCountSensor", s); } + for (sensor::Sensor *s : this->zone_still_target_count_sensors_) { + LOG_SENSOR(" ", "NthZoneStillTargetCountSensor", s); + } + for (sensor::Sensor *s : this->zone_moving_target_count_sensors_) { + LOG_SENSOR(" ", "NthZoneMovingTargetCountSensor", s); + } #endif #ifdef USE_TEXT_SENSOR LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_); @@ -136,10 +142,11 @@ void LD2450Component::loop() { } // Count targets in zone -uint8_t LD2450Component::count_targets_in_zone_(const Zone &zone) { +uint8_t LD2450Component::count_targets_in_zone_(const Zone &zone, bool is_moving) { uint8_t count = 0; for (auto &index : this->target_info_) { - if (index.x >= zone.x1 && index.x <= zone.x2 && index.y >= zone.y1 && index.y <= zone.y2) { + if (index.x > zone.x1 && index.x < zone.x2 && index.y > zone.y1 && index.y < zone.y2 && + index.is_moving == is_moving) { count++; } } @@ -318,12 +325,14 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) { int16_t ts = 0; int16_t angle; std::string direction; + bool is_moving; #ifdef USE_SENSOR // Loop thru targets // X for (index = 0; index < MAX_TARGETS; index++) { start = TARGET_X + index * 8; + is_moving = false; sensor::Sensor *sx = this->move_x_sensors_[index]; if (sx != nullptr) { val = this->decode_coordinate_(buffer[start], buffer[start + 1]); @@ -348,8 +357,10 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) { if (ss != nullptr) { val = this->decode_speed_(buffer[start], buffer[start + 1]); ts = val; - if (val > 0) + if (val > 0) { + is_moving = true; moving_target_count++; + } if (ss->get_state() != val) { ss->publish_state(val); } @@ -404,20 +415,44 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) { this->target_info_[index].x = tx; this->target_info_[index].y = ty; + this->target_info_[index].is_moving = is_moving; } // End loop thru targets #ifdef USE_SENSOR // Loop thru zones + uint8_t zone_still_targets = 0; + uint8_t zone_moving_targets = 0; + uint8_t zone_all_targets = 0; for (index = 0; index < MAX_ZONES; index++) { - // Publish Target Count in Zones - sensor::Sensor *sztc = this->zone_target_count_sensors_[index]; - if (sztc != nullptr) { - val = this->count_targets_in_zone_(this->zone_config_[index]); - if (sztc->get_state() != val) { - sztc->publish_state(val); + // Publish Still Target Count in Zones + sensor::Sensor *szstc = this->zone_still_target_count_sensors_[index]; + if (szstc != nullptr) { + zone_still_targets = this->count_targets_in_zone_(this->zone_config_[index], false); + if (szstc->get_state() != zone_still_targets) { + szstc->publish_state(zone_still_targets); } } + + // Publish Moving Target Count in Zones + sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index]; + if (szmtc != nullptr) { + zone_moving_targets = this->count_targets_in_zone_(this->zone_config_[index], true); + if (szmtc->get_state() != zone_moving_targets) { + szmtc->publish_state(zone_moving_targets); + } + } + + zone_all_targets = zone_still_targets + zone_moving_targets; + + // Publish All Target Count in Zones + sensor::Sensor *sztc = this->zone_target_count_sensors_[index]; + if (sztc != nullptr) { + if (sztc->get_state() != zone_all_targets) { + sztc->publish_state(zone_all_targets); + } + } + } // End loop thru zones still_target_count = target_count - moving_target_count; @@ -744,6 +779,12 @@ void LD2450Component::set_move_resolution_sensor(int target, sensor::Sensor *s) void LD2450Component::set_zone_target_count_sensor(int zone, sensor::Sensor *s) { this->zone_target_count_sensors_[zone] = s; } +void LD2450Component::set_zone_still_target_count_sensor(int zone, sensor::Sensor *s) { + this->zone_still_target_count_sensors_[zone] = s; +} +void LD2450Component::set_zone_moving_target_count_sensor(int zone, sensor::Sensor *s) { + this->zone_moving_target_count_sensors_[zone] = s; +} #endif #ifdef USE_TEXT_SENSOR void LD2450Component::set_direction_text_sensor(int target, text_sensor::TextSensor *s) { diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index f5b4a356e4..06da2e8cb7 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -52,6 +52,7 @@ static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450 struct Target { int16_t x; int16_t y; + bool is_moving; }; // Zone coordinate struct @@ -189,6 +190,8 @@ class LD2450Component : public Component, public uart::UARTDevice { void set_move_distance_sensor(int target, sensor::Sensor *s); void set_move_resolution_sensor(int target, sensor::Sensor *s); void set_zone_target_count_sensor(int zone, sensor::Sensor *s); + void set_zone_still_target_count_sensor(int zone, sensor::Sensor *s); + void set_zone_moving_target_count_sensor(int zone, sensor::Sensor *s); #endif protected: @@ -261,7 +264,7 @@ class LD2450Component : public Component, public uart::UARTDevice { std::string version_; std::string mac_; bool get_timeout_status_(int32_t check_millis); - uint8_t count_targets_in_zone_(const Zone &zone); + uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving); #ifdef USE_TEXT_SENSOR std::vector direction_text_sensors_ = std::vector(3); #endif @@ -279,6 +282,8 @@ class LD2450Component : public Component, public uart::UARTDevice { std::vector move_distance_sensors_ = std::vector(MAX_TARGETS); std::vector move_resolution_sensors_ = std::vector(MAX_TARGETS); std::vector zone_target_count_sensors_ = std::vector(MAX_ZONES); + std::vector zone_still_target_count_sensors_ = std::vector(MAX_ZONES); + std::vector zone_moving_target_count_sensors_ = std::vector(MAX_ZONES); #endif }; diff --git a/esphome/components/ld2450/sensor.py b/esphome/components/ld2450/sensor.py index f0c51503ff..3f58f5b6ce 100644 --- a/esphome/components/ld2450/sensor.py +++ b/esphome/components/ld2450/sensor.py @@ -85,6 +85,12 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( icon="mdi:map-marker-account", ), + cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( + icon="mdi:map-marker-account", + ), + cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( + icon="mdi:map-marker-account", + ), } ) for n in range(MAX_ZONES) @@ -127,6 +133,13 @@ async def to_code(config): sens = await sensor.new_sensor(resolution_config) cg.add(ld2450_component.set_move_resolution_sensor(n, sens)) for n in range(MAX_ZONES): - if zone_target_count_config := config.get(f"zone_{n+1}_target_count"): - sens = await sensor.new_sensor(zone_target_count_config) - cg.add(ld2450_component.set_zone_target_count_sensor(n, sens)) + if zone_config := config.get(f"zone_{n+1}"): + if target_count_config := zone_config.get(CONF_TARGET_COUNT): + sens = await sensor.new_sensor(target_count_config) + cg.add(ld2450_component.set_zone_target_count_sensor(n, sens)) + if still_target_count_config := zone_config.get(CONF_STILL_TARGET_COUNT): + sens = await sensor.new_sensor(still_target_count_config) + cg.add(ld2450_component.set_zone_still_target_count_sensor(n, sens)) + if moving_target_count_config := zone_config.get(CONF_MOVING_TARGET_COUNT): + sens = await sensor.new_sensor(moving_target_count_config) + cg.add(ld2450_component.set_zone_moving_target_count_sensor(n, sens)) diff --git a/tests/test1.1.yaml b/tests/test1.1.yaml index b1523edeb9..49b114e01b 100644 --- a/tests/test1.1.yaml +++ b/tests/test1.1.yaml @@ -302,13 +302,25 @@ sensor: name: Target-3 Resolution zone_1: target_count: - name: "Zone-1 Target Count" + name: Zone-1 All Target Count + still_target_count: + name: Zone-1 Still Target Count + moving_target_count: + name: Zone-1 Moving Target Count zone_2: target_count: - name: "Zone-2 Target Count" + name: Zone-2 All Target Count + still_target_count: + name: Zone-2 Still Target Count + moving_target_count: + name: Zone-2 Moving Target Count zone_3: target_count: - name: "Zone-3 Target Count" + name: Zone-3 All Target Count + still_target_count: + name: Zone-3 Still Target Count + moving_target_count: + name: Zone-3 Moving Target Count binary_sensor: - platform: ld2450