Add option to use MQTT abbreviations (#2641)

This commit is contained in:
Paul Monigatti
2021-10-31 15:34:08 +13:00
committed by GitHub
parent 7eee3cdc7f
commit 331a3ac387
14 changed files with 609 additions and 64 deletions
+7 -5
View File
@@ -1,6 +1,8 @@
#include "mqtt_sensor.h"
#include "esphome/core/log.h"
#include "mqtt_const.h"
#ifdef USE_MQTT
#ifdef USE_SENSOR
@@ -42,19 +44,19 @@ void MQTTSensorComponent::disable_expire_after() { this->expire_after_ = 0; }
void MQTTSensorComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryConfig &config) {
if (!this->sensor_->get_device_class().empty())
root["device_class"] = this->sensor_->get_device_class();
root[MQTT_DEVICE_CLASS] = this->sensor_->get_device_class();
if (!this->sensor_->get_unit_of_measurement().empty())
root["unit_of_measurement"] = this->sensor_->get_unit_of_measurement();
root[MQTT_UNIT_OF_MEASUREMENT] = this->sensor_->get_unit_of_measurement();
if (this->get_expire_after() > 0)
root["expire_after"] = this->get_expire_after() / 1000;
root[MQTT_EXPIRE_AFTER] = this->get_expire_after() / 1000;
if (this->sensor_->get_force_update())
root["force_update"] = true;
root[MQTT_FORCE_UPDATE] = true;
if (this->sensor_->get_state_class() != STATE_CLASS_NONE)
root["state_class"] = state_class_to_string(this->sensor_->get_state_class());
root[MQTT_STATE_CLASS] = state_class_to_string(this->sensor_->get_state_class());
config.command_topic = false;
}