diff --git a/esphome/components/modem/switch/gnss_switch.cpp b/esphome/components/modem/switch/gnss_switch.cpp index 04612624ad..672a8fe7e0 100644 --- a/esphome/components/modem/switch/gnss_switch.cpp +++ b/esphome/components/modem/switch/gnss_switch.cpp @@ -35,15 +35,17 @@ optional GnssSwitch::get_modem_gnss_state() { optional gnss_state = nullopt; auto at_command_result = global_modem_component->send_at(this->command_ + "?"); if (at_command_result) { - std::string modem_state = at_command_result.result; + std::string modem_state = at_command_result.output; std::string delimiter = ": "; std::size_t pos = modem_state.find(delimiter); if (pos != std::string::npos) { pos += delimiter.length(); if (modem_state[pos] == '1') { - return true; + gnss_state = true; } else if (modem_state[pos] == '0') { - return false; + gnss_state = false; + if (!gnss_state.value()) { + } } } } @@ -55,13 +57,24 @@ void GnssSwitch::dump_config() { LOG_SWITCH("", "Modem GNSS Switch", this); } void GnssSwitch::setup() { this->state = this->get_initial_state_with_restore_mode().value_or(false); } void GnssSwitch::loop() { + static uint32_t next_loop_millis = millis(); + + if ((millis() < next_loop_millis)) { + // some commands need some delay + yield(); + return; + } + if (!this->modem_state_.has_value()) { this->modem_state_ = this->get_modem_gnss_state(); + next_loop_millis = millis() + 5000; // soft delay } else { - if ((this->state != this->modem_state_)) { + if ((this->state != this->modem_state_.value())) { + ESP_LOGI(TAG, "gnss switch state: %d, modem state: %d", this->state, this->modem_state_.value()); if (global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0"))) { - this->modem_state_ = this->state; + this->modem_state_ = nullopt; this->publish_state(this->state); + next_loop_millis = millis() + 5000; // soft delay } } }