Refactor and cleanup

This commit is contained in:
Hareesh M U 2023-11-09 12:44:45 +00:00
parent 15d3f17179
commit 21af3f4eee
5 changed files with 60 additions and 42 deletions

View file

@ -12,19 +12,23 @@ CONF_HAS_TARGET = "has_target"
CONF_HAS_MOVING_TARGET = "has_moving_target" CONF_HAS_MOVING_TARGET = "has_moving_target"
CONF_HAS_STILL_TARGET = "has_still_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 = { CONFIG_SCHEMA = {
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema( cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY, device_class=DEVICE_CLASS_OCCUPANCY,
icon="mdi:shield-account", icon=ICON_SHIELD_ACCOUNT,
), ),
cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema( cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_MOTION, device_class=DEVICE_CLASS_MOTION,
icon="mdi:target-account", icon=ICON_TARGET_ACCOUNT,
), ),
cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema( cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY, device_class=DEVICE_CLASS_OCCUPANCY,
icon="mdi:meditation", icon=ICON_MEDITATION,
), ),
} }

View file

@ -25,8 +25,8 @@ void LD2450Component::setup() {
this->set_presence_timeout(); this->set_presence_timeout();
#endif #endif
this->read_all_info(); this->read_all_info();
ESP_LOGCONFIG(TAG, "Mac Address : %s", const_cast<char *>(this->mac_.c_str())); ESP_LOGCONFIG(TAG, "Mac Address: %s", const_cast<char *>(this->mac_.c_str()));
ESP_LOGCONFIG(TAG, "Firmware Version : %s", const_cast<char *>(this->version_.c_str())); ESP_LOGCONFIG(TAG, "Firmware Version: %s", const_cast<char *>(this->version_.c_str()));
ESP_LOGCONFIG(TAG, "HLK-LD2450 setup complete"); ESP_LOGCONFIG(TAG, "HLK-LD2450 setup complete");
#ifdef USE_API #ifdef USE_API
ESP_LOGCONFIG(TAG, "Registering services"); ESP_LOGCONFIG(TAG, "Registering services");
@ -413,6 +413,7 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) {
} }
#endif #endif
// Store target info for zone target count
this->target_info_[index].x = tx; this->target_info_[index].x = tx;
this->target_info_[index].y = ty; this->target_info_[index].y = ty;
this->target_info_[index].is_moving = is_moving; 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); szstc->publish_state(zone_still_targets);
} }
} }
// Publish Moving Target Count in Zones // Publish Moving Target Count in Zones
sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index]; sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index];
if (szmtc != nullptr) { if (szmtc != nullptr) {
@ -557,8 +557,9 @@ std::string format_mac(uint8_t *buffer) {
return mac; return mac;
} }
// Handle the UART serial ack data
bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) { 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) { if (len < 10) {
ESP_LOGE(TAG, "Error with last command: Incorrect length"); ESP_LOGE(TAG, "Error with last command: Incorrect length");
return true; return true;
@ -577,16 +578,16 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) {
} }
switch (buffer[COMMAND]) { switch (buffer[COMMAND]) {
case lowbyte(CMD_ENABLE_CONF): case lowbyte(CMD_ENABLE_CONF):
ESP_LOGV(TAG, "Handled Enable conf command"); ESP_LOGV(TAG, "Handled Enable Conf command");
break; break;
case lowbyte(CMD_DISABLE_CONF): case lowbyte(CMD_DISABLE_CONF):
ESP_LOGV(TAG, "Handled Disabled conf command"); ESP_LOGV(TAG, "Handled Disabled Conf command");
break; break;
case lowbyte(CMD_SET_BAUD_RATE): 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 #ifdef USE_SELECT
if (this->baud_rate_select_ != nullptr) { 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 #endif
break; break;
@ -640,7 +641,7 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, int len) {
this->publish_zone_type(); this->publish_zone_type();
#ifdef USE_SELECT #ifdef USE_SELECT
if (this->zone_type_select_ != nullptr) { 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 #endif
if (buffer[10] == 0x00) { if (buffer[10] == 0x00) {
@ -676,16 +677,16 @@ void LD2450Component::readline_(int readch, uint8_t *buffer, int len) {
} }
if (pos >= 4) { if (pos >= 4) {
if (buffer[pos - 2] == 0x55 && buffer[pos - 1] == 0xCC) { 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); this->handle_periodic_data_(buffer, pos);
pos = 0; // Reset position index ready for next time pos = 0; // Reset position index ready for next time
} else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 && } else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 &&
buffer[pos - 1] == 0x01) { 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)) { if (this->handle_ack_data_(buffer, pos)) {
pos = 0; // Reset position index ready for next time pos = 0; // Reset position index ready for next time
} else { } 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 #endif
} }
// Set Single/Multiplayer // Set Single/Multiplayer target detection
void LD2450Component::set_multi_target(bool enable) { void LD2450Component::set_multi_target(bool enable) {
this->set_config_mode_(true); this->set_config_mode_(true);
uint8_t cmd = enable ? CMD_MULTI_TARGET : CMD_SINGLE_TARGET; uint8_t cmd = enable ? CMD_MULTI_TARGET : CMD_SINGLE_TARGET;
this->send_command_(cmd, nullptr, 0); this->send_command_(cmd, nullptr, 0);
this->set_config_mode_(false); this->set_config_mode_(false);
// this->set_timeout(200, [this]() { this->read_all_info(); });
} }
// LD2450 factory reset // LD2450 factory reset

View file

@ -40,8 +40,6 @@
namespace esphome { namespace esphome {
namespace ld2450 { namespace ld2450 {
#define CHECK_BIT(var, pos) (((var) >> (pos)) & 1)
// Constants // Constants
static const uint16_t START_DELAY = 5000; // Sensor startup delay 5 sec. 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. 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; int16_t y2 = 0;
}; };
// Commands // LD2450 UART Serial Commands
static const uint8_t CMD_ENABLE_CONF = 0x00FF; static const uint8_t CMD_ENABLE_CONF = 0x00FF;
static const uint8_t CMD_DISABLE_CONF = 0x00FE; static const uint8_t CMD_DISABLE_CONF = 0x00FE;
static const uint8_t CMD_VERSION = 0x00A0; static const uint8_t CMD_VERSION = 0x00A0;
@ -88,27 +86,27 @@ enum BaudRateStructure : uint8_t {
BAUD_RATE_460800 = 8 BAUD_RATE_460800 = 8
}; };
// Convert Baud Rate enum to int
static const std::map<std::string, uint8_t> BAUD_RATE_ENUM_TO_INT{ static const std::map<std::string, uint8_t> BAUD_RATE_ENUM_TO_INT{
{"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400}, {"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400},
{"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400}, {"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400},
{"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}}; {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}};
// Zone Type struct
enum ZoneTypeStructure : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2 }; enum ZoneTypeStructure : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2 };
// Convert Zone Type int to Enum
static const std::map<ZoneTypeStructure, std::string> ZONE_TYPE_INT_TO_ENUM{ static const std::map<ZoneTypeStructure, std::string> ZONE_TYPE_INT_TO_ENUM{
{ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, {ZONE_FILTER, "Filter"}}; {ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, {ZONE_FILTER, "Filter"}};
// Convert Zone Type enum to int
static const std::map<std::string, uint8_t> ZONE_TYPE_ENUM_TO_INT{ static const std::map<std::string, uint8_t> ZONE_TYPE_ENUM_TO_INT{
{"Disabled", ZONE_DISABLED}, {"Detection", ZONE_DETECTION}, {"Filter", ZONE_FILTER}}; {"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_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA};
static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01}; 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 { enum PeriodicDataStructure : uint8_t {
TARGET_X = 4, TARGET_X = 4,
TARGET_Y = 6, TARGET_Y = 6,
@ -119,7 +117,7 @@ enum PeriodicDataStructure : uint8_t {
enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 }; enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 };
enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 }; 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 #ifdef USE_API
class LD2450Component : public Component, public uart::UARTDevice, public esphome::api::CustomAPIDevice { class LD2450Component : public Component, public uart::UARTDevice, public esphome::api::CustomAPIDevice {
#else #else

View file

@ -20,6 +20,11 @@ CONF_Y1 = "y1"
CONF_X2 = "x2" CONF_X2 = "x2"
CONF_Y2 = "y2" 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) PresenceTimeoutNumber = ld2450_ns.class_("PresenceTimeoutNumber", number.Number)
ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number) ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number)
@ -44,28 +49,28 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend(
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
entity_category=ENTITY_CATEGORY_CONFIG, 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( cv.Required(CONF_Y1): number.number_schema(
ZoneCoordinateNumber, ZoneCoordinateNumber,
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:arrow-top-left", icon=ICON_ARROW_TOP_LEFT,
), ),
cv.Required(CONF_X2): number.number_schema( cv.Required(CONF_X2): number.number_schema(
ZoneCoordinateNumber, ZoneCoordinateNumber,
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
entity_category=ENTITY_CATEGORY_CONFIG, 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( cv.Required(CONF_Y2): number.number_schema(
ZoneCoordinateNumber, ZoneCoordinateNumber,
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:arrow-bottom-right", icon=ICON_ARROW_BOTTOM_RIGHT,
), ),
} }
) )

View file

@ -27,17 +27,28 @@ CONF_X = "x"
CONF_Y = "y" CONF_Y = "y"
CONF_ANGLE = "angle" 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( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( 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( 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( 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( cv.Optional(CONF_X): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
icon="mdi:alpha-x-box-outline", icon=ICON_ALPHA_X_BOX_OUTLINE,
), ),
cv.Optional(CONF_Y): sensor.sensor_schema( cv.Optional(CONF_Y): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
icon="mdi:alpha-y-box-outline", icon=ICON_ALPHA_Y_BOX_OUTLINE,
), ),
cv.Optional(CONF_SPEED): sensor.sensor_schema( cv.Optional(CONF_SPEED): sensor.sensor_schema(
device_class=DEVICE_CLASS_SPEED, device_class=DEVICE_CLASS_SPEED,
unit_of_measurement=UNIT_MILLIMETER_PER_SECOND, unit_of_measurement=UNIT_MILLIMETER_PER_SECOND,
icon="mdi:speedometer-slow", icon=ICON_SPEEDOMETER_SLOW,
), ),
cv.Optional(CONF_ANGLE): sensor.sensor_schema( cv.Optional(CONF_ANGLE): sensor.sensor_schema(
unit_of_measurement=UNIT_DEGREES, 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( cv.Optional(CONF_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, unit_of_measurement=UNIT_MILLIMETER,
icon="mdi:map-marker-distance", icon=ICON_MAP_MARKER_DISTANCE,
), ),
cv.Optional(CONF_RESOLUTION): sensor.sensor_schema( cv.Optional(CONF_RESOLUTION): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
unit_of_measurement=UNIT_MILLIMETER, 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(f"zone_{n+1}"): cv.Schema(
{ {
cv.Optional(CONF_TARGET_COUNT): sensor.sensor_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( 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( cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema(
icon="mdi:map-marker-account", icon=ICON_MAP_MARKER_ACCOUNT,
), ),
} }
) )