From c2e52f4b1177224a80e5585bbdc3151a61ec5a6d Mon Sep 17 00:00:00 2001
From: Citric Li <37475446+limengdu@users.noreply.github.com>
Date: Wed, 22 Jan 2025 08:01:15 +0800
Subject: [PATCH] Add: Human Presence and Target Count to the Seeed Studio
 MR60BHA2 (#8010)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Spencer Yan <spencer@spenyan.com>
---
 .../seeed_mr60bha2/binary_sensor.py           | 25 ++++++++++++++++
 .../seeed_mr60bha2/seeed_mr60bha2.cpp         | 28 ++++++++++++++++--
 .../seeed_mr60bha2/seeed_mr60bha2.h           | 29 ++++++-------------
 esphome/components/seeed_mr60bha2/sensor.py   | 10 ++++++-
 tests/components/seeed_mr60bha2/common.yaml   |  7 +++++
 5 files changed, 76 insertions(+), 23 deletions(-)
 create mode 100644 esphome/components/seeed_mr60bha2/binary_sensor.py

diff --git a/esphome/components/seeed_mr60bha2/binary_sensor.py b/esphome/components/seeed_mr60bha2/binary_sensor.py
new file mode 100644
index 0000000000..ae9e1c23e6
--- /dev/null
+++ b/esphome/components/seeed_mr60bha2/binary_sensor.py
@@ -0,0 +1,25 @@
+import esphome.codegen as cg
+from esphome.components import binary_sensor
+import esphome.config_validation as cv
+from esphome.const import (
+    DEVICE_CLASS_OCCUPANCY,
+    CONF_HAS_TARGET,
+)
+from . import CONF_MR60BHA2_ID, MR60BHA2Component
+
+DEPENDENCIES = ["seeed_mr60bha2"]
+
+CONFIG_SCHEMA = {
+    cv.GenerateID(CONF_MR60BHA2_ID): cv.use_id(MR60BHA2Component),
+    cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
+        device_class=DEVICE_CLASS_OCCUPANCY, icon="mdi:motion-sensor"
+    ),
+}
+
+
+async def to_code(config):
+    mr60bha2_component = await cg.get_variable(config[CONF_MR60BHA2_ID])
+
+    if has_target_config := config.get(CONF_HAS_TARGET):
+        sens = await binary_sensor.new_binary_sensor(has_target_config)
+        cg.add(mr60bha2_component.set_has_target_binary_sensor(sens))
diff --git a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp
index 50d709c3b0..75f3f092a6 100644
--- a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp
+++ b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.cpp
@@ -1,6 +1,7 @@
 #include "seeed_mr60bha2.h"
 #include "esphome/core/log.h"
 
+#include <cinttypes>
 #include <utility>
 
 namespace esphome {
@@ -12,10 +13,14 @@ static const char *const TAG = "seeed_mr60bha2";
 // items in an easy-to-read format, including the configuration key-value pairs.
 void MR60BHA2Component::dump_config() {
   ESP_LOGCONFIG(TAG, "MR60BHA2:");
+#ifdef USE_BINARY_SENSOR
+  LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->has_target_binary_sensor_);
+#endif
 #ifdef USE_SENSOR
   LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_);
   LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_);
   LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_);
+  LOG_SENSOR(" ", "Target Number Sensor", this->num_targets_sensor_);
 #endif
 }
 
@@ -94,7 +99,8 @@ bool MR60BHA2Component::validate_message_() {
   uint16_t frame_type = encode_uint16(data[5], data[6]);
 
   if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER &&
-      frame_type != DISTANCE_TYPE_BUFFER) {
+      frame_type != DISTANCE_TYPE_BUFFER && frame_type != PEOPLE_EXIST_TYPE_BUFFER &&
+      frame_type != PRINT_CLOUD_BUFFER) {
     return false;
   }
 
@@ -144,6 +150,18 @@ void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, c
         }
       }
       break;
+    case PEOPLE_EXIST_TYPE_BUFFER:
+      if (this->has_target_binary_sensor_ != nullptr && length >= 2) {
+        uint16_t has_target_int = encode_uint16(data[1], data[0]);
+        this->has_target_binary_sensor_->publish_state(has_target_int);
+        if (has_target_int == 0) {
+          this->breath_rate_sensor_->publish_state(0.0);
+          this->heart_rate_sensor_->publish_state(0.0);
+          this->distance_sensor_->publish_state(0.0);
+          this->num_targets_sensor_->publish_state(0);
+        }
+      }
+      break;
     case HEART_RATE_TYPE_BUFFER:
       if (this->heart_rate_sensor_ != nullptr && length >= 4) {
         uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]);
@@ -155,7 +173,7 @@ void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, c
       }
       break;
     case DISTANCE_TYPE_BUFFER:
-      if (!data[0]) {
+      if (data[0] != 0) {
         if (this->distance_sensor_ != nullptr && length >= 8) {
           uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]);
           float distance_float;
@@ -164,6 +182,12 @@ void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, c
         }
       }
       break;
+    case PRINT_CLOUD_BUFFER:
+      if (this->num_targets_sensor_ != nullptr && length >= 4) {
+        uint32_t current_num_targets_int = encode_uint32(data[3], data[2], data[1], data[0]);
+        this->num_targets_sensor_->publish_state(current_num_targets_int);
+      }
+      break;
     default:
       break;
   }
diff --git a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h
index 0a4f21f1ad..d20c8e50cc 100644
--- a/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h
+++ b/esphome/components/seeed_mr60bha2/seeed_mr60bha2.h
@@ -1,6 +1,9 @@
 #pragma once
 #include "esphome/core/component.h"
 #include "esphome/core/defines.h"
