ESPHome  2024.12.2
mqtt_sensor.cpp
Go to the documentation of this file.
1 #include <cinttypes>
2 #include "mqtt_sensor.h"
3 #include "esphome/core/log.h"
4 
5 #include "mqtt_const.h"
6 
7 #ifdef USE_MQTT
8 #ifdef USE_SENSOR
9 
10 #ifdef USE_DEEP_SLEEP
12 #endif
13 
14 namespace esphome {
15 namespace mqtt {
16 
17 static const char *const TAG = "mqtt.sensor";
18 
19 using namespace esphome::sensor;
20 
22 
24  this->sensor_->add_on_state_callback([this](float state) { this->publish_state(state); });
25 }
26 
28  ESP_LOGCONFIG(TAG, "MQTT Sensor '%s':", this->sensor_->get_name().c_str());
29  if (this->get_expire_after() > 0) {
30  ESP_LOGCONFIG(TAG, " Expire After: %" PRIu32 "s", this->get_expire_after() / 1000);
31  }
32  LOG_MQTT_COMPONENT(true, false)
33 }
34 
35 std::string MQTTSensorComponent::component_type() const { return "sensor"; }
36 const EntityBase *MQTTSensorComponent::get_entity() const { return this->sensor_; }
37 
39  if (this->expire_after_.has_value())
40  return *this->expire_after_;
41  return 0;
42 }
43 void MQTTSensorComponent::set_expire_after(uint32_t expire_after) { this->expire_after_ = expire_after; }
45 
47  if (!this->sensor_->get_device_class().empty())
49 
50  if (!this->sensor_->get_unit_of_measurement().empty())
52 
53  if (this->get_expire_after() > 0)
54  root[MQTT_EXPIRE_AFTER] = this->get_expire_after() / 1000;
55 
56  if (this->sensor_->get_force_update())
57  root[MQTT_FORCE_UPDATE] = true;
58 
61 
62  config.command_topic = false;
63 }
65  if (this->sensor_->has_state()) {
66  return this->publish_state(this->sensor_->state);
67  } else {
68  return true;
69  }
70 }
72  if (mqtt::global_mqtt_client->is_publish_nan_as_none() && std::isnan(value))
73  return this->publish(this->get_state_topic_(), "None");
74  int8_t accuracy = this->sensor_->get_accuracy_decimals();
75  return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
76 }
77 std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }
78 
79 } // namespace mqtt
80 } // namespace esphome
81 
82 #endif
83 #endif // USE_MQTT
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:78
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
Definition: mqtt_sensor.cpp:21
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition: sensor.cpp:52
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
Definition: helpers.cpp:432
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
optional< uint32_t > expire_after_
Definition: mqtt_sensor.h:52
bool has_value() const
Definition: optional.h:87
void disable_expire_after()
Disable Home Assistant value expiry.
Definition: mqtt_sensor.cpp:44
bool command_topic
If the command topic should be included. Default to true.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
void setup() override
Override setup.
Definition: mqtt_sensor.cpp:23
MQTTClientComponent * global_mqtt_client
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: sensor.cpp:88
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
constexpr const char *const MQTT_STATE_CLASS
Definition: mqtt_const.h:211
void set_expire_after(uint32_t expire_after)
Setup an expiry, 0 disables it.
Definition: mqtt_sensor.cpp:43
std::string state_class_to_string(StateClass state_class)
Definition: sensor.cpp:9
constexpr const char *const MQTT_UNIT_OF_MEASUREMENT
Definition: mqtt_const.h:262
constexpr const char *const MQTT_FORCE_UPDATE
Definition: mqtt_const.h:90
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: entity_base.cpp:87
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:33
std::string unique_id() override
Definition: mqtt_sensor.cpp:77
constexpr const char * c_str() const
Definition: string_ref.h:68
constexpr const char *const MQTT_EXPIRE_AFTER
Definition: mqtt_const.h:80
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:25
constexpr const char *const MQTT_DEVICE_CLASS
Definition: mqtt_const.h:58
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
Base-class for all sensors.
Definition: sensor.h:57
uint32_t get_expire_after() const
Get the expire_after in milliseconds used for Home Assistant discovery, first checks override...
Definition: mqtt_sensor.cpp:38
std::string component_type() const override
Override for MQTTComponent, returns "sensor".
Definition: mqtt_sensor.cpp:35
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_sensor.cpp:46
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
const StringRef & get_name() const
Definition: entity_base.cpp:10
bool state
Definition: fan.h:34
const EntityBase * get_entity() const override
Definition: mqtt_sensor.cpp:36