refactor(mr60fda2): fall text sensor to fall binary sensor

This commit is contained in:
Spencer Yan 2024-10-22 16:27:06 +08:00
parent 31d27cb8db
commit 88e80e8247
3 changed files with 14 additions and 13 deletions

View file

@ -1,24 +1,33 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import DEVICE_CLASS_OCCUPANCY
from esphome.const import DEVICE_CLASS_OCCUPANCY, DEVICE_CLASS_SAFETY
from . import CONF_MR60FDA2_ID, MR60FDA2Component
DEPENDENCIES = ["seeed_mr60fda2"]
CONF_PEOPLE_EXIST = "people_exist"
CONF_IS_FALL = "is_fall"
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(
device_class=DEVICE_CLASS_SAFETY, icon="mdi:emergency"
),
}
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):
sens = await binary_sensor.new_binary_sensor(is_fall_config)
cg.add(mr60fda2_component.set_is_fall_binary_sensor(sens))

View file

@ -14,6 +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_);
#endif
#ifdef USE_BUTTON
LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_);
@ -24,9 +25,6 @@ void MR60FDA2Component::dump_config() {
LOG_SELECT(" ", "Height Threshold Select", this->height_threshold_select_);
LOG_SELECT(" ", "Sensitivity Select", this->sensitivity_select_);
#endif
#ifdef USE_TEXT_SENSOR
LOG_TEXT_SENSOR(" ", "Is Fall Text Sensor", this->is_fall_text_sensor_);
#endif
}
// Initialisation functions
@ -227,12 +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_text_sensor_ != nullptr) {
if (this->current_frame_buf_[LEN_TO_HEAD_CKSUM] == 0) {
this->is_fall_text_sensor_->publish_state("Normal");
} else if (this->current_frame_buf_[LEN_TO_HEAD_CKSUM] == 1) {
this->is_fall_text_sensor_->publish_state("Falling");
}
if (this->is_fall_binary_sensor_ != nullptr) {
this->is_fall_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
}
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;

View file

@ -61,6 +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)
#endif
#ifdef USE_BUTTON
SUB_BUTTON(get_radar_parameters)
@ -71,9 +72,6 @@ class MR60FDA2Component : public Component,
SUB_SELECT(height_threshold)
SUB_SELECT(sensitivity)
#endif
#ifdef USE_TEXT_SENSOR
SUB_TEXT_SENSOR(is_fall)
#endif
protected:
uint8_t current_frame_locate_;