ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ld24xx.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <memory>
6
7#ifdef USE_SENSOR
10
11#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
12 protected: \
13 ld24xx::SensorWithDedup<dedup_type> *name##_sensor_{nullptr}; \
14\
15 public: \
16 void set_##name##_sensor(sensor::Sensor *sensor) { \
17 this->name##_sensor_ = new ld24xx::SensorWithDedup<dedup_type>(sensor); \
18 }
19#endif
20
21#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
22 if ((sensor) != nullptr) { \
23 LOG_SENSOR(tag, name, (sensor)->sens); \
24 }
25
26#define SAFE_PUBLISH_SENSOR(sensor, value) \
27 if ((sensor) != nullptr) { \
28 (sensor)->publish_state_if_not_dup(value); \
29 }
30
31#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) \
32 if ((sensor) != nullptr) { \
33 (sensor)->publish_state_unknown(); \
34 }
35
36#define highbyte(val) (uint8_t)((val) >> 8)
37#define lowbyte(val) (uint8_t)((val) &0xff)
38
39namespace esphome {
40namespace ld24xx {
41
42#ifdef USE_SENSOR
43// Helper class to store a sensor with a deduplicator & publish state only when the value changes
44template<typename T> class SensorWithDedup {
45 public:
47
49 if (this->publish_dedup.next(state)) {
50 this->sens->publish_state(static_cast<float>(state));
51 }
52 }
53
55 if (this->publish_dedup.next_unknown()) {
56 this->sens->publish_state(NAN);
57 }
58 }
59
62};
63#endif
64} // namespace ld24xx
65} // namespace esphome
Helper class to deduplicate items in a series of values.
Definition helpers.h:628
Deduplicator< T > publish_dedup
Definition ld24xx.h:61
SensorWithDedup(sensor::Sensor *sens)
Definition ld24xx.h:46
void publish_state_if_not_dup(T state)
Definition ld24xx.h:48
Base-class for all sensors.
Definition sensor.h:59
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7