From 21af3f4eee4880b1b121bbdf2fdbbd249c8ae801 Mon Sep 17 00:00:00 2001 From: Hareesh M U Date: Thu, 9 Nov 2023 12:44:45 +0000 Subject: [PATCH] Refactor and cleanup --- esphome/components/ld2450/binary_sensor.py | 10 ++++-- esphome/components/ld2450/ld2450.cpp | 28 ++++++++-------- esphome/components/ld2450/ld2450.h | 16 ++++----- esphome/components/ld2450/number/__init__.py | 13 +++++--- esphome/components/ld2450/sensor.py | 35 +++++++++++++------- 5 files changed, 60 insertions(+), 42 deletions(-) diff --git a/esphome/components/ld2450/binary_sensor.py b/esphome/components/ld2450/binary_sensor.py index d16d1f79cb..e5ea7a6a85 100644 --- a/esphome/components/ld2450/binary_sensor.py +++ b/esphome/components/ld2450/binary_sensor.py @@ -12,19 +12,23 @@ CONF_HAS_TARGET = "has_target" CONF_HAS_MOVING_TARGET = "has_moving_target" CONF_HAS_STILL_TARGET = "has_still_target" +ICON_SHIELD_ACCOUNT = "mdi:shield-account" +ICON_TARGET_ACCOUNT = "mdi:target-account" +ICON_MEDITATION = "mdi:meditation" + 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, - icon="mdi:shield-account", + icon=ICON_SHIELD_ACCOUNT, ), cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_MOTION, - icon="mdi:target-account", + icon=ICON_TARGET_ACCOUNT, ), cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_OCCUPANCY, - icon="mdi:meditation", + icon=ICON_MEDITATION, ), } diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index b2961cfa97..2bdaddfd1b 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -25,8 +25,8 @@ void LD2450Component::setup() { this->set_presence_timeout(); #endif this->read_all_info(); - 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, "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"); @@ -413,6 +413,7 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) { } #endif + // Store target info for zone target count this->target_info_[index].x = tx; this->target_info_[index].y = ty; this->target_info_[index].is_moving = is_moving; @@ -433,7 +434,6 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) { 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) { @@ -557,8 +557,9 @@ std::string format_mac(uint8_t *buffer) { return mac; } +// Handle the UART serial ack data bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) { - ESP_LOGD(TAG, "Handling ACK DATA for COMMAND %02X", buffer[COMMAND]); + ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", buffer[COMMAND]); if (len < 10) { ESP_LOGE(TAG, "Error with last command: Incorrect length"); return true; @@ -577,16 +578,16 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) { } switch (buffer[COMMAND]) { case lowbyte(CMD_ENABLE_CONF): - ESP_LOGV(TAG, "Handled Enable conf command"); + ESP_LOGV(TAG, "Handled Enable Conf command"); break; case lowbyte(CMD_DISABLE_CONF): - ESP_LOGV(TAG, "Handled Disabled conf command"); + ESP_LOGV(TAG, "Handled Disabled Conf command"); break; case lowbyte(CMD_SET_BAUD_RATE): - ESP_LOGV(TAG, "Handled baud rate change command"); + ESP_LOGV(TAG, "Handled Baud rate change command"); #ifdef USE_SELECT if (this->baud_rate_select_ != nullptr) { - ESP_LOGV(TAG, "Change baud rate component config to %s and reinstall", this->baud_rate_select_->state.c_str()); + ESP_LOGV(TAG, "Change Baud rate to %s", this->baud_rate_select_->state.c_str()); } #endif break; @@ -640,7 +641,7 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) { this->publish_zone_type(); #ifdef USE_SELECT if (this->zone_type_select_ != nullptr) { - ESP_LOGV(TAG, "Change Zone Type component config to: %s", this->zone_type_select_->state.c_str()); + ESP_LOGV(TAG, "Change Zone Type to: %s", this->zone_type_select_->state.c_str()); } #endif if (buffer[10] == 0x00) { @@ -676,16 +677,16 @@ void LD2450Component::readline_(int readch, uint8_t *buffer, int len) { } if (pos >= 4) { if (buffer[pos - 2] == 0x55 && buffer[pos - 1] == 0xCC) { - ESP_LOGV(TAG, "Handle Periodic Radar Data"); + ESP_LOGV(TAG, "Handle LD2450 Periodic Radar Data"); this->handle_periodic_data_(buffer, pos); pos = 0; // Reset position index ready for next time } else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 && buffer[pos - 1] == 0x01) { - ESP_LOGV(TAG, "Handle Commad ACK Data"); + ESP_LOGV(TAG, "Handle LD2450 Commad ACK Data"); if (this->handle_ack_data_(buffer, pos)) { pos = 0; // Reset position index ready for next time } else { - ESP_LOGV(TAG, "Command ACK Data incomplete"); + ESP_LOGV(TAG, "LD2450 Command ACK Data incomplete"); } } } @@ -734,13 +735,12 @@ void LD2450Component::publish_zone_type() { #endif } -// Set Single/Multiplayer +// Set Single/Multiplayer target detection void LD2450Component::set_multi_target(bool enable) { this->set_config_mode_(true); uint8_t cmd = enable ? CMD_MULTI_TARGET : CMD_SINGLE_TARGET; this->send_command_(cmd, nullptr, 0); this->set_config_mode_(false); - // this->set_timeout(200, [this]() { this->read_all_info(); }); } // LD2450 factory reset diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index 06da2e8cb7..9c9a9b6898 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -40,8 +40,6 @@ namespace esphome { namespace ld2450 { -#define CHECK_BIT(var, pos) (((var) >> (pos)) & 1) - // Constants static const uint16_t START_DELAY = 5000; // Sensor startup delay 5 sec. static const uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec. @@ -63,7 +61,7 @@ struct Zone { int16_t y2 = 0; }; -// Commands +// LD2450 UART Serial Commands static const uint8_t CMD_ENABLE_CONF = 0x00FF; static const uint8_t CMD_DISABLE_CONF = 0x00FE; static const uint8_t CMD_VERSION = 0x00A0; @@ -88,27 +86,27 @@ enum BaudRateStructure : uint8_t { BAUD_RATE_460800 = 8 }; +// Convert Baud Rate enum to int static const std::map BAUD_RATE_ENUM_TO_INT{ {"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400}, {"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400}, {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}}; +// Zone Type struct enum ZoneTypeStructure : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2 }; +// Convert Zone Type int to Enum static const std::map ZONE_TYPE_INT_TO_ENUM{ {ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, {ZONE_FILTER, "Filter"}}; +// Convert Zone Type enum to int static const std::map ZONE_TYPE_ENUM_TO_INT{ {"Disabled", ZONE_DISABLED}, {"Detection", ZONE_DETECTION}, {"Filter", ZONE_FILTER}}; -// Command Header & Footer +// LD2450 UART serial Command Header & Footer static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA}; static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01}; -// Data Header & Footer -static const uint8_t DATA_FRAME_HEADER[4] = {0xAA, 0xFF, 0x03, 0x00}; -static const uint8_t DATA_FRAME_END[2] = {0x55, 0xCC}; - enum PeriodicDataStructure : uint8_t { TARGET_X = 4, TARGET_Y = 6, @@ -119,7 +117,7 @@ enum PeriodicDataStructure : uint8_t { enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 }; enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 }; - +// Use CustomAPIDevice if API is defined in YAML - used for service calls to set/reset zone #ifdef USE_API class LD2450Component : public Component, public uart::UARTDevice, public esphome::api::CustomAPIDevice { #else diff --git a/esphome/components/ld2450/number/__init__.py b/esphome/components/ld2450/number/__init__.py index e5c0331696..6371ccd43a 100644 --- a/esphome/components/ld2450/number/__init__.py +++ b/esphome/components/ld2450/number/__init__.py @@ -20,6 +20,11 @@ CONF_Y1 = "y1" CONF_X2 = "x2" CONF_Y2 = "y2" +ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE = "mdi:arrow-top-left-bold-box-outline" +ICON_ARROW_TOP_LEFT = "mdi:arrow-top-left" +ICON_ARROW_BOTTOM_RIGHT_BOLD_BOX_OUTLINE = "mdi:arrow-bottom-right-bold-box-outline" +ICON_ARROW_BOTTOM_RIGHT = "mdi:arrow-bottom-right" + PresenceTimeoutNumber = ld2450_ns.class_("PresenceTimeoutNumber", number.Number) ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number) @@ -44,28 +49,28 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, entity_category=ENTITY_CATEGORY_CONFIG, - icon="mdi:arrow-top-left-bold-box-outline", + icon=ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE, ), cv.Required(CONF_Y1): number.number_schema( ZoneCoordinateNumber, device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, entity_category=ENTITY_CATEGORY_CONFIG, - icon="mdi:arrow-top-left", + icon=ICON_ARROW_TOP_LEFT, ), cv.Required(CONF_X2): number.number_schema( ZoneCoordinateNumber, device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, entity_category=ENTITY_CATEGORY_CONFIG, - icon="mdi:arrow-bottom-right-bold-box-outline", + icon=ICON_ARROW_BOTTOM_RIGHT_BOLD_BOX_OUTLINE, ), cv.Required(CONF_Y2): number.number_schema( ZoneCoordinateNumber, device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, entity_category=ENTITY_CATEGORY_CONFIG, - icon="mdi:arrow-bottom-right", + icon=ICON_ARROW_BOTTOM_RIGHT, ), } ) diff --git a/esphome/components/ld2450/sensor.py b/esphome/components/ld2450/sensor.py index 3f58f5b6ce..08511615ac 100644 --- a/esphome/components/ld2450/sensor.py +++ b/esphome/components/ld2450/sensor.py @@ -27,17 +27,28 @@ CONF_X = "x" CONF_Y = "y" CONF_ANGLE = "angle" +ICON_ACCOUNT_GROUP = "mdi:account-group" +ICON_ACCOUNT_SWITCH = "mdi:account-switch" +ICON_HUMAN_GREETING_PROXIMITY = "mdi:human-greeting-proximity" +ICON_ALPHA_X_BOX_OUTLINE = "mdi:alpha-x-box-outline" +ICON_ALPHA_Y_BOX_OUTLINE = "mdi:alpha-y-box-outline" +ICON_SPEEDOMETER_SLOW = "mdi:speedometer-slow" +ICON_FORMAT_TEXT_ROTATION_ANGLE_UP = "mdi:format-text-rotation-angle-up" +ICON_MAP_MARKER_DISTANCE = "mdi:map-marker-distance" +ICON_RELATION_ZERO_OR_ONE_TO_ZERO_OR_ONE = "mdi:relation-zero-or-one-to-zero-or-one" +ICON_MAP_MARKER_ACCOUNT = "mdi:map-marker-account" + CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:account-group", + icon=ICON_ACCOUNT_GROUP, ), cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:human-greeting-proximity", + icon=ICON_HUMAN_GREETING_PROXIMITY, ), cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:account-switch", + icon=ICON_ACCOUNT_SWITCH, ), } ) @@ -49,31 +60,31 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( cv.Optional(CONF_X): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, - icon="mdi:alpha-x-box-outline", + icon=ICON_ALPHA_X_BOX_OUTLINE, ), cv.Optional(CONF_Y): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, - icon="mdi:alpha-y-box-outline", + icon=ICON_ALPHA_Y_BOX_OUTLINE, ), cv.Optional(CONF_SPEED): sensor.sensor_schema( device_class=DEVICE_CLASS_SPEED, unit_of_measurement=UNIT_MILLIMETER_PER_SECOND, - icon="mdi:speedometer-slow", + icon=ICON_SPEEDOMETER_SLOW, ), cv.Optional(CONF_ANGLE): sensor.sensor_schema( unit_of_measurement=UNIT_DEGREES, - icon="mdi:format-text-rotation-angle-up", + icon=ICON_FORMAT_TEXT_ROTATION_ANGLE_UP, ), cv.Optional(CONF_DISTANCE): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, - icon="mdi:map-marker-distance", + icon=ICON_MAP_MARKER_DISTANCE, ), cv.Optional(CONF_RESOLUTION): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_MILLIMETER, - icon="mdi:relation-zero-or-one-to-zero-or-one", + icon=ICON_RELATION_ZERO_OR_ONE_TO_ZERO_OR_ONE, ), } ) @@ -83,13 +94,13 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( cv.Optional(f"zone_{n+1}"): cv.Schema( { cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:map-marker-account", + icon=ICON_MAP_MARKER_ACCOUNT, ), cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:map-marker-account", + icon=ICON_MAP_MARKER_ACCOUNT, ), cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( - icon="mdi:map-marker-account", + icon=ICON_MAP_MARKER_ACCOUNT, ), } )