mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Add dial support for sim800l component (#1558)
This commit is contained in:
		| @@ -17,6 +17,7 @@ Sim800LReceivedMessageTrigger = sim800l_ns.class_('Sim800LReceivedMessageTrigger | |||||||
|  |  | ||||||
| # Actions | # Actions | ||||||
| Sim800LSendSmsAction = sim800l_ns.class_('Sim800LSendSmsAction', automation.Action) | Sim800LSendSmsAction = sim800l_ns.class_('Sim800LSendSmsAction', automation.Action) | ||||||
|  | Sim800LDialAction = sim800l_ns.class_('Sim800LDialAction', automation.Action) | ||||||
|  |  | ||||||
| CONF_ON_SMS_RECEIVED = 'on_sms_received' | CONF_ON_SMS_RECEIVED = 'on_sms_received' | ||||||
| CONF_RECIPIENT = 'recipient' | 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) |     template_ = yield cg.templatable(config[CONF_MESSAGE], args, cg.std_string) | ||||||
|     cg.add(var.set_message(template_)) |     cg.add(var.set_message(template_)) | ||||||
|     yield var |     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 | ||||||
|   | |||||||
| @@ -20,6 +20,9 @@ void Sim800LComponent::update() { | |||||||
|     if (this->registered_ && this->send_pending_) { |     if (this->registered_ && this->send_pending_) { | ||||||
|       this->send_cmd_("AT+CSCS=\"GSM\""); |       this->send_cmd_("AT+CSCS=\"GSM\""); | ||||||
|       this->state_ = STATE_SENDINGSMS1; |       this->state_ = STATE_SENDINGSMS1; | ||||||
|  |     } else if (this->registered_ && this->dial_pending_) { | ||||||
|  |       this->send_cmd_("AT+CSCS=\"GSM\""); | ||||||
|  |       this->state_ = STATE_DIALING1; | ||||||
|     } else { |     } else { | ||||||
|       this->send_cmd_("AT"); |       this->send_cmd_("AT"); | ||||||
|       this->state_ = STATE_CHECK_AT; |       this->state_ = STATE_CHECK_AT; | ||||||
| @@ -212,6 +215,23 @@ void Sim800LComponent::parse_cmd_(std::string message) { | |||||||
|         this->expect_ack_ = true; |         this->expect_ack_ = true; | ||||||
|       } |       } | ||||||
|       break; |       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: |     default: | ||||||
|       ESP_LOGD(TAG, "Unhandled: %s - %d", message.c_str(), this->state_); |       ESP_LOGD(TAG, "Unhandled: %s - %d", message.c_str(), this->state_); | ||||||
|       break; |       break; | ||||||
| @@ -259,6 +279,12 @@ void Sim800LComponent::dump_config() { | |||||||
|   ESP_LOGCONFIG(TAG, "SIM800L:"); |   ESP_LOGCONFIG(TAG, "SIM800L:"); | ||||||
|   ESP_LOGCONFIG(TAG, "  RSSI: %d dB", this->rssi_); |   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 sim800l | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -29,7 +29,9 @@ enum State { | |||||||
|   STATE_RECEIVEDSMS, |   STATE_RECEIVEDSMS, | ||||||
|   STATE_DELETEDSMS, |   STATE_DELETEDSMS, | ||||||
|   STATE_DISABLE_ECHO, |   STATE_DISABLE_ECHO, | ||||||
|   STATE_PARSE_SMS_OK |   STATE_PARSE_SMS_OK, | ||||||
|  |   STATE_DIALING1, | ||||||
|  |   STATE_DIALING2 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| class Sim800LComponent : public uart::UARTDevice, public PollingComponent { | class Sim800LComponent : public uart::UARTDevice, public PollingComponent { | ||||||
| @@ -42,6 +44,7 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { | |||||||
|     this->callback_.add(std::move(callback)); |     this->callback_.add(std::move(callback)); | ||||||
|   } |   } | ||||||
|   void send_sms(std::string recipient, std::string message); |   void send_sms(std::string recipient, std::string message); | ||||||
|  |   void dial(std::string recipient); | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   void send_cmd_(std::string); |   void send_cmd_(std::string); | ||||||
| @@ -60,6 +63,7 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { | |||||||
|   std::string recipient_; |   std::string recipient_; | ||||||
|   std::string outgoing_message_; |   std::string outgoing_message_; | ||||||
|   bool send_pending_; |   bool send_pending_; | ||||||
|  |   bool dial_pending_; | ||||||
|  |  | ||||||
|   CallbackManager<void(std::string, std::string)> callback_; |   CallbackManager<void(std::string, std::string)> callback_; | ||||||
| }; | }; | ||||||
| @@ -88,5 +92,19 @@ template<typename... Ts> class Sim800LSendSmsAction : public Action<Ts...> { | |||||||
|   Sim800LComponent *parent_; |   Sim800LComponent *parent_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class Sim800LDialAction : public Action<Ts...> { | ||||||
|  |  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 sim800l | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -842,6 +842,8 @@ sim800l: | |||||||
|     - sim800l.send_sms: |     - sim800l.send_sms: | ||||||
|         message: 'hello you' |         message: 'hello you' | ||||||
|         recipient: '+1234' |         recipient: '+1234' | ||||||
|  |     - sim800l.dial: | ||||||
|  |         recipient: '+1234' | ||||||
|  |  | ||||||
| dfplayer: | dfplayer: | ||||||
|   on_finished_playback: |   on_finished_playback: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user