esphome-ratgdo/components/ratgdo/binary_sensor/ratgdo_binary_sensor.cpp

60 lines
2.6 KiB
C++
Raw Normal View History

2023-06-07 10:37:51 -05:00
#include "ratgdo_binary_sensor.h"
#include "../ratgdo_state.h"
2023-06-07 11:41:02 -05:00
#include "esphome/core/log.h"
2023-06-07 10:37:51 -05:00
namespace esphome {
namespace ratgdo {
2023-06-07 11:41:02 -05:00
static const char* const TAG = "ratgdo.binary_sensor";
2023-06-07 11:40:49 -05:00
2023-06-07 17:24:46 -05:00
void RATGDOBinarySensor::setup()
{
2023-06-09 18:27:04 -05:00
if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTION || this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_OBSTRUCTION)
2023-06-07 17:28:29 -05:00
this->publish_state(false);
2023-06-07 17:24:46 -05:00
}
2023-06-07 11:41:02 -05:00
void RATGDOBinarySensor::dump_config()
{
LOG_BINARY_SENSOR("", "RATGDO BinarySensor", this);
2023-06-07 21:40:07 -05:00
if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTION) {
ESP_LOGCONFIG(TAG, " Type: Motion");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_OBSTRUCTION) {
ESP_LOGCONFIG(TAG, " Type: Obstruction");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTOR) {
ESP_LOGCONFIG(TAG, " Type: Motor");
2023-06-09 18:04:29 -05:00
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_BUTTON) {
2023-06-09 18:04:32 -05:00
ESP_LOGCONFIG(TAG, " Type: Button");
2023-06-07 21:40:07 -05:00
}
2023-06-07 11:41:02 -05:00
}
2023-06-07 14:08:35 -05:00
void RATGDOBinarySensor::on_motion_state(MotionState state)
2023-06-07 11:41:02 -05:00
{
2023-06-09 16:48:48 -05:00
if (this->binary_sensor_type_ != SensorType::RATGDO_SENSOR_MOTION)
return;
2023-06-07 14:48:20 -05:00
ESP_LOGD(TAG, "name: %s this->type_:%d on_motion_state: %d", this->get_name(), this->binary_sensor_type_, state);
2023-06-09 16:48:48 -05:00
this->publish_state(state == MotionState::MOTION_STATE_DETECTED);
2023-06-07 11:41:02 -05:00
}
2023-06-07 14:08:35 -05:00
void RATGDOBinarySensor::on_obstruction_state(ObstructionState state)
2023-06-07 11:41:02 -05:00
{
2023-06-09 16:48:48 -05:00
if (this->binary_sensor_type_ != SensorType::RATGDO_SENSOR_OBSTRUCTION)
return;
2023-06-07 14:48:20 -05:00
ESP_LOGD(TAG, "name: %s this->type_:%d on_obstruction_state: %d", this->get_name(), this->binary_sensor_type_, state);
2023-06-09 16:48:48 -05:00
this->publish_state(state == ObstructionState::OBSTRUCTION_STATE_OBSTRUCTED);
2023-06-07 11:41:02 -05:00
}
2023-06-07 21:40:07 -05:00
void RATGDOBinarySensor::on_motor_state(MotorState state)
{
2023-06-09 16:48:48 -05:00
if (this->binary_sensor_type_ != SensorType::RATGDO_SENSOR_MOTOR)
return;
2023-06-07 21:40:07 -05:00
ESP_LOGD(TAG, "name: %s this->type_:%d on_motor_state: %d", this->get_name(), this->binary_sensor_type_, state);
2023-06-09 16:48:48 -05:00
this->publish_state(state == MotorState::MOTOR_STATE_ON);
2023-06-07 21:40:07 -05:00
}
2023-06-09 18:04:29 -05:00
void RATGDOBinarySensor::on_button_state(ButtonState state)
{
if (this->binary_sensor_type_ != SensorType::RATGDO_SENSOR_BUTTON)
return;
ESP_LOGD(TAG, "name: %s this->type_:%d on_button_state: %d", this->get_name(), this->binary_sensor_type_, state);
this->publish_state(state == ButtonState::BUTTON_STATE_PRESSED);
2023-06-09 18:04:32 -05:00
}
2023-06-07 17:49:37 -05:00
2023-06-07 11:41:02 -05:00
} // namespace ratgdo
} // namespace esphome