ESPHome  2022.9.2
mqtt_sensor.cpp
Go to the documentation of this file.
1 #include "mqtt_sensor.h"
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_SENSOR
8 
9 #ifdef USE_DEEP_SLEEP
11 #endif
12 
13 namespace esphome {
14 namespace mqtt {
15 
16 static const char *const TAG = "mqtt.sensor";
17 
18 using namespace esphome::sensor;
19 
20 MQTTSensorComponent::MQTTSensorComponent(Sensor *sensor) : sensor_(sensor) {}
21 
23  this->sensor_->add_on_state_callback([this](float state) { this->publish_state(state); });
24 }
25 
27  ESP_LOGCONFIG(TAG, "MQTT Sensor '%s':", this->sensor_->get_name().c_str());
28  if (this->get_expire_after() > 0) {
29  ESP_LOGCONFIG(TAG, " Expire After: %us", this->get_expire_after() / 1000);
30  }
31  LOG_MQTT_COMPONENT(true, false)
32 }
33 
34 std::string MQTTSensorComponent::component_type() const { return "sensor"; }
35 const 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 void MQTTSensorComponent::set_expire_after(uint32_t expire_after) { this->expire_after_ = expire_after; }
44 
46  if (!this->sensor_->get_device_class().empty())
48 
49  if (!this->sensor_->get_unit_of_measurement().empty())
51 
52  if (this->get_expire_after() > 0)
53  root[MQTT_EXPIRE_AFTER] = this->get_expire_after() / 1000;
54 
55  if (this->sensor_->get_force_update())
56  root[MQTT_FORCE_UPDATE] = true;
57 
60 
61  config.command_topic = false;
62 }
64  if (this->sensor_->has_state()) {
65  return this->publish_state(this->sensor_->state);
66  } else {
67  return true;
68  }
69 }
71  int8_t accuracy = this->sensor_->get_accuracy_decimals();
72  return this->publish(this->get_state_topic_(), value_accuracy_to_string(value, accuracy));
73 }
74 std::string MQTTSensorComponent::unique_id() { return this->sensor_->unique_id(); }
75 
76 } // namespace mqtt
77 } // namespace esphome
78 
79 #endif
80 #endif // USE_MQTT
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:80
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
Definition: mqtt_sensor.cpp:20
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:85
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:265
const std::string & get_name() const
Definition: entity_base.cpp:11
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:43
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:22
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: sensor.cpp:26
virtual std::string unique_id()
A unique ID for this sensor, empty for no unique id.
Definition: sensor.cpp:121
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:133
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:42
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().
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:62
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: sensor.cpp:50
std::string unique_id() override
Definition: mqtt_sensor.cpp:74
constexpr const char *const MQTT_EXPIRE_AFTER
Definition: mqtt_const.h:71
Definition: a4988.cpp:4
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:130
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:39
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:48
uint32_t get_expire_after() const
Get the expire_after in milliseconds used for Home Assistant discovery, first checks override...
Definition: mqtt_sensor.cpp:37
std::string component_type() const override
Override for MQTTComponent, returns "sensor".
Definition: mqtt_sensor.cpp:34
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
Definition: mqtt_sensor.cpp:45
bool state
Definition: fan.h:34
const EntityBase * get_entity() const override
Definition: mqtt_sensor.cpp:35