ESPHome  2022.5.1
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:
23  explicit MQTTSensorComponent(sensor::Sensor *sensor);
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:20
optional< uint32_t > expire_after_
Definition: mqtt_sensor.h:52
void disable_expire_after()
Disable Home Assistant value expiry.
Definition: mqtt_sensor.cpp:43
void setup() override
Override setup.
Definition: mqtt_sensor.cpp:22
void set_expire_after(uint32_t expire_after)
Setup an expiry, 0 disables it.
Definition: mqtt_sensor.cpp:42
Simple Helper struct used for Home Assistant MQTT send_discovery().
std::string unique_id() override
Definition: mqtt_sensor.cpp:74
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:47
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
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:35