ESPHome  2023.11.6
Go to the documentation of this file.
1 #include <cinttypes>
2 #include "mqtt_sensor.h"
3 #include "esphome/core/log.h"
5 #include "mqtt_const.h"
7 #ifdef USE_MQTT
8 #ifdef USE_SENSOR
10 #ifdef USE_DEEP_SLEEP
12 #endif
14 namespace esphome {
15 namespace mqtt {
17 static const char *const TAG = "mqtt.sensor";
19 using namespace esphome::sensor;
21 MQTTSensorComponent::MQTTSensorComponent(Sensor *sensor) : sensor_(sensor) {}
24  this->sensor_->add_on_state_callback([this](float state) { this->publish_state(state); });
25 }
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 }
35 std::string MQTTSensorComponent::component_type() const { return "sensor"; }
36 const EntityBase *MQTTSensorComponent::get_entity() const { return this->sensor_; }
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; }
47  if (!this->sensor_->get_device_class().empty())
50  if (!this->sensor_->get_unit_of_measurement().empty())
53  if (this->get_expire_after() > 0)
54  root[MQTT_EXPIRE_AFTER] = this->get_expire_after() / 1000;
56  if (this->sensor_->get_force_update())
57  root[MQTT_FORCE_UPDATE] = true;
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(); }
77 } // namespace mqtt
78 } // namespace esphome
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:401
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:198
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:244
constexpr const char *const MQTT_FORCE_UPDATE
Definition: mqtt_const.h:76
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:71
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:55
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