+#ifdef USE_BINARY_SENSOR
+#include "esphome/components/binary_sensor/binary_sensor.h"
+#endif
 #ifdef USE_SENSOR
 #include "esphome/components/sensor/sensor.h"
 #endif
@@ -12,37 +15,23 @@
 
 namespace esphome {
 namespace seeed_mr60bha2 {
-
-static const uint8_t DATA_BUF_MAX_SIZE = 12;
-static const uint8_t FRAME_BUF_MAX_SIZE = 21;
-static const uint8_t LEN_TO_HEAD_CKSUM = 8;
-static const uint8_t LEN_TO_DATA_FRAME = 9;
-
 static const uint8_t FRAME_HEADER_BUFFER = 0x01;
 static const uint16_t BREATH_RATE_TYPE_BUFFER = 0x0A14;
+static const uint16_t PEOPLE_EXIST_TYPE_BUFFER = 0x0F09;
 static const uint16_t HEART_RATE_TYPE_BUFFER = 0x0A15;
 static const uint16_t DISTANCE_TYPE_BUFFER = 0x0A16;
-
-enum FrameLocation {
-  LOCATE_FRAME_HEADER,
-  LOCATE_ID_FRAME1,
-  LOCATE_ID_FRAME2,
-  LOCATE_LENGTH_FRAME_H,
-  LOCATE_LENGTH_FRAME_L,
-  LOCATE_TYPE_FRAME1,
-  LOCATE_TYPE_FRAME2,
-  LOCATE_HEAD_CKSUM_FRAME,  // Header checksum: [from the first byte to the previous byte of the HEAD_CKSUM bit]
-  LOCATE_DATA_FRAME,
-  LOCATE_DATA_CKSUM_FRAME,  // Data checksum: [from the first to the previous byte of the DATA_CKSUM bit]
-  LOCATE_PROCESS_FRAME,
-};
+static const uint16_t PRINT_CLOUD_BUFFER = 0x0A04;
 
 class MR60BHA2Component : public Component,
                           public uart::UARTDevice {  // The class name must be the name defined by text_sensor.py
+#ifdef USE_BINARY_SENSOR
+  SUB_BINARY_SENSOR(has_target);
+#endif
 #ifdef USE_SENSOR
   SUB_SENSOR(breath_rate);
   SUB_SENSOR(heart_rate);
   SUB_SENSOR(distance);
+  SUB_SENSOR(num_targets);
 #endif
 
  public:
diff --git a/esphome/components/seeed_mr60bha2/sensor.py b/esphome/components/seeed_mr60bha2/sensor.py
index 5f30b363bf..916d4b4ba2 100644
--- a/esphome/components/seeed_mr60bha2/sensor.py
+++ b/esphome/components/seeed_mr60bha2/sensor.py
@@ -7,6 +7,7 @@ from esphome.const import (
     ICON_HEART_PULSE,
     ICON_PULSE,
     ICON_SIGNAL,
+    ICON_COUNTER,
     STATE_CLASS_MEASUREMENT,
     UNIT_BEATS_PER_MINUTE,
     UNIT_CENTIMETER,
@@ -18,12 +19,13 @@ DEPENDENCIES = ["seeed_mr60bha2"]
 
 CONF_BREATH_RATE = "breath_rate"
 CONF_HEART_RATE = "heart_rate"
+CONF_NUM_TARGETS = "num_targets"
 
 CONFIG_SCHEMA = cv.Schema(
     {
         cv.GenerateID(CONF_MR60BHA2_ID): cv.use_id(MR60BHA2Component),
         cv.Optional(CONF_BREATH_RATE): sensor.sensor_schema(
-            accuracy_decimals=2,
+            accuracy_decimals=0,
             state_class=STATE_CLASS_MEASUREMENT,
             icon=ICON_PULSE,
         ),
@@ -40,6 +42,9 @@ CONFIG_SCHEMA = cv.Schema(
             accuracy_decimals=2,
             icon=ICON_SIGNAL,
         ),
+        cv.Optional(CONF_NUM_TARGETS): sensor.sensor_schema(
+            icon=ICON_COUNTER,
+        ),
     }
 )
 
@@ -55,3 +60,6 @@ async def to_code(config):
     if distance_config := config.get(CONF_DISTANCE):
         sens = await sensor.new_sensor(distance_config)
         cg.add(mr60bha2_component.set_distance_sensor(sens))
+    if num_targets_config := config.get(CONF_NUM_TARGETS):
+        sens = await sensor.new_sensor(num_targets_config)
+        cg.add(mr60bha2_component.set_num_targets_sensor(sens))
diff --git a/tests/components/seeed_mr60bha2/common.yaml b/tests/components/seeed_mr60bha2/common.yaml
index e9d0c735af..9eb0c8d527 100644
--- a/tests/components/seeed_mr60bha2/common.yaml
+++ b/tests/components/seeed_mr60bha2/common.yaml
@@ -9,6 +9,11 @@ uart:
 seeed_mr60bha2:
   id: my_seeed_mr60bha2
 
+binary_sensor:
+  - platform: seeed_mr60bha2
+    has_target:
+      name: "Person Information"
+
 sensor:
   - platform: seeed_mr60bha2
     breath_rate:
@@ -17,3 +22,5 @@ sensor:
       name: "Real-time heart rate"
     distance:
       name: "Distance to detection object"
+    num_targets:
+      name: "Target Number"