ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mqtt_sensor.cpp
Go to the documentation of this file.
1#include <cinttypes>
2#include "mqtt_sensor.h"
3#include "esphome/core/log.h"
5
6#include "mqtt_const.h"
7
8#ifdef USE_MQTT
9#ifdef USE_SENSOR
10
11#ifdef USE_DEEP_SLEEP
13#endif
14
15namespace esphome::mqtt {
16
17ESPHOME_LOG_TAG(TAG, "mqtt.sensor");
18
19using namespace esphome::sensor;
20
22
24 this->sensor_->add_on_state_callback([this](float state) { this->publish_state(state); });
25}
26
28 LOG_MQTT_COMPONENT(true, false);
29 if (this->get_expire_after() > 0) {
30 ESP_LOGCONFIG(TAG, " Expire After: %" PRIu32 "s", this->get_expire_after() / 1000);
31 }
32}
33
35const EntityBase *MQTTSensorComponent::get_entity() const { return this->sensor_; }
36
38 if (this->expire_after_.has_value())
39 return *this->expire_after_;
40 return 0;
41}
42
44 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
45 if (this->sensor_->has_accuracy_decimals()) {
46 root[MQTT_SUGGESTED_DISPLAY_PRECISION] = this->sensor_->get_accuracy_decimals();
47 }
48
49 const auto unit_of_measurement = this->sensor_->get_unit_of_measurement_ref();
50 if (!unit_of_measurement.empty()) {
51 root[MQTT_UNIT_OF_MEASUREMENT] = unit_of_measurement;
52 }
53 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
54
55 if (this->get_expire_after() > 0)
56 root[MQTT_EXPIRE_AFTER] = this->get_expire_after() / 1000;
57
58 if (this->sensor_->get_force_update())
59 root[MQTT_FORCE_UPDATE] = true;
60
62 root[MQTT_STATE_CLASS] = reinterpret_cast<ProgmemStr>(state_class_to_string(this->sensor_->get_state_class()));
63 }
64
65 config.command_topic = false;
66}
68 if (this->sensor_->has_state()) {
69 return this->publish_state(this->sensor_->state);
70 } else {
71 return true;
72 }
73}
75 char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
76 return this->publish_float_(this->get_state_topic_to_(topic_buf), value, this->sensor_->get_accuracy_decimals());
77}
78
79} // namespace esphome::mqtt
80
81#endif
82#endif // USE_MQTT
StringRef get_unit_of_measurement_ref() const
bool has_state() const
bool publish_float_(StringRef topic, float value, int8_t accuracy)
Publish a float formatted to accuracy, or "None" for NaN when publish_nan_as_none is set.
StringRef get_state_topic_to_(std::span< char, MQTT_DEFAULT_TOPIC_MAX_LEN > buf) const
Get the MQTT state topic into a buffer (no heap allocation for non-lambda custom topics).
uint32_t get_expire_after() const
Get the expire_after in milliseconds used for Home Assistant discovery, first checks override.
optional< uint32_t > expire_after_
Definition mqtt_sensor.h:50
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
void setup() override
Override setup.
Base-class for all sensors.
Definition sensor.h:47
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:121
bool has_accuracy_decimals() const
Check if the accuracy in decimals has been manually set.
Definition sensor.h:56
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:140
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:59
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:45
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:70
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
MQTT_COMPONENT_TYPE(MQTTAlarmControlPanelComponent, "alarm_control_panel") const EntityBase *MQTTAlarmControlPanelComponent
const LogString * state_class_to_string(StateClass state_class)
Definition sensor.cpp:38
const __FlashStringHelper * ProgmemStr
Definition progmem.h:31
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.