From 26abbbdee3df8c41620e5ac66cc7af0e28ecb826 Mon Sep 17 00:00:00 2001 From: Jochen Scheib Date: Fri, 20 Oct 2023 16:54:36 +0200 Subject: [PATCH] Allow setting rx and tx pin --- README.md | 26 +++++++++----------------- components/hcpbridge/__init__.py | 14 +++++++++++++- components/hcpbridge/hcpbridge.cpp | 6 +++++- components/hcpbridge/hcpbridge.h | 7 +++++++ components/hcpbridge/hoermann.cpp | 4 ++-- components/hcpbridge/hoermann.h | 4 ++-- example_hcpbridge.yaml | 5 +++++ secrets.yaml.example | 1 + 8 files changed, 44 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index e73248f..7222908 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ This is a esphome-based adaption of the HCPBridge. Credits for the initial devel ### Example esphome configuration -``` +```YAML esphome: name: hcpbridge libraries: - - emelianov/modbus-esp8266 + - emelianov/modbus-esp8266 # Required for communication with the modbus platformio_options: board_build.f_cpu: 240000000L board_build.flash_mode: qio @@ -26,6 +26,7 @@ esphome: external_components: source: github://mapero/esphome-hcpbridge + refresh: 0s # Ensure you always get the latest version esp32: board: adafruit_feather_esp32s3 @@ -33,7 +34,7 @@ esp32: type: arduino hcpbridge: - is_connected: + is_connected: # Sensor to display the connection status to the motor name: "HCPBridge Connected" cover: @@ -44,21 +45,12 @@ switch: - platform: hcpbridge name: Garage Light +# API to communicate with home assistant api: + encryption: + key: !secret api_key -web_server: - port: 80 - -wifi: - ssid: !secret wifi_ssid - password: !secret wifi_password - -# Enable logging -logger: - level: DEBUG - baud_rate: 9600 - -# Example configuration entry +# Enable OTA updates ota: safe_mode: true ``` @@ -96,7 +88,7 @@ You can find more information on the project here: [Hörmann garage door via MQT - [ ] Use esphome modbus component instead of own code - [ ] Map additional functions to esphome - [ ] Use callbacks instead of pollingComponent -- [ ] Expert options for the HCPBridge component (GPIOs ...) +- [x] Expert options for the HCPBridge component (GPIOs ...) # Contribute diff --git a/components/hcpbridge/__init__.py b/components/hcpbridge/__init__.py index cfedc4a..c5e8b3f 100644 --- a/components/hcpbridge/__init__.py +++ b/components/hcpbridge/__init__.py @@ -2,15 +2,19 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import binary_sensor +from esphome import pins from esphome.const import ( CONF_ID, DEVICE_CLASS_CONNECTIVITY, + CONF_RX_PIN, + CONF_TX_PIN, ) AUTO_LOAD = ["binary_sensor"] CONF_IS_CONNECTED = "is_connected" + hcpbridge_ns = cg.esphome_ns.namespace("hcpbridge") HCPBridge = hcpbridge_ns.class_("HCPBridge", cg.Component) @@ -22,6 +26,8 @@ cv.Required(CONF_IS_CONNECTED): binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_CONNECTIVITY, ), + cv.Optional(CONF_RX_PIN): pins.gpio_input_pin_schema, + cv.Optional(CONF_TX_PIN): pins.gpio_output_pin_schema, }) ) @@ -31,4 +37,10 @@ async def to_code(config): if CONF_IS_CONNECTED in config: sens = await binary_sensor.new_binary_sensor(config[CONF_IS_CONNECTED]) - cg.add(var.set_is_connected(sens)) \ No newline at end of file + cg.add(var.set_is_connected(sens)) + if CONF_RX_PIN in config: + rx_pin = await cg.gpio_pin_expression(config[CONF_RX_PIN]) + cg.add(var.set_rx_pin(rx_pin)) + if CONF_TX_PIN in config: + tx_pin = await cg.gpio_pin_expression(config[CONF_TX_PIN]) + cg.add(var.set_tx_pin(tx_pin)) \ No newline at end of file diff --git a/components/hcpbridge/hcpbridge.cpp b/components/hcpbridge/hcpbridge.cpp index f817ed1..d089931 100644 --- a/components/hcpbridge/hcpbridge.cpp +++ b/components/hcpbridge/hcpbridge.cpp @@ -8,8 +8,12 @@ namespace esphome void HCPBridge::setup() { this->is_connected_->publish_state(false); + + int8_t rx = this->rx_pin_ == nullptr ? 18 : this->rx_pin_->get_pin(); + int8_t tx = this->tx_pin_ == nullptr ? 17 : this->tx_pin_->get_pin(); + this->engine = &HoermannGarageEngine::getInstance(); - this->engine->setup(); + this->engine->setup(rx, tx); } void HCPBridge::update() { diff --git a/components/hcpbridge/hcpbridge.h b/components/hcpbridge/hcpbridge.h index cbabfd0..c925cb2 100644 --- a/components/hcpbridge/hcpbridge.h +++ b/components/hcpbridge/hcpbridge.h @@ -2,6 +2,8 @@ #include "esphome/core/component.h" #include "esphome/components/binary_sensor/binary_sensor.h" +#include "esphome/core/defines.h" +#include "esphome/core/hal.h" #include "hoermann.h" @@ -17,11 +19,16 @@ namespace esphome void update() override; void set_is_connected(binary_sensor::BinarySensor *is_connected) { this->is_connected_ = is_connected; } + void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; } + void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; } + HoermannGarageEngine* engine; protected: binary_sensor::BinarySensor *is_connected_; + InternalGPIOPin *tx_pin_; + InternalGPIOPin *rx_pin_; }; } } \ No newline at end of file diff --git a/components/hcpbridge/hoermann.cpp b/components/hcpbridge/hoermann.cpp index 2870358..c02c306 100644 --- a/components/hcpbridge/hoermann.cpp +++ b/components/hcpbridge/hoermann.cpp @@ -36,9 +36,9 @@ HoermannGarageEngine &HoermannGarageEngine::getInstance() return instance; } -void HoermannGarageEngine::setup() +void HoermannGarageEngine::setup(int8_t rx, int8_t tx) { - RS485.begin(57600, SERIAL_8E1, PIN_RXD, PIN_TXD); + RS485.begin(57600, SERIAL_8E1, rx, tx); mb.begin(&RS485); mb.slave(SLAVE_ID); diff --git a/components/hcpbridge/hoermann.h b/components/hcpbridge/hoermann.h index d790987..0386597 100644 --- a/components/hcpbridge/hoermann.h +++ b/components/hcpbridge/hoermann.h @@ -19,7 +19,7 @@ #define PIN_RXD 18 #else #define PIN_TXD 17 // UART 2 TXT - G17 -#define PIN_RXD 16 // UART 2 RXD - G16 +#define PIN_RXD 16 // UART 2 RXD - G16 #endif // workaround as my Supramatic did not Report the Status 0x0A when it's en vent Position @@ -108,7 +108,7 @@ class HoermannGarageEngine static HoermannGarageEngine& getInstance(); - void setup(); + void setup(int8_t rx, int8_t tx); void handleModbus(); Modbus::ResultCode onRequest(Modbus::FunctionCode fc, const Modbus::RequestData data); void setCommandValuesToRead(); diff --git a/example_hcpbridge.yaml b/example_hcpbridge.yaml index e4bb886..48105c7 100644 --- a/example_hcpbridge.yaml +++ b/example_hcpbridge.yaml @@ -12,6 +12,7 @@ esphome: external_components: source: github://mapero/esphome-hcpbridge + refresh: 0s esp32: board: adafruit_feather_esp32s3 @@ -21,6 +22,8 @@ esp32: hcpbridge: is_connected: name: "HCPBridge Connected" + rx_pin: 18 # optional, default=18 + tx_pin: 17 # optional, default=17 cover: - platform: hcpbridge @@ -31,6 +34,8 @@ switch: name: Garage Light api: + encryption: + key: !secret api_key web_server: port: 80 diff --git a/secrets.yaml.example b/secrets.yaml.example index 38475f5..d60400b 100644 --- a/secrets.yaml.example +++ b/secrets.yaml.example @@ -1,2 +1,3 @@ wifi_ssid: "" wifi_password: "" +api_key: "" \ No newline at end of file