ESPHome  2024.9.0
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();
182  resp.once = it.once;
184  state_subs_at_++;
185  }
186  }
187  }
188 }
189 
190 std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
191  return App.get_name() + component_type + entity->get_object_id();
192 }
193 
195  // remote initiated disconnect_client
196  // don't close yet, we still need to send the disconnect response
197  // close will happen on next loop
198  ESP_LOGD(TAG, "%s requested disconnected", this->client_combined_info_.c_str());
199  this->next_close_ = true;
200  DisconnectResponse resp;
201  return resp;
202 }
204  // pass
205 }
206 
207 #ifdef USE_BINARY_SENSOR
209  if (!this->state_subscription_)
210  return false;
211 
213  resp.key = binary_sensor->get_object_id_hash();
214  resp.state = state;
215  resp.missing_state = !binary_sensor->has_state();
216  return this->send_binary_sensor_state_response(resp);
217 }
220  msg.object_id = binary_sensor->get_object_id();
221  msg.key = binary_sensor->get_object_id_hash();
222  if (binary_sensor->has_own_name())
223  msg.name = binary_sensor->get_name();
224  msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
225  msg.device_class = binary_sensor->get_device_class();
226  msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
227  msg.disabled_by_default = binary_sensor->is_disabled_by_default();
228  msg.icon = binary_sensor->get_icon();
229  msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
231 }
232 #endif
233 
234 #ifdef USE_COVER
236  if (!this->state_subscription_)
237  return false;
238 
239  auto traits = cover->get_traits();
240  CoverStateResponse resp{};
241  resp.key = cover->get_object_id_hash();
242  resp.legacy_state =
244  resp.position = cover->position;
245  if (traits.get_supports_tilt())
246  resp.tilt = cover->tilt;
247  resp.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
248  return this->send_cover_state_response(resp);
249 }
251  auto traits = cover->get_traits();
253  msg.key = cover->get_object_id_hash();
254  msg.object_id = cover->get_object_id();
255  if (cover->has_own_name())
256  msg.name = cover->get_name();
257  msg.unique_id = get_default_unique_id("cover", cover);
258  msg.assumed_state = traits.get_is_assumed_state();
259  msg.supports_position = traits.get_supports_position();
260  msg.supports_tilt = traits.get_supports_tilt();
261  msg.supports_stop = traits.get_supports_stop();
262  msg.device_class = cover->get_device_class();
264  msg.icon = cover->get_icon();
265  msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
266  return this->send_list_entities_cover_response(msg);
267 }
269  cover::Cover *cover = App.get_cover_by_key(msg.key);
270  if (cover == nullptr)
271  return;
272 
273  auto call = cover->make_call();
274  if (msg.has_legacy_command) {
275  switch (msg.legacy_command) {
277  call.set_command_open();
278  break;
280  call.set_command_close();
281  break;
283  call.set_command_stop();
284  break;
285  }
286  }
287  if (msg.has_position)
288  call.set_position(msg.position);
289  if (msg.has_tilt)
290  call.set_tilt(msg.tilt);
291  if (msg.stop)
292  call.set_command_stop();
293  call.perform();
294 }
295 #endif
296 
297 #ifdef USE_FAN
299  if (!this->state_subscription_)
300  return false;
301 
302  auto traits = fan->get_traits();
303  FanStateResponse resp{};
304  resp.key = fan->get_object_id_hash();
305  resp.state = fan->state;
306  if (traits.supports_oscillation())
307  resp.oscillating = fan->oscillating;
308  if (traits.supports_speed()) {
309  resp.speed_level = fan->speed;
310  }
311  if (traits.supports_direction())
312  resp.direction = static_cast<enums::FanDirection>(fan->direction);
313  if (traits.supports_preset_modes())
314  resp.preset_mode = fan->preset_mode;
315  return this->send_fan_state_response(resp);
316 }
318  auto traits = fan->get_traits();
320  msg.key = fan->get_object_id_hash();
321  msg.object_id = fan->get_object_id();
322  if (fan->has_own_name())
323  msg.name = fan->get_name();
324  msg.unique_id = get_default_unique_id("fan", fan);
325  msg.supports_oscillation = traits.supports_oscillation();
326  msg.supports_speed = traits.supports_speed();
327  msg.supports_direction = traits.supports_direction();
328  msg.supported_speed_count = traits.supported_speed_count();
329  for (auto const &preset : traits.supported_preset_modes())
330  msg.supported_preset_modes.push_back(preset);
332  msg.icon = fan->get_icon();
333  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
334  return this->send_list_entities_fan_response(msg);
335 }
337  fan::Fan *fan = App.get_fan_by_key(msg.key);
338  if (fan == nullptr)
339  return;
340 
341  auto call = fan->make_call();
342  if (msg.has_state)
343  call.set_state(msg.state);
344  if (msg.has_oscillating)
345  call.set_oscillating(msg.oscillating);
346  if (msg.has_speed_level) {
347  // Prefer level
348  call.set_speed(msg.speed_level);
349  }
350  if (msg.has_direction)
351  call.set_direction(static_cast<fan::FanDirection>(msg.direction));
352  if (msg.has_preset_mode)
353  call.set_preset_mode(msg.preset_mode);
354  call.perform();
355 }
356 #endif
357 
358 #ifdef USE_LIGHT
360  if (!this->state_subscription_)
361  return false;
362 
363  auto traits = light->get_traits();
364  auto values = light->remote_values;
365  auto color_mode = values.get_color_mode();
366  LightStateResponse resp{};
367 
368  resp.key = light->get_object_id_hash();
369  resp.state = values.is_on();
370  resp.color_mode = static_cast<enums::ColorMode>(color_mode);
371  resp.brightness = values.get_brightness();
372  resp.color_brightness = values.get_color_brightness();
373  resp.red = values.get_red();
374  resp.green = values.get_green();
375  resp.blue = values.get_blue();
376  resp.white = values.get_white();
377  resp.color_temperature = values.get_color_temperature();
378  resp.cold_white = values.get_cold_white();
379  resp.warm_white = values.get_warm_white();
380  if (light->supports_effects())
381  resp.effect = light->get_effect_name();
382  return this->send_light_state_response(resp);
383 }
385  auto traits = light->get_traits();
387  msg.key = light->get_object_id_hash();
388  msg.object_id = light->get_object_id();
389  if (light->has_own_name())
390  msg.name = light->get_name();
391  msg.unique_id = get_default_unique_id("light", light);
392 
394  msg.icon = light->get_icon();
395  msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
396 
397  for (auto mode : traits.get_supported_color_modes())
398  msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
399 
400  msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS);
401  msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB);
403  msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) ||
404  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE));
405  msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
406  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE);
407 
409  msg.min_mireds = traits.get_min_mireds();
410  msg.max_mireds = traits.get_max_mireds();
411  }
412  if (light->supports_effects()) {
413  msg.effects.emplace_back("None");
414  for (auto *effect : light->get_effects())
415  msg.effects.push_back(effect->get_name());
416  }
417  return this->send_list_entities_light_response(msg);
418 }
421  if (light == nullptr)
422  return;
423 
424  auto call = light->make_call();
425  if (msg.has_state)
426  call.set_state(msg.state);
427  if (msg.has_brightness)
428  call.set_brightness(msg.brightness);
429  if (msg.has_color_mode)
430  call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
431  if (msg.has_color_brightness)
433  if (msg.has_rgb) {
434  call.set_red(msg.red);
435  call.set_green(msg.green);
436  call.set_blue(msg.blue);
437  }
438  if (msg.has_white)
439  call.set_white(msg.white);
440  if (msg.has_color_temperature)
442  if (msg.has_cold_white)
443  call.set_cold_white(msg.cold_white);
444  if (msg.has_warm_white)
445  call.set_warm_white(msg.warm_white);
446  if (msg.has_transition_length)
448  if (msg.has_flash_length)
449  call.set_flash_length(msg.flash_length);
450  if (msg.has_effect)
451  call.set_effect(msg.effect);
452  call.perform();
453 }
454 #endif
455 
456 #ifdef USE_SENSOR
458  if (!this->state_subscription_)
459  return false;
460 
461  SensorStateResponse resp{};
462  resp.key = sensor->get_object_id_hash();
463  resp.state = state;
464  resp.missing_state = !sensor->has_state();
465  return this->send_sensor_state_response(resp);
466 }
469  msg.key = sensor->get_object_id_hash();
470  msg.object_id = sensor->get_object_id();
471  if (sensor->has_own_name())
472  msg.name = sensor->get_name();
473  msg.unique_id = sensor->unique_id();
474  if (msg.unique_id.empty())
475  msg.unique_id = get_default_unique_id("sensor", sensor);
476  msg.icon = sensor->get_icon();
479  msg.force_update = sensor->get_force_update();
480  msg.device_class = sensor->get_device_class();
481  msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
483  msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
484  return this->send_list_entities_sensor_response(msg);
485 }
486 #endif
487 
488 #ifdef USE_SWITCH
490  if (!this->state_subscription_)
491  return false;
492 
493  SwitchStateResponse resp{};
494  resp.key = a_switch->get_object_id_hash();
495  resp.state = state;
496  return this->send_switch_state_response(resp);
497 }
500  msg.key = a_switch->get_object_id_hash();
501  msg.object_id = a_switch->get_object_id();
502  if (a_switch->has_own_name())
503  msg.name = a_switch->get_name();
504  msg.unique_id = get_default_unique_id("switch", a_switch);
505  msg.icon = a_switch->get_icon();
506  msg.assumed_state = a_switch->assumed_state();
508  msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
509  msg.device_class = a_switch->get_device_class();
510  return this->send_list_entities_switch_response(msg);
511 }
513  switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
514  if (a_switch == nullptr)
515  return;
516 
517  if (msg.state) {
518  a_switch->turn_on();
519  } else {
520  a_switch->turn_off();
521  }
522 }
523 #endif
524 
525 #ifdef USE_TEXT_SENSOR
527  if (!this->state_subscription_)
528  return false;
529 
531  resp.key = text_sensor->get_object_id_hash();
532  resp.state = std::move(state);
533  resp.missing_state = !text_sensor->has_state();
534  return this->send_text_sensor_state_response(resp);
535 }
538  msg.key = text_sensor->get_object_id_hash();
539  msg.object_id = text_sensor->get_object_id();
540  msg.name = text_sensor->get_name();
541  msg.unique_id = text_sensor->unique_id();
542  if (msg.unique_id.empty())
543  msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
544  msg.icon = text_sensor->get_icon();
545  msg.disabled_by_default = text_sensor->is_disabled_by_default();
546  msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
547  msg.device_class = text_sensor->get_device_class();
548  return this->send_list_entities_text_sensor_response(msg);
549 }
550 #endif
551 
552 #ifdef USE_CLIMATE
554  if (!this->state_subscription_)
555  return false;
556 
557  auto traits = climate->get_traits();
558  ClimateStateResponse resp{};
559  resp.key = climate->get_object_id_hash();
560  resp.mode = static_cast<enums::ClimateMode>(climate->mode);
561  resp.action = static_cast<enums::ClimateAction>(climate->action);
562  if (traits.get_supports_current_temperature())
563  resp.current_temperature = climate->current_temperature;
564  if (traits.get_supports_two_point_target_temperature()) {
565  resp.target_temperature_low = climate->target_temperature_low;
566  resp.target_temperature_high = climate->target_temperature_high;
567  } else {
568  resp.target_temperature = climate->target_temperature;
569  }
570  if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
571  resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
572  if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value())
573  resp.custom_fan_mode = climate->custom_fan_mode.value();
574  if (traits.get_supports_presets() && climate->preset.has_value()) {
575  resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
576  }
577  if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value())
578  resp.custom_preset = climate->custom_preset.value();
579  if (traits.get_supports_swing_modes())
580  resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
581  if (traits.get_supports_current_humidity())
582  resp.current_humidity = climate->current_humidity;
583  if (traits.get_supports_target_humidity())
584  resp.target_humidity = climate->target_humidity;
585  return this->send_climate_state_response(resp);
586 }
588  auto traits = climate->get_traits();
590  msg.key = climate->get_object_id_hash();
591  msg.object_id = climate->get_object_id();
592  if (climate->has_own_name())
593  msg.name = climate->get_name();
594  msg.unique_id = get_default_unique_id("climate", climate);
595 
597  msg.icon = climate->get_icon();
598  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
599 
600  msg.supports_current_temperature = traits.get_supports_current_temperature();
601  msg.supports_current_humidity = traits.get_supports_current_humidity();
602  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
603  msg.supports_target_humidity = traits.get_supports_target_humidity();
604 
605  for (auto mode : traits.get_supported_modes())
606  msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
607 
608  msg.visual_min_temperature = traits.get_visual_min_temperature();
609  msg.visual_max_temperature = traits.get_visual_max_temperature();
610  msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
611  msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
612  msg.visual_min_humidity = traits.get_visual_min_humidity();
613  msg.visual_max_humidity = traits.get_visual_max_humidity();
614 
615  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
616  msg.supports_action = traits.get_supports_action();
617 
618  for (auto fan_mode : traits.get_supported_fan_modes())
619  msg.supported_fan_modes.push_back(static_cast<enums::ClimateFanMode>(fan_mode));
620  for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
622  for (auto preset : traits.get_supported_presets())
623  msg.supported_presets.push_back(static_cast<enums::ClimatePreset>(preset));
624  for (auto const &custom_preset : traits.get_supported_custom_presets())
626  for (auto swing_mode : traits.get_supported_swing_modes())
627  msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
628  return this->send_list_entities_climate_response(msg);
629 }
631  climate::Climate *climate = App.get_climate_by_key(msg.key);
632  if (climate == nullptr)
633  return;
634 
635  auto call = climate->make_call();
636  if (msg.has_mode)
637  call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
638  if (msg.has_target_temperature)
644  if (msg.has_target_humidity)
646  if (msg.has_fan_mode)
647  call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
648  if (msg.has_custom_fan_mode)
649  call.set_fan_mode(msg.custom_fan_mode);
650  if (msg.has_preset)
651  call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
652  if (msg.has_custom_preset)
653  call.set_preset(msg.custom_preset);
654  if (msg.has_swing_mode)
655  call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
656  call.perform();
657 }
658 #endif
659 
660 #ifdef USE_NUMBER
662  if (!this->state_subscription_)
663  return false;
664 
665  NumberStateResponse resp{};
666  resp.key = number->get_object_id_hash();
667  resp.state = state;
668  resp.missing_state = !number->has_state();
669  return this->send_number_state_response(resp);
670 }
673  msg.key = number->get_object_id_hash();
674  msg.object_id = number->get_object_id();
675  if (number->has_own_name())
676  msg.name = number->get_name();
677  msg.unique_id = get_default_unique_id("number", number);
678  msg.icon = number->get_icon();
680  msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
682  msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
683  msg.device_class = number->traits.get_device_class();
684 
685  msg.min_value = number->traits.get_min_value();
686  msg.max_value = number->traits.get_max_value();
687  msg.step = number->traits.get_step();
688 
689  return this->send_list_entities_number_response(msg);
690 }
692  number::Number *number = App.get_number_by_key(msg.key);
693  if (number == nullptr)
694  return;
695 
696  auto call = number->make_call();
697  call.set_value(msg.state);
698  call.perform();
699 }
700 #endif
701 
702 #ifdef USE_DATETIME_DATE
704  if (!this->state_subscription_)
705  return false;
706 
707  DateStateResponse resp{};
708  resp.key = date->get_object_id_hash();
709  resp.missing_state = !date->has_state();
710  resp.year = date->year;
711  resp.month = date->month;
712  resp.day = date->day;
713  return this->send_date_state_response(resp);
714 }
717  msg.key = date->get_object_id_hash();
718  msg.object_id = date->get_object_id();
719  if (date->has_own_name())
720  msg.name = date->get_name();
721  msg.unique_id = get_default_unique_id("date", date);
722  msg.icon = date->get_icon();
724  msg.entity_category = static_cast<enums::EntityCategory>(date->get_entity_category());
725 
726  return this->send_list_entities_date_response(msg);
727 }
730  if (date == nullptr)
731  return;
732 
733  auto call = date->make_call();
734  call.set_date(msg.year, msg.month, msg.day);
735  call.perform();
736 }
737 #endif
738 
739 #ifdef USE_DATETIME_TIME
741  if (!this->state_subscription_)
742  return false;
743 
744  TimeStateResponse resp{};
745  resp.key = time->get_object_id_hash();
746  resp.missing_state = !time->has_state();
747  resp.hour = time->hour;
748  resp.minute = time->minute;
749  resp.second = time->second;
750  return this->send_time_state_response(resp);
751 }
754  msg.key = time->get_object_id_hash();
755  msg.object_id = time->get_object_id();
756  if (time->has_own_name())
757  msg.name = time->get_name();
758  msg.unique_id = get_default_unique_id("time", time);
759  msg.icon = time->get_icon();
761  msg.entity_category = static_cast<enums::EntityCategory>(time->get_entity_category());
762 
763  return this->send_list_entities_time_response(msg);
764 }
767  if (time == nullptr)
768  return;
769 
770  auto call = time->make_call();
771  call.set_time(msg.hour, msg.minute, msg.second);
772  call.perform();
773 }
774 #endif
775 
776 #ifdef USE_DATETIME_DATETIME
778  if (!this->state_subscription_)
779  return false;
780 
781  DateTimeStateResponse resp{};
782  resp.key = datetime->get_object_id_hash();
783  resp.missing_state = !datetime->has_state();
784  if (datetime->has_state()) {
785  ESPTime state = datetime->state_as_esptime();
786  resp.epoch_seconds = state.timestamp;
787  }
788  return this->send_date_time_state_response(resp);
789 }
792  msg.key = datetime->get_object_id_hash();
793  msg.object_id = datetime->get_object_id();
794  if (datetime->has_own_name())
795  msg.name = datetime->get_name();
796  msg.unique_id = get_default_unique_id("datetime", datetime);
797  msg.icon = datetime->get_icon();
799  msg.entity_category = static_cast<enums::EntityCategory>(datetime->get_entity_category());
800 
801  return this->send_list_entities_date_time_response(msg);
802 }
805  if (datetime == nullptr)
806  return;
807 
808  auto call = datetime->make_call();
809  call.set_datetime(msg.epoch_seconds);
810  call.perform();
811 }
812 #endif
813 
814 #ifdef USE_TEXT
816  if (!this->state_subscription_)
817  return false;
818 
819  TextStateResponse resp{};
820  resp.key = text->get_object_id_hash();
821  resp.state = std::move(state);
822  resp.missing_state = !text->has_state();
823  return this->send_text_state_response(resp);
824 }
827  msg.key = text->get_object_id_hash();
828  msg.object_id = text->get_object_id();
829  msg.name = text->get_name();
830  msg.icon = text->get_icon();
832  msg.entity_category = static_cast<enums::EntityCategory>(text->get_entity_category());
833  msg.mode = static_cast<enums::TextMode>(text->traits.get_mode());
834 
835  msg.min_length = text->traits.get_min_length();
836  msg.max_length = text->traits.get_max_length();
837  msg.pattern = text->traits.get_pattern();
838 
839  return this->send_list_entities_text_response(msg);
840 }
842  text::Text *text = App.get_text_by_key(msg.key);
843  if (text == nullptr)
844  return;
845 
846  auto call = text->make_call();
847  call.set_value(msg.state);
848  call.perform();
849 }
850 #endif
851 
852 #ifdef USE_SELECT
854  if (!this->state_subscription_)
855  return false;
856 
857  SelectStateResponse resp{};
858  resp.key = select->get_object_id_hash();
859  resp.state = std::move(state);
860  resp.missing_state = !select->has_state();
861  return this->send_select_state_response(resp);
862 }
865  msg.key = select->get_object_id_hash();
866  msg.object_id = select->get_object_id();
867  if (select->has_own_name())
868  msg.name = select->get_name();
869  msg.unique_id = get_default_unique_id("select", select);
870  msg.icon = select->get_icon();
872  msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
873 
874  for (const auto &option : select->traits.get_options())
875  msg.options.push_back(option);
876 
877  return this->send_list_entities_select_response(msg);
878 }
880  select::Select *select = App.get_select_by_key(msg.key);
881  if (select == nullptr)
882  return;
883 
884  auto call = select->make_call();
885  call.set_option(msg.state);
886  call.perform();
887 }
888 #endif
889 
890 #ifdef USE_BUTTON
893  msg.key = button->get_object_id_hash();
894  msg.object_id = button->get_object_id();
895  if (button->has_own_name())
896  msg.name = button->get_name();
897  msg.unique_id = get_default_unique_id("button", button);
898  msg.icon = button->get_icon();
900  msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
901  msg.device_class = button->get_device_class();
902  return this->send_list_entities_button_response(msg);
903 }
905  button::Button *button = App.get_button_by_key(msg.key);
906  if (button == nullptr)
907  return;
908 
909  button->press();
910 }
911 #endif
912 
913 #ifdef USE_LOCK
915  if (!this->state_subscription_)
916  return false;
917 
918  LockStateResponse resp{};
919  resp.key = a_lock->get_object_id_hash();
920  resp.state = static_cast<enums::LockState>(state);
921  return this->send_lock_state_response(resp);
922 }
925  msg.key = a_lock->get_object_id_hash();
926  msg.object_id = a_lock->get_object_id();
927  if (a_lock->has_own_name())
928  msg.name = a_lock->get_name();
929  msg.unique_id = get_default_unique_id("lock", a_lock);
930  msg.icon = a_lock->get_icon();
931  msg.assumed_state = a_lock->traits.get_assumed_state();
933  msg.entity_category = static_cast<enums::EntityCategory>(a_lock->get_entity_category());
934  msg.supports_open = a_lock->traits.get_supports_open();
935  msg.requires_code = a_lock->traits.get_requires_code();
936  return this->send_list_entities_lock_response(msg);
937 }
939  lock::Lock *a_lock = App.get_lock_by_key(msg.key);
940  if (a_lock == nullptr)
941  return;
942 
943  switch (msg.command) {
944  case enums::LOCK_UNLOCK:
945  a_lock->unlock();
946  break;
947  case enums::LOCK_LOCK:
948  a_lock->lock();
949  break;
950  case enums::LOCK_OPEN:
951  a_lock->open();
952  break;
953  }
954 }
955 #endif
956 
957 #ifdef USE_VALVE
959  if (!this->state_subscription_)
960  return false;
961 
962  ValveStateResponse resp{};
963  resp.key = valve->get_object_id_hash();
964  resp.position = valve->position;
965  resp.current_operation = static_cast<enums::ValveOperation>(valve->current_operation);
966  return this->send_valve_state_response(resp);
967 }
969  auto traits = valve->get_traits();
971  msg.key = valve->get_object_id_hash();
972  msg.object_id = valve->get_object_id();
973  if (valve->has_own_name())
974  msg.name = valve->get_name();
975  msg.unique_id = get_default_unique_id("valve", valve);
976  msg.icon = valve->get_icon();
978  msg.entity_category = static_cast<enums::EntityCategory>(valve->get_entity_category());
979  msg.device_class = valve->get_device_class();
980  msg.assumed_state = traits.get_is_assumed_state();
981  msg.supports_position = traits.get_supports_position();
982  msg.supports_stop = traits.get_supports_stop();
983  return this->send_list_entities_valve_response(msg);
984 }
986  valve::Valve *valve = App.get_valve_by_key(msg.key);
987  if (valve == nullptr)
988  return;
989 
990  auto call = valve->make_call();
991  if (msg.has_position)
992  call.set_position(msg.position);
993  if (msg.stop)
994  call.set_command_stop();
995  call.perform();
996 }
997 #endif
998 
999 #ifdef USE_MEDIA_PLAYER
1001  if (!this->state_subscription_)
1002  return false;
1003 
1004  MediaPlayerStateResponse resp{};
1005  resp.key = media_player->get_object_id_hash();
1006 
1009  : media_player->state;
1010  resp.state = static_cast<enums::MediaPlayerState>(report_state);
1011  resp.volume = media_player->volume;
1012  resp.muted = media_player->is_muted();
1013  return this->send_media_player_state_response(resp);
1014 }
1017  msg.key = media_player->get_object_id_hash();
1018  msg.object_id = media_player->get_object_id();
1019  if (media_player->has_own_name())
1020  msg.name = media_player->get_name();
1021  msg.unique_id = get_default_unique_id("media_player", media_player);
1022  msg.icon = media_player->get_icon();
1023  msg.disabled_by_default = media_player->is_disabled_by_default();
1024  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
1025 
1026  auto traits = media_player->get_traits();
1027  msg.supports_pause = traits.get_supports_pause();
1028 
1029  for (auto &supported_format : traits.get_supported_formats()) {
1030  MediaPlayerSupportedFormat media_format;
1031  media_format.format = supported_format.format;
1032  media_format.sample_rate = supported_format.sample_rate;
1033  media_format.num_channels = supported_format.num_channels;
1034  media_format.purpose = static_cast<enums::MediaPlayerFormatPurpose>(supported_format.purpose);
1035  media_format.sample_bytes = supported_format.sample_bytes;
1036  msg.supported_formats.push_back(media_format);
1037  }
1038 
1039  return this->send_list_entities_media_player_response(msg);
1040 }
1043  if (media_player == nullptr)
1044  return;
1045 
1046  auto call = media_player->make_call();
1047  if (msg.has_command) {
1048  call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
1049  }
1050  if (msg.has_volume) {
1051  call.set_volume(msg.volume);
1052  }
1053  if (msg.has_media_url) {
1054  call.set_media_url(msg.media_url);
1055  }
1056  if (msg.has_announcement) {
1057  call.set_announcement(msg.announcement);
1058  }
1059  call.perform();
1060 }
1061 #endif
1062 
1063 #ifdef USE_ESP32_CAMERA
1064 void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
1065  if (!this->state_subscription_)
1066  return;
1067  if (this->image_reader_.available())
1068  return;
1069  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
1070  image->was_requested_by(esphome::esp32_camera::IDLE))
1071  this->image_reader_.set_image(std::move(image));
1072 }
1075  msg.key = camera->get_object_id_hash();
1076  msg.object_id = camera->get_object_id();
1077  if (camera->has_own_name())
1078  msg.name = camera->get_name();
1079  msg.unique_id = get_default_unique_id("camera", camera);
1081  msg.icon = camera->get_icon();
1082  msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
1083  return this->send_list_entities_camera_response(msg);
1084 }
1086  if (esp32_camera::global_esp32_camera == nullptr)
1087  return;
1088 
1089  if (msg.single)
1091  if (msg.stream) {
1093 
1094  App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
1096  });
1097  }
1098 }
1099 #endif
1100 
1101 #ifdef USE_HOMEASSISTANT_TIME
1105 }
1106 #endif
1107 
1108 #ifdef USE_BLUETOOTH_PROXY
1111 }
1114 }
1116  if (this->client_api_version_major_ < 1 || this->client_api_version_minor_ < 7) {
1118  for (auto &service : resp.service_data) {
1119  service.legacy_data.assign(service.data.begin(), service.data.end());
1120  service.data.clear();
1121  }
1122  for (auto &manufacturer_data : resp.manufacturer_data) {
1123  manufacturer_data.legacy_data.assign(manufacturer_data.data.begin(), manufacturer_data.data.end());
1124  manufacturer_data.data.clear();
1125  }
1126  return this->send_bluetooth_le_advertisement_response(resp);
1127  }
1128  return this->send_bluetooth_le_advertisement_response(msg);
1129 }
1132 }
1135 }
1138 }
1141 }
1144 }
1147 }
1148 
1151 }
1152 
1158  return resp;
1159 }
1160 #endif
1161 
1162 #ifdef USE_VOICE_ASSISTANT
1164  if (voice_assistant::global_voice_assistant != nullptr) {
1166  }
1167 }
1169  if (voice_assistant::global_voice_assistant != nullptr) {
1170  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1171  return;
1172  }
1173 
1174  if (msg.error) {
1176  return;
1177  }
1178  if (msg.port == 0) {
1179  // Use API Audio
1181  } else {
1182  struct sockaddr_storage storage;
1183  socklen_t len = sizeof(storage);
1184  this->helper_->getpeername((struct sockaddr *) &storage, &len);
1186  }
1187  }
1188 };
1190  if (voice_assistant::global_voice_assistant != nullptr) {
1191  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1192  return;
1193  }
1194 
1196  }
1197 }
1199  if (voice_assistant::global_voice_assistant != nullptr) {
1200  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1201  return;
1202  }
1203 
1205  }
1206 };
1208  if (voice_assistant::global_voice_assistant != nullptr) {
1209  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1210  return;
1211  }
1212 
1214  }
1215 };
1216 
1218  if (voice_assistant::global_voice_assistant != nullptr) {
1219  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1220  return;
1221  }
1222 
1224  }
1225 }
1226 
1230  if (voice_assistant::global_voice_assistant != nullptr) {
1231  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1232  return resp;
1233  }
1234 
1236  for (auto &wake_word : config.available_wake_words) {
1237  VoiceAssistantWakeWord resp_wake_word;
1238  resp_wake_word.id = wake_word.id;
1239  resp_wake_word.wake_word = wake_word.wake_word;
1240  for (const auto &lang : wake_word.trained_languages) {
1241  resp_wake_word.trained_languages.push_back(lang);
1242  }
1243  resp.available_wake_words.push_back(std::move(resp_wake_word));
1244  }
1245  resp.max_active_wake_words = config.max_active_wake_words;
1246  }
1247  return resp;
1248 }
1249 
1251  if (voice_assistant::global_voice_assistant != nullptr) {
1252  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1253  return;
1254  }
1255 
1257  }
1258 }
1259 
1260 #endif
1261 
1262 #ifdef USE_ALARM_CONTROL_PANEL
1264  if (!this->state_subscription_)
1265  return false;
1266 
1268  resp.key = a_alarm_control_panel->get_object_id_hash();
1269  resp.state = static_cast<enums::AlarmControlPanelState>(a_alarm_control_panel->get_state());
1270  return this->send_alarm_control_panel_state_response(resp);
1271 }
1274  msg.key = a_alarm_control_panel->get_object_id_hash();
1275  msg.object_id = a_alarm_control_panel->get_object_id();
1276  msg.name = a_alarm_control_panel->get_name();
1277  msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel);
1278  msg.icon = a_alarm_control_panel->get_icon();
1279  msg.disabled_by_default = a_alarm_control_panel->is_disabled_by_default();
1280  msg.entity_category = static_cast<enums::EntityCategory>(a_alarm_control_panel->get_entity_category());
1281  msg.supported_features = a_alarm_control_panel->get_supported_features();
1282  msg.requires_code = a_alarm_control_panel->get_requires_code();
1283  msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm();
1285 }
1288  if (a_alarm_control_panel == nullptr)
1289  return;
1290 
1291  auto call = a_alarm_control_panel->make_call();
1292  switch (msg.command) {
1294  call.disarm();
1295  break;
1297  call.arm_away();
1298  break;
1300  call.arm_home();
1301  break;
1303  call.arm_night();
1304  break;
1306  call.arm_vacation();
1307  break;
1309  call.arm_custom_bypass();
1310  break;
1312  call.pending();
1313  break;
1314  }
1315  call.set_code(msg.code);
1316  call.perform();
1317 }
1318 #endif
1319 
1320 #ifdef USE_EVENT
1321 bool APIConnection::send_event(event::Event *event, std::string event_type) {
1322  EventResponse resp{};
1323  resp.key = event->get_object_id_hash();
1324  resp.event_type = std::move(event_type);
1325  return this->send_event_response(resp);
1326 }
1329  msg.key = event->get_object_id_hash();
1330  msg.object_id = event->get_object_id();
1331  if (event->has_own_name())
1332  msg.name = event->get_name();
1333  msg.unique_id = get_default_unique_id("event", event);
1334  msg.icon = event->get_icon();
1335  msg.disabled_by_default = event->is_disabled_by_default();
1336  msg.entity_category = static_cast<enums::EntityCategory>(event->get_entity_category());
1337  msg.device_class = event->get_device_class();
1338  for (const auto &event_type : event->get_event_types())
1339  msg.event_types.push_back(event_type);
1340  return this->send_list_entities_event_response(msg);
1341 }
1342 #endif
1343 
1344 #ifdef USE_UPDATE
1346  if (!this->state_subscription_)
1347  return false;
1348 
1349  UpdateStateResponse resp{};
1350  resp.key = update->get_object_id_hash();
1351  resp.missing_state = !update->has_state();
1352  if (update->has_state()) {
1353  resp.in_progress = update->state == update::UpdateState::UPDATE_STATE_INSTALLING;
1354  if (update->update_info.has_progress) {
1355  resp.has_progress = true;
1356  resp.progress = update->update_info.progress;
1357  }
1358  resp.current_version = update->update_info.current_version;
1359  resp.latest_version = update->update_info.latest_version;
1360  resp.title = update->update_info.title;
1361  resp.release_summary = update->update_info.summary;
1362  resp.release_url = update->update_info.release_url;
1363  }
1364 
1365  return this->send_update_state_response(resp);
1366 }
1369  msg.key = update->get_object_id_hash();
1370  msg.object_id = update->get_object_id();
1371  if (update->has_own_name())
1372  msg.name = update->get_name();
1373  msg.unique_id = get_default_unique_id("update", update);
1374  msg.icon = update->get_icon();
1376  msg.entity_category = static_cast<enums::EntityCategory>(update->get_entity_category());
1377  msg.device_class = update->get_device_class();
1378  return this->send_list_entities_update_response(msg);
1379 }
1382  if (update == nullptr)
1383  return;
1384 
1385  switch (msg.command) {
1387  update->perform();
1388  break;
1390  update->check();
1391  break;
1393  ESP_LOGE(TAG, "UPDATE_COMMAND_NONE not handled. Check client is sending the correct command");
1394  break;
1395  default:
1396  ESP_LOGW(TAG, "Unknown update command: %" PRIu32, msg.command);
1397  break;
1398  }
1399 }
1400 #endif
1401 
1402 bool APIConnection::send_log_message(int level, const char *tag, const char *line) {
1403  if (this->log_subscription_ < level)
1404  return false;
1405 
1406  // Send raw so that we don't copy too much
1407  auto buffer = this->create_buffer();
1408  // LogLevel level = 1;
1409  buffer.encode_uint32(1, static_cast<uint32_t>(level));
1410  // string message = 3;
1411  buffer.encode_string(3, line, strlen(line));
1412  // SubscribeLogsResponse - 29
1413  return this->send_buffer(buffer, 29);
1414 }
1415 
1417  this->client_info_ = msg.client_info;
1418  this->client_peername_ = this->helper_->getpeername();
1419  this->client_combined_info_ = this->client_info_ + " (" + this->client_peername_ + ")";
1420  this->helper_->set_log_info(this->client_combined_info_);
1423  ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
1424  this->client_peername_.c_str(), this->client_api_version_major_, this->client_api_version_minor_);
1425 
1426  HelloResponse resp;
1427  resp.api_version_major = 1;
1428  resp.api_version_minor = 10;
1429  resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
1430  resp.name = App.get_name();
1431 
1432  this->connection_state_ = ConnectionState::CONNECTED;
1433  return resp;
1434 }
1436  bool correct = this->parent_->check_password(msg.password);
1437 
1438  ConnectResponse resp;
1439  // bool invalid_password = 1;
1440  resp.invalid_password = !correct;
1441  if (correct) {
1442  ESP_LOGD(TAG, "%s: Connected successfully", this->client_combined_info_.c_str());
1443  this->connection_state_ = ConnectionState::AUTHENTICATED;
1445 #ifdef USE_HOMEASSISTANT_TIME
1447  this->send_time_request();
1448  }
1449 #endif
1450  }
1451  return resp;
1452 }
1454  DeviceInfoResponse resp{};
1455  resp.uses_password = this->parent_->uses_password();
1456  resp.name = App.get_name();
1457  resp.friendly_name = App.get_friendly_name();
1458  resp.suggested_area = App.get_area();
1459  resp.mac_address = get_mac_address_pretty();
1460  resp.esphome_version = ESPHOME_VERSION;
1461  resp.compilation_time = App.get_compilation_time();
1462 #if defined(USE_ESP8266) || defined(USE_ESP32)
1463  resp.manufacturer = "Espressif";
1464 #elif defined(USE_RP2040)
1465  resp.manufacturer = "Raspberry Pi";
1466 #elif defined(USE_BK72XX)
1467  resp.manufacturer = "Beken";
1468 #elif defined(USE_RTL87XX)
1469  resp.manufacturer = "Realtek";
1470 #elif defined(USE_HOST)
1471  resp.manufacturer = "Host";
1472 #endif
1473  resp.model = ESPHOME_BOARD;
1474 #ifdef USE_DEEP_SLEEP
1475  resp.has_deep_sleep = deep_sleep::global_has_deep_sleep;
1476 #endif
1477 #ifdef ESPHOME_PROJECT_NAME
1478  resp.project_name = ESPHOME_PROJECT_NAME;
1479  resp.project_version = ESPHOME_PROJECT_VERSION;
1480 #endif
1481 #ifdef USE_WEBSERVER
1482  resp.webserver_port = USE_WEBSERVER_PORT;
1483 #endif
1484 #ifdef USE_BLUETOOTH_PROXY
1485  resp.legacy_bluetooth_proxy_version = bluetooth_proxy::global_bluetooth_proxy->get_legacy_version();
1486  resp.bluetooth_proxy_feature_flags = bluetooth_proxy::global_bluetooth_proxy->get_feature_flags();
1487 #endif
1488 #ifdef USE_VOICE_ASSISTANT
1489  resp.legacy_voice_assistant_version = voice_assistant::global_voice_assistant->get_legacy_version();
1490  resp.voice_assistant_feature_flags = voice_assistant::global_voice_assistant->get_feature_flags();
1491 #endif
1492  return resp;
1493 }
1495  for (auto &it : this->parent_->get_state_subs()) {
1496  if (it.entity_id == msg.entity_id && it.attribute.value() == msg.attribute) {
1497  it.callback(msg.state);
1498  }
1499  }
1500 }
1502  bool found = false;
1503  for (auto *service : this->parent_->get_user_services()) {
1504  if (service->execute_service(msg)) {
1505  found = true;
1506  }
1507  }
1508  if (!found) {
1509  ESP_LOGV(TAG, "Could not find matching service!");
1510  }
1511 }
1513  state_subs_at_ = 0;
1514 }
1515 bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) {
1516  if (this->remove_)
1517  return false;
1518  if (!this->helper_->can_write_without_blocking()) {
1519  delay(0);
1520  APIError err = this->helper_->loop();
1521  if (err != APIError::OK) {
1522  on_fatal_error();
1523  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
1524  api_error_to_str(err), errno);
1525  return false;
1526  }
1527  if (!this->helper_->can_write_without_blocking()) {
1528  // SubscribeLogsResponse
1529  if (message_type != 29) {
1530  ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
1531  }
1532  delay(0);
1533  return false;
1534  }
1535  }
1536 
1537  APIError err = this->helper_->write_packet(message_type, buffer.get_buffer()->data(), buffer.get_buffer()->size());
1538  if (err == APIError::WOULD_BLOCK)
1539  return false;
1540  if (err != APIError::OK) {
1541  on_fatal_error();
1542  if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
1543  ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
1544  } else {
1545  ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
1546  errno);
1547  }
1548  return false;
1549  }
1550  // Do not set last_traffic_ on send
1551  return true;
1552 }
1554  this->on_fatal_error();
1555  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_combined_info_.c_str());
1556 }
1558  this->on_fatal_error();
1559  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_combined_info_.c_str());
1560 }
1562  this->helper_->close();
1563  this->remove_ = true;
1564 }
1565 
1566 } // namespace api
1567 } // 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
bool send_date_state_response(const DateStateResponse &msg)
bool send_time_info(datetime::TimeEntity *time)
AlarmControlPanelState get_state() const
Get the state.
enums::EntityCategory entity_category
Definition: api_pb2.h:660
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:500
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:592
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< MediaPlayerSupportedFormat > supported_formats
Definition: api_pb2.h:1300
void datetime_command(const DateTimeCommandRequest &msg) override
std::vector< uint8_t > * get_buffer() const
Definition: proto.h:268
void request_image(CameraRequester requester)
std::vector< std::string > active_wake_words
Definition: api_pb2.h:1890
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:418
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:123
LightCall & set_red(optional< float > red)
Set the red RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:600
void start_stream(CameraRequester requester)
enums::EntityCategory entity_category
Definition: api_pb2.h:1199
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:632
void on_set_configuration(const std::vector< std::string > &active_wake_words)
void on_voice_assistant_audio(const VoiceAssistantAudio &msg) override
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:1251
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:640
bool send_switch_state(switch_::Switch *a_switch, bool state)
TimeCall & set_time(uint8_t hour, uint8_t minute, uint8_t second)
Definition: time_entity.cpp:66
void update_command(const UpdateCommandRequest &msg) override
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:328
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:388
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
bool send_event_response(const EventResponse &msg)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
TextTraits traits
Definition: text.h:27
BluetoothConnectionsFreeResponse subscribe_bluetooth_connections_free(const SubscribeBluetoothConnectionsFreeRequest &msg) override
datetime::DateTimeEntity * get_datetime_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:368
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)
bool send_valve_state(valve::Valve *valve)
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< VoiceAssistantWakeWord > available_wake_words
Definition: api_pb2.h:1876
float position
The position of the valve from 0.0 (fully closed) to 1.0 (fully open).
Definition: valve.h:116
std::vector< enums::ClimatePreset > supported_presets
Definition: api_pb2.h:1010
Base class for all buttons.
Definition: button.h:29
enums::EntityCategory entity_category
Definition: api_pb2.h:1152
bool send_camera_info(esp32_camera::ESP32Camera *camera)
std::vector< std::string > options
Definition: api_pb2.h:1150
bool send_fan_state(fan::Fan *fan)
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
virtual FanTraits get_traits()=0
enums::EntityCategory entity_category
Definition: api_pb2.h:694
enums::EntityCategory entity_category
Definition: api_pb2.h:1957
std::set< std::string > get_event_types() const
Definition: event.h:28
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:398
bool send_binary_sensor_state_response(const BinarySensorStateResponse &msg)
const UpdateState & state
Definition: update_entity.h:40
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)
bool send_list_entities_valve_response(const ListEntitiesValveResponse &msg)
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:268
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:208
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:205
std::unique_ptr< APIFrameHelper > helper_
SelectTraits traits
Definition: select.h:34
enums::ClimateSwingMode swing_mode
Definition: api_pb2.h:1073
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
enums::EntityCategory entity_category
Definition: api_pb2.h:2194
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:584
enums::ColorMode color_mode
Definition: api_pb2.h:615
void send_camera_state(std::shared_ptr< esp32_camera::CameraImage > image)
bool send_date_info(datetime::DateEntity *date)
std::vector< BluetoothServiceData > service_data
Definition: api_pb2.h:1378
NumberCall & set_value(float value)
Definition: number_call.cpp:10
bool send_list_entities_event_response(const ListEntitiesEventResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:2108
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:1011
int get_max_length() const
Definition: text_traits.h:21
Base-class for all text inputs.
Definition: text.h:24
ValveCall & set_command_stop()
Set the command to stop the valve.
Definition: valve.cpp:59
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:560
float target_humidity
The target humidity of the climate device.
Definition: climate.h:196
virtual ValveTraits get_traits()=0
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)
bool send_event(event::Event *event, std::string event_type)
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:431
ValveCall make_call()
Construct a new valve call used to control the valve.
Definition: valve.cpp:127
bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor)
enums::ClimateFanMode fan_mode
Definition: api_pb2.h:1071
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:308
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)
bool send_event_info(event::Event *event)
bool send_update_info(update::UpdateEntity *update)
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
std::vector< std::string > event_types
Definition: api_pb2.h:2110
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:298
void perform()
Perform the valve call.
Definition: valve.cpp:71
void lock()
Turn this lock on.
Definition: lock.cpp:30
enums::EntityCategory entity_category
Definition: api_pb2.h:955
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:1077
void time_command(const TimeCommandRequest &msg) override
void date_command(const DateCommandRequest &msg) override
enums::EntityCategory entity_category
Definition: api_pb2.h:2008
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition: api_server.h:125
bool send_list_entities_text_sensor_response(const ListEntitiesTextSensorResponse &msg)
FanCall & set_speed(int speed)
Definition: fan.h:59
std::vector< std::string > trained_languages
Definition: api_pb2.h:1856
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
DateCall & set_date(uint16_t year, uint8_t month, uint8_t day)
Definition: date_entity.cpp:97
enums::EntityCategory entity_category
Definition: api_pb2.h:440
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:540
const UpdateInfo & update_info
Definition: update_entity.h:39
std::vector< std::string > effects
Definition: api_pb2.h:568
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:39
bool send_valve_info(valve::Valve *valve)
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
bool send_datetime_info(datetime::DateTimeEntity *datetime)
bool send_time_state(datetime::TimeEntity *time)
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:1009
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:181
enums::LockCommand command
Definition: api_pb2.h:1230
bool send_list_entities_button_response(const ListEntitiesButtonResponse &msg)
bool has_state() const
Return whether this Datetime has gotten a full state yet.
Definition: datetime_base.h:16
void on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) override
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:1937
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
bool send_update_state(update::UpdateEntity *update)
enums::EntityCategory entity_category
Definition: api_pb2.h:1103
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:552
void on_disconnect_response(const DisconnectResponse &value) override
light::LightState * get_light_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:318
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:202
bool send_list_entities_number_response(const ListEntitiesNumberResponse &msg)
bool send_switch_state_response(const SwitchStateResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:2142
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:648
VoiceAssistantConfigurationResponse voice_assistant_get_configuration(const VoiceAssistantConfigurationRequest &msg) override
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:258
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_date_time_state_response(const DateTimeStateResponse &msg)
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override
esphome::binary_sensor::BinarySensor * binary_sensor
Definition: statsd.h:40
NumberTraits traits
Definition: number.h:49
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
std::string client_info
Definition: api_pb2.h:244
enums::MediaPlayerFormatPurpose purpose
Definition: api_pb2.h:1279
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:1007
void on_audio(const api::VoiceAssistantAudio &msg)
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:656
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 on_timer_event(const api::VoiceAssistantTimerEventResponse &msg)
void switch_command(const SwitchCommandRequest &msg) override
ConnectResponse connect(const ConnectRequest &msg) override
MediaPlayerCall & set_announcement(bool announce)
bool send_light_info(light::LightState *light)
EntityCategory get_entity_category() const
Definition: entity_base.cpp:39
datetime::DateEntity * get_date_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:348
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)
ESPTime state_as_esptime() const override
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_list_entities_date_time_response(const ListEntitiesDateTimeResponse &msg)
bool send_date_state(datetime::DateEntity *date)
enums::EntityCategory entity_category
Definition: api_pb2.h:2239
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:378
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:374
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:338
enums::EntityCategory entity_category
Definition: api_pb2.h:499
bool send_list_entities_switch_response(const ListEntitiesSwitchResponse &msg)
enums::UpdateCommand command
Definition: api_pb2.h:2276
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:568
bool send_list_entities_date_response(const ListEntitiesDateResponse &msg)
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:608
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:561
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:22
Implementation of SPI Controller mode.
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:1379
void unlock()
Turn this lock off.
Definition: lock.cpp:35
Base class for all valve devices.
Definition: valve.h:105
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)
ValveOperation current_operation
The current operation of the valve (idle, opening, closing).
Definition: valve.h:110
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
bool send_list_entities_update_response(const ListEntitiesUpdateResponse &msg)
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)
bool send_list_entities_time_response(const ListEntitiesTimeResponse &msg)
NumberMode get_mode() const
Definition: number_traits.h:29
valve::Valve * get_valve_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:408
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
void on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) override
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:624
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:576
bool send_datetime_state(datetime::DateTimeEntity *datetime)
update::UpdateEntity * get_update_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:453
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:472
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:693
datetime::TimeEntity * get_time_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:358
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:616
std::vector< enums::ClimateSwingMode > supported_swing_modes
Definition: api_pb2.h:1008
void on_announce(const api::VoiceAssistantAnnounceRequest &msg)
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
DateTimeCall & set_datetime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
enums::SensorStateClass state_class
Definition: api_pb2.h:657
HelloResponse hello(const HelloRequest &msg) override
bool send_valve_state_response(const ValveStateResponse &msg)
std::string get_compilation_time() const
Definition: application.h:215
ValveCall & set_position(float position)
Set the call to a certain target position.
Definition: valve.cpp:67
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:571
bool send_time_state_response(const TimeStateResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:1014
std::vector< uint8_t > container
enums::MediaPlayerCommand command
Definition: api_pb2.h:1330
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
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
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:1001
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
void valve_command(const ValveCommandRequest &msg) override
void voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) override
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
enums::EntityCategory entity_category
Definition: api_pb2.h:2058
bool send_update_state_response(const UpdateStateResponse &msg)