ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
espectre.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_ESPECTRE
6
11#include <espectre_sdk.h>
12#include <cmath>
13#include <string>
14#ifdef USE_BINARY_SENSOR
16#endif
17#ifdef USE_NUMBER
19#endif
20#ifdef USE_SELECT
22#endif
23#ifdef USE_SENSOR
25#endif
26#ifdef USE_SWITCH
28#endif
29
30namespace esphome::espectre {
31
32class ESPectreComponent final : public Component, public ::espectre::IRuntimeListener {
33#ifdef USE_NUMBER
34 SUB_NUMBER(threshold)
35 SUB_NUMBER(motion_on_hits)
36 SUB_NUMBER(motion_off_hits)
37#endif
38#ifdef USE_SELECT
39 SUB_SELECT(traffic_mode)
40 SUB_SELECT(detection_algorithm)
41 SUB_SELECT(csi_sample_spacing)
42#endif
43#ifdef USE_SWITCH
44 SUB_SWITCH(sensing)
45#endif
46
47 public:
48 void setup() override;
49 void loop() override;
50 void dump_config() override;
51 void on_shutdown() override;
52 // Register the SDK's Wi-Fi event handlers after network setup, before station startup.
53 float get_setup_priority() const override { return setup_priority::WIFI + 1.0f; }
54
55 void set_detection_algorithm(::espectre::DetectionAlgorithm value) {
56 this->runtime_.config().detection_algorithm = value;
57 }
58 void set_csi_capture_profile(::espectre::CsiCapturePolicy value) {
59 this->runtime_.config().csi_capture_policy = value;
60 }
61 void set_traffic_generator_mode(::espectre::TrafficGeneratorMode value) {
62 this->runtime_.config().traffic_generator_mode = value;
63 }
64 void set_traffic_generator_target_ip(const std::string &value) {
65 this->runtime_.config().traffic_generator_target_ip = value;
66 }
67 void set_traffic_generator_target_mac(const std::string &value) {
68 this->runtime_.config().traffic_generator_target_mac = value;
69 }
70 void set_csi_sample_spacing(::espectre::TemporalCsiSpacing value) {
71 this->runtime_.config().csi_sample_spacing = value;
72 }
73 void set_csi_traffic_multicast_group(const std::string &value) {
74 this->runtime_.config().csi_traffic_multicast_group = value;
75 }
76 void set_motion_on_hits(uint8_t value) { this->runtime_.config().motion_on_hits = value; }
77 void set_motion_off_hits(uint8_t value) { this->runtime_.config().motion_off_hits = value; }
78 void set_window_size_ms(uint32_t value) { this->runtime_.config().window_size_ms = value; }
79 void set_csi_target_pps(uint32_t value) { this->runtime_.config().csi_target_pps = value; }
80 void set_evaluation_interval_ms(uint32_t value) { this->runtime_.config().evaluation_interval_ms = value; }
81 void set_lowpass_enabled(bool value) { this->runtime_.config().lowpass_enabled = value; }
82 void set_lowpass_cutoff(float value) { this->runtime_.config().lowpass_cutoff = value; }
83 void set_hampel_enabled(bool value) { this->runtime_.config().hampel_enabled = value; }
84 void set_hampel_window(uint8_t value) { this->runtime_.config().hampel_window = value; }
85 void set_hampel_threshold(float value) { this->runtime_.config().hampel_threshold = value; }
86 void set_wifi_band_policy(::espectre::WifiBandPolicy value) { this->runtime_.config().wifi_band_policy = value; }
87 // Queue controls so entity automations cannot re-enter the SDK from a listener callback.
88 void recalibrate() { this->recalibrate_pending_ = true; }
89#ifdef USE_NUMBER
90 void request_threshold(float value) { this->pending_threshold_ = value; }
91 void request_motion_on_hits(float value) { this->pending_motion_on_hits_ = static_cast<uint8_t>(std::lround(value)); }
92 void request_motion_off_hits(float value) {
93 this->pending_motion_off_hits_ = static_cast<uint8_t>(std::lround(value));
94 }
95#endif
96#ifdef USE_SELECT
97 void request_traffic_generator_mode(::espectre::TrafficGeneratorMode mode) { this->pending_traffic_mode_ = mode; }
99 void request_traffic_generator_mode(const char *name);
101 void request_detection_algorithm(const char *name);
103 void request_csi_sample_spacing(const char *name);
104#endif
105#ifdef USE_SWITCH
106 void request_sensing(bool enabled) { this->pending_sensing_ = enabled; }
107#endif
108
109#ifdef USE_BINARY_SENSOR
110 SUB_BINARY_SENSOR(motion)
111 SUB_BINARY_SENSOR(calibrating)
112#endif
113#ifdef USE_SENSOR
114 SUB_SENSOR(movement)
115#endif
117 const ::espectre::RuntimeDiagnosticsSample *diagnostics_sample() const { return this->runtime_.diagnostics_sample(); }
118
119 protected:
120 // Called once per SDK detector evaluation (every evaluation_interval), which bounds the movement publish rate.
121 void on_live_telemetry(const ::espectre::RuntimeSnapshot &snapshot) override { this->movement_pending_ = true; }
122 void on_calibration_finished(const ::espectre::RuntimeSnapshot &snapshot, bool success) override;
123 void on_runtime_fault(const char *message) override;
124 void start_runtime_();
125 void schedule_restart_();
126 void invalidate_sensing_();
127 void stop_();
128#ifdef USE_SELECT
129 void apply_pending_traffic_mode_();
130 void publish_traffic_mode_();
131#endif
132 // Threshold, motion hits, detection algorithm, sample spacing and sensing.
133 void restore_controls_();
134 void apply_pending_controls_();
135 void publish_controls_();
136
137 static constexpr uint32_t RESTART_DELAY_MS = 30000;
138
139 ::espectre::RuntimeFrontendController runtime_;
140 uint32_t restart_requested_ms_{0};
141#ifdef USE_NUMBER
142 ESPPreferenceObject motion_on_hits_pref_;
143 ESPPreferenceObject motion_off_hits_pref_;
144 float pending_threshold_{NAN};
145 // Zero means no request; the runtime accepts 1 to 20.
146 uint8_t pending_motion_on_hits_{0};
147 uint8_t pending_motion_off_hits_{0};
148#endif
149#ifdef USE_SELECT
150 ESPPreferenceObject traffic_mode_pref_;
151 ESPPreferenceObject detection_algorithm_pref_;
152 ESPPreferenceObject csi_sample_spacing_pref_;
153 optional<::espectre::TrafficGeneratorMode> pending_traffic_mode_;
154 optional<::espectre::DetectionAlgorithm> pending_detection_algorithm_;
155 optional<::espectre::TemporalCsiSpacing> pending_csi_sample_spacing_;
156#endif
157#ifdef USE_SWITCH
158 optional<bool> pending_sensing_;
159#endif
160 bool recalibrate_pending_{false};
161 bool movement_pending_{false};
162 bool ready_{false};
163 bool motion_state_{false};
164 bool calibrating_state_{false};
165 bool calibrating_published_{false};
166 bool runtime_fault_{false};
167 bool running_{false};
168 bool restart_pending_{false};
169 bool calibrated_{false};
170 bool roaming_suppressed_{false};
171};
172
173} // namespace esphome::espectre
174
175#endif // USE_ESPECTRE
BedjetMode mode
BedJet operating mode.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
const LogString * message
Definition component.cpp:35
constexpr float WIFI
Definition component.h:50
uint32_t value
Definition hal.cpp:41
static void uint32_t