From c603f012e643befaf9d62dd397bbb31f90c8683f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 2 Jul 2024 17:59:37 -0700 Subject: [PATCH] chore: add clang pre-commit (#299) --- .pre-commit-config.yaml | 9 +++++++++ components/ratgdo/__init__.py | 4 ++-- components/ratgdo/common.h | 2 +- components/ratgdo/dry_contact.cpp | 23 ++++++++++++----------- components/ratgdo/dry_contact.h | 11 ++++++----- components/ratgdo/ratgdo.cpp | 12 ++++-------- components/ratgdo/ratgdo.h | 6 +++--- components/ratgdo/secplus1.h | 9 ++++----- components/ratgdo/secplus2.h | 8 ++++---- static/index.html | 8 ++++---- static/v25iboard_drycontact.yaml | 2 +- 11 files changed, 50 insertions(+), 44 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 596558e..b1339ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,3 +7,12 @@ repos: - id: trailing-whitespace - id: end-of-file-fixer - id: check-added-large-files +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: v18.1.8 + hooks: + - id: clang-format + types_or: + - "c++" + - "c" + - "cuda" + args: [-style=Webkit, -i] diff --git a/components/ratgdo/__init__.py b/components/ratgdo/__init__.py index f1ce1bc..5153cad 100644 --- a/components/ratgdo/__init__.py +++ b/components/ratgdo/__init__.py @@ -51,7 +51,7 @@ def validate_protocol(config): raise cv.Invalid("dry_contact_close_sensor and dry_contact_open_sensor are only valid when using protocol drycontact") # if config.get(CONF_PROTOCOL, None) == PROTOCOL_DRYCONTACT and CONF_DRY_CONTACT_OPEN_SENSOR not in config: # raise cv.Invalid("dry_contact_open_sensor is required when using protocol drycontact") - return config + return config CONFIG_SCHEMA = cv.All( cv.Schema( @@ -144,4 +144,4 @@ async def to_code(config): cg.add(var.set_discrete_open_pin(pin)) if CONF_DISCRETE_CLOSE_PIN in config and config[CONF_DISCRETE_CLOSE_PIN]: pin = await cg.gpio_pin_expression(config[CONF_DISCRETE_CLOSE_PIN]) - cg.add(var.set_discrete_close_pin(pin)) \ No newline at end of file + cg.add(var.set_discrete_close_pin(pin)) diff --git a/components/ratgdo/common.h b/components/ratgdo/common.h index e6571b5..3638cf4 100644 --- a/components/ratgdo/common.h +++ b/components/ratgdo/common.h @@ -1,4 +1,4 @@ #pragma once #define ESP_LOG1 ESP_LOGV -#define ESP_LOG2 ESP_LOGV \ No newline at end of file +#define ESP_LOG2 ESP_LOGV diff --git a/components/ratgdo/dry_contact.cpp b/components/ratgdo/dry_contact.cpp index 9860e16..8589a7c 100644 --- a/components/ratgdo/dry_contact.cpp +++ b/components/ratgdo/dry_contact.cpp @@ -2,10 +2,10 @@ #include "dry_contact.h" #include "ratgdo.h" +#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "esphome/core/gpio.h" #include "esphome/core/log.h" #include "esphome/core/scheduler.h" -#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" namespace esphome { namespace ratgdo { @@ -19,7 +19,7 @@ namespace ratgdo { this->scheduler_ = scheduler; this->tx_pin_ = tx_pin; this->rx_pin_ = rx_pin; - + this->open_limit_reached_ = 0; this->last_open_limit_ = 0; this->close_limit_reached_ = 0; @@ -58,18 +58,19 @@ namespace ratgdo { this->close_limit_reached_ = state; this->send_door_state(); } - - void DryContact::send_door_state(){ - if(this->open_limit_reached_){ + + void DryContact::send_door_state() + { + if (this->open_limit_reached_) { this->door_state_ = DoorState::OPEN; - }else if(this->close_limit_reached_){ + } else if (this->close_limit_reached_) { this->door_state_ = DoorState::CLOSED; - }else if(!this->close_limit_reached_ && !this->open_limit_reached_){ - if(this->last_close_limit_){ + } else if (!this->close_limit_reached_ && !this->open_limit_reached_) { + if (this->last_close_limit_) { this->door_state_ = DoorState::OPENING; } - if(this->last_open_limit_){ + if (this->last_open_limit_) { this->door_state_ = DoorState::CLOSING; } } @@ -102,14 +103,14 @@ namespace ratgdo { ESP_LOG1(TAG, "Door action: %s", DoorAction_to_string(action)); - if (action == DoorAction::OPEN){ + if (action == DoorAction::OPEN) { this->discrete_open_pin_->digital_write(1); this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] { this->discrete_open_pin_->digital_write(0); }); } - if (action == DoorAction::CLOSE){ + if (action == DoorAction::CLOSE) { this->discrete_close_pin_->digital_write(1); this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] { this->discrete_close_pin_->digital_write(0); diff --git a/components/ratgdo/dry_contact.h b/components/ratgdo/dry_contact.h index 09362b6..dee045d 100644 --- a/components/ratgdo/dry_contact.h +++ b/components/ratgdo/dry_contact.h @@ -1,9 +1,9 @@ #pragma once #include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial -#include "esphome/core/optional.h" -#include "esphome/core/gpio.h" #include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" +#include "esphome/core/gpio.h" +#include "esphome/core/optional.h" #include "callbacks.h" #include "observable.h" @@ -36,13 +36,15 @@ namespace ratgdo { void set_close_limit(bool state); void send_door_state(); - void set_discrete_open_pin(InternalGPIOPin* pin) { + void set_discrete_open_pin(InternalGPIOPin* pin) + { this->discrete_open_pin_ = pin; this->discrete_open_pin_->setup(); this->discrete_open_pin_->pin_mode(gpio::FLAG_OUTPUT); } - void set_discrete_close_pin(InternalGPIOPin* pin) { + void set_discrete_close_pin(InternalGPIOPin* pin) + { this->discrete_close_pin_ = pin; this->discrete_close_pin_->setup(); this->discrete_close_pin_->pin_mode(gpio::FLAG_OUTPUT); @@ -68,7 +70,6 @@ namespace ratgdo { bool last_open_limit_; bool close_limit_reached_; bool last_close_limit_; - }; } // namespace secplus1 diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index a9f49c8..af017d3 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -690,21 +690,17 @@ namespace ratgdo { void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor) { dry_contact_open_sensor_ = dry_contact_open_sensor; - dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) - { + dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) { this->protocol_->set_open_limit(sensor_value); - } - ); + }); } void RATGDOComponent::set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor) { dry_contact_close_sensor_ = dry_contact_close_sensor; - dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) - { + dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) { this->protocol_->set_close_limit(sensor_value); - } - ); + }); } } // namespace ratgdo diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index fbb92b1..fe7da6e 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -13,10 +13,10 @@ #pragma once +#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "esphome/core/component.h" #include "esphome/core/hal.h" #include "esphome/core/preferences.h" -#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "callbacks.h" #include "macros.h" @@ -95,8 +95,8 @@ namespace ratgdo { // dry contact methods void set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_); void set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_); - void set_discrete_open_pin(InternalGPIOPin* pin){ this->protocol_->set_discrete_open_pin(pin); } - void set_discrete_close_pin(InternalGPIOPin* pin){ this->protocol_->set_discrete_close_pin(pin); } + void set_discrete_open_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_open_pin(pin); } + void set_discrete_close_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_close_pin(pin); } Result call_protocol(Args args); diff --git a/components/ratgdo/secplus1.h b/components/ratgdo/secplus1.h index c2cd5d9..76e8dc9 100644 --- a/components/ratgdo/secplus1.h +++ b/components/ratgdo/secplus1.h @@ -97,11 +97,10 @@ namespace ratgdo { const Traits& traits() const { return this->traits_; } // methods not used by secplus1 - void set_open_limit(bool state){} - void set_close_limit(bool state){} - void set_discrete_open_pin(InternalGPIOPin* pin){} - void set_discrete_close_pin(InternalGPIOPin* pin){} - + void set_open_limit(bool state) { } + void set_close_limit(bool state) { } + void set_discrete_open_pin(InternalGPIOPin* pin) { } + void set_discrete_close_pin(InternalGPIOPin* pin) { } protected: void wall_panel_emulation(size_t index = 0); diff --git a/components/ratgdo/secplus2.h b/components/ratgdo/secplus2.h index 6ea02b9..bb79d91 100644 --- a/components/ratgdo/secplus2.h +++ b/components/ratgdo/secplus2.h @@ -102,10 +102,10 @@ namespace ratgdo { const Traits& traits() const { return this->traits_; } // methods not used by secplus2 - void set_open_limit(bool state){} - void set_close_limit(bool state){} - void set_discrete_open_pin(InternalGPIOPin* pin){} - void set_discrete_close_pin(InternalGPIOPin* pin){} + void set_open_limit(bool state) { } + void set_close_limit(bool state) { } + void set_discrete_open_pin(InternalGPIOPin* pin) { } + void set_discrete_close_pin(InternalGPIOPin* pin) { } protected: void increment_rolling_code_counter(int delta = 1); diff --git a/static/index.html b/static/index.html index d77e636..77e016c 100644 --- a/static/index.html +++ b/static/index.html @@ -289,25 +289,25 @@ const button = document.querySelector("esp-web-install-button"); var protocol = document.querySelector('input[name="protocol"]:checked').value; var hardware = document.querySelector('input[name="hardware"]:checked').value; - + if(protocol === "drycontact"){ document.querySelector("#wiring_diagram").src = "wiring_diagrams/dry_contact_diagram.png"; }else{ document.querySelector("#wiring_diagram").src = "wiring_diagrams/secplus_diagram.png"; } - + if(protocol !== "secplusv2" && (hardware === "v2board_esp8266_d1_mini" || hardware === "v2board_esp32_d1_mini")){ alert("ratgdo version 2.0 only works with Security + 2.0"); document.querySelector('input[name="protocol"][value="secplusv2"]').checked = true; return; } - + if(protocol === "secplusv2"){ protocol = ""; }else{ protocol = `_${protocol}`; } - + button.manifest = `${hardware}${protocol}-manifest.json`; }) ); diff --git a/static/v25iboard_drycontact.yaml b/static/v25iboard_drycontact.yaml index 431456b..d34a9ca 100644 --- a/static/v25iboard_drycontact.yaml +++ b/static/v25iboard_drycontact.yaml @@ -48,4 +48,4 @@ wifi: ap: logger: - level: DEBUG \ No newline at end of file + level: DEBUG