ESPHome  2024.11.0
mqtt_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_MQTT
6 #ifdef USE_SENSOR
7 
9 #include "mqtt_component.h"
10 
11 namespace esphome {
12 namespace mqtt {
13 
15  public:
24 
26  void set_expire_after(uint32_t expire_after);
28  void disable_expire_after();
29 
30  void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override;
31 
32  // ========== INTERNAL METHODS ==========
33  // (In most use cases you won't need these)
35  void setup() override;
36 
37  void dump_config() override;
38 
40  uint32_t get_expire_after() const;
41 
42  bool publish_state(float value);
43  bool send_initial_state() override;
44 
45  protected:
47  std::string component_type() const override;
48  const EntityBase *get_entity() const override;
49  std::string unique_id() override;
50 
52  optional<uint32_t> expire_after_; // Override the expire after advertised to Home Assistant
53 };
54 
55 } // namespace mqtt
56 } // namespace esphome
57 
58 #endif
59 #endif // USE_MQTT
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
Definition: mqtt_sensor.cpp:21
optional< uint32_t > expire_after_
Definition: mqtt_sensor.h:52
void disable_expire_after()
Disable Home Assistant value expiry.
Definition: mqtt_sensor.cpp:44
void setup() override
Override setup.
Definition: mqtt_sensor.cpp:23
void set_expire_after(uint32_t expire_after)
Setup an expiry, 0 disables it.
Definition: mqtt_sensor.cpp:43
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::string unique_id() override
Definition: mqtt_sensor.cpp:75
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...
const EntityBase * get_entity() const override
Definition: mqtt_sensor.cpp:36