From 113232ebb6d7c0109f483d5cb0992739a8c416fd Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Tue, 15 Feb 2022 01:01:50 -0300 Subject: [PATCH] add sim800l diagnostics (#3136) --- esphome/components/sim800l/__init__.py | 6 +++- esphome/components/sim800l/binary_sensor.py | 36 +++++++++++++++++++++ esphome/components/sim800l/sensor.py | 33 +++++++++++++++++++ esphome/components/sim800l/sim800l.cpp | 34 +++++++++++++++---- esphome/components/sim800l/sim800l.h | 24 +++++++++++++- 5 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 esphome/components/sim800l/binary_sensor.py create mode 100644 esphome/components/sim800l/sensor.py diff --git a/esphome/components/sim800l/__init__.py b/esphome/components/sim800l/__init__.py index 4143627084..564b685b37 100644 --- a/esphome/components/sim800l/__init__.py +++ b/esphome/components/sim800l/__init__.py @@ -1,7 +1,10 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import automation -from esphome.const import CONF_ID, CONF_TRIGGER_ID +from esphome.const import ( + CONF_ID, + CONF_TRIGGER_ID, +) from esphome.components import uart DEPENDENCIES = ["uart"] @@ -20,6 +23,7 @@ Sim800LReceivedMessageTrigger = sim800l_ns.class_( Sim800LSendSmsAction = sim800l_ns.class_("Sim800LSendSmsAction", automation.Action) Sim800LDialAction = sim800l_ns.class_("Sim800LDialAction", automation.Action) +CONF_SIM800L_ID = "sim800l_id" CONF_ON_SMS_RECEIVED = "on_sms_received" CONF_RECIPIENT = "recipient" CONF_MESSAGE = "message" diff --git a/esphome/components/sim800l/binary_sensor.py b/esphome/components/sim800l/binary_sensor.py new file mode 100644 index 0000000000..7cee04374b --- /dev/null +++ b/esphome/components/sim800l/binary_sensor.py @@ -0,0 +1,36 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import binary_sensor +from esphome.const import ( + CONF_DEVICE_CLASS, + CONF_ENTITY_CATEGORY, + DEVICE_CLASS_CONNECTIVITY, + ENTITY_CATEGORY_DIAGNOSTIC, +) +from . import CONF_SIM800L_ID, Sim800LComponent + +DEPENDENCIES = ["sim800l"] + +CONF_REGISTERED = "registered" + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_SIM800L_ID): cv.use_id(Sim800LComponent), + cv.Optional(CONF_REGISTERED): binary_sensor.BINARY_SENSOR_SCHEMA.extend( + { + cv.Optional( + CONF_DEVICE_CLASS, default=DEVICE_CLASS_CONNECTIVITY + ): binary_sensor.device_class, + cv.Optional( + CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_DIAGNOSTIC + ): cv.entity_category, + } + ), +} + + +async def to_code(config): + sim800l_component = await cg.get_variable(config[CONF_SIM800L_ID]) + + if CONF_REGISTERED in config: + sens = await binary_sensor.new_binary_sensor(config[CONF_REGISTERED]) + cg.add(sim800l_component.set_registered_binary_sensor(sens)) diff --git a/esphome/components/sim800l/sensor.py b/esphome/components/sim800l/sensor.py new file mode 100644 index 0000000000..156bd6a040 --- /dev/null +++ b/esphome/components/sim800l/sensor.py @@ -0,0 +1,33 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import sensor +from esphome.const import ( + DEVICE_CLASS_SIGNAL_STRENGTH, + ENTITY_CATEGORY_DIAGNOSTIC, + STATE_CLASS_MEASUREMENT, + UNIT_DECIBEL_MILLIWATT, +) +from . import CONF_SIM800L_ID, Sim800LComponent + +DEPENDENCIES = ["sim800l"] + +CONF_RSSI = "rssi" + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_SIM800L_ID): cv.use_id(Sim800LComponent), + cv.Optional(CONF_RSSI): sensor.sensor_schema( + unit_of_measurement=UNIT_DECIBEL_MILLIWATT, + accuracy_decimals=0, + device_class=DEVICE_CLASS_SIGNAL_STRENGTH, + state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), +} + + +async def to_code(config): + sim800l_component = await cg.get_variable(config[CONF_SIM800L_ID]) + + if CONF_RSSI in config: + sens = await sensor.new_sensor(config[CONF_RSSI]) + cg.add(sim800l_component.set_rssi_sensor(sens)) diff --git a/esphome/components/sim800l/sim800l.cpp b/esphome/components/sim800l/sim800l.cpp index eb6d62ca33..709e241491 100644 --- a/esphome/components/sim800l/sim800l.cpp +++ b/esphome/components/sim800l/sim800l.cpp @@ -117,7 +117,7 @@ void Sim800LComponent::parse_cmd_(std::string message) { this->state_ = STATE_CREG; } } - this->registered_ = registered; + set_registered_(registered); break; } case STATE_CSQ: @@ -128,8 +128,17 @@ void Sim800LComponent::parse_cmd_(std::string message) { if (message.compare(0, 5, "+CSQ:") == 0) { size_t comma = message.find(',', 6); if (comma != 6) { - this->rssi_ = parse_number(message.substr(6, comma - 6)).value_or(0); - ESP_LOGD(TAG, "RSSI: %d", this->rssi_); + int rssi = parse_number(message.substr(6, comma - 6)).value_or(0); + +#ifdef USE_SENSOR + if (this->rssi_sensor_ != nullptr) { + this->rssi_sensor_->publish_state(rssi); + } else { + ESP_LOGD(TAG, "RSSI: %d", rssi); + } +#else + ESP_LOGD(TAG, "RSSI: %d", rssi); +#endif } } this->expect_ack_ = true; @@ -201,7 +210,7 @@ void Sim800LComponent::parse_cmd_(std::string message) { this->write(26); this->state_ = STATE_SENDINGSMS3; } else { - this->registered_ = false; + set_registered_(false); this->state_ = STATE_INIT; this->send_cmd_("AT+CMEE=2"); this->write(26); @@ -226,7 +235,7 @@ void Sim800LComponent::parse_cmd_(std::string message) { this->state_ = STATE_INIT; this->dial_pending_ = false; } else { - this->registered_ = false; + this->set_registered_(false); this->state_ = STATE_INIT; this->send_cmd_("AT+CMEE=2"); this->write(26); @@ -277,7 +286,12 @@ void Sim800LComponent::send_sms(const std::string &recipient, const std::string } void Sim800LComponent::dump_config() { ESP_LOGCONFIG(TAG, "SIM800L:"); - ESP_LOGCONFIG(TAG, " RSSI: %d dB", this->rssi_); +#ifdef USE_BINARY_SENSOR + LOG_BINARY_SENSOR(" ", "Registered", this->registered_binary_sensor_); +#endif +#ifdef USE_SENSOR + LOG_SENSOR(" ", "Rssi", this->rssi_sensor_); +#endif } void Sim800LComponent::dial(const std::string &recipient) { ESP_LOGD(TAG, "Dialing %s", recipient.c_str()); @@ -286,5 +300,13 @@ void Sim800LComponent::dial(const std::string &recipient) { this->update(); } +void Sim800LComponent::set_registered_(bool registered) { + this->registered_ = registered; +#ifdef USE_BINARY_SENSOR + if (this->registered_binary_sensor_ != nullptr) + this->registered_binary_sensor_->publish_state(registered); +#endif +} + } // namespace sim800l } // namespace esphome diff --git a/esphome/components/sim800l/sim800l.h b/esphome/components/sim800l/sim800l.h index 4f738b0a8c..3535b96283 100644 --- a/esphome/components/sim800l/sim800l.h +++ b/esphome/components/sim800l/sim800l.h @@ -2,7 +2,14 @@ #include +#include "esphome/core/defines.h" #include "esphome/core/component.h" +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif #include "esphome/components/uart/uart.h" #include "esphome/core/automation.h" @@ -42,6 +49,14 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { void update() override; void loop() override; void dump_config() override; +#ifdef USE_BINARY_SENSOR + void set_registered_binary_sensor(binary_sensor::BinarySensor *registered_binary_sensor) { + registered_binary_sensor_ = registered_binary_sensor; + } +#endif +#ifdef USE_SENSOR + void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; } +#endif void add_on_sms_received_callback(std::function callback) { this->callback_.add(std::move(callback)); } @@ -51,7 +66,15 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { protected: void send_cmd_(const std::string &message); void parse_cmd_(std::string message); + void set_registered_(bool registered); +#ifdef USE_BINARY_SENSOR + binary_sensor::BinarySensor *registered_binary_sensor_{nullptr}; +#endif + +#ifdef USE_SENSOR + sensor::Sensor *rssi_sensor_{nullptr}; +#endif std::string sender_; char read_buffer_[SIM800L_READ_BUFFER_LENGTH]; size_t read_pos_{0}; @@ -60,7 +83,6 @@ class Sim800LComponent : public uart::UARTDevice, public PollingComponent { bool expect_ack_{false}; sim800l::State state_{STATE_IDLE}; bool registered_{false}; - int rssi_{0}; std::string recipient_; std::string outgoing_message_;