ESPHome 2025.5.0
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"
4
5#include "mqtt_const.h"
6
7#ifdef USE_MQTT
8#ifdef USE_SENSOR
9
10#ifdef USE_DEEP_SLEEP
12#endif
13
14namespace esphome {
15namespace mqtt {
16
17static const char *const 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 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
35std::string MQTTSensorComponent::component_type() const { return "sensor"; }
36const 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}
43void 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}
77std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }
78
79} // namespace mqtt
80} // namespace esphome
81
82#endif
83#endif // USE_MQTT
std::string get_device_class()
Get the device class, using the manual override if set.
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
uint32_t get_expire_after() const
Get the expire_after in milliseconds used for Home Assistant discovery, first checks override.
std::string component_type() const override
Override for MQTTComponent, returns "sensor".
const EntityBase * get_entity() const override
optional< uint32_t > expire_after_
Definition mqtt_sensor.h:52
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
std::string unique_id() override
void set_expire_after(uint32_t expire_after)
Setup an expiry, 0 disables it.
void disable_expire_after()
Disable Home Assistant value expiry.
void setup() override
Override setup.
bool has_value() const
Definition optional.h:87
Base-class for all sensors.
Definition sensor.h:57
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
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet.
Definition sensor.cpp:97
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition sensor.cpp:33
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition sensor.cpp:88
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition sensor.cpp:25
bool get_force_update() const
Get whether force update mode is enabled.
Definition sensor.h:78
bool state
Definition fan.h:0
constexpr const char *const MQTT_UNIT_OF_MEASUREMENT
Definition mqtt_const.h:264
constexpr const char *const MQTT_FORCE_UPDATE
Definition mqtt_const.h:92
MQTTClientComponent * global_mqtt_client
constexpr const char *const MQTT_STATE_CLASS
Definition mqtt_const.h:213
constexpr const char *const MQTT_EXPIRE_AFTER
Definition mqtt_const.h:82
constexpr const char *const MQTT_DEVICE_CLASS
Definition mqtt_const.h:58
std::string state_class_to_string(StateClass state_class)
Definition sensor.cpp:9
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:435
Simple Helper struct used for Home Assistant MQTT send_discovery().
bool command_topic
If the command topic should be included. Default to true.