ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
mqtt_sensor.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_MQTT
6#ifdef USE_SENSOR
7
9#include "mqtt_component.h"
10
11namespace esphome {
12namespace mqtt {
13
15 public:
23 explicit MQTTSensorComponent(sensor::Sensor *sensor);
24
26 void set_expire_after(uint32_t 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
MQTTComponent is the base class for all components that interact with MQTT to expose certain function...
uint32_t get_expire_after() const
Get the expire_after in milliseconds used for Home Assistant discovery, first checks override.
std::string component_type() const override
Override for MQTTComponent, returns "sensor".
const EntityBase * get_entity() const override
optional< uint32_t > expire_after_
Definition mqtt_sensor.h:52
MQTTSensorComponent(sensor::Sensor *sensor)
Construct this MQTTSensorComponent instance with the provided friendly_name and sensor.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
std::string unique_id() override
void set_expire_after(uint32_t expire_after)
Setup an expiry, 0 disables it.
void disable_expire_after()
Disable Home Assistant value expiry.
void setup() override
Override setup.
Base-class for all sensors.
Definition sensor.h:57
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().