From adb920748822c917b05b34709c77ba3902edba3d Mon Sep 17 00:00:00 2001 From: Spencer Yan Date: Tue, 22 Oct 2024 17:05:52 +0800 Subject: [PATCH] fix(mr60fda2): fall detected sensor --- .../seeed_mr60fda2/binary_sensor.py | 8 +++---- .../seeed_mr60fda2/seeed_mr60fda2.cpp | 12 +++++----- .../seeed_mr60fda2/seeed_mr60fda2.h | 2 +- .../components/seeed_mr60fda2/text_sensor.py | 24 ------------------- tests/components/seeed_mr60fda2/common.yaml | 7 ++---- 5 files changed, 13 insertions(+), 40 deletions(-) delete mode 100644 esphome/components/seeed_mr60fda2/text_sensor.py diff --git a/esphome/components/seeed_mr60fda2/binary_sensor.py b/esphome/components/seeed_mr60fda2/binary_sensor.py index 919e4ab29c..3024dce97b 100644 --- a/esphome/components/seeed_mr60fda2/binary_sensor.py +++ b/esphome/components/seeed_mr60fda2/binary_sensor.py @@ -8,14 +8,14 @@ from . import CONF_MR60FDA2_ID, MR60FDA2Component DEPENDENCIES = ["seeed_mr60fda2"] CONF_PEOPLE_EXIST = "people_exist" -CONF_IS_FALL = "is_fall" +CONF_FALL_DETECTED = "fall_detected" CONFIG_SCHEMA = { cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component), cv.Optional(CONF_PEOPLE_EXIST): binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_OCCUPANCY, icon="mdi:motion-sensor" ), - cv.Optional(CONF_IS_FALL): binary_sensor.binary_sensor_schema( + cv.Optional(CONF_FALL_DETECTED): binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_SAFETY, icon="mdi:emergency" ), } @@ -23,11 +23,11 @@ CONFIG_SCHEMA = { async def to_code(config): mr60fda2_component = await cg.get_variable(config[CONF_MR60FDA2_ID]) - + if people_exist_config := config.get(CONF_PEOPLE_EXIST): sens = await binary_sensor.new_binary_sensor(people_exist_config) cg.add(mr60fda2_component.set_people_exist_binary_sensor(sens)) - if is_fall_config := config.get(CONF_IS_FALL): + if is_fall_config := config.get(CONF_FALL_DETECTED): sens = await binary_sensor.new_binary_sensor(is_fall_config) cg.add(mr60fda2_component.set_is_fall_binary_sensor(sens)) diff --git a/esphome/components/seeed_mr60fda2/seeed_mr60fda2.cpp b/esphome/components/seeed_mr60fda2/seeed_mr60fda2.cpp index 7cf57d1642..523035c4d9 100644 --- a/esphome/components/seeed_mr60fda2/seeed_mr60fda2.cpp +++ b/esphome/components/seeed_mr60fda2/seeed_mr60fda2.cpp @@ -14,7 +14,7 @@ void MR60FDA2Component::dump_config() { ESP_LOGCONFIG(TAG, "MR60FDA2:"); #ifdef USE_BINARY_SENSOR LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->people_exist_binary_sensor_); - LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->is_fall_binary_sensor_); + LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->fall_detected_binary_sensor_); #endif #ifdef USE_BUTTON LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_); @@ -225,8 +225,8 @@ void MR60FDA2Component::split_frame_(uint8_t buffer) { void MR60FDA2Component::process_frame_() { switch (this->current_frame_type_) { case IS_FALL_TYPE_BUFFER: - if (this->is_fall_binary_sensor_ != nullptr) { - this->is_fall_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]); + if (this->fall_detected_binary_sensor_ != nullptr) { + this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]); } this->current_frame_locate_ = LOCATE_FRAME_HEADER; break; @@ -279,17 +279,17 @@ void MR60FDA2Component::process_frame_() { float install_height_float; memcpy(&install_height_float, ¤t_install_height_int_, sizeof(float)); select_index_ = find_nearest_index_(install_height_float, INSTALL_HEIGHT, 7); - this->install_height_select_->publish_state(this->install_height_select_.at(select_index_)); + this->install_height_select_->publish_state(this->install_height_select_->at(select_index_).value()); this->current_height_threshold_int_ = encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]); float height_threshold_float; memcpy(&height_threshold_float, ¤t_height_threshold_int_, sizeof(float)); select_index_ = find_nearest_index_(height_threshold_float, HEIGHT_THRESHOLD, 7); - this->height_threshold_select_->publish_state(this->height_threshold_select_.at(select_index_)); + this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index_).value()); this->current_sensitivity_ = encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]); select_index_ = find_nearest_index_(this->current_sensitivity_, SENSITIVITY, 3); - this->sensitivity_select_->publish_state(this->sensitivity_select_.at(select_index_)); + this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index_).value()); ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %u", install_height_float, height_threshold_float, this->current_sensitivity_); this->current_frame_locate_ = LOCATE_FRAME_HEADER; diff --git a/esphome/components/seeed_mr60fda2/seeed_mr60fda2.h b/esphome/components/seeed_mr60fda2/seeed_mr60fda2.h index 25e7caea59..47b18e6b54 100644 --- a/esphome/components/seeed_mr60fda2/seeed_mr60fda2.h +++ b/esphome/components/seeed_mr60fda2/seeed_mr60fda2.h @@ -61,7 +61,7 @@ class MR60FDA2Component : public Component, public uart::UARTDevice { // The class name must be the name defined by text_sensor.py #ifdef USE_BINARY_SENSOR SUB_BINARY_SENSOR(people_exist) - SUB_BINARY_SENSOR(is_fall) + SUB_BINARY_SENSOR(fall_detected) #endif #ifdef USE_BUTTON SUB_BUTTON(get_radar_parameters) diff --git a/esphome/components/seeed_mr60fda2/text_sensor.py b/esphome/components/seeed_mr60fda2/text_sensor.py deleted file mode 100644 index 1c1ea6c2d2..0000000000 --- a/esphome/components/seeed_mr60fda2/text_sensor.py +++ /dev/null @@ -1,24 +0,0 @@ -import esphome.codegen as cg -from esphome.components import text_sensor -import esphome.config_validation as cv -from esphome.const import ENTITY_CATEGORY_DIAGNOSTIC - -from . import CONF_MR60FDA2_ID, MR60FDA2Component - -DEPENDENCIES = ["seeed_mr60fda2"] - -CONF_IS_FALL = "is_fall" - -CONFIG_SCHEMA = { - cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component), - cv.Optional(CONF_IS_FALL): text_sensor.text_sensor_schema( - entity_category=ENTITY_CATEGORY_DIAGNOSTIC, icon="mdi:walk" - ), -} - - -async def to_code(config): - mr60fda2_component = await cg.get_variable(config[CONF_MR60FDA2_ID]) - if is_fall_config := config.get(CONF_IS_FALL): - sens = await text_sensor.new_text_sensor(is_fall_config) - cg.add(mr60fda2_component.set_is_fall_text_sensor(sens)) diff --git a/tests/components/seeed_mr60fda2/common.yaml b/tests/components/seeed_mr60fda2/common.yaml index cb46052e3d..47e10025d3 100644 --- a/tests/components/seeed_mr60fda2/common.yaml +++ b/tests/components/seeed_mr60fda2/common.yaml @@ -10,15 +10,12 @@ seeed_mr60fda2: id: my_seeed_mr60fda2 uart_id: seeed_mr60fda2_uart -text_sensor: - - platform: seeed_mr60fda2 - is_fall: - name: "Falling Information" - binary_sensor: - platform: seeed_mr60fda2 people_exist: name: "Person Information" + fall_detected: + name: "Falling Information" button: - platform: seeed_mr60fda2