10#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
18struct TrafficModeOption {
20 ::espectre::TrafficGeneratorMode mode;
23static constexpr TrafficModeOption TRAFFIC_MODE_OPTIONS[] = {
24 {
"ping", ::espectre::TrafficGeneratorMode::PING},
25 {
"dns", ::espectre::TrafficGeneratorMode::DNS},
26 {
"dns_tcp", ::espectre::TrafficGeneratorMode::DNS_TCP},
27 {
"wifi_raw", ::espectre::TrafficGeneratorMode::WIFI_RAW},
28 {
"external", ::espectre::TrafficGeneratorMode::EXTERNAL_HOST},
31static const char *traffic_mode_name(::espectre::TrafficGeneratorMode
mode) {
32 for (
const auto &option : TRAFFIC_MODE_OPTIONS) {
33 if (option.mode ==
mode)
39static const char *csi_capture_profile_name(::espectre::CsiCapturePolicy profile) {
41 case ::espectre::CsiCapturePolicy::LLTF:
42 return LOG_STR_LITERAL(
"lltf");
43 case ::espectre::CsiCapturePolicy::HT_VHT:
44 return LOG_STR_LITERAL(
"ht_vht");
46 return LOG_STR_LITERAL(
"auto");
52 for (
const auto &option : TRAFFIC_MODE_OPTIONS) {
53 if (strcmp(option.name, name) == 0) {
61static int log_level(::espectre::LogLevel level) {
63 case ::espectre::LogLevel::ERROR:
64 return ESPHOME_LOG_LEVEL_ERROR;
65 case ::espectre::LogLevel::WARNING:
66 return ESPHOME_LOG_LEVEL_WARN;
67 case ::espectre::LogLevel::INFO:
68 return ESPHOME_LOG_LEVEL_INFO;
69 case ::espectre::LogLevel::DEBUG:
70 return ESPHOME_LOG_LEVEL_DEBUG;
72 return ESPHOME_LOG_LEVEL_VERBOSE;
78 this->
runtime_.config().wifi_scan_results_managed_externally =
true;
86#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
94 ::espectre::set_log_sink({
96 .enabled = [](
void *, ::espectre::LogLevel level,
const char *) {
return log_level(level) <= ESPHOME_LOG_LEVEL; },
97 .write = [](
void *, ::espectre::LogLevel level,
const char *tag,
int line,
const char *
format,
101 ESP_LOGE(TAG,
"Runtime setup failed");
117 ESP_LOGW(TAG,
"Restarting the runtime in %" PRIu32
" s",
RESTART_DELAY_MS / 1000);
127 const auto yaml_mode = this->
runtime_.config().traffic_generator_mode;
133 const auto mode =
static_cast<::espectre::TrafficGeneratorMode
>(saved);
134 const char *name = traffic_mode_name(
mode);
144 if (this->
runtime_.set_traffic_generator_mode(mode)) {
145 const auto saved =
static_cast<uint8_t
>(
mode);
148 ESP_LOGW(TAG,
"Traffic generator mode %s was rejected", traffic_mode_name(
mode));
156 const char *name = traffic_mode_name(this->
runtime_.config().traffic_generator_mode);
180 if (!this->
runtime_.trigger_recalibration()) {
181 ESP_LOGW(TAG,
"Recalibration is not available");
192 const auto &snapshot = this->
runtime_.snapshot();
193#ifdef USE_BINARY_SENSOR
194 if (this->calibrating_binary_sensor_ !=
nullptr &&
198 this->calibrating_binary_sensor_->publish_state(snapshot.calibrating);
201 if (!snapshot.ready_to_publish) {
208#ifdef USE_BINARY_SENSOR
209 const bool motion = snapshot.motion_state == ::espectre::MotionState::MOTION;
210 if (this->motion_binary_sensor_ !=
nullptr && (!this->
ready_ || this->
motion_state_ != motion)) {
212 this->motion_binary_sensor_->publish_state(motion);
217 this->movement_sensor_->publish_state(snapshot.movement_metric);
224#ifdef USE_BINARY_SENSOR
225 if (this->motion_binary_sensor_ !=
nullptr)
226 this->motion_binary_sensor_->invalidate_state();
229 if (this->movement_sensor_ !=
nullptr)
230 this->movement_sensor_->publish_state(NAN);
235void ESPectreComponent::on_calibration_finished(const ::espectre::RuntimeSnapshot &snapshot,
bool success) {
239 ESP_LOGI(TAG,
"Calibration complete");
245 ESP_LOGW(TAG,
"Calibration failed; retaining the previous threshold");
249 ESP_LOGE(TAG,
"Runtime fault: %s",
message);
254#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
265 ::espectre::clear_log_sink();
267#ifdef USE_BINARY_SENSOR
268 if (this->calibrating_binary_sensor_ !=
nullptr)
269 this->calibrating_binary_sensor_->invalidate_state();
279 const auto &config = this->
runtime_.config();
282 " Detection algorithm: %s\n"
283 " CSI capture profile: %s\n"
284 " Traffic generator mode: %s\n"
285 " Motion on/off hits: %u/%u",
286 config.detection_algorithm == ::espectre::DetectionAlgorithm::LIGHTWEIGHT
287 ? LOG_STR_LITERAL(
"lightweight")
288 : LOG_STR_LITERAL(
"high_accuracy"),
289 csi_capture_profile_name(config.csi_capture_policy), traffic_mode_name(config.traffic_generator_mode),
290 config.motion_on_hits, config.motion_off_hits);
291#ifdef USE_BINARY_SENSOR
292 LOG_BINARY_SENSOR(
" ",
"Motion", this->motion_binary_sensor_);
293 LOG_BINARY_SENSOR(
" ",
"Calibrating", this->calibrating_binary_sensor_);
296 LOG_SENSOR(
" ",
"Movement score", this->movement_sensor_);
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_set_warning()
void status_clear_error()
void status_clear_warning()
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
uint32_t restart_requested_ms_
SUB_BINARY_SENSOR(motion) SUB_BINARY_SENSOR(calibrating) SUB_SENSOR(movement) const void on_runtime_fault(const char *message) override
Latest one-second runtime diagnostics sample, or nullptr before the runtime starts.
void request_traffic_generator_mode(::espectre::TrafficGeneratorMode mode)
optional<::espectre::TrafficGeneratorMode > pending_traffic_mode_
void dump_config() override
bool recalibrate_pending_
void apply_pending_traffic_mode_()
void restore_traffic_mode_()
void on_shutdown() override
void invalidate_sensing_()
ESPPreferenceObject traffic_mode_pref_
bool calibrating_published_
void publish_traffic_mode_()
static constexpr uint32_t RESTART_DELAY_MS
::espectre::RuntimeFrontendController runtime_
select::Select * traffic_mode_select_
bool has_option(const std::string &option) const
Return whether this select component contains the provided option.
void publish_state(const std::string &state)
void release_roaming_suppression()
Release a roaming suppression request.
void request_roaming_suppression()
Request that post-connect roaming scans be suppressed.
const LogString * message
ESPHOME_LOG_TAG(TAG, "espectre")
WiFiComponent * global_wifi_component
Application App
Global storage of Application pointer - only one Application can exist.
void HOT esp_log_vprintf_(int level, const char *tag, int line, const char *format, va_list args)