This commit is contained in:
J. Nick Koston 2023-06-05 19:57:06 -05:00
parent 2a1d2f053c
commit b15063763a
No known key found for this signature in database
2 changed files with 397 additions and 394 deletions

View File

@ -2,12 +2,13 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_ID from esphome.const import CONF_ID
from esphome import pins, automation from esphome import pins, automation
from esphome.components import uart
DEPENDENCIES = ["preferences"] DEPENDENCIES = ["preferences", "uart"]
ratgdo_ns = cg.esphome_ns.namespace("ratgdo") ratgdo_ns = cg.esphome_ns.namespace("ratgdo")
RATGDO = ratgdo_ns.class_("RATGDOComponent", cg.Component) RATGDO = ratgdo_ns.class_("RATGDOComponent", cg.Component, uart.UARTDevice)
CONF_OUTPUT_GDO = "output_gdo_pin" CONF_OUTPUT_GDO = "output_gdo_pin"
@ -46,7 +47,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_STATUS_DOOR, default=DEFAULT_STATUS_DOOR): pins.internal_gpio_input_pin_schema, cv.Optional(CONF_STATUS_DOOR, default=DEFAULT_STATUS_DOOR): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_STATUS_OBST, default=DEFAULT_STATUS_OBST): pins.internal_gpio_input_pin_schema, cv.Optional(CONF_STATUS_OBST, default=DEFAULT_STATUS_OBST): pins.internal_gpio_input_pin_schema,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA).extend(uart.UART_DEVICE_SCHEMA)
async def to_code(config): async def to_code(config):

View File

