From 273cd9e43443db76a3c2c22b6e0e3060458750b6 Mon Sep 17 00:00:00 2001 From: Nicolas Bachschwell Date: Tue, 7 Oct 2025 13:15:04 +0200 Subject: [PATCH] Added hoermann_door.stop_polling and .start_polling as automations should that need arise somehow! --- external_components/hoermann_door/__init__.py | 24 +++++-- external_components/hoermann_door/hoermann.h | 65 +++++++++++++++---- garage1.yaml | 5 +- 3 files changed, 73 insertions(+), 21 deletions(-) diff --git a/external_components/hoermann_door/__init__.py b/external_components/hoermann_door/__init__.py index ae6e126..2f9c11a 100644 --- a/external_components/hoermann_door/__init__.py +++ b/external_components/hoermann_door/__init__.py @@ -1,6 +1,6 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome import pins +from esphome import pins, automation from esphome.components import uart from esphome.const import ( CONF_ID @@ -11,6 +11,8 @@ DEPENDENCIES=["uart"] hoermann_door_ns = cg.esphome_ns.namespace('hoermann_door') HoermannDoor = hoermann_door_ns.class_('HoermannMainComponent', cg.Component) +StopPolling = hoermann_door_ns.class_('StopPollingAction', automation.Action) +StartPolling = hoermann_door_ns.class_('StartPollingAction', automation.Action) CONF_UART_ENTRY = "uart_connection" CONF_TX_PIN = "tx_pin" @@ -43,19 +45,29 @@ CONFIG_SCHEMA = cv.All( validate_config ) +ACTION_SCHEMA = automation.maybe_simple_id({ + cv.GenerateID(): cv.use_id(HoermannDoor) +}) + +@automation.register_action("hoermann_door.stop_polling", StopPolling, ACTION_SCHEMA) +async def stop_polling(config, action_id, template_arg, args): + parent = await cg.get_variable(config[CONF_ID]) + return cg.new_Pvariable(action_id, template_arg, parent) + +@automation.register_action("hoermann_door.start_polling", StartPolling, ACTION_SCHEMA) +async def start_polling(config, action_id, template_arg, args): + parent = await cg.get_variable(config[CONF_ID]) + return cg.new_Pvariable(action_id, template_arg, parent) + async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) - #yield mqtt.register_mqtt_component(var, config) - - #btnReset = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN]) - #cg.add(var.set_input_pin(btnReset)) code = await cg.get_variable(config[CONF_UART_ENTRY]) cg.add(var.set_seriel_connection(code)) code2 = await cg.gpio_pin_expression(config[CONF_TX_PIN]) - cg.add(var.set_tx_on_pin(code2)) # Needs be configured before sensor + cg.add(var.set_tx_on_pin(code2)) log_level = config[CONF_LOG_LEVEL] cg.add(var.set_log_level(log_level)) \ No newline at end of file diff --git a/external_components/hoermann_door/hoermann.h b/external_components/hoermann_door/hoermann.h index 701c607..cef8678 100644 --- a/external_components/hoermann_door/hoermann.h +++ b/external_components/hoermann_door/hoermann.h @@ -52,18 +52,36 @@ class HoermannMainComponent: public Component{ this->emulator = new HCIEmulator(this->_tx_on, this->_uart); this->emulator->setLogLevel(this->log_level); + this->start_polling(); + } + + void stop_polling(){ + this->set_continue_ = false; + if(modBusTask != NULL){ + // Wait for the task to be deleted + while(eTaskGetState(modBusTask) != eDeleted){ + vTaskDelay(1); + } + modBusTask = NULL; + } this->_tx_on->digital_write(false); - this->set_continue_ = true; - xTaskCreatePinnedToCore( - dispatcherFn, // Function to implement the task - "ModBusTask", // Name of the task - 10000, // Stack size in words - this, // Task input parameter - // 1, // Priority of the task - configMAX_PRIORITIES - 1, - &modBusTask, // Task handle. - 1 // Core where the task should run - ); + } + + void start_polling() { + if(modBusTask == NULL){ + this->_tx_on->digital_write(false); + this->set_continue_ = true; + xTaskCreatePinnedToCore( + dispatcherFn, // Function to implement the task + "ModBusTask", // Name of the task + 10000, // Stack size in words + this, // Task input parameter + // 1, // Priority of the task + configMAX_PRIORITIES - 1, + &modBusTask, // Task handle. + 1 // Core where the task should run + ); + } } void modBusPolling(void *parameter) @@ -83,8 +101,10 @@ class HoermannMainComponent: public Component{ void dump_config() override { ESP_LOGCONFIG(COMP_TAG, "hoermann_door_component:"); - ESP_LOGCONFIG(COMP_TAG, " UART: %d", this->_uart->get_baud_rate()); - //LOG_PIN(TAG, this->_tx_on); + ESP_LOGCONFIG(COMP_TAG, " UART: "); + ESP_LOGCONFIG(COMP_TAG, " UART is configured"); + LOG_PIN(" TX_ON_PIN", this->_tx_on); + ESP_LOGCONFIG(COMP_TAG, " Log Level: %d", this->log_level); } }; @@ -94,6 +114,25 @@ void dispatcherFn(void *arg) x->modBusPolling(arg); } +template class StopPollingAction: public Action { + public: + StopPollingAction(HoermannMainComponent *motor) : motor_(motor) {} + + void play(Ts... x) override { this->motor_->stop_polling(); } + + protected: + HoermannMainComponent *motor_; +}; +template class StartPollingAction: public Action { + public: + StartPollingAction(HoermannMainComponent *motor) : motor_(motor) {} + + void play(Ts... x) override { this->motor_->start_polling(); } + + protected: + HoermannMainComponent *motor_; +}; + } } \ No newline at end of file diff --git a/garage1.yaml b/garage1.yaml index 3f309e8..b123a80 100644 --- a/garage1.yaml +++ b/garage1.yaml @@ -12,6 +12,9 @@ esp32: ota: - platform: esphome password: !secret gd_passwd + on_begin: + then: + - hoermann_door.stop_polling: door_controll_internal api: encryption: @@ -44,8 +47,6 @@ uart: data_bits: 8 # Standard, just defined for clarity parity: EVEN # The chip uses even parity stop_bits: 1 # Standard, just defined for clarity - - hoermann_door: id: door_controll_internal