mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[ld2450] Replace `throttle` with native filters (#10196)
				
					
				
			Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -17,9 +17,8 @@ CONFIG_SCHEMA = cv.All( | ||||
|     cv.Schema( | ||||
|         { | ||||
|             cv.GenerateID(): cv.declare_id(LD2450Component), | ||||
|             cv.Optional(CONF_THROTTLE, default="1000ms"): cv.All( | ||||
|                 cv.positive_time_period_milliseconds, | ||||
|                 cv.Range(min=cv.TimePeriod(milliseconds=1)), | ||||
|             cv.Optional(CONF_THROTTLE): cv.invalid( | ||||
|                 f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead" | ||||
|             ), | ||||
|         } | ||||
|     ) | ||||
| @@ -46,4 +45,3 @@ async def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     await cg.register_component(var, config) | ||||
|     await uart.register_uart_device(var, config) | ||||
|     cg.add(var.set_throttle(config[CONF_THROTTLE])) | ||||
|   | ||||
| @@ -21,14 +21,17 @@ CONFIG_SCHEMA = { | ||||
|     cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), | ||||
|     cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema( | ||||
|         device_class=DEVICE_CLASS_OCCUPANCY, | ||||
|         filters=[{"settle": cv.TimePeriod(milliseconds=1000)}], | ||||
|         icon=ICON_SHIELD_ACCOUNT, | ||||
|     ), | ||||
|     cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema( | ||||
|         device_class=DEVICE_CLASS_MOTION, | ||||
|         filters=[{"settle": cv.TimePeriod(milliseconds=1000)}], | ||||
|         icon=ICON_TARGET_ACCOUNT, | ||||
|     ), | ||||
|     cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema( | ||||
|         device_class=DEVICE_CLASS_OCCUPANCY, | ||||
|         filters=[{"settle": cv.TimePeriod(milliseconds=1000)}], | ||||
|         icon=ICON_MEDITATION, | ||||
|     ), | ||||
| } | ||||
|   | ||||
| @@ -199,9 +199,8 @@ void LD2450Component::dump_config() { | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "LD2450:\n" | ||||
|                 "  Firmware version: %s\n" | ||||
|                 "  MAC address: %s\n" | ||||
|                 "  Throttle: %u ms", | ||||
|                 version.c_str(), mac_str.c_str(), this->throttle_); | ||||
|                 "  MAC address: %s", | ||||
|                 version.c_str(), mac_str.c_str()); | ||||
| #ifdef USE_BINARY_SENSOR | ||||
|   ESP_LOGCONFIG(TAG, "Binary Sensors:"); | ||||
|   LOG_BINARY_SENSOR("  ", "MovingTarget", this->moving_target_binary_sensor_); | ||||
| @@ -431,11 +430,6 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu | ||||
| //  [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC] | ||||
| //   Header       Target 1                  Target 2                  Target 3                  End | ||||
| void LD2450Component::handle_periodic_data_() { | ||||
|   // Early throttle check - moved before any processing to save CPU cycles | ||||
|   if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   if (this->buffer_pos_ < 29) {  // header (4 bytes) + 8 x 3 target data + footer (2 bytes) | ||||
|     ESP_LOGE(TAG, "Invalid length"); | ||||
|     return; | ||||
| @@ -446,8 +440,6 @@ void LD2450Component::handle_periodic_data_() { | ||||
|     ESP_LOGE(TAG, "Invalid header/footer"); | ||||
|     return; | ||||
|   } | ||||
|   // Save the timestamp after validating the frame so, if invalid, we'll take the next frame immediately | ||||
|   this->last_periodic_millis_ = App.get_loop_component_start_time(); | ||||
|  | ||||
|   int16_t target_count = 0; | ||||
|   int16_t still_target_count = 0; | ||||
|   | ||||
| @@ -110,7 +110,6 @@ class LD2450Component : public Component, public uart::UARTDevice { | ||||
|   void dump_config() override; | ||||
|   void loop() override; | ||||
|   void set_presence_timeout(); | ||||
|   void set_throttle(uint16_t value) { this->throttle_ = value; } | ||||
|   void read_all_info(); | ||||
|   void query_zone_info(); | ||||
|   void restart_and_read_all_info(); | ||||
| @@ -161,11 +160,9 @@ class LD2450Component : public Component, public uart::UARTDevice { | ||||
|   bool get_timeout_status_(uint32_t check_millis); | ||||
|   uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving); | ||||
|  | ||||
|   uint32_t last_periodic_millis_ = 0; | ||||
|   uint32_t presence_millis_ = 0; | ||||
|   uint32_t still_presence_millis_ = 0; | ||||
|   uint32_t moving_presence_millis_ = 0; | ||||
|   uint16_t throttle_ = 0; | ||||
|   uint16_t timeout_ = 5; | ||||
|   uint8_t buffer_data_[MAX_LINE_LENGTH]; | ||||
|   uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0}; | ||||
|   | ||||
| @@ -42,16 +42,43 @@ CONFIG_SCHEMA = cv.Schema( | ||||
|     { | ||||
|         cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), | ||||
|         cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( | ||||
|             icon=ICON_ACCOUNT_GROUP, | ||||
|             accuracy_decimals=0, | ||||
|             filters=[ | ||||
|                 { | ||||
|                     "timeout": { | ||||
|                         "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                         "value": "last", | ||||
|                     } | ||||
|                 }, | ||||
|                 {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|             ], | ||||
|             icon=ICON_ACCOUNT_GROUP, | ||||
|         ), | ||||
|         cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( | ||||
|             icon=ICON_HUMAN_GREETING_PROXIMITY, | ||||
|             accuracy_decimals=0, | ||||
|             filters=[ | ||||
|                 { | ||||
|                     "timeout": { | ||||
|                         "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                         "value": "last", | ||||
|                     } | ||||
|                 }, | ||||
|                 {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|             ], | ||||
|             icon=ICON_HUMAN_GREETING_PROXIMITY, | ||||
|         ), | ||||
|         cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( | ||||
|             icon=ICON_ACCOUNT_SWITCH, | ||||
|             accuracy_decimals=0, | ||||
|             filters=[ | ||||
|                 { | ||||
|                     "timeout": { | ||||
|                         "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                         "value": "last", | ||||
|                     } | ||||
|                 }, | ||||
|                 {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|             ], | ||||
|             icon=ICON_ACCOUNT_SWITCH, | ||||
|         ), | ||||
|     } | ||||
| ) | ||||
| @@ -62,32 +89,86 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( | ||||
|             { | ||||
|                 cv.Optional(CONF_X): sensor.sensor_schema( | ||||
|                     device_class=DEVICE_CLASS_DISTANCE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_ALPHA_X_BOX_OUTLINE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_Y): sensor.sensor_schema( | ||||
|                     device_class=DEVICE_CLASS_DISTANCE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_ALPHA_Y_BOX_OUTLINE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_SPEED): sensor.sensor_schema( | ||||
|                     device_class=DEVICE_CLASS_SPEED, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER_PER_SECOND, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_SPEEDOMETER_SLOW, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER_PER_SECOND, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_ANGLE): sensor.sensor_schema( | ||||
|                     unit_of_measurement=UNIT_DEGREES, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_FORMAT_TEXT_ROTATION_ANGLE_UP, | ||||
|                     unit_of_measurement=UNIT_DEGREES, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_DISTANCE): sensor.sensor_schema( | ||||
|                     device_class=DEVICE_CLASS_DISTANCE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_MAP_MARKER_DISTANCE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_RESOLUTION): sensor.sensor_schema( | ||||
|                     device_class=DEVICE_CLASS_DISTANCE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_RELATION_ZERO_OR_ONE_TO_ZERO_OR_ONE, | ||||
|                     unit_of_measurement=UNIT_MILLIMETER, | ||||
|                 ), | ||||
|             } | ||||
|         ) | ||||
| @@ -97,16 +178,43 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( | ||||
|         cv.Optional(f"zone_{n + 1}"): cv.Schema( | ||||
|             { | ||||
|                 cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                     accuracy_decimals=0, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                     accuracy_decimals=0, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                 ), | ||||
|                 cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                     accuracy_decimals=0, | ||||
|                     filters=[ | ||||
|                         { | ||||
|                             "timeout": { | ||||
|                                 "timeout": cv.TimePeriod(milliseconds=1000), | ||||
|                                 "value": "last", | ||||
|                             } | ||||
|                         }, | ||||
|                         {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}, | ||||
|                     ], | ||||
|                     icon=ICON_MAP_MARKER_ACCOUNT, | ||||
|                 ), | ||||
|             } | ||||
|         ) | ||||
|   | ||||
| @@ -9,7 +9,6 @@ uart: | ||||
| ld2450: | ||||
|   - id: ld2450_radar | ||||
|     uart_id: ld2450_uart | ||||
|     throttle: 1000ms | ||||
|  | ||||
| button: | ||||
|   - platform: ld2450 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user