diff --git a/esphome/components/sim800l/__init__.py b/esphome/components/sim800l/__init__.py index 762e045598..0c4215d8e4 100644 --- a/esphome/components/sim800l/__init__.py +++ b/esphome/components/sim800l/__init__.py @@ -17,6 +17,7 @@ Sim800LReceivedMessageTrigger = sim800l_ns.class_('Sim800LReceivedMessageTrigger # Actions Sim800LSendSmsAction = sim800l_ns.class_('Sim800LSendSmsAction', automation.Action) +Sim800LDialAction = sim800l_ns.class_('Sim800LDialAction', automation.Action) CONF_ON_SMS_RECEIVED = 'on_sms_received' CONF_RECIPIENT = 'recipient' @@ -57,3 +58,18 @@ def sim800l_send_sms_to_code(config, action_id, template_arg, args): template_ = yield cg.templatable(config[CONF_MESSAGE], args, cg.std_string) cg.add(var.set_message(template_)) yield var + + +SIM800L_DIAL_SCHEMA = cv.Schema({ + cv.GenerateID(): cv.use_id(Sim800LComponent), + cv.Required(CONF_RECIPIENT): cv.templatable(cv.string_strict), +}) + + +@automation.register_action('sim800l.dial', Sim800LDialAction, SIM800L_DIAL_SCHEMA) +def sim800l_dial_to_code(config, action_id, template_arg, args): + paren = yield cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = yield cg.templatable(config[CONF_RECIPIENT], args, cg.std_string) + cg.add(var.set_recipient(template_)) + yield var diff --git a/esphome/components/sim800l/sim800l.cpp b/esphome/components/sim800l/sim800l.cpp index 9f8c733fa9..ac4c8fc2d2 100644 --- a/esphome/components/sim800l/sim800l.cpp +++ b/esphome/components/sim800l/sim800l.cpp @@ -20,6 +20,9 @@ void Sim800LComponent::update() { if (this->registered_ && this->send_pending_) { this->send_cmd_("AT+CSCS=\"GSM\""); this->state_ = STATE_SENDINGSMS1; + } else if (this->registered_ && this->dial_pending_) { + this->send_cmd_("AT+CSCS=\"GSM\""); + this->state_ = STATE_DIALING1; } else { this->send_cmd_("AT"); this->state_ = STATE_CHECK_AT; @@ -212,6 +215,23 @@ void Sim800LComponent::parse_cmd_(std::string message) { this->expect_ack_ = true; } break; + case STATE_DIALING1: + this->send_cmd_("ATD" + this->recipient_ + ';'); + this->state_ = STATE_DIALING2; + break; + case STATE_DIALING2: + if (message == "OK") { + // Dialing + ESP_LOGD(TAG, "Dialing: '%s'", this->recipient_.c_str()); + this->state_ = STATE_INIT; + this->dial_pending_ = false; + } else { + this->registered_ = false; + this->state_ = STATE_INIT; + this->send_cmd_("AT+CMEE=2"); + this->write(26); + } + break; default: ESP_LOGD(TAG, "Unhandled: %s - %d", message.c_str(), this->state_); break; @@ -259,6 +279,12 @@ void Sim800LComponent::dump_config() { ESP_LOGCONFIG(TAG, "SIM800L:"); ESP_LOGCONFIG(TAG, " RSSI: %d dB", this->rssi_); } +void Sim800LComponent::dial(std::string recipient) { + ESP_LOGD(TAG, "Dialing %s", recipient.c_str()); + this->recipient_ = recipient; + this->dial_pending_ = true; + this->update(); +} } // namespace sim800l } // namespace esphome diff --git a/esphome/components/sim800l/sim800l.h b/esphome/components/sim800l/sim800l.h index 696eb8890f..f8ccf88977 100644 --- a/esphome/components/sim800l/sim800l.h +++ b/esphome/components/sim800l/sim800l.h @@ -29,7 +29,9 @@ enum State { STATE_RECEIVEDSMS, STATE_DELETEDSMS, STATE_DISABLE_ECHO, - STATE_PARSE_SMS_OK + STATE_PARSE_SMS_OK, + STATE_DIALING1, + STATE_DIALING2 }; class Sim800LComponent : public uart::UARTDevice, public PollingComponent { @@ -42,6 +44,7 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { this->callback_.add(std::move(callback)); } void send_sms(std::string recipient, std::string message); + void dial(std::string recipient); protected: void send_cmd_(std::string); @@ -60,6 +63,7 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { std::string recipient_; std::string outgoing_message_; bool send_pending_; + bool dial_pending_; CallbackManager callback_; }; @@ -88,5 +92,19 @@ template class Sim800LSendSmsAction : public Action { Sim800LComponent *parent_; }; +template class Sim800LDialAction : public Action { + public: + Sim800LDialAction(Sim800LComponent *parent) : parent_(parent) {} + TEMPLATABLE_VALUE(std::string, recipient) + + void play(Ts... x) { + auto recipient = this->recipient_.value(x...); + this->parent_->dial(recipient); + } + + protected: + Sim800LComponent *parent_; +}; + } // namespace sim800l } // namespace esphome diff --git a/tests/test3.yaml b/tests/test3.yaml index b38a7414b8..46b2d6b3a0 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -842,6 +842,8 @@ sim800l: - sim800l.send_sms: message: 'hello you' recipient: '+1234' + - sim800l.dial: + recipient: '+1234' dfplayer: on_finished_playback: