mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	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>
This commit is contained in:
		
							
								
								
									
										25
									
								
								esphome/components/seeed_mr60bha2/binary_sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								esphome/components/seeed_mr60bha2/binary_sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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)) | ||||||
| @@ -1,6 +1,7 @@ | |||||||
| #include "seeed_mr60bha2.h" | #include "seeed_mr60bha2.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | #include <cinttypes> | ||||||
| #include <utility> | #include <utility> | ||||||
|  |  | ||||||
| namespace esphome { | 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. | // items in an easy-to-read format, including the configuration key-value pairs. | ||||||
| void MR60BHA2Component::dump_config() { | void MR60BHA2Component::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "MR60BHA2:"); |   ESP_LOGCONFIG(TAG, "MR60BHA2:"); | ||||||
|  | #ifdef USE_BINARY_SENSOR | ||||||
|  |   LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->has_target_binary_sensor_); | ||||||
|  | #endif | ||||||
| #ifdef USE_SENSOR | #ifdef USE_SENSOR | ||||||
|   LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_); |   LOG_SENSOR(" ", "Breath Rate Sensor", this->breath_rate_sensor_); | ||||||
|   LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_); |   LOG_SENSOR(" ", "Heart Rate Sensor", this->heart_rate_sensor_); | ||||||
|   LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_); |   LOG_SENSOR(" ", "Distance Sensor", this->distance_sensor_); | ||||||
|  |   LOG_SENSOR(" ", "Target Number Sensor", this->num_targets_sensor_); | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -94,7 +99,8 @@ bool MR60BHA2Component::validate_message_() { | |||||||
|   uint16_t frame_type = encode_uint16(data[5], data[6]); |   uint16_t frame_type = encode_uint16(data[5], data[6]); | ||||||
|  |  | ||||||
|   if (frame_type != BREATH_RATE_TYPE_BUFFER && frame_type != HEART_RATE_TYPE_BUFFER && |   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; |     return false; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -144,6 +150,18 @@ void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, c | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       break; |       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: |     case HEART_RATE_TYPE_BUFFER: | ||||||
|       if (this->heart_rate_sensor_ != nullptr && length >= 4) { |       if (this->heart_rate_sensor_ != nullptr && length >= 4) { | ||||||
|         uint32_t current_heart_rate_int = encode_uint32(data[3], data[2], data[1], data[0]); |         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; |       break; | ||||||
|     case DISTANCE_TYPE_BUFFER: |     case DISTANCE_TYPE_BUFFER: | ||||||
|       if (!data[0]) { |       if (data[0] != 0) { | ||||||
|         if (this->distance_sensor_ != nullptr && length >= 8) { |         if (this->distance_sensor_ != nullptr && length >= 8) { | ||||||
|           uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]); |           uint32_t current_distance_int = encode_uint32(data[7], data[6], data[5], data[4]); | ||||||
|           float distance_float; |           float distance_float; | ||||||
| @@ -164,6 +182,12 @@ void MR60BHA2Component::process_frame_(uint16_t frame_id, uint16_t frame_type, c | |||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       break; |       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: |     default: | ||||||
|       break; |       break; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -1,6 +1,9 @@ | |||||||
| #pragma once | #pragma once | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "esphome/core/defines.h" | #include "esphome/core/defines.h" | ||||||
|  | #ifdef USE_BINARY_SENSOR | ||||||
|  | #include "esphome/components/binary_sensor/binary_sensor.h" | ||||||
|  | #endif | ||||||
| #ifdef USE_SENSOR | #ifdef USE_SENSOR | ||||||
| #include "esphome/components/sensor/sensor.h" | #include "esphome/components/sensor/sensor.h" | ||||||
| #endif | #endif | ||||||
| @@ -12,37 +15,23 @@ | |||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace seeed_mr60bha2 { | 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 uint8_t FRAME_HEADER_BUFFER = 0x01; | ||||||
| static const uint16_t BREATH_RATE_TYPE_BUFFER = 0x0A14; | 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 HEART_RATE_TYPE_BUFFER = 0x0A15; | ||||||
| static const uint16_t DISTANCE_TYPE_BUFFER = 0x0A16; | static const uint16_t DISTANCE_TYPE_BUFFER = 0x0A16; | ||||||
|  | static const uint16_t PRINT_CLOUD_BUFFER = 0x0A04; | ||||||
| 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, |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| class MR60BHA2Component : public Component, | class MR60BHA2Component : public Component, | ||||||
|                           public uart::UARTDevice {  // The class name must be the name defined by text_sensor.py |                           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 | #ifdef USE_SENSOR | ||||||
|   SUB_SENSOR(breath_rate); |   SUB_SENSOR(breath_rate); | ||||||
|   SUB_SENSOR(heart_rate); |   SUB_SENSOR(heart_rate); | ||||||
|   SUB_SENSOR(distance); |   SUB_SENSOR(distance); | ||||||
|  |   SUB_SENSOR(num_targets); | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  public: |  public: | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ from esphome.const import ( | |||||||
|     ICON_HEART_PULSE, |     ICON_HEART_PULSE, | ||||||
|     ICON_PULSE, |     ICON_PULSE, | ||||||
|     ICON_SIGNAL, |     ICON_SIGNAL, | ||||||
|  |     ICON_COUNTER, | ||||||
|     STATE_CLASS_MEASUREMENT, |     STATE_CLASS_MEASUREMENT, | ||||||
|     UNIT_BEATS_PER_MINUTE, |     UNIT_BEATS_PER_MINUTE, | ||||||
|     UNIT_CENTIMETER, |     UNIT_CENTIMETER, | ||||||
| @@ -18,12 +19,13 @@ DEPENDENCIES = ["seeed_mr60bha2"] | |||||||
|  |  | ||||||
| CONF_BREATH_RATE = "breath_rate" | CONF_BREATH_RATE = "breath_rate" | ||||||
| CONF_HEART_RATE = "heart_rate" | CONF_HEART_RATE = "heart_rate" | ||||||
|  | CONF_NUM_TARGETS = "num_targets" | ||||||
|  |  | ||||||
| CONFIG_SCHEMA = cv.Schema( | CONFIG_SCHEMA = cv.Schema( | ||||||
|     { |     { | ||||||
|         cv.GenerateID(CONF_MR60BHA2_ID): cv.use_id(MR60BHA2Component), |         cv.GenerateID(CONF_MR60BHA2_ID): cv.use_id(MR60BHA2Component), | ||||||
|         cv.Optional(CONF_BREATH_RATE): sensor.sensor_schema( |         cv.Optional(CONF_BREATH_RATE): sensor.sensor_schema( | ||||||
|             accuracy_decimals=2, |             accuracy_decimals=0, | ||||||
|             state_class=STATE_CLASS_MEASUREMENT, |             state_class=STATE_CLASS_MEASUREMENT, | ||||||
|             icon=ICON_PULSE, |             icon=ICON_PULSE, | ||||||
|         ), |         ), | ||||||
| @@ -40,6 +42,9 @@ CONFIG_SCHEMA = cv.Schema( | |||||||
|             accuracy_decimals=2, |             accuracy_decimals=2, | ||||||
|             icon=ICON_SIGNAL, |             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): |     if distance_config := config.get(CONF_DISTANCE): | ||||||
|         sens = await sensor.new_sensor(distance_config) |         sens = await sensor.new_sensor(distance_config) | ||||||
|         cg.add(mr60bha2_component.set_distance_sensor(sens)) |         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)) | ||||||
|   | |||||||
| @@ -9,6 +9,11 @@ uart: | |||||||
| seeed_mr60bha2: | seeed_mr60bha2: | ||||||
|   id: my_seeed_mr60bha2 |   id: my_seeed_mr60bha2 | ||||||
|  |  | ||||||
|  | binary_sensor: | ||||||
|  |   - platform: seeed_mr60bha2 | ||||||
|  |     has_target: | ||||||
|  |       name: "Person Information" | ||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: seeed_mr60bha2 |   - platform: seeed_mr60bha2 | ||||||
|     breath_rate: |     breath_rate: | ||||||
| @@ -17,3 +22,5 @@ sensor: | |||||||
|       name: "Real-time heart rate" |       name: "Real-time heart rate" | ||||||
|     distance: |     distance: | ||||||
|       name: "Distance to detection object" |       name: "Distance to detection object" | ||||||
|  |     num_targets: | ||||||
|  |       name: "Target Number" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user