ESPHome  2024.2.1
api_connection.cpp
Go to the documentation of this file.
1 #include "api_connection.h"
2 #include <cerrno>
3 #include <cinttypes>
4 #include <utility>
7 #include "esphome/core/hal.h"
8 #include "esphome/core/log.h"
9 #include "esphome/core/version.h"
10 
11 #ifdef USE_DEEP_SLEEP
13 #endif
14 #ifdef USE_HOMEASSISTANT_TIME
16 #endif
17 #ifdef USE_BLUETOOTH_PROXY
19 #endif
20 #ifdef USE_VOICE_ASSISTANT
22 #endif
23 
24 namespace esphome {
25 namespace api {
26 
27 static const char *const TAG = "api.connection";
28 static const int ESP32_CAMERA_STOP_STREAM = 5000;
29 
30 APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
31  : parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
32  this->proto_write_buffer_.reserve(64);
33 
34 #if defined(USE_API_PLAINTEXT)
35  this->helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
36 #elif defined(USE_API_NOISE)
37  this->helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
38 #else
39 #error "No frame helper defined"
40 #endif
41 }
43  this->last_traffic_ = millis();
44 
45  APIError err = this->helper_->init();
46  if (err != APIError::OK) {
48  ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
49  errno);
50  return;
51  }
52  this->client_info_ = helper_->getpeername();
53  this->client_peername_ = this->client_info_;
54  this->helper_->set_log_info(this->client_info_);
55 }
56 
58 #ifdef USE_BLUETOOTH_PROXY
59  if (bluetooth_proxy::global_bluetooth_proxy->get_api_connection() == this) {
61  }
62 #endif
63 #ifdef USE_VOICE_ASSISTANT
64  if (voice_assistant::global_voice_assistant->get_api_connection() == this) {
66  }
67 #endif
68 }
69 
71  if (this->remove_)
72  return;
73 
74  if (!network::is_connected()) {
75  // when network is disconnected force disconnect immediately
76  // don't wait for timeout
77  this->on_fatal_error();
78  ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", this->client_combined_info_.c_str());
79  return;
80  }
81  if (this->next_close_) {
82  // requested a disconnect
83  this->helper_->close();
84  this->remove_ = true;
85  return;
86  }
87 
88  APIError err = this->helper_->loop();
89  if (err != APIError::OK) {
91  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
92  api_error_to_str(err), errno);
93  return;
94  }
95  ReadPacketBuffer buffer;
96  err = this->helper_->read_packet(&buffer);
97  if (err == APIError::WOULD_BLOCK) {
98  // pass
99  } else if (err != APIError::OK) {
100  on_fatal_error();
101  if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) {
102  ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
103  } else if (err == APIError::CONNECTION_CLOSED) {
104  ESP_LOGW(TAG, "%s: Connection closed", this->client_combined_info_.c_str());
105  } else {
106  ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
107  errno);
108  }
109  return;
110  } else {
111  this->last_traffic_ = millis();
112  // read a packet
113  this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]);
114  if (this->remove_)
115  return;
116  }
117 
120 
121  static uint32_t keepalive = 60000;
122  static uint8_t max_ping_retries = 60;
123  static uint16_t ping_retry_interval = 1000;
124  const uint32_t now = millis();
125  if (this->sent_ping_) {
126  // Disconnect if not responded within 2.5*keepalive
127  if (now - this->last_traffic_ > (keepalive * 5) / 2) {
128  on_fatal_error();
129  ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_combined_info_.c_str());
130  }
131  } else if (now - this->last_traffic_ > keepalive && now > this->next_ping_retry_) {
132  ESP_LOGVV(TAG, "Sending keepalive PING...");
133  this->sent_ping_ = this->send_ping_request(PingRequest());
134  if (!this->sent_ping_) {
135  this->next_ping_retry_ = now + ping_retry_interval;
136  this->ping_retries_++;
137  if (this->ping_retries_ >= max_ping_retries) {
138  on_fatal_error();
139  ESP_LOGE(TAG, "%s: Sending keepalive failed %d time(s). Disconnecting...", this->client_combined_info_.c_str(),
140  this->ping_retries_);
141  } else if (this->ping_retries_ >= 10) {
142  ESP_LOGW(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
143  this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
144  } else {
145  ESP_LOGD(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
146  this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
147  }
148  }
149  }
150 
151 #ifdef USE_ESP32_CAMERA
152  if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) {
153  uint32_t to_send = std::min((size_t) 1024, this->image_reader_.available());
154  auto buffer = this->create_buffer();
155  // fixed32 key = 1;
156  buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
157  // bytes data = 2;
158  buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send);
159  // bool done = 3;
160  bool done = this->image_reader_.available() == to_send;
161  buffer.encode_bool(3, done);
162  bool success = this->send_buffer(buffer, 44);
163 
164  if (success) {
165  this->image_reader_.consume_data(to_send);
166  }
167  if (success && done) {
168  this->image_reader_.return_image();
169  }
170  }
171 #endif
172 
173  if (state_subs_at_ != -1) {
174  const auto &subs = this->parent_->get_state_subs();
175  if (state_subs_at_ >= (int) subs.size()) {
176  state_subs_at_ = -1;
177  } else {
178  auto &it = subs[state_subs_at_];
180  resp.entity_id = it.entity_id;
181  resp.attribute = it.attribute.value();
183  state_subs_at_++;
184  }
185  }
186  }
187 }
188 
189 std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
190  return App.get_name() + component_type + entity->get_object_id();
191 }
192 
194  // remote initiated disconnect_client
195  // don't close yet, we still need to send the disconnect response
196  // close will happen on next loop
197  ESP_LOGD(TAG, "%s requested disconnected", this->client_combined_info_.c_str());
198  this->next_close_ = true;
199  DisconnectResponse resp;
200  return resp;
201 }
203  // pass
204 }
205 
206 #ifdef USE_BINARY_SENSOR
208  if (!this->state_subscription_)
209  return false;
210 
212  resp.key = binary_sensor->get_object_id_hash();
213  resp.state = state;
214  resp.missing_state = !binary_sensor->has_state();
215  return this->send_binary_sensor_state_response(resp);
216 }
219  msg.object_id = binary_sensor->get_object_id();
220  msg.key = binary_sensor->get_object_id_hash();
221  if (binary_sensor->has_own_name())
222  msg.name = binary_sensor->get_name();
223  msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
224  msg.device_class = binary_sensor->get_device_class();
225  msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
226  msg.disabled_by_default = binary_sensor->is_disabled_by_default();
227  msg.icon = binary_sensor->get_icon();
228  msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
230 }
231 #endif
232 
233 #ifdef USE_COVER
235  if (!this->state_subscription_)
236  return false;
237 
238  auto traits = cover->get_traits();
239  CoverStateResponse resp{};
240  resp.key = cover->get_object_id_hash();
241  resp.legacy_state =
243  resp.position = cover->position;
244  if (traits.get_supports_tilt())
245  resp.tilt = cover->tilt;
246  resp.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
247  return this->send_cover_state_response(resp);
248 }
250  auto traits = cover->get_traits();
252  msg.key = cover->get_object_id_hash();
253  msg.object_id = cover->get_object_id();
254  if (cover->has_own_name())
255  msg.name = cover->get_name();
256  msg.unique_id = get_default_unique_id("cover", cover);
257  msg.assumed_state = traits.get_is_assumed_state();
258  msg.supports_position = traits.get_supports_position();
259  msg.supports_tilt = traits.get_supports_tilt();
260  msg.supports_stop = traits.get_supports_stop();
261  msg.device_class = cover->get_device_class();
263  msg.icon = cover->get_icon();
264  msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
265  return this->send_list_entities_cover_response(msg);
266 }
268  cover::Cover *cover = App.get_cover_by_key(msg.key);
269  if (cover == nullptr)
270  return;
271 
272  auto call = cover->make_call();
273  if (msg.has_legacy_command) {
274  switch (msg.legacy_command) {
276  call.set_command_open();
277  break;
279  call.set_command_close();
280  break;
282  call.set_command_stop();
283  break;
284  }
285  }
286  if (msg.has_position)
287  call.set_position(msg.position);
288  if (msg.has_tilt)
289  call.set_tilt(msg.tilt);
290  if (msg.stop)
291  call.set_command_stop();
292  call.perform();
293 }
294 #endif
295 
296 #ifdef USE_FAN
298  if (!this->state_subscription_)
299  return false;
300 
301  auto traits = fan->get_traits();
302  FanStateResponse resp{};
303  resp.key = fan->get_object_id_hash();
304  resp.state = fan->state;
305  if (traits.supports_oscillation())
306  resp.oscillating = fan->oscillating;
307  if (traits.supports_speed()) {
308  resp.speed_level = fan->speed;
309  }
310  if (traits.supports_direction())
311  resp.direction = static_cast<enums::FanDirection>(fan->direction);
312  if (traits.supports_preset_modes())
313  resp.preset_mode = fan->preset_mode;
314  return this->send_fan_state_response(resp);
315 }
317  auto traits = fan->get_traits();
319  msg.key = fan->get_object_id_hash();
320  msg.object_id = fan->get_object_id();
321  if (fan->has_own_name())
322  msg.name = fan->get_name();
323  msg.unique_id = get_default_unique_id("fan", fan);
324  msg.supports_oscillation = traits.supports_oscillation();
325  msg.supports_speed = traits.supports_speed();
326  msg.supports_direction = traits.supports_direction();
327  msg.supported_speed_count = traits.supported_speed_count();
328  for (auto const &preset : traits.supported_preset_modes())
329  msg.supported_preset_modes.push_back(preset);
331  msg.icon = fan->get_icon();
332  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
333  return this->send_list_entities_fan_response(msg);
334 }
336  fan::Fan *fan = App.get_fan_by_key(msg.key);
337  if (fan == nullptr)
338  return;
339 
340  auto call = fan->make_call();
341  if (msg.has_state)
342  call.set_state(msg.state);
343  if (msg.has_oscillating)
344  call.set_oscillating(msg.oscillating);
345  if (msg.has_speed_level) {
346  // Prefer level
347  call.set_speed(msg.speed_level);
348  }
349  if (msg.has_direction)
350  call.set_direction(static_cast<fan::FanDirection>(msg.direction));
351  if (msg.has_preset_mode)
352  call.set_preset_mode(msg.preset_mode);
353  call.perform();
354 }
355 #endif
356 
357 #ifdef USE_LIGHT
359  if (!this->state_subscription_)
360  return false;
361 
362  auto traits = light->get_traits();
363  auto values = light->remote_values;
364  auto color_mode = values.get_color_mode();
365  LightStateResponse resp{};
366 
367  resp.key = light->get_object_id_hash();
368  resp.state = values.is_on();
369  resp.color_mode = static_cast<enums::ColorMode>(color_mode);
370  resp.brightness = values.get_brightness();
371  resp.color_brightness = values.get_color_brightness();
372  resp.red = values.get_red();
373  resp.green = values.get_green();
374  resp.blue = values.get_blue();
375  resp.white = values.get_white();
376  resp.color_temperature = values.get_color_temperature();
377  resp.cold_white = values.get_cold_white();
378  resp.warm_white = values.get_warm_white();
379  if (light->supports_effects())
380  resp.effect = light->get_effect_name();
381  return this->send_light_state_response(resp);
382 }
384  auto traits = light->get_traits();
386  msg.key = light->get_object_id_hash();
387  msg.object_id = light->get_object_id();
388  if (light->has_own_name())
389  msg.name = light->get_name();
390  msg.unique_id = get_default_unique_id("light", light);
391 
393  msg.icon = light->get_icon();
394  msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
395 
396  for (auto mode : traits.get_supported_color_modes())
397  msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
398 
399  msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS);
400  msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB);
402  msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) ||
403  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE));
404  msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
405  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE);
406 
408  msg.min_mireds = traits.get_min_mireds();
409  msg.max_mireds = traits.get_max_mireds();
410  }
411  if (light->supports_effects()) {
412  msg.effects.emplace_back("None");
413  for (auto *effect : light->get_effects())
414  msg.effects.push_back(effect->get_name());
415  }
416  return this->send_list_entities_light_response(msg);
417 }
420  if (light == nullptr)
421  return;
422 
423  auto call = light->make_call();
424  if (msg.has_state)
425  call.set_state(msg.state);
426  if (msg.has_brightness)
427  call.set_brightness(msg.brightness);
428  if (msg.has_color_mode)
429  call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
430  if (msg.has_color_brightness)
432  if (msg.has_rgb) {
433  call.set_red(msg.red);
434  call.set_green(msg.green);
435  call.set_blue(msg.blue);
436  }
437  if (msg.has_white)
438  call.set_white(msg.white);
439  if (msg.has_color_temperature)
441  if (msg.has_cold_white)
442  call.set_cold_white(msg.cold_white);
443  if (msg.has_warm_white)
444  call.set_warm_white(msg.warm_white);
445  if (msg.has_transition_length)
447  if (msg.has_flash_length)
448  call.set_flash_length(msg.flash_length);
449  if (msg.has_effect)
450  call.set_effect(msg.effect);
451  call.perform();
452 }
453 #endif
454 
455 #ifdef USE_SENSOR
457  if (!this->state_subscription_)
458  return false;
459 
460  SensorStateResponse resp{};
461  resp.key = sensor->get_object_id_hash();
462  resp.state = state;
463  resp.missing_state = !sensor->has_state();
464  return this->send_sensor_state_response(resp);
465 }
468  msg.key = sensor->get_object_id_hash();
469  msg.object_id = sensor->get_object_id();
470  if (sensor->has_own_name())
471  msg.name = sensor->get_name();
472  msg.unique_id = sensor->unique_id();
473  if (msg.unique_id.empty())
474  msg.unique_id = get_default_unique_id("sensor", sensor);
475  msg.icon = sensor->get_icon();
478  msg.force_update = sensor->get_force_update();
479  msg.device_class = sensor->get_device_class();
480  msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
482  msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
483  return this->send_list_entities_sensor_response(msg);
484 }
485 #endif
486 
487 #ifdef USE_SWITCH
489  if (!this->state_subscription_)
490  return false;
491 
492  SwitchStateResponse resp{};
493  resp.key = a_switch->get_object_id_hash();
494  resp.state = state;
495  return this->send_switch_state_response(resp);
496 }
499  msg.key = a_switch->get_object_id_hash();
500  msg.object_id = a_switch->get_object_id();
501  if (a_switch->has_own_name())
502  msg.name = a_switch->get_name();
503  msg.unique_id = get_default_unique_id("switch", a_switch);
504  msg.icon = a_switch->get_icon();
505  msg.assumed_state = a_switch->assumed_state();
507  msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
508  msg.device_class = a_switch->get_device_class();
509  return this->send_list_entities_switch_response(msg);
510 }
512  switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
513  if (a_switch == nullptr)
514  return;
515 
516  if (msg.state) {
517  a_switch->turn_on();
518  } else {
519  a_switch->turn_off();
520  }
521 }
522 #endif
523 
524 #ifdef USE_TEXT_SENSOR
526  if (!this->state_subscription_)
527  return false;
528 
530  resp.key = text_sensor->get_object_id_hash();
531  resp.state = std::move(state);
532  resp.missing_state = !text_sensor->has_state();
533  return this->send_text_sensor_state_response(resp);
534 }
537  msg.key = text_sensor->get_object_id_hash();
538  msg.object_id = text_sensor->get_object_id();
539  msg.name = text_sensor->get_name();
540  msg.unique_id = text_sensor->unique_id();
541  if (msg.unique_id.empty())
542  msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
543  msg.icon = text_sensor->get_icon();
544  msg.disabled_by_default = text_sensor->is_disabled_by_default();
545  msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
546  return this->send_list_entities_text_sensor_response(msg);
547 }
548 #endif
549 
550 #ifdef USE_CLIMATE
552  if (!this->state_subscription_)
553  return false;
554 
555  auto traits = climate->get_traits();
556  ClimateStateResponse resp{};
557  resp.key = climate->get_object_id_hash();
558  resp.mode = static_cast<enums::ClimateMode>(climate->mode);
559  resp.action = static_cast<enums::ClimateAction>(climate->action);
560  if (traits.get_supports_current_temperature())
561  resp.current_temperature = climate->current_temperature;
562  if (traits.get_supports_two_point_target_temperature()) {
563  resp.target_temperature_low = climate->target_temperature_low;
564  resp.target_temperature_high = climate->target_temperature_high;
565  } else {
566  resp.target_temperature = climate->target_temperature;
567  }
568  if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
569  resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
570  if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value())
571  resp.custom_fan_mode = climate->custom_fan_mode.value();
572  if (traits.get_supports_presets() && climate->preset.has_value()) {
573  resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
574  }
575  if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value())
576  resp.custom_preset = climate->custom_preset.value();
577  if (traits.get_supports_swing_modes())
578  resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
579  if (traits.get_supports_current_humidity())
580  resp.current_humidity = climate->current_humidity;
581  if (traits.get_supports_target_humidity())
582  resp.target_humidity = climate->target_humidity;
583  return this->send_climate_state_response(resp);
584 }
586  auto traits = climate->get_traits();
588  msg.key = climate->get_object_id_hash();
589  msg.object_id = climate->get_object_id();
590  if (climate->has_own_name())
591  msg.name = climate->get_name();
592  msg.unique_id = get_default_unique_id("climate", climate);
593 
595  msg.icon = climate->get_icon();
596  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
597 
598  msg.supports_current_temperature = traits.get_supports_current_temperature();
599  msg.supports_current_humidity = traits.get_supports_current_humidity();
600  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
601  msg.supports_target_humidity = traits.get_supports_target_humidity();
602 
603  for (auto mode : traits.get_supported_modes())
604  msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
605 
606  msg.visual_min_temperature = traits.get_visual_min_temperature();
607  msg.visual_max_temperature = traits.get_visual_max_temperature();
608  msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
609  msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
610  msg.visual_min_humidity = traits.get_visual_min_humidity();
611  msg.visual_max_humidity = traits.get_visual_max_humidity();
612 
613  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
614  msg.supports_action = traits.get_supports_action();
615 
616  for (auto fan_mode : traits.get_supported_fan_modes())
617  msg.supported_fan_modes.push_back(static_cast<enums::ClimateFanMode>(fan_mode));
618  for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
620  for (auto preset : traits.get_supported_presets())
621  msg.supported_presets.push_back(static_cast<enums::ClimatePreset>(preset));
622  for (auto const &custom_preset : traits.get_supported_custom_presets())
624  for (auto swing_mode : traits.get_supported_swing_modes())
625  msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
626  return this->send_list_entities_climate_response(msg);
627 }
629  climate::Climate *climate = App.get_climate_by_key(msg.key);
630  if (climate == nullptr)
631  return;
632 
633  auto call = climate->make_call();
634  if (msg.has_mode)
635  call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
636  if (msg.has_target_temperature)
642  if (msg.has_target_humidity)
644  if (msg.has_fan_mode)
645  call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
646  if (msg.has_custom_fan_mode)
647  call.set_fan_mode(msg.custom_fan_mode);
648  if (msg.has_preset)
649  call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
650  if (msg.has_custom_preset)
651  call.set_preset(msg.custom_preset);
652  if (msg.has_swing_mode)
653  call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
654  call.perform();
655 }
656 #endif
657 
658 #ifdef USE_NUMBER
660  if (!this->state_subscription_)
661  return false;
662 
663  NumberStateResponse resp{};
664  resp.key = number->get_object_id_hash();
665  resp.state = state;
666  resp.missing_state = !number->has_state();
667  return this->send_number_state_response(resp);
668 }
671  msg.key = number->get_object_id_hash();
672  msg.object_id = number->get_object_id();
673  if (number->has_own_name())
674  msg.name = number->get_name();
675  msg.unique_id = get_default_unique_id("number", number);
676  msg.icon = number->get_icon();
678  msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
680  msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
681  msg.device_class = number->traits.get_device_class();
682 
683  msg.min_value = number->traits.get_min_value();
684  msg.max_value = number->traits.get_max_value();
685  msg.step = number->traits.get_step();
686 
687  return this->send_list_entities_number_response(msg);
688 }
690  number::Number *number = App.get_number_by_key(msg.key);
691  if (number == nullptr)
692  return;
693 
694  auto call = number->make_call();
695  call.set_value(msg.state);
696  call.perform();
697 }
698 #endif
699 
700 #ifdef USE_TEXT
702  if (!this->state_subscription_)
703  return false;
704 
705  TextStateResponse resp{};
706  resp.key = text->get_object_id_hash();
707  resp.state = std::move(state);
708  resp.missing_state = !text->has_state();
709  return this->send_text_state_response(resp);
710 }
713  msg.key = text->get_object_id_hash();
714  msg.object_id = text->get_object_id();
715  msg.name = text->get_name();
716  msg.icon = text->get_icon();
718  msg.entity_category = static_cast<enums::EntityCategory>(text->get_entity_category());
719  msg.mode = static_cast<enums::TextMode>(text->traits.get_mode());
720 
721  msg.min_length = text->traits.get_min_length();
722  msg.max_length = text->traits.get_max_length();
723  msg.pattern = text->traits.get_pattern();
724 
725  return this->send_list_entities_text_response(msg);
726 }
728  text::Text *text = App.get_text_by_key(msg.key);
729  if (text == nullptr)
730  return;
731 
732  auto call = text->make_call();
733  call.set_value(msg.state);
734  call.perform();
735 }
736 #endif
737 
738 #ifdef USE_SELECT
740  if (!this->state_subscription_)
741  return false;
742 
743  SelectStateResponse resp{};
744  resp.key = select->get_object_id_hash();
745  resp.state = std::move(state);
746  resp.missing_state = !select->has_state();
747  return this->send_select_state_response(resp);
748 }
751  msg.key = select->get_object_id_hash();
752  msg.object_id = select->get_object_id();
753  if (select->has_own_name())
754  msg.name = select->get_name();
755  msg.unique_id = get_default_unique_id("select", select);
756  msg.icon = select->get_icon();
758  msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
759 
760  for (const auto &option : select->traits.get_options())
761  msg.options.push_back(option);
762 
763  return this->send_list_entities_select_response(msg);
764 }
766  select::Select *select = App.get_select_by_key(msg.key);
767  if (select == nullptr)
768  return;
769 
770  auto call = select->make_call();
771  call.set_option(msg.state);
772  call.perform();
773 }
774 #endif
775 
776 #ifdef USE_BUTTON
779  msg.key = button->get_object_id_hash();
780  msg.object_id = button->get_object_id();
781  if (button->has_own_name())
782  msg.name = button->get_name();
783  msg.unique_id = get_default_unique_id("button", button);
784  msg.icon = button->get_icon();
786  msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
787  msg.device_class = button->get_device_class();
788  return this->send_list_entities_button_response(msg);
789 }
791  button::Button *button = App.get_button_by_key(msg.key);
792  if (button == nullptr)
793  return;
794 
795  button->press();
796 }
797 #endif
798 
799 #ifdef USE_LOCK
801  if (!this->state_subscription_)
802  return false;
803 
804  LockStateResponse resp{};
805  resp.key = a_lock->get_object_id_hash();
806  resp.state = static_cast<enums::LockState>(state);
807  return this->send_lock_state_response(resp);
808 }
811  msg.key = a_lock->get_object_id_hash();
812  msg.object_id = a_lock->get_object_id();
813  if (a_lock->has_own_name())
814  msg.name = a_lock->get_name();
815  msg.unique_id = get_default_unique_id("lock", a_lock);
816  msg.icon = a_lock->get_icon();
817  msg.assumed_state = a_lock->traits.get_assumed_state();
819  msg.entity_category = static_cast<enums::EntityCategory>(a_lock->get_entity_category());
820  msg.supports_open = a_lock->traits.get_supports_open();
821  msg.requires_code = a_lock->traits.get_requires_code();
822  return this->send_list_entities_lock_response(msg);
823 }
825  lock::Lock *a_lock = App.get_lock_by_key(msg.key);
826  if (a_lock == nullptr)
827  return;
828 
829  switch (msg.command) {
830  case enums::LOCK_UNLOCK:
831  a_lock->unlock();
832  break;
833  case enums::LOCK_LOCK:
834  a_lock->lock();
835  break;
836  case enums::LOCK_OPEN:
837  a_lock->open();
838  break;
839  }
840 }
841 #endif
842 
843 #ifdef USE_MEDIA_PLAYER
845  if (!this->state_subscription_)
846  return false;
847 
849  resp.key = media_player->get_object_id_hash();
850  resp.state = static_cast<enums::MediaPlayerState>(media_player->state);
851  resp.volume = media_player->volume;
852  resp.muted = media_player->is_muted();
853  return this->send_media_player_state_response(resp);
854 }
857  msg.key = media_player->get_object_id_hash();
858  msg.object_id = media_player->get_object_id();
859  if (media_player->has_own_name())
860  msg.name = media_player->get_name();
861  msg.unique_id = get_default_unique_id("media_player", media_player);
862  msg.icon = media_player->get_icon();
863  msg.disabled_by_default = media_player->is_disabled_by_default();
864  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
865 
866  auto traits = media_player->get_traits();
867  msg.supports_pause = traits.get_supports_pause();
868 
870 }
873  if (media_player == nullptr)
874  return;
875 
876  auto call = media_player->make_call();
877  if (msg.has_command) {
878  call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
879  }
880  if (msg.has_volume) {
881  call.set_volume(msg.volume);
882  }
883  if (msg.has_media_url) {
884  call.set_media_url(msg.media_url);
885  }
886  call.perform();
887 }
888 #endif
889 
890 #ifdef USE_ESP32_CAMERA
891 void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
892  if (!this->state_subscription_)
893  return;
894  if (this->image_reader_.available())
895  return;
896  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
897  image->was_requested_by(esphome::esp32_camera::IDLE))
898  this->image_reader_.set_image(std::move(image));
899 }
902  msg.key = camera->get_object_id_hash();
903  msg.object_id = camera->get_object_id();
904  if (camera->has_own_name())
905  msg.name = camera->get_name();
906  msg.unique_id = get_default_unique_id("camera", camera);
908  msg.icon = camera->get_icon();
909  msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
910  return this->send_list_entities_camera_response(msg);
911 }
913  if (esp32_camera::global_esp32_camera == nullptr)
914  return;
915 
916  if (msg.single)
918  if (msg.stream) {
920 
921  App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
923  });
924  }
925 }
926 #endif
927 
928 #ifdef USE_HOMEASSISTANT_TIME
932 }
933 #endif
934 
935 #ifdef USE_BLUETOOTH_PROXY
938 }
941 }
943  if (this->client_api_version_major_ < 1 || this->client_api_version_minor_ < 7) {
945  for (auto &service : resp.service_data) {
946  service.legacy_data.assign(service.data.begin(), service.data.end());
947  service.data.clear();
948  }
949  for (auto &manufacturer_data : resp.manufacturer_data) {
950  manufacturer_data.legacy_data.assign(manufacturer_data.data.begin(), manufacturer_data.data.end());
951  manufacturer_data.data.clear();
952  }
953  return this->send_bluetooth_le_advertisement_response(resp);
954  }
956 }
959 }
962 }
965 }
968 }
971 }
974 }
975 
978 }
979 
985  return resp;
986 }
987 #endif
988 
989 #ifdef USE_VOICE_ASSISTANT
993  }
994 }
997  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
998  return;
999  }
1000 
1001  if (msg.error) {
1003  return;
1004  }
1005  struct sockaddr_storage storage;
1006  socklen_t len = sizeof(storage);
1007  this->helper_->getpeername((struct sockaddr *) &storage, &len);
1009  }
1010 };
1012  if (voice_assistant::global_voice_assistant != nullptr) {
1013  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1014  return;
1015  }
1016 
1018  }
1019 }
1020 
1021 #endif
1022 
1023 #ifdef USE_ALARM_CONTROL_PANEL
1025  if (!this->state_subscription_)
1026  return false;
1027 
1029  resp.key = a_alarm_control_panel->get_object_id_hash();
1030  resp.state = static_cast<enums::AlarmControlPanelState>(a_alarm_control_panel->get_state());
1031  return this->send_alarm_control_panel_state_response(resp);
1032 }
1035  msg.key = a_alarm_control_panel->get_object_id_hash();
1036  msg.object_id = a_alarm_control_panel->get_object_id();
1037  msg.name = a_alarm_control_panel->get_name();
1038  msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel);
1039  msg.icon = a_alarm_control_panel->get_icon();
1040  msg.disabled_by_default = a_alarm_control_panel->is_disabled_by_default();
1041  msg.entity_category = static_cast<enums::EntityCategory>(a_alarm_control_panel->get_entity_category());
1042  msg.supported_features = a_alarm_control_panel->get_supported_features();
1043  msg.requires_code = a_alarm_control_panel->get_requires_code();
1044  msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm();
1046 }
1049  if (a_alarm_control_panel == nullptr)
1050  return;
1051 
1052  auto call = a_alarm_control_panel->make_call();
1053  switch (msg.command) {
1055  call.disarm();
1056  break;
1058  call.arm_away();
1059  break;
1061  call.arm_home();
1062  break;
1064  call.arm_night();
1065  break;
1067  call.arm_vacation();
1068  break;
1070  call.arm_custom_bypass();
1071  break;
1073  call.pending();
1074  break;
1075  }
1076  call.set_code(msg.code);
1077  call.perform();
1078 }
1079 #endif
1080 
1081 bool APIConnection::send_log_message(int level, const char *tag, const char *line) {
1082  if (this->log_subscription_ < level)
1083  return false;
1084 
1085  // Send raw so that we don't copy too much
1086  auto buffer = this->create_buffer();
1087  // LogLevel level = 1;
1088  buffer.encode_uint32(1, static_cast<uint32_t>(level));
1089  // string message = 3;
1090  buffer.encode_string(3, line, strlen(line));
1091  // SubscribeLogsResponse - 29
1092  return this->send_buffer(buffer, 29);
1093 }
1094 
1096  this->client_info_ = msg.client_info;
1097  this->client_peername_ = this->helper_->getpeername();
1098  this->client_combined_info_ = this->client_info_ + " (" + this->client_peername_ + ")";
1099  this->helper_->set_log_info(this->client_combined_info_);
1102  ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
1103  this->client_peername_.c_str(), this->client_api_version_major_, this->client_api_version_minor_);
1104 
1105  HelloResponse resp;
1106  resp.api_version_major = 1;
1107  resp.api_version_minor = 9;
1108  resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
1109  resp.name = App.get_name();
1110 
1111  this->connection_state_ = ConnectionState::CONNECTED;
1112  return resp;
1113 }
1115  bool correct = this->parent_->check_password(msg.password);
1116 
1117  ConnectResponse resp;
1118  // bool invalid_password = 1;
1119  resp.invalid_password = !correct;
1120  if (correct) {
1121  ESP_LOGD(TAG, "%s: Connected successfully", this->client_combined_info_.c_str());
1122  this->connection_state_ = ConnectionState::AUTHENTICATED;
1124 #ifdef USE_HOMEASSISTANT_TIME
1126  this->send_time_request();
1127  }
1128 #endif
1129  }
1130  return resp;
1131 }
1133  DeviceInfoResponse resp{};
1134  resp.uses_password = this->parent_->uses_password();
1135  resp.name = App.get_name();
1136  resp.friendly_name = App.get_friendly_name();
1137  resp.suggested_area = App.get_area();
1138  resp.mac_address = get_mac_address_pretty();
1139  resp.esphome_version = ESPHOME_VERSION;
1140  resp.compilation_time = App.get_compilation_time();
1141 #if defined(USE_ESP8266) || defined(USE_ESP32)
1142  resp.manufacturer = "Espressif";
1143 #elif defined(USE_RP2040)
1144  resp.manufacturer = "Raspberry Pi";
1145 #elif defined(USE_BK72XX)
1146  resp.manufacturer = "Beken";
1147 #elif defined(USE_RTL87XX)
1148  resp.manufacturer = "Realtek";
1149 #elif defined(USE_HOST)
1150  resp.manufacturer = "Host";
1151 #endif
1152  resp.model = ESPHOME_BOARD;
1153 #ifdef USE_DEEP_SLEEP
1154  resp.has_deep_sleep = deep_sleep::global_has_deep_sleep;
1155 #endif
1156 #ifdef ESPHOME_PROJECT_NAME
1157  resp.project_name = ESPHOME_PROJECT_NAME;
1158  resp.project_version = ESPHOME_PROJECT_VERSION;
1159 #endif
1160 #ifdef USE_WEBSERVER
1161  resp.webserver_port = USE_WEBSERVER_PORT;
1162 #endif
1163 #ifdef USE_BLUETOOTH_PROXY
1164  resp.legacy_bluetooth_proxy_version = bluetooth_proxy::global_bluetooth_proxy->get_legacy_version();
1165  resp.bluetooth_proxy_feature_flags = bluetooth_proxy::global_bluetooth_proxy->get_feature_flags();
1166 #endif
1167 #ifdef USE_VOICE_ASSISTANT
1168  resp.voice_assistant_version = voice_assistant::global_voice_assistant->get_version();
1169 #endif
1170  return resp;
1171 }
1173  for (auto &it : this->parent_->get_state_subs()) {
1174  if (it.entity_id == msg.entity_id && it.attribute.value() == msg.attribute) {
1175  it.callback(msg.state);
1176  }
1177  }
1178 }
1180  bool found = false;
1181  for (auto *service : this->parent_->get_user_services()) {
1182  if (service->execute_service(msg)) {
1183  found = true;
1184  }
1185  }
1186  if (!found) {
1187  ESP_LOGV(TAG, "Could not find matching service!");
1188  }
1189 }
1191  state_subs_at_ = 0;
1192 }
1193 bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) {
1194  if (this->remove_)
1195  return false;
1196  if (!this->helper_->can_write_without_blocking()) {
1197  delay(0);
1198  APIError err = this->helper_->loop();
1199  if (err != APIError::OK) {
1200  on_fatal_error();
1201  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
1202  api_error_to_str(err), errno);
1203  return false;
1204  }
1205  if (!this->helper_->can_write_without_blocking()) {
1206  // SubscribeLogsResponse
1207  if (message_type != 29) {
1208  ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
1209  }
1210  delay(0);
1211  return false;
1212  }
1213  }
1214 
1215  APIError err = this->helper_->write_packet(message_type, buffer.get_buffer()->data(), buffer.get_buffer()->size());
1216  if (err == APIError::WOULD_BLOCK)
1217  return false;
1218  if (err != APIError::OK) {
1219  on_fatal_error();
1220  if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
1221  ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
1222  } else {
1223  ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
1224  errno);
1225  }
1226  return false;
1227  }
1228  // Do not set last_traffic_ on send
1229  return true;
1230 }
1232  this->on_fatal_error();
1233  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_combined_info_.c_str());
1234 }
1236  this->on_fatal_error();
1237  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_combined_info_.c_str());
1238 }
1240  this->helper_->close();
1241  this->remove_ = true;
1242 }
1243 
1244 } // namespace api
1245 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:78
Base class for all switches.
Definition: switch.h:39
value_type const & value() const
Definition: optional.h:89
float current_humidity
The current humidity of the climate device, as reported from the integration.
Definition: climate.h:182
bool send_list_entities_binary_sensor_response(const ListEntitiesBinarySensorResponse &msg)
bool state
The current on/off state of the fan.
Definition: fan.h:110
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state)
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition: climate.h:202
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
bool has_own_name() const
Definition: entity_base.h:23
AlarmControlPanelState get_state() const
Get the state.
enums::EntityCategory entity_category
Definition: api_pb2.h:635
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
std::vector< std::string > supported_preset_modes
Definition: api_pb2.h:475
LightCall & set_color_brightness(optional< float > brightness)
Set the color brightness of the light from 0.0 (no color) to 1.0 (fully on)
Definition: light_call.cpp:593
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
bool has_state() const
Return whether this number has gotten a full state yet.
Definition: number.h:52
std::vector< uint8_t > * get_buffer() const
Definition: proto.h:268
void request_image(CameraRequester requester)
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:321
bool send_text_info(text::Text *text)
bool send_climate_info(climate::Climate *climate)
MediaPlayerCall & set_command(MediaPlayerCommand command)
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:102
LightCall & set_red(optional< float > red)
Set the red RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:601
void start_stream(CameraRequester requester)
enums::EntityCategory entity_category
Definition: api_pb2.h:1171
LightCall & set_color_temperature(optional< float > color_temperature)
Set the color temperature of the light in mireds for CWWW or RGBWW lights.
Definition: light_call.cpp:633
void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override
TextMode get_mode() const
Definition: text_traits.h:29
bool send_cover_info(cover::Cover *cover)
bool send_text_state_response(const TextStateResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:1223
bool send_ping_request(const PingRequest &msg)
LightCall & set_cold_white(optional< float > cold_white)
Set the cold white value of the light from 0.0 to 1.0.
Definition: light_call.cpp:641
bool send_switch_state(switch_::Switch *a_switch, bool state)
float target_temperature
The target temperature of the climate device.
Definition: climate.h:186
VoiceAssistant * global_voice_assistant
std::string get_effect_name()
Return the name of the current effect, or if no effect is active "None".
climate::Climate * get_climate_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:276
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:303
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition: cover.cpp:149
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
TextTraits traits
Definition: text.h:27
BluetoothConnectionsFreeResponse subscribe_bluetooth_connections_free(const SubscribeBluetoothConnectionsFreeRequest &msg) override
std::string get_default_unique_id(const std::string &component_type, EntityBase *entity)
InitialStateIterator initial_state_iterator_
const char * api_error_to_str(APIError err)
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: text_sensor.cpp:68
TextCall & set_value(const std::string &value)
Definition: text_call.cpp:10
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition: cover.h:116
std::vector< enums::ClimatePreset > supported_presets
Definition: api_pb2.h:982
Base class for all buttons.
Definition: button.h:29
enums::EntityCategory entity_category
Definition: api_pb2.h:1124
bool send_camera_info(esp32_camera::ESP32Camera *camera)
std::vector< std::string > options
Definition: api_pb2.h:1122
bool send_fan_state(fan::Fan *fan)
virtual FanTraits get_traits()=0
enums::EntityCategory entity_category
Definition: api_pb2.h:669
enums::EntityCategory entity_category
Definition: api_pb2.h:1808
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:312
bool send_binary_sensor_state_response(const BinarySensorStateResponse &msg)
bool check_password(const std::string &password) const
Definition: api_server.cpp:148
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
bool send_climate_state(climate::Climate *climate)
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:173
virtual bool get_requires_code() const =0
Returns if the alarm_control_panel has a code.
bool send_button_info(button::Button *button)
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:222
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:58
uint32_t socklen_t
Definition: headers.h:97
bool send_list_entities_fan_response(const ListEntitiesFanResponse &msg)
DisconnectResponse disconnect(const DisconnectRequest &msg) override
virtual bool is_status_binary_sensor() const
bool send_number_state_response(const NumberStateResponse &msg)
bool send_lock_state(lock::Lock *a_lock, lock::LockState state)
bool send_text_state(text::Text *text, std::string state)
const std::string & get_area() const
Get the area of this Application set by pre_setup().
Definition: application.h:166
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:163
std::unique_ptr< APIFrameHelper > helper_
SelectTraits traits
Definition: select.h:34
enums::ClimateSwingMode swing_mode
Definition: api_pb2.h:1045
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:191
void media_player_command(const MediaPlayerCommandRequest &msg) override
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool send_list_entities_select_response(const ListEntitiesSelectResponse &msg)
Color temperature can be controlled.
void client_subscription(api::APIConnection *client, bool subscribe)
HomeassistantTime * global_homeassistant_time
LightCall & set_color_mode(optional< ColorMode > color_mode)
Set the color mode of the light.
Definition: light_call.cpp:585
enums::ColorMode color_mode
Definition: api_pb2.h:590
void send_camera_state(std::shared_ptr< esp32_camera::CameraImage > image)
std::vector< BluetoothServiceData > service_data
Definition: api_pb2.h:1331
NumberCall & set_value(float value)
Definition: number_call.cpp:10
bool has_value() const
Definition: optional.h:87
void execute_service(const ExecuteServiceRequest &msg) override
std::vector< std::string > supported_custom_presets
Definition: api_pb2.h:983
int get_max_length() const
Definition: text_traits.h:21
Base-class for all text inputs.
Definition: text.h:24
TextCall make_call()
Instantiate a TextCall object to modify this text component&#39;s state.
Definition: text.h:35
void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
Definition: light_call.cpp:561
float target_humidity
The target humidity of the climate device.
Definition: climate.h:196
virtual bool is_muted() const
Definition: media_player.h:80
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition: climate.cpp:237
AlarmControlPanelCall make_call()
Make a AlarmControlPanelCall.
bool send_number_info(number::Number *number)
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override
float tilt
The current tilt value of the cover from 0.0 to 1.0.
Definition: cover.h:124
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
void stop_stream(CameraRequester requester)
std::string get_object_id() const
Definition: entity_base.cpp:43
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
virtual MediaPlayerTraits get_traits()=0
ClimateSwingMode swing_mode
Definition: climate.h:581
void on_no_setup_connection() override
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
alarm_control_panel::AlarmControlPanel * get_alarm_control_panel_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:333
bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor)
enums::ClimateFanMode fan_mode
Definition: api_pb2.h:1043
LockTraits traits
Definition: lock.h:124
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
Definition: climate.h:205
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:258
virtual CoverTraits get_traits()=0
void bluetooth_device_request(const api::BluetoothDeviceRequest &msg)
BluetoothProxy * global_bluetooth_proxy
Device is in away preset.
Definition: climate_mode.h:88
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition: climate.cpp:260
bool send_select_info(select::Select *select)
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: sensor.cpp:88
bool send_bluetooth_le_advertisement_response(const BluetoothLEAdvertisementResponse &msg)
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition: climate.cpp:479
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:249
void lock()
Turn this lock on.
Definition: lock.cpp:30
enums::EntityCategory entity_category
Definition: api_pb2.h:927
std::string get_icon() const
Definition: entity_base.cpp:30
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:38
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:256
enums::ClimatePreset preset
Definition: api_pb2.h:1049
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition: api_server.h:104
bool send_list_entities_text_sensor_response(const ListEntitiesTextSensorResponse &msg)
FanCall & set_speed(int speed)
Definition: fan.h:59
bool send_sensor_state(sensor::Sensor *sensor, float state)
Brightness of cold and warm white output can be controlled.
void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg)
void press()
Press this button.
Definition: button.cpp:9
bool send_select_state(select::Select *select, std::string state)
std::vector< std::string > get_options() const
bool send_list_entities_light_response(const ListEntitiesLightResponse &msg)
std::string preset_mode
Definition: fan.h:118
enums::EntityCategory entity_category
Definition: api_pb2.h:415
ESP32Camera * global_esp32_camera
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:208
enums::FanDirection direction
Definition: api_pb2.h:515
std::vector< std::string > effects
Definition: api_pb2.h:543
bool send_text_sensor_state_response(const TextSensorStateResponse &msg)
virtual bool has_state() const
Return whether this binary sensor has outputted a state.
uint8_t custom_preset
Definition: climate.h:579
std::vector< uint8_t > proto_write_buffer_
bool send_list_entities_cover_response(const ListEntitiesCoverResponse &msg)
bool send_log_message(int level, const char *tag, const char *line)
Base-class for all numbers.
Definition: number.h:39
Brightness of white channel can be controlled separately from other channels.
int speed
The current fan speed level.
Definition: fan.h:114
bool send_list_entities_camera_response(const ListEntitiesCameraResponse &msg)
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition: climate.cpp:199
bool has_state() const
Return whether this text input has gotten a full state yet.
Definition: text.h:32
std::vector< std::string > supported_custom_fan_modes
Definition: api_pb2.h:981
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
enums::LockCommand command
Definition: api_pb2.h:1202
bool send_list_entities_button_response(const ListEntitiesButtonResponse &msg)
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
Definition: climate.cpp:157
const float COVER_OPEN
Definition: cover.cpp:9
bool send_media_player_info(media_player::MediaPlayer *media_player)
bool send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg)
void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition: climate.cpp:440
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
bool send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel)
SelectCall make_call()
Instantiate a SelectCall object to modify this select component&#39;s state.
Definition: select.h:42
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: entity_base.cpp:87
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state)
void bluetooth_device_request(const BluetoothDeviceRequest &msg) override
bool send_fan_info(fan::Fan *fan)
enums::AlarmControlPanelStateCommand command
Definition: api_pb2.h:1788
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
Application App
Global storage of Application pointer - only one Application can exist.
void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg)
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:33
enums::EntityCategory entity_category
Definition: api_pb2.h:1075
const std::vector< LightEffect * > & get_effects() const
Get all effects for this light state.
ColorMode get_color_mode() const
Get the color mode of these light color values.
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
Definition: light_call.cpp:553
void on_disconnect_response(const DisconnectResponse &value) override
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:267
void on_get_time_response(const GetTimeResponse &value) override
bool get_assumed_state() const
Definition: lock.h:44
void button_command(const ButtonCommandRequest &msg) override
void bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg)
Master brightness of the light can be controlled.
void bluetooth_gatt_read_descriptor(const BluetoothGATTReadDescriptorRequest &msg) override
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:160
bool send_list_entities_number_response(const ListEntitiesNumberResponse &msg)
bool send_switch_state_response(const SwitchStateResponse &msg)
bool send_cover_state(cover::Cover *cover)
bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) override
LightCall & set_warm_white(optional< float > warm_white)
Set the warm white value of the light from 0.0 to 1.0.
Definition: light_call.cpp:649
void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override
switch_::Switch * get_switch_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:213
bool send_list_entities_lock_response(const ListEntitiesLockResponse &msg)
ClimateCall & set_target_humidity(float target_humidity)
Set the target humidity of the climate device.
Definition: climate.cpp:268
void unsubscribe_api_connection(api::APIConnection *api_connection)
ClimateFanMode fan_mode
Definition: climate.h:573
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override
NumberTraits traits
Definition: number.h:49
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
std::string client_info
Definition: api_pb2.h:220
void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override
void bluetooth_gatt_get_services(const BluetoothGATTGetServicesRequest &msg) override
bool send_list_entities_sensor_response(const ListEntitiesSensorResponse &msg)
std::vector< enums::ClimateFanMode > supported_fan_modes
Definition: api_pb2.h:979
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:211
LightCall & set_effect(optional< std::string > effect)
Set the effect of the light by its name.
Definition: light_call.cpp:657
bool send_list_entities_alarm_control_panel_response(const ListEntitiesAlarmControlPanelResponse &msg)
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition: climate.cpp:264
esp32_camera::CameraImageReader image_reader_
void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg)
void set_image(std::shared_ptr< CameraImage > image)
bool send_select_state_response(const SelectStateResponse &msg)
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition: climate.h:199
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:122
void switch_command(const SwitchCommandRequest &msg) override
ConnectResponse connect(const ConnectRequest &msg) override
bool send_light_info(light::LightState *light)
void start_streaming(struct sockaddr_storage *addr, uint16_t port)
EntityCategory get_entity_category() const
Definition: entity_base.cpp:39
NumberCall make_call()
Definition: number.h:45
FanCall & set_state(bool binary_state)
Definition: fan.h:41
void select_command(const SelectCommandRequest &msg) override
bool send_light_state_response(const LightStateResponse &msg)
bool send_sensor_info(sensor::Sensor *sensor)
std::string size_t len
Definition: helpers.h:292
FanCall & set_preset_mode(const std::string &preset_mode)
Definition: fan.h:75
void open()
Open (unlatch) this lock.
Definition: lock.cpp:40
DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override
bool send_media_player_state_response(const MediaPlayerStateResponse &msg)
text::Text * get_text_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:294
bool send_subscribe_home_assistant_state_response(const SubscribeHomeAssistantStateResponse &msg)
ClimateCall & set_mode(ClimateMode mode)
Set the mode of the climate device.
Definition: climate.cpp:133
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:314
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:285
enums::EntityCategory entity_category
Definition: api_pb2.h:474
bool send_list_entities_switch_response(const ListEntitiesSwitchResponse &msg)
FanCall make_call()
Definition: fan.cpp:114
LightCall & set_flash_length(optional< uint32_t > flash_length)
Start and set the flash length of this call in milliseconds.
Definition: light_call.cpp:569
bool get_requires_code() const
Definition: lock.h:42
LightCall & set_green(optional< float > green)
Set the green RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:609
bool uses_password() const
Definition: api_server.cpp:147
Base-class for all selects.
Definition: select.h:31
bool send_light_state(light::LightState *light)
std::vector< enums::ColorMode > supported_color_modes
Definition: api_pb2.h:536
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:22
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void on_unauthenticated_access() override
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
void bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg)
void text_command(const TextCommandRequest &msg) override
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition: select.h:39
void lock_command(const LockCommandRequest &msg) override
std::vector< BluetoothServiceData > manufacturer_data
Definition: api_pb2.h:1332
void unlock()
Turn this lock off.
Definition: lock.cpp:35
bool send_media_player_state(media_player::MediaPlayer *media_player)
bool send_sensor_state_response(const SensorStateResponse &msg)
bool send_cover_state_response(const CoverStateResponse &msg)
bool send_list_entities_text_response(const ListEntitiesTextResponse &msg)
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
Color can be controlled using RGB format (includes a brightness control for the color).
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:77
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
void light_command(const LightCommandRequest &msg) override
bool send_lock_info(lock::Lock *a_lock)
NumberMode get_mode() const
Definition: number_traits.h:29
bool send_alarm_control_panel_state_response(const AlarmControlPanelStateResponse &msg)
bool send_list_entities_climate_response(const ListEntitiesClimateResponse &msg)
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:25
LightCall & set_white(optional< float > white)
Set the white value value of the light from 0.0 to 1.0 for RGBW[W] lights.
Definition: light_call.cpp:625
LightCall & set_brightness(optional< float > brightness)
Set the target brightness of the light from 0.0 (fully off) to 1.0 (fully on)
Definition: light_call.cpp:577
void number_command(const NumberCommandRequest &msg) override
ListEntitiesIterator list_entities_iterator_
void subscribe_api_connection(api::APIConnection *api_connection, uint32_t flags)
int get_min_length() const
Definition: text_traits.h:19
bool send_switch_info(switch_::Switch *a_switch)
bool send_text_sensor_info(text_sensor::TextSensor *text_sensor)
enums::LegacyCoverCommand legacy_command
Definition: api_pb2.h:447
Base-class for all sensors.
Definition: sensor.h:57
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:590
bool send_lock_state_response(const LockStateResponse &msg)
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
virtual uint32_t get_supported_features() const =0
A numeric representation of the supported features as per HomeAssistant.
LightCall & set_blue(optional< float > blue)
Set the blue RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:617
std::vector< enums::ClimateSwingMode > supported_swing_modes
Definition: api_pb2.h:980
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
enums::SensorStateClass state_class
Definition: api_pb2.h:632
HelloResponse hello(const HelloRequest &msg) override
std::string get_compilation_time() const
Definition: application.h:173
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:546
enums::EntityCategory entity_category
Definition: api_pb2.h:986
std::vector< uint8_t > container
enums::MediaPlayerCommand command
Definition: api_pb2.h:1285
MediaPlayerCall & set_media_url(const std::string &url)
bool is_disabled_by_default() const
Definition: entity_base.cpp:26
void climate_command(const ClimateCommandRequest &msg) override
bool send_number_state(number::Number *number, float state)
APIConnection(std::unique_ptr< socket::Socket > socket, APIServer *parent)
ProtoWriteBuffer create_buffer() override
uint8_t custom_fan_mode
Definition: climate.h:574
bool get_supports_open() const
Definition: lock.h:40
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
std::string get_pattern() const
Definition: text_traits.h:25
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:189
SelectCall & set_option(const std::string &option)
Definition: select_call.cpp:10
void cover_command(const CoverCommandRequest &msg) override
void bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg)
std::vector< enums::ClimateMode > supported_modes
Definition: api_pb2.h:973
const StringRef & get_name() const
Definition: entity_base.cpp:10
void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override
void camera_image(const CameraImageRequest &msg) override
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
ClimatePreset preset
Definition: climate.h:578
MediaPlayerCall & set_volume(float volume)
Base class for all locks.
Definition: lock.h:103
ClimateAction action
The active state of the climate device.
Definition: climate.h:176
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
void on_event(const api::VoiceAssistantEventResponse &msg)
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:15
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
virtual bool get_requires_code_to_arm() const =0
Returns if the alarm_control_panel requires a code to arm.
void fan_command(const FanCommandRequest &msg) override