mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
add sim800l diagnostics (#3136)
This commit is contained in:
parent
a13a1225b7
commit
113232ebb6
5 changed files with 125 additions and 8 deletions
|
@ -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"
|
||||
|
|
36
esphome/components/sim800l/binary_sensor.py
Normal file
36
esphome/components/sim800l/binary_sensor.py
Normal file
|
@ -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))
|
33
esphome/components/sim800l/sensor.py
Normal file
33
esphome/components/sim800l/sensor.py
Normal file
|
@ -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))
|
|
@ -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<int>(message.substr(6, comma - 6)).value_or(0);
|
||||
ESP_LOGD(TAG, "RSSI: %d", this->rssi_);
|
||||
int rssi = parse_number<int>(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
|
||||
|
|
|
@ -2,7 +2,14 @@
|
|||
|
||||
#include <utility>
|
||||
|
||||
#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<void(std::string, std::string)> 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_;
|
||||
|
|
Loading…
Reference in a new issue