Added hoermann_door.stop_polling and .start_polling as automations should that need arise somehow!

This commit is contained in:
2025-10-07 13:15:04 +02:00
parent 3b88f1e988
commit 273cd9e434
3 changed files with 73 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import pins from esphome import pins, automation
from esphome.components import uart from esphome.components import uart
from esphome.const import ( from esphome.const import (
CONF_ID CONF_ID
@@ -11,6 +11,8 @@ DEPENDENCIES=["uart"]
hoermann_door_ns = cg.esphome_ns.namespace('hoermann_door') hoermann_door_ns = cg.esphome_ns.namespace('hoermann_door')
HoermannDoor = hoermann_door_ns.class_('HoermannMainComponent', cg.Component) 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_UART_ENTRY = "uart_connection"
CONF_TX_PIN = "tx_pin" CONF_TX_PIN = "tx_pin"
@@ -43,19 +45,29 @@ CONFIG_SCHEMA = cv.All(
validate_config 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): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) 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]) code = await cg.get_variable(config[CONF_UART_ENTRY])
cg.add(var.set_seriel_connection(code)) cg.add(var.set_seriel_connection(code))
code2 = await cg.gpio_pin_expression(config[CONF_TX_PIN]) 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] log_level = config[CONF_LOG_LEVEL]
cg.add(var.set_log_level(log_level)) cg.add(var.set_log_level(log_level))

View File

@@ -52,18 +52,36 @@ class HoermannMainComponent: public Component{
this->emulator = new HCIEmulator(this->_tx_on, this->_uart); this->emulator = new HCIEmulator(this->_tx_on, this->_uart);
this->emulator->setLogLevel(this->log_level); 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->_tx_on->digital_write(false);
this->set_continue_ = true; }
xTaskCreatePinnedToCore(
dispatcherFn, // Function to implement the task void start_polling() {
"ModBusTask", // Name of the task if(modBusTask == NULL){
10000, // Stack size in words this->_tx_on->digital_write(false);
this, // Task input parameter this->set_continue_ = true;
// 1, // Priority of the task xTaskCreatePinnedToCore(
configMAX_PRIORITIES - 1, dispatcherFn, // Function to implement the task
&modBusTask, // Task handle. "ModBusTask", // Name of the task
1 // Core where the task should run 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) void modBusPolling(void *parameter)
@@ -83,8 +101,10 @@ class HoermannMainComponent: public Component{
void dump_config() override { void dump_config() override {
ESP_LOGCONFIG(COMP_TAG, "hoermann_door_component:"); ESP_LOGCONFIG(COMP_TAG, "hoermann_door_component:");
ESP_LOGCONFIG(COMP_TAG, " UART: %d", this->_uart->get_baud_rate()); ESP_LOGCONFIG(COMP_TAG, " UART: ");
//LOG_PIN(TAG, this->_tx_on); 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); x->modBusPolling(arg);
} }
template<typename... Ts> class StopPollingAction: public Action<Ts...> {
public:
StopPollingAction(HoermannMainComponent *motor) : motor_(motor) {}
void play(Ts... x) override { this->motor_->stop_polling(); }
protected:
HoermannMainComponent *motor_;
};
template<typename... Ts> class StartPollingAction: public Action<Ts...> {
public:
StartPollingAction(HoermannMainComponent *motor) : motor_(motor) {}
void play(Ts... x) override { this->motor_->start_polling(); }
protected:
HoermannMainComponent *motor_;
};
} }
} }

View File

@@ -12,6 +12,9 @@ esp32:
ota: ota:
- platform: esphome - platform: esphome
password: !secret gd_passwd password: !secret gd_passwd
on_begin:
then:
- hoermann_door.stop_polling: door_controll_internal
api: api:
encryption: encryption:
@@ -44,8 +47,6 @@ uart:
data_bits: 8 # Standard, just defined for clarity data_bits: 8 # Standard, just defined for clarity
parity: EVEN # The chip uses even parity parity: EVEN # The chip uses even parity
stop_bits: 1 # Standard, just defined for clarity stop_bits: 1 # Standard, just defined for clarity
hoermann_door: hoermann_door:
id: door_controll_internal id: door_controll_internal