Binary sensors support
This commit is contained in:
parent
27ca86d650
commit
9949beb4be
|
@ -3,6 +3,7 @@ import esphome.config_validation as cv
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from esphome import automation, pins
|
from esphome import automation, pins
|
||||||
from esphome.const import CONF_ID, CONF_TRIGGER_ID
|
from esphome.const import CONF_ID, CONF_TRIGGER_ID
|
||||||
|
from esphome.components import binary_sensor
|
||||||
|
|
||||||
DEPENDENCIES = ["preferences"]
|
DEPENDENCIES = ["preferences"]
|
||||||
MULTI_CONF = True
|
MULTI_CONF = True
|
||||||
|
@ -36,6 +37,9 @@ PROTOCOL_SECPLUSV2 = "secplusv2"
|
||||||
PROTOCOL_DRYCONTACT = "drycontact"
|
PROTOCOL_DRYCONTACT = "drycontact"
|
||||||
SUPPORTED_PROTOCOLS = [PROTOCOL_SECPLUSV1, PROTOCOL_SECPLUSV2, PROTOCOL_DRYCONTACT]
|
SUPPORTED_PROTOCOLS = [PROTOCOL_SECPLUSV1, PROTOCOL_SECPLUSV2, PROTOCOL_DRYCONTACT]
|
||||||
|
|
||||||
|
DOOR_CLOSED = "door_closed"
|
||||||
|
DOOR_OPEN = "door_open"
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.Schema(
|
CONFIG_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.declare_id(RATGDO),
|
cv.GenerateID(): cv.declare_id(RATGDO),
|
||||||
|
@ -56,6 +60,12 @@ CONFIG_SCHEMA = cv.Schema(
|
||||||
cv.Optional(CONF_PROTOCOL, default=PROTOCOL_SECPLUSV2): vol.In(
|
cv.Optional(CONF_PROTOCOL, default=PROTOCOL_SECPLUSV2): vol.In(
|
||||||
SUPPORTED_PROTOCOLS
|
SUPPORTED_PROTOCOLS
|
||||||
),
|
),
|
||||||
|
cv.Optional(DOOR_CLOSED): cv.Any(
|
||||||
|
cv.none, cv.use_id(binary_sensor.BinarySensor)
|
||||||
|
),
|
||||||
|
cv.Optional(DOOR_OPEN): cv.Any(
|
||||||
|
cv.none, cv.use_id(binary_sensor.BinarySensor)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
).extend(cv.COMPONENT_SCHEMA)
|
).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
|
||||||
|
@ -104,3 +114,10 @@ async def to_code(config):
|
||||||
elif config[CONF_PROTOCOL] == PROTOCOL_DRYCONTACT:
|
elif config[CONF_PROTOCOL] == PROTOCOL_DRYCONTACT:
|
||||||
cg.add_define("PROTOCOL_DRYCONTACT")
|
cg.add_define("PROTOCOL_DRYCONTACT")
|
||||||
cg.add(var.init_protocol())
|
cg.add(var.init_protocol())
|
||||||
|
if DOOR_CLOSED in config and config[DOOR_CLOSED] is not None:
|
||||||
|
door_closed = await cg.get_variable(config[DOOR_CLOSED])
|
||||||
|
cg.add(var.set_door_closed_sensor(door_closed))
|
||||||
|
if DOOR_OPEN in config and config[DOOR_OPEN] is not None:
|
||||||
|
door_open = await cg.get_variable(config[DOOR_OPEN])
|
||||||
|
cg.add(var.set_door_open_sensor(door_open))
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define ESP_LOG1 ESP_LOGD
|
#define ESP_LOG1 ESP_LOGD
|
||||||
#define ESP_LOG2 ESP_LOGD
|
#define ESP_LOG2 ESP_LOGD
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/application.h"
|
||||||
#include "esphome/core/gpio.h"
|
#include "esphome/core/gpio.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ratgdo {
|
namespace ratgdo {
|
||||||
|
@ -48,6 +49,43 @@ namespace ratgdo {
|
||||||
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
|
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->door_open_sensor_ != nullptr) {
|
||||||
|
this->door_open_sensor_->add_on_state_callback([=](bool state) {
|
||||||
|
ESP_LOGD(TAG, "Door open sensor: %d", state);
|
||||||
|
if (state) {
|
||||||
|
// this->sensor_door_state_ = DoorState::OPEN;
|
||||||
|
this->received(DoorState::OPEN);
|
||||||
|
} else {
|
||||||
|
this->received(DoorState::CLOSING);
|
||||||
|
if (*this->closing_duration != 0 && this->door_closed_sensor_ != nullptr) {
|
||||||
|
this->set_timeout((*this->closing_duration +1)*1000, [=] {
|
||||||
|
if (!this->door_closed_sensor_->state) {
|
||||||
|
this->received(DoorState::STOPPED);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (this->door_closed_sensor_ != nullptr) {
|
||||||
|
this->door_closed_sensor_->add_on_state_callback([=](bool state) {
|
||||||
|
ESP_LOGD(TAG, "Door closed sensor: %d", state);
|
||||||
|
if (state) {
|
||||||
|
// this->sensor_door_state_ = DoorState::OPEN;
|
||||||
|
this->received(DoorState::CLOSED);
|
||||||
|
} else {
|
||||||
|
this->received(DoorState::OPENING);
|
||||||
|
if (*this->opening_duration != 0 && this->door_open_sensor_ != nullptr) {
|
||||||
|
this->set_timeout((*this->opening_duration + 1)*1000, [=] {
|
||||||
|
if (!this->door_open_sensor_->state) {
|
||||||
|
this->received(DoorState::STOPPED);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_);
|
this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_);
|
||||||
|
|
||||||
// many things happening at startup, use some delay for sync
|
// many things happening at startup, use some delay for sync
|
||||||
|
@ -304,7 +342,7 @@ namespace ratgdo {
|
||||||
{
|
{
|
||||||
ESP_LOG1(TAG, "Schedule position sync: delta %f, start position: %f, start moving: %d",
|
ESP_LOG1(TAG, "Schedule position sync: delta %f, start position: %f, start moving: %d",
|
||||||
this->door_move_delta, this->door_start_position, this->door_start_moving);
|
this->door_move_delta, this->door_start_position, this->door_start_moving);
|
||||||
auto duration = this->door_move_delta > 0 ? *this->opening_duration : *this->closing_duration;
|
auto duration = this->door_move_delta * (this->door_move_delta > 0 ? *this->opening_duration : *this->closing_duration);
|
||||||
if (duration == 0) {
|
if (duration == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
class InternalGPIOPin;
|
class InternalGPIOPin;
|
||||||
|
namespace binary_sensor {
|
||||||
|
class BinarySensor;
|
||||||
|
}
|
||||||
namespace ratgdo {
|
namespace ratgdo {
|
||||||
|
|
||||||
class RATGDOComponent;
|
class RATGDOComponent;
|
||||||
|
@ -91,6 +94,9 @@ namespace ratgdo {
|
||||||
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }
|
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }
|
||||||
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }
|
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }
|
||||||
|
|
||||||
|
void set_door_open_sensor(binary_sensor::BinarySensor* sensor) { this->door_open_sensor_ = sensor; }
|
||||||
|
void set_door_closed_sensor(binary_sensor::BinarySensor* sensor) { this->door_closed_sensor_ = sensor; }
|
||||||
|
|
||||||
Result call_protocol(Args args);
|
Result call_protocol(Args args);
|
||||||
|
|
||||||
void received(const DoorState door_state);
|
void received(const DoorState door_state);
|
||||||
|
@ -171,6 +177,10 @@ namespace ratgdo {
|
||||||
protocol::Protocol* protocol_;
|
protocol::Protocol* protocol_;
|
||||||
bool obstruction_from_status_ { false };
|
bool obstruction_from_status_ { false };
|
||||||
|
|
||||||
|
DoorState sensor_door_state_ { DoorState::UNKNOWN };
|
||||||
|
binary_sensor::BinarySensor* door_open_sensor_;
|
||||||
|
binary_sensor::BinarySensor* door_closed_sensor_;
|
||||||
|
|
||||||
InternalGPIOPin* output_gdo_pin_;
|
InternalGPIOPin* output_gdo_pin_;
|
||||||
InternalGPIOPin* input_gdo_pin_;
|
InternalGPIOPin* input_gdo_pin_;
|
||||||
InternalGPIOPin* input_obst_pin_;
|
InternalGPIOPin* input_obst_pin_;
|
||||||
|
|
Loading…
Reference in New Issue