@ -88,7 +88,11 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::setup() class RATGDOComponent : public Component, public UARTDevice {
public:
RATGDOComponent(UARTComponent *parent) : UARTDevice(parent) {}
void setup()
{ {
this->pref_ = global_preferences->make_preference<int>(734874333U); this->pref_ = global_preferences->make_preference<int>(734874333U);
if (!this->pref_.load(&this->rollingCodeCounter)) { if (!this->pref_.load(&this->rollingCodeCounter)) {
@ -136,7 +140,7 @@ namespace ratgdo {
} }
void RATGDOComponent::loop() void loop()
{ {
ESP_LOGD(TAG, "loop rollingCodeCounter: %d", this->rollingCodeCounter); ESP_LOGD(TAG, "loop rollingCodeCounter: %d", this->rollingCodeCounter);
obstructionLoop(); obstructionLoop();
@ -146,7 +150,7 @@ namespace ratgdo {
//ESP_LOGD(TAG, "Door State: %s", this->doorState.c_str()); //ESP_LOGD(TAG, "Door State: %s", this->doorState.c_str());
} }
void RATGDOComponent::readRollingCode(uint8_t &door, uint8_t &light, uint8_t &lock, uint8_t &motion, uint8_t &obstruction){ void readRollingCode(uint8_t &door, uint8_t &light, uint8_t &lock, uint8_t &motion, uint8_t &obstruction){
uint32_t rolling = 0; uint32_t rolling = 0;
uint64_t fixed = 0; uint64_t fixed = 0;
uint32_t data = 0; uint32_t data = 0;
@ -179,7 +183,7 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::getRollingCode(const char* command) void getRollingCode(const char* command)
{ {
uint64_t id = 0x539; uint64_t id = 0x539;
@ -233,7 +237,7 @@ namespace ratgdo {
return; return;
} }
void RATGDOComponent::printRollingCode() void printRollingCode()
{ {
for (int i = 0; i < CODE_LENGTH; i++) { for (int i = 0; i < CODE_LENGTH; i++) {
if (this->txRollingCode[i] <= 0x0f) if (this->txRollingCode[i] <= 0x0f)
@ -243,7 +247,7 @@ namespace ratgdo {
} }
// handle changes to the dry contact state // handle changes to the dry contact state
void RATGDOComponent::dryContactLoop() void dryContactLoop()
{ {
if (this->store_.dryContactDoorOpen) { if (this->store_.dryContactDoorOpen) {
ESP_LOGD(TAG, "Dry Contact: open the door"); ESP_LOGD(TAG, "Dry Contact: open the door");
@ -265,9 +269,7 @@ namespace ratgdo {
} }
/*************************** OBSTRUCTION DETECTION ***************************/ /*************************** OBSTRUCTION DETECTION ***************************/
void obstructionLoop()
void RATGDOComponent::obstructionLoop()
{ {
long currentMillis = millis(); long currentMillis = millis();
static unsigned long lastMillis = 0; static unsigned long lastMillis = 0;
@ -301,7 +303,7 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::gdoStateLoop(){ void gdoStateLoop(){
if(!this->swSerial.available()) { if(!this->swSerial.available()) {
//ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin()); //ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin());
return; return;
@ -347,7 +349,7 @@ namespace ratgdo {
} }
void RATGDOComponent::statusUpdateLoop(){ void statusUpdateLoop(){
// initialize to unknown // initialize to unknown
static uint8_t previousDoorState = 0; static uint8_t previousDoorState = 0;
static uint8_t previousLightState = 2; static uint8_t previousLightState = 2;
@ -370,25 +372,25 @@ namespace ratgdo {
previousObstructionState = this->store_.obstructionState; previousObstructionState = this->store_.obstructionState;
} }
void RATGDOComponent::sendDoorStatus(){ void sendDoorStatus(){
ESP_LOGD(TAG, "Door state %d", this->store_.doorState); ESP_LOGD(TAG, "Door state %d", this->store_.doorState);
this->status_door_pin_->digital_write(this->store_.doorState == 1); this->status_door_pin_->digital_write(this->store_.doorState == 1);
} }
void RATGDOComponent::sendLightStatus(){ void sendLightStatus(){
ESP_LOGD(TAG, "Light state %d", this->store_.lightState); ESP_LOGD(TAG, "Light state %d", this->store_.lightState);
} }
void RATGDOComponent::sendLockStatus(){ void sendLockStatus(){
ESP_LOGD(TAG, "Lock state %d", this->store_.lockState); ESP_LOGD(TAG, "Lock state %d", this->store_.lockState);
} }
void RATGDOComponent::sendMotionStatus(){ void sendMotionStatus(){
ESP_LOGD(TAG, "Motion state %d", this->store_.motionState); ESP_LOGD(TAG, "Motion state %d", this->store_.motionState);
this->store_.motionState = 0; // reset motion state this->store_.motionState = 0; // reset motion state
} }
void RATGDOComponent::sendObstructionStatus(){ void sendObstructionStatus(){
ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState); ESP_LOGD(TAG, "Obstruction state %d", this->store_.obstructionState);
this->status_obst_pin_->digital_write(this->store_.obstructionState == 0); this->status_obst_pin_->digital_write(this->store_.obstructionState == 0);
} }
@ -402,7 +404,7 @@ namespace ratgdo {
* The opener requires a specific duration low/high pulse before it will accept * The opener requires a specific duration low/high pulse before it will accept
* a message * a message
*/ */
void RATGDOComponent::transmit(const unsigned char * payload) void transmit(const unsigned char * payload)
{ {
this->output_gdo_pin_->digital_write(true); // pull the line high for 1305 micros so the this->output_gdo_pin_->digital_write(true); // pull the line high for 1305 micros so the
// door opener responds to the message // door opener responds to the message
@ -413,7 +415,7 @@ namespace ratgdo {
this->swSerial.write(payload, CODE_LENGTH); this->swSerial.write(payload, CODE_LENGTH);
} }
void RATGDOComponent::sync() void sync()
{ {
getRollingCode("reboot1"); getRollingCode("reboot1");
transmit(this->txRollingCode); transmit(this->txRollingCode);
@ -442,7 +444,7 @@ namespace ratgdo {
this->pref_.save(&this->rollingCodeCounter); this->pref_.save(&this->rollingCodeCounter);
} }
void RATGDOComponent::openDoor() void openDoor()
{ {
if(this->doorStates[this->store_.doorState] == "open" || doorStates[this->store_.doorState] == "opening"){ if(this->doorStates[this->store_.doorState] == "open" || doorStates[this->store_.doorState] == "opening"){
ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]);
@ -451,7 +453,7 @@ namespace ratgdo {
toggleDoor(); toggleDoor();
} }
void RATGDOComponent::closeDoor() void closeDoor()
{ {
if(this->doorStates[this->store_.doorState] == "closed" || doorStates[this->store_.doorState] == "closing"){ if(this->doorStates[this->store_.doorState] == "closed" || doorStates[this->store_.doorState] == "closing"){
ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]); ESP_LOGD(TAG, "The door is already %s", this->doorStates[this->store_.doorState]);
@ -460,7 +462,7 @@ namespace ratgdo {
toggleDoor(); toggleDoor();
} }
void RATGDOComponent::stopDoor(){ void stopDoor(){
if(this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing"){ if(this->doorStates[this->store_.doorState] == "opening" || doorStates[this->store_.doorState] == "closing"){
toggleDoor(); toggleDoor();
}else{ }else{
@ -468,7 +470,7 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::toggleDoor() void toggleDoor()
{ {
getRollingCode("door1"); getRollingCode("door1");
transmit(this->txRollingCode); transmit(this->txRollingCode);
@ -479,7 +481,7 @@ namespace ratgdo {
} }
void RATGDOComponent::lightOn(){ void lightOn(){
if(this->lightStates[this->store_.lightState] == "on"){ if(this->lightStates[this->store_.lightState] == "on"){
ESP_LOGD(TAG, "already on"); ESP_LOGD(TAG, "already on");
}else{ }else{
@ -487,7 +489,7 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::lightOff(){ void lightOff(){
if(this->lightStates[this->store_.lightState] == "off"){ if(this->lightStates[this->store_.lightState] == "off"){
ESP_LOGD(TAG, "already off"); ESP_LOGD(TAG, "already off");
}else{ }else{
@ -495,12 +497,12 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::toggleLight(){ void toggleLight(){
sendCommand("light"); sendCommand("light");
} }
// Lock functions // Lock functions
void RATGDOComponent::lock(){ void lock(){
if(this->lockStates[this->store_.lockState] == "locked"){ if(this->lockStates[this->store_.lockState] == "locked"){
ESP_LOGD(TAG, "already locked"); ESP_LOGD(TAG, "already locked");
}else{ }else{
@ -508,7 +510,7 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::unlock(){ void unlock(){
if(this->lockStates[this->store_.lockState] == "unlocked"){ if(this->lockStates[this->store_.lockState] == "unlocked"){
ESP_LOGD(TAG, "already unlocked"); ESP_LOGD(TAG, "already unlocked");
}else{ }else{
@ -516,15 +518,15 @@ namespace ratgdo {
} }
} }
void RATGDOComponent::toggleLock(){ void toggleLock(){
sendCommand("lock"); sendCommand("lock");
} }
void RATGDOComponent::sendCommand(const char* command){ void sendCommand(const char* command){
getRollingCode(command); getRollingCode(command);
transmit(this->txRollingCode); transmit(this->txRollingCode);
this->pref_.save(&this->rollingCodeCounter); this->pref_.save(&this->rollingCodeCounter);
} }
}
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome