ESPHome  2024.4.0
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 
21 MQTTSensorComponent::MQTTSensorComponent(Sensor *sensor) : sensor_(sensor) {}
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  int8_t accuracy = this->sensor_->get_accuracy_decimals();
73  return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
74 }
75 std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }
76 
77 } // namespace mqtt
78 } // namespace esphome
79 
80 #endif
81 #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:412
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
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:200
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:246
constexpr const char *const MQTT_FORCE_UPDATE
Definition: mqtt_const.h:78
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:75
constexpr const char * c_str() const
Definition: string_ref.h:68
constexpr const char *const MQTT_EXPIRE_AFTER
Definition: mqtt_const.h:73
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:57
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
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