From 00f54725e34d92981dd12e324de20542c0563344 Mon Sep 17 00:00:00 2001 From: oarcher Date: Tue, 13 Aug 2024 17:24:31 +0200 Subject: [PATCH] on_not_responding nw state cb --- esphome/components/modem/automation.h | 8 +++++--- esphome/components/modem/modem_component.cpp | 15 ++++++--------- esphome/components/modem/modem_component.h | 1 - 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/esphome/components/modem/automation.h b/esphome/components/modem/automation.h index 6c58114091..950b38c04a 100644 --- a/esphome/components/modem/automation.h +++ b/esphome/components/modem/automation.h @@ -10,11 +10,13 @@ namespace esphome { namespace modem { class ModemOnNotRespondingTrigger : public Trigger<> { - // not managed by `add_on_state_callback`, because we want to execute the callback - // as a single mode script (we have to know when the callback has ended) public: explicit ModemOnNotRespondingTrigger(ModemComponent *parent) { - parent->set_not_responding_cb(static_cast *>(this)); + parent->add_on_state_callback([this, parent](ModemComponentState old_state, ModemComponentState state) { + if (!parent->is_failed() && state == ModemComponentState::NOT_RESPONDING) { + this->trigger(); + } + }); } }; diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index a06d4de24e..c4de1b3f0a 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -297,16 +297,13 @@ void ModemComponent::loop() { this->status_clear_warning(); this->component_state_ = ModemComponentState::DISCONNECTED; } else { - if (!this->internal_state_.powered_on) { - this->poweron_(); - } else if (this->not_responding_cb_) { - if (!this->not_responding_cb_->is_action_running()) { - ESP_LOGD(TAG, "Calling 'on_not_responding' callback"); - this->not_responding_cb_->trigger(); - } - } else { - ESP_LOGW(TAG, "Modem not responding, and no 'on_not_responding' action defined"); + this->modem_lazy_init_(); + if (!this->modem_sync_()) { + ESP_LOGE(TAG, "Unable to recover modem"); } + // if (!this->internal_state_.powered_on) { + // this->poweron_(); + // } } } break; diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index 9407e66a22..d845b5132e 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -64,7 +64,6 @@ class ModemComponent : public Component { void set_status_pin(GPIOPin *status_pin) { this->status_pin_ = status_pin; } void set_pin_code(const std::string &pin_code) { this->pin_code_ = pin_code; } void set_apn(const std::string &apn) { this->apn_ = apn; } - void set_not_responding_cb(Trigger<> *not_responding_cb) { this->not_responding_cb_ = not_responding_cb; } void enable_cmux() { this->cmux_ = true; } void enable_debug(); void add_init_at_command(const std::string &cmd) { this->init_at_commands_.push_back(cmd); }