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