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

72 lines
3.5 KiB
C++
Raw Normal View History

2023-06-07 15:37:51 +00:00
#include "ratgdo_binary_sensor.h"
#include "../ratgdo_state.h"
2023-06-07 16:41:02 +00:00
#include "esphome/core/log.h"
2023-06-07 15:37:51 +00:00
namespace esphome {
namespace ratgdo {
2023-06-07 16:41:02 +00:00
static const char* const TAG = "ratgdo.binary_sensor";
2023-06-07 16:40:49 +00:00
2023-06-07 22:24:46 +00:00
void RATGDOBinarySensor::setup()
{
if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTION) {
this->publish_initial_state(false);
this->parent_->subscribe_motion_state([=](MotionState state) {
this->publish_state(state == MotionState::DETECTED);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_OBSTRUCTION) {
this->publish_initial_state(false);
this->parent_->subscribe_obstruction_state([=](ObstructionState state) {
this->publish_state(state == ObstructionState::OBSTRUCTED);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_MOTOR) {
this->parent_->subscribe_motor_state([=](MotorState state) {
this->publish_state(state == MotorState::ON);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_BUTTON) {
this->publish_initial_state(false);
this->parent_->subscribe_button_state([=](ButtonState state) {
this->publish_state(state == ButtonState::PRESSED);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_DETECTED) {
2024-10-28 17:56:16 +00:00
this->publish_initial_state(false);
this->parent_->subscribe_vehicle_detected_state([=](VehicleDetectedState state) {
this->publish_state(state == VehicleDetectedState::YES);
this->parent_->presence_change(state == VehicleDetectedState::YES);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_ARRIVING) {
2024-10-28 17:56:16 +00:00
this->publish_initial_state(false);
this->parent_->subscribe_vehicle_arriving_state([=](VehicleArrivingState state) {
this->publish_state(state == VehicleArrivingState::YES);
});
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_LEAVING) {
2024-10-28 17:56:16 +00:00
this->publish_initial_state(false);
this->parent_->subscribe_vehicle_leaving_state([=](VehicleLeavingState state) {
this->publish_state(state == VehicleLeavingState::YES);
});
}
2023-06-07 22:24:46 +00:00
}
2023-06-07 16:41:02 +00:00
void RATGDOBinarySensor::dump_config()
{
LOG_BINARY_SENSOR("", "RATGDO BinarySensor", this);
2023-06-08 02:40:07 +00: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 23:04:29 +00:00
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_BUTTON) {
2023-06-09 23:04:32 +00:00
ESP_LOGCONFIG(TAG, " Type: Button");
2024-10-28 17:56:16 +00:00
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_DETECTED) {
ESP_LOGCONFIG(TAG, " Type: VehicleDetected");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_ARRIVING) {
ESP_LOGCONFIG(TAG, " Type: VehicleArriving");
} else if (this->binary_sensor_type_ == SensorType::RATGDO_SENSOR_VEHICLE_LEAVING) {
ESP_LOGCONFIG(TAG, " Type: VehicleLeaving");
2023-06-08 02:40:07 +00:00
}
2023-06-07 16:41:02 +00:00
}
2023-06-07 22:49:37 +00:00
2023-06-07 16:41:02 +00:00
} // namespace ratgdo
} // namespace esphome