ESPHome  2024.11.2
api_connection.cpp
Go to the documentation of this file.
1 #include "api_connection.h"
2 #ifdef USE_API
3 #include <cerrno>
4 #include <cinttypes>
5 #include <utility>
8 #include "esphome/core/hal.h"
9 #include "esphome/core/log.h"
10 #include "esphome/core/version.h"
11 
12 #ifdef USE_DEEP_SLEEP
14 #endif
15 #ifdef USE_HOMEASSISTANT_TIME
17 #endif
18 #ifdef USE_BLUETOOTH_PROXY
20 #endif
21 #ifdef USE_VOICE_ASSISTANT
23 #endif
24 
25 namespace esphome {
26 namespace api {
27 
28 static const char *const TAG = "api.connection";
29 static const int ESP32_CAMERA_STOP_STREAM = 5000;
30 
31 APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
32  : parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
33  this->proto_write_buffer_.reserve(64);
34 
35 #if defined(USE_API_PLAINTEXT)
36  this->helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
37 #elif defined(USE_API_NOISE)
38  this->helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
39 #else
40 #error "No frame helper defined"
41 #endif
42 }
44  this->last_traffic_ = millis();
45 
46  APIError err = this->helper_->init();
47  if (err != APIError::OK) {
49  ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
50  errno);
51  return;
52  }
53  this->client_info_ = helper_->getpeername();
54  this->client_peername_ = this->client_info_;
55  this->helper_->set_log_info(this->client_info_);
56 }
57 
59 #ifdef USE_BLUETOOTH_PROXY
60  if (bluetooth_proxy::global_bluetooth_proxy->get_api_connection() == this) {
62  }
63 #endif
64 #ifdef USE_VOICE_ASSISTANT
65  if (voice_assistant::global_voice_assistant->get_api_connection() == this) {
67  }
68 #endif
69 }
70 
72  if (this->remove_)
73  return;
74 
75  if (!network::is_connected()) {
76  // when network is disconnected force disconnect immediately
77  // don't wait for timeout
78  this->on_fatal_error();
79  ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", this->client_combined_info_.c_str());
80  return;
81  }
82  if (this->next_close_) {
83  // requested a disconnect
84  this->helper_->close();
85  this->remove_ = true;
86  return;
87  }
88 
89  APIError err = this->helper_->loop();
90  if (err != APIError::OK) {
92  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
93  api_error_to_str(err), errno);
94  return;
95  }
96  ReadPacketBuffer buffer;
97  err = this->helper_->read_packet(&buffer);
98  if (err == APIError::WOULD_BLOCK) {
99  // pass
100  } else if (err != APIError::OK) {
101  on_fatal_error();
102  if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) {
103  ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
104  } else if (err == APIError::CONNECTION_CLOSED) {
105  ESP_LOGW(TAG, "%s: Connection closed", this->client_combined_info_.c_str());
106  } else {
107  ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
108  errno);
109  }
110  return;
111  } else {
112  this->last_traffic_ = millis();
113  // read a packet
114  this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]);
115  if (this->remove_)
116  return;
117  }
118 
121 
122  static uint32_t keepalive = 60000;
123  static uint8_t max_ping_retries = 60;
124  static uint16_t ping_retry_interval = 1000;
125  const uint32_t now = millis();
126  if (this->sent_ping_) {
127  // Disconnect if not responded within 2.5*keepalive
128  if (now - this->last_traffic_ > (keepalive * 5) / 2) {
129  on_fatal_error();
130  ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_combined_info_.c_str());
131  }
132  } else if (now - this->last_traffic_ > keepalive && now > this->next_ping_retry_) {
133  ESP_LOGVV(TAG, "Sending keepalive PING...");
134  this->sent_ping_ = this->send_ping_request(PingRequest());
135  if (!this->sent_ping_) {
136  this->next_ping_retry_ = now + ping_retry_interval;
137  this->ping_retries_++;
138  if (this->ping_retries_ >= max_ping_retries) {
139  on_fatal_error();
140  ESP_LOGE(TAG, "%s: Sending keepalive failed %d time(s). Disconnecting...", this->client_combined_info_.c_str(),
141  this->ping_retries_);
142  } else if (this->ping_retries_ >= 10) {
143  ESP_LOGW(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
144  this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
145  } else {
146  ESP_LOGD(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
147  this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
148  }
149  }
150  }
151 
152 #ifdef USE_ESP32_CAMERA
153  if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) {
154  uint32_t to_send = std::min((size_t) 1024, this->image_reader_.available());
155  auto buffer = this->create_buffer();
156  // fixed32 key = 1;
157  buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
158  // bytes data = 2;
159  buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send);
160  // bool done = 3;
161  bool done = this->image_reader_.available() == to_send;
162  buffer.encode_bool(3, done);
163  bool success = this->send_buffer(buffer, 44);
164 
165  if (success) {
166  this->image_reader_.consume_data(to_send);
167  }
168  if (success && done) {
169  this->image_reader_.return_image();
170  }
171  }
172 #endif
173 
174  if (state_subs_at_ != -1) {
175  const auto &subs = this->parent_->get_state_subs();
176  if (state_subs_at_ >= (int) subs.size()) {
177  state_subs_at_ = -1;
178  } else {
179  auto &it = subs[state_subs_at_];
181  resp.entity_id = it.entity_id;
182  resp.attribute = it.attribute.value();
183  resp.once = it.once;
185  state_subs_at_++;
186  }
187  }
188  }
189 }
190 
191 std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
192  return App.get_name() + component_type + entity->get_object_id();
193 }
194 
196  // remote initiated disconnect_client
197  // don't close yet, we still need to send the disconnect response
198  // close will happen on next loop
199  ESP_LOGD(TAG, "%s requested disconnected", this->client_combined_info_.c_str());
200  this->next_close_ = true;
201  DisconnectResponse resp;
202  return resp;
203 }
205  // pass
206 }
207 
208 #ifdef USE_BINARY_SENSOR
210  if (!this->state_subscription_)
211  return false;
212 
214  resp.key = binary_sensor->get_object_id_hash();
215  resp.state = state;
216  resp.missing_state = !binary_sensor->has_state();
217  return this->send_binary_sensor_state_response(resp);
218 }
221  msg.object_id = binary_sensor->get_object_id();
222  msg.key = binary_sensor->get_object_id_hash();
223  if (binary_sensor->has_own_name())
224  msg.name = binary_sensor->get_name();
225  msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
226  msg.device_class = binary_sensor->get_device_class();
227  msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
228  msg.disabled_by_default = binary_sensor->is_disabled_by_default();
229  msg.icon = binary_sensor->get_icon();
230  msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
232 }
233 #endif
234 
235 #ifdef USE_COVER
237  if (!this->state_subscription_)
238  return false;
239 
240  auto traits = cover->get_traits();
241  CoverStateResponse resp{};
242  resp.key = cover->get_object_id_hash();
243  resp.legacy_state =
245  resp.position = cover->position;
246  if (traits.get_supports_tilt())
247  resp.tilt = cover->tilt;
248  resp.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
249  return this->send_cover_state_response(resp);
250 }
252  auto traits = cover->get_traits();
254  msg.key = cover->get_object_id_hash();
255  msg.object_id = cover->get_object_id();
256  if (cover->has_own_name())
257  msg.name = cover->get_name();
258  msg.unique_id = get_default_unique_id("cover", cover);
259  msg.assumed_state = traits.get_is_assumed_state();
260  msg.supports_position = traits.get_supports_position();
261  msg.supports_tilt = traits.get_supports_tilt();
262  msg.supports_stop = traits.get_supports_stop();
263  msg.device_class = cover->get_device_class();
265  msg.icon = cover->get_icon();
266  msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
267  return this->send_list_entities_cover_response(msg);
268 }
270  cover::Cover *cover = App.get_cover_by_key(msg.key);
271  if (cover == nullptr)
272  return;
273 
274  auto call = cover->make_call();
275  if (msg.has_legacy_command) {
276  switch (msg.legacy_command) {
278  call.set_command_open();
279  break;
281  call.set_command_close();
282  break;
284  call.set_command_stop();
285  break;
286  }
287  }
288  if (msg.has_position)
289  call.set_position(msg.position);
290  if (msg.has_tilt)
291  call.set_tilt(msg.tilt);
292  if (msg.stop)
293  call.set_command_stop();
294  call.perform();
295 }
296 #endif
297 
298 #ifdef USE_FAN
300  if (!this->state_subscription_)
301  return false;
302 
303  auto traits = fan->get_traits();
304  FanStateResponse resp{};
305  resp.key = fan->get_object_id_hash();
306  resp.state = fan->state;
307  if (traits.supports_oscillation())
308  resp.oscillating = fan->oscillating;
309  if (traits.supports_speed()) {
310  resp.speed_level = fan->speed;
311  }
312  if (traits.supports_direction())
313  resp.direction = static_cast<enums::FanDirection>(fan->direction);
314  if (traits.supports_preset_modes())
315  resp.preset_mode = fan->preset_mode;
316  return this->send_fan_state_response(resp);
317 }
319  auto traits = fan->get_traits();
321  msg.key = fan->get_object_id_hash();
322  msg.object_id = fan->get_object_id();
323  if (fan->has_own_name())
324  msg.name = fan->get_name();
325  msg.unique_id = get_default_unique_id("fan", fan);
326  msg.supports_oscillation = traits.supports_oscillation();
327  msg.supports_speed = traits.supports_speed();
328  msg.supports_direction = traits.supports_direction();
329  msg.supported_speed_count = traits.supported_speed_count();
330  for (auto const &preset : traits.supported_preset_modes())
331  msg.supported_preset_modes.push_back(preset);
333  msg.icon = fan->get_icon();
334  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
335  return this->send_list_entities_fan_response(msg);
336 }
338  fan::Fan *fan = App.get_fan_by_key(msg.key);
339  if (fan == nullptr)
340  return;
341 
342  auto call = fan->make_call();
343  if (msg.has_state)
344  call.set_state(msg.state);
345  if (msg.has_oscillating)
346  call.set_oscillating(msg.oscillating);
347  if (msg.has_speed_level) {
348  // Prefer level
349  call.set_speed(msg.speed_level);
350  }
351  if (msg.has_direction)
352  call.set_direction(static_cast<fan::FanDirection>(msg.direction));
353  if (msg.has_preset_mode)
354  call.set_preset_mode(msg.preset_mode);
355  call.perform();
356 }
357 #endif
358 
359 #ifdef USE_LIGHT
361  if (!this->state_subscription_)
362  return false;
363 
364  auto traits = light->get_traits();
365  auto values = light->remote_values;
366  auto color_mode = values.get_color_mode();
367  LightStateResponse resp{};
368 
369  resp.key = light->get_object_id_hash();
370  resp.state = values.is_on();
371  resp.color_mode = static_cast<enums::ColorMode>(color_mode);
372  resp.brightness = values.get_brightness();
373  resp.color_brightness = values.get_color_brightness();
374  resp.red = values.get_red();
375  resp.green = values.get_green();
376  resp.blue = values.get_blue();
377  resp.white = values.get_white();
378  resp.color_temperature = values.get_color_temperature();
379  resp.cold_white = values.get_cold_white();
380  resp.warm_white = values.get_warm_white();
381  if (light->supports_effects())
382  resp.effect = light->get_effect_name();
383  return this->send_light_state_response(resp);
384 }
386  auto traits = light->get_traits();
388  msg.key = light->get_object_id_hash();
389  msg.object_id = light->get_object_id();
390  if (light->has_own_name())
391  msg.name = light->get_name();
392  msg.unique_id = get_default_unique_id("light", light);
393 
395  msg.icon = light->get_icon();
396  msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
397 
398  for (auto mode : traits.get_supported_color_modes())
399  msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
400 
401  msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS);
402  msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB);
404  msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) ||
405  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE));
406  msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
407  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE);
408 
410  msg.min_mireds = traits.get_min_mireds();
411  msg.max_mireds = traits.get_max_mireds();
412  }
413  if (light->supports_effects()) {
414  msg.effects.emplace_back("None");
415  for (auto *effect : light->get_effects())
416  msg.effects.push_back(effect->get_name());
417  }
418  return this->send_list_entities_light_response(msg);
419 }
422  if (light == nullptr)
423  return;
424 
425  auto call = light->make_call();
426  if (msg.has_state)
427  call.set_state(msg.state);
428  if (msg.has_brightness)
429  call.set_brightness(msg.brightness);
430  if (msg.has_color_mode)
431  call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
432  if (msg.has_color_brightness)
434  if (msg.has_rgb) {
435  call.set_red(msg.red);
436  call.set_green(msg.green);
437  call.set_blue(msg.blue);
438  }
439  if (msg.has_white)
440  call.set_white(msg.white);
441  if (msg.has_color_temperature)
443  if (msg.has_cold_white)
444  call.set_cold_white(msg.cold_white);
445  if (msg.has_warm_white)
446  call.set_warm_white(msg.warm_white);
447  if (msg.has_transition_length)
449  if (msg.has_flash_length)
450  call.set_flash_length(msg.flash_length);
451  if (msg.has_effect)
452  call.set_effect(msg.effect);
453  call.perform();
454 }
455 #endif
456 
457 #ifdef USE_SENSOR
459  if (!this->state_subscription_)
460  return false;
461 
462  SensorStateResponse resp{};
463  resp.key = sensor->get_object_id_hash();
464  resp.state = state;
465  resp.missing_state = !sensor->has_state();
466  return this->send_sensor_state_response(resp);
467 }
470  msg.key = sensor->get_object_id_hash();
471  msg.object_id = sensor->get_object_id();
472  if (sensor->has_own_name())
473  msg.name = sensor->get_name();
474  msg.unique_id = sensor->unique_id();
475  if (msg.unique_id.empty())
476  msg.unique_id = get_default_unique_id("sensor", sensor);
477  msg.icon = sensor->get_icon();
480  msg.force_update = sensor->get_force_update();
481  msg.device_class = sensor->get_device_class();
482  msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
484  msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
485  return this->send_list_entities_sensor_response(msg);
486 }
487 #endif
488 
489 #ifdef USE_SWITCH
491  if (!this->state_subscription_)
492  return false;
493 
494  SwitchStateResponse resp{};
495  resp.key = a_switch->get_object_id_hash();
496  resp.state = state;
497  return this->send_switch_state_response(resp);
498 }
501  msg.key = a_switch->get_object_id_hash();
502  msg.object_id = a_switch->get_object_id();
503  if (a_switch->has_own_name())
504  msg.name = a_switch->get_name();
505  msg.unique_id = get_default_unique_id("switch", a_switch);
506  msg.icon = a_switch->get_icon();
507  msg.assumed_state = a_switch->assumed_state();
509  msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
510  msg.device_class = a_switch->get_device_class();
511  return this->send_list_entities_switch_response(msg);
512 }
514  switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
515  if (a_switch == nullptr)
516  return;
517 
518  if (msg.state) {
519  a_switch->turn_on();
520  } else {
521  a_switch->turn_off();
522  }
523 }
524 #endif
525 
526 #ifdef USE_TEXT_SENSOR
528  if (!this->state_subscription_)
529  return false;
530 
532  resp.key = text_sensor->get_object_id_hash();
533  resp.state = std::move(state);
534  resp.missing_state = !text_sensor->has_state();
535  return this->send_text_sensor_state_response(resp);
536 }
539  msg.key = text_sensor->get_object_id_hash();
540  msg.object_id = text_sensor->get_object_id();
541  msg.name = text_sensor->get_name();
542  msg.unique_id = text_sensor->unique_id();
543  if (msg.unique_id.empty())
544  msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
545  msg.icon = text_sensor->get_icon();
546  msg.disabled_by_default = text_sensor->is_disabled_by_default();
547  msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
548  msg.device_class = text_sensor->get_device_class();
549  return this->send_list_entities_text_sensor_response(msg);
550 }
551 #endif
552 
553 #ifdef USE_CLIMATE
555  if (!this->state_subscription_)
556  return false;
557 
558  auto traits = climate->get_traits();
559  ClimateStateResponse resp{};
560  resp.key = climate->get_object_id_hash();
561  resp.mode = static_cast<enums::ClimateMode>(climate->mode);
562  resp.action = static_cast<enums::ClimateAction>(climate->action);
563  if (traits.get_supports_current_temperature())
564  resp.current_temperature = climate->current_temperature;
565  if (traits.get_supports_two_point_target_temperature()) {
566  resp.target_temperature_low = climate->target_temperature_low;
567  resp.target_temperature_high = climate->target_temperature_high;
568  } else {
569  resp.target_temperature = climate->target_temperature;
570  }
571  if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
572  resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
573  if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value())
574  resp.custom_fan_mode = climate->custom_fan_mode.value();
575  if (traits.get_supports_presets() && climate->preset.has_value()) {
576  resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
577  }
578  if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value())
579  resp.custom_preset = climate->custom_preset.value();
580  if (traits.get_supports_swing_modes())
581  resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
582  if (traits.get_supports_current_humidity())
583  resp.current_humidity = climate->current_humidity;
584  if (traits.get_supports_target_humidity())
585  resp.target_humidity = climate->target_humidity;
586  return this->send_climate_state_response(resp);
587 }
589  auto traits = climate->get_traits();
591  msg.key = climate->get_object_id_hash();
592  msg.object_id = climate->get_object_id();
593  if (climate->has_own_name())
594  msg.name = climate->get_name();
595  msg.unique_id = get_default_unique_id("climate", climate);
596 
598  msg.icon = climate->get_icon();
599  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
600 
601  msg.supports_current_temperature = traits.get_supports_current_temperature();
602  msg.supports_current_humidity = traits.get_supports_current_humidity();
603  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
604  msg.supports_target_humidity = traits.get_supports_target_humidity();
605 
606  for (auto mode : traits.get_supported_modes())
607  msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
608 
609  msg.visual_min_temperature = traits.get_visual_min_temperature();
610  msg.visual_max_temperature = traits.get_visual_max_temperature();
611  msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
612  msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
613  msg.visual_min_humidity = traits.get_visual_min_humidity();
614  msg.visual_max_humidity = traits.get_visual_max_humidity();
615 
616  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
617  msg.supports_action = traits.get_supports_action();
618 
619  for (auto fan_mode : traits.get_supported_fan_modes())
620  msg.supported_fan_modes.push_back(static_cast<enums::ClimateFanMode>(fan_mode));
621  for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
623  for (auto preset : traits.get_supported_presets())
624  msg.supported_presets.push_back(static_cast<enums::ClimatePreset>(preset));
625  for (auto const &custom_preset : traits.get_supported_custom_presets())
627  for (auto swing_mode : traits.get_supported_swing_modes())
628  msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
629  return this->send_list_entities_climate_response(msg);
630 }
632  climate::Climate *climate = App.get_climate_by_key(msg.key);
633  if (climate == nullptr)
634  return;
635 
636  auto call = climate->make_call();
637  if (msg.has_mode)
638  call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
639  if (msg.has_target_temperature)
645  if (msg.has_target_humidity)
647  if (msg.has_fan_mode)
648  call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
649  if (msg.has_custom_fan_mode)
650  call.set_fan_mode(msg.custom_fan_mode);
651  if (msg.has_preset)
652  call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
653  if (msg.has_custom_preset)
654  call.set_preset(msg.custom_preset);
655  if (msg.has_swing_mode)
656  call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
657  call.perform();
658 }
659 #endif
660 
661 #ifdef USE_NUMBER
663  if (!this->state_subscription_)
664  return false;
665 
666  NumberStateResponse resp{};
667  resp.key = number->get_object_id_hash();
668  resp.state = state;
669  resp.missing_state = !number->has_state();
670  return this->send_number_state_response(resp);
671 }
674  msg.key = number->get_object_id_hash();
675  msg.object_id = number->get_object_id();
676  if (number->has_own_name())
677  msg.name = number->get_name();
678  msg.unique_id = get_default_unique_id("number", number);
679  msg.icon = number->get_icon();
681  msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
683  msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
684  msg.device_class = number->traits.get_device_class();
685 
686  msg.min_value = number->traits.get_min_value();
687  msg.max_value = number->traits.get_max_value();
688  msg.step = number->traits.get_step();
689 
690  return this->send_list_entities_number_response(msg);
691 }
693  number::Number *number = App.get_number_by_key(msg.key);
694  if (number == nullptr)
695  return;
696 
697  auto call = number->make_call();
698  call.set_value(msg.state);
699  call.perform();
700 }
701 #endif
702 
703 #ifdef USE_DATETIME_DATE
705  if (!this->state_subscription_)
706  return false;
707 
708  DateStateResponse resp{};
709  resp.key = date->get_object_id_hash();
710  resp.missing_state = !date->has_state();
711  resp.year = date->year;
712  resp.month = date->month;
713  resp.day = date->day;
714  return this->send_date_state_response(resp);
715 }
718  msg.key = date->get_object_id_hash();
719  msg.object_id = date->get_object_id();
720  if (date->has_own_name())
721  msg.name = date->get_name();
722  msg.unique_id = get_default_unique_id("date", date);
723  msg.icon = date->get_icon();
725  msg.entity_category = static_cast<enums::EntityCategory>(date->get_entity_category());
726 
727  return this->send_list_entities_date_response(msg);
728 }
731  if (date == nullptr)
732  return;
733 
734  auto call = date->make_call();
735  call.set_date(msg.year, msg.month, msg.day);
736  call.perform();
737 }
738 #endif
739 
740 #ifdef USE_DATETIME_TIME
742  if (!this->state_subscription_)
743  return false;
744 
745  TimeStateResponse resp{};
746  resp.key = time->get_object_id_hash();
747  resp.missing_state = !time->has_state();
748  resp.hour = time->hour;
749  resp.minute = time->minute;
750  resp.second = time->second;
751  return this->send_time_state_response(resp);
752 }
755  msg.key = time->get_object_id_hash();
756  msg.object_id = time->get_object_id();
757  if (time->has_own_name())
758  msg.name = time->get_name();
759  msg.unique_id = get_default_unique_id("time", time);
760  msg.icon = time->get_icon();
762  msg.entity_category = static_cast<enums::EntityCategory>(time->get_entity_category());
763 
764  return this->send_list_entities_time_response(msg);
765 }
768  if (time == nullptr)
769  return;
770 
771  auto call = time->make_call();
772  call.set_time(msg.hour, msg.minute, msg.second);
773  call.perform();
774 }
775 #endif
776 
777 #ifdef USE_DATETIME_DATETIME
779  if (!this->state_subscription_)
780  return false;
781 
782  DateTimeStateResponse resp{};
783  resp.key = datetime->get_object_id_hash();
784  resp.missing_state = !datetime->has_state();
785  if (datetime->has_state()) {
786  ESPTime state = datetime->state_as_esptime();
787  resp.epoch_seconds = state.timestamp;
788  }
789  return this->send_date_time_state_response(resp);
790 }
793  msg.key = datetime->get_object_id_hash();
794  msg.object_id = datetime->get_object_id();
795  if (datetime->has_own_name())
796  msg.name = datetime->get_name();
797  msg.unique_id = get_default_unique_id("datetime", datetime);
798  msg.icon = datetime->get_icon();
800  msg.entity_category = static_cast<enums::EntityCategory>(datetime->get_entity_category());
801 
802  return this->send_list_entities_date_time_response(msg);
803 }
806  if (datetime == nullptr)
807  return;
808 
809  auto call = datetime->make_call();
810  call.set_datetime(msg.epoch_seconds);
811  call.perform();
812 }
813 #endif
814 
815 #ifdef USE_TEXT
817  if (!this->state_subscription_)
818  return false;
819 
820  TextStateResponse resp{};
821  resp.key = text->get_object_id_hash();
822  resp.state = std::move(state);
823  resp.missing_state = !text->has_state();
824  return this->send_text_state_response(resp);
825 }
828  msg.key = text->get_object_id_hash();
829  msg.object_id = text->get_object_id();
830  msg.name = text->get_name();
831  msg.icon = text->get_icon();
833  msg.entity_category = static_cast<enums::EntityCategory>(text->get_entity_category());
834  msg.mode = static_cast<enums::TextMode>(text->traits.get_mode());
835 
836  msg.min_length = text->traits.get_min_length();
837  msg.max_length = text->traits.get_max_length();
838  msg.pattern = text->traits.get_pattern();
839 
840  return this->send_list_entities_text_response(msg);
841 }
843  text::Text *text = App.get_text_by_key(msg.key);
844  if (text == nullptr)
845  return;
846 
847  auto call = text->make_call();
848  call.set_value(msg.state);
849  call.perform();
850 }
851 #endif
852 
853 #ifdef USE_SELECT
855  if (!this->state_subscription_)
856  return false;
857 
858  SelectStateResponse resp{};
859  resp.key = select->get_object_id_hash();
860  resp.state = std::move(state);
861  resp.missing_state = !select->has_state();
862  return this->send_select_state_response(resp);
863 }
866  msg.key = select->get_object_id_hash();
867  msg.object_id = select->get_object_id();
868  if (select->has_own_name())
869  msg.name = select->get_name();
870  msg.unique_id = get_default_unique_id("select", select);
871  msg.icon = select->get_icon();
873  msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
874 
875  for (const auto &option : select->traits.get_options())
876  msg.options.push_back(option);
877 
878  return this->send_list_entities_select_response(msg);
879 }
881  select::Select *select = App.get_select_by_key(msg.key);
882  if (select == nullptr)
883  return;
884 
885  auto call = select->make_call();
886  call.set_option(msg.state);
887  call.perform();
888 }
889 #endif
890 
891 #ifdef USE_BUTTON
894  msg.key = button->get_object_id_hash();
895  msg.object_id = button->get_object_id();
896  if (button->has_own_name())
897  msg.name = button->get_name();
898  msg.unique_id = get_default_unique_id("button", button);
899  msg.icon = button->get_icon();
901  msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
902  msg.device_class = button->get_device_class();
903  return this->send_list_entities_button_response(msg);
904 }
906  button::Button *button = App.get_button_by_key(msg.key);
907  if (button == nullptr)
908  return;
909 
910  button->press();
911 }
912 #endif
913 
914 #ifdef USE_LOCK
916  if (!this->state_subscription_)
917  return false;
918 
919  LockStateResponse resp{};
920  resp.key = a_lock->get_object_id_hash();
921  resp.state = static_cast<enums::LockState>(state);
922  return this->send_lock_state_response(resp);
923 }
926  msg.key = a_lock->get_object_id_hash();
927  msg.object_id = a_lock->get_object_id();
928  if (a_lock->has_own_name())
929  msg.name = a_lock->get_name();
930  msg.unique_id = get_default_unique_id("lock", a_lock);
931  msg.icon = a_lock->get_icon();
932  msg.assumed_state = a_lock->traits.get_assumed_state();
934  msg.entity_category = static_cast<enums::EntityCategory>(a_lock->get_entity_category());
935  msg.supports_open = a_lock->traits.get_supports_open();
936  msg.requires_code = a_lock->traits.get_requires_code();
937  return this->send_list_entities_lock_response(msg);
938 }
940  lock::Lock *a_lock = App.get_lock_by_key(msg.key);
941  if (a_lock == nullptr)
942  return;
943 
944  switch (msg.command) {
945  case enums::LOCK_UNLOCK:
946  a_lock->unlock();
947  break;
948  case enums::LOCK_LOCK:
949  a_lock->lock();
950  break;
951  case enums::LOCK_OPEN:
952  a_lock->open();
953  break;
954  }
955 }
956 #endif
957 
958 #ifdef USE_VALVE
960  if (!this->state_subscription_)
961  return false;
962 
963  ValveStateResponse resp{};
964  resp.key = valve->get_object_id_hash();
965  resp.position = valve->position;
966  resp.current_operation = static_cast<enums::ValveOperation>(valve->current_operation);
967  return this->send_valve_state_response(resp);
968 }
970  auto traits = valve->get_traits();
972  msg.key = valve->get_object_id_hash();
973  msg.object_id = valve->get_object_id();
974  if (valve->has_own_name())
975  msg.name = valve->get_name();
976  msg.unique_id = get_default_unique_id("valve", valve);
977  msg.icon = valve->get_icon();
979  msg.entity_category = static_cast<enums::EntityCategory>(valve->get_entity_category());
980  msg.device_class = valve->get_device_class();
981  msg.assumed_state = traits.get_is_assumed_state();
982  msg.supports_position = traits.get_supports_position();
983  msg.supports_stop = traits.get_supports_stop();
984  return this->send_list_entities_valve_response(msg);
985 }
987  valve::Valve *valve = App.get_valve_by_key(msg.key);
988  if (valve == nullptr)
989  return;
990 
991  auto call = valve->make_call();
992  if (msg.has_position)
993  call.set_position(msg.position);
994  if (msg.stop)
995  call.set_command_stop();
996  call.perform();
997 }
998 #endif
999 
1000 #ifdef USE_MEDIA_PLAYER
1002  if (!this->state_subscription_)
1003  return false;
1004 
1005  MediaPlayerStateResponse resp{};
1006  resp.key = media_player->get_object_id_hash();
1007 
1010  : media_player->state;
1011  resp.state = static_cast<enums::MediaPlayerState>(report_state);
1012  resp.volume = media_player->volume;
1013  resp.muted = media_player->is_muted();
1014  return this->send_media_player_state_response(resp);
1015 }
1018  msg.key = media_player->get_object_id_hash();
1019  msg.object_id = media_player->get_object_id();
1020  if (media_player->has_own_name())
1021  msg.name = media_player->get_name();
1022  msg.unique_id = get_default_unique_id("media_player", media_player);
1023  msg.icon = media_player->get_icon();
1024  msg.disabled_by_default = media_player->is_disabled_by_default();
1025  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
1026 
1027  auto traits = media_player->get_traits();
1028  msg.supports_pause = traits.get_supports_pause();
1029 
1030  for (auto &supported_format : traits.get_supported_formats()) {
1031  MediaPlayerSupportedFormat media_format;
1032  media_format.format = supported_format.format;
1033  media_format.sample_rate = supported_format.sample_rate;
1034  media_format.num_channels = supported_format.num_channels;
1035  media_format.purpose = static_cast<enums::MediaPlayerFormatPurpose>(supported_format.purpose);
1036  media_format.sample_bytes = supported_format.sample_bytes;
1037  msg.supported_formats.push_back(media_format);
1038  }
1039 
1040  return this->send_list_entities_media_player_response(msg);
1041 }
1044  if (media_player == nullptr)
1045  return;
1046 
1047  auto call = media_player->make_call();
1048  if (msg.has_command) {
1049  call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
1050  }
1051  if (msg.has_volume) {
1052  call.set_volume(msg.volume);
1053  }
1054  if (msg.has_media_url) {
1055  call.set_media_url(msg.media_url);
1056  }
1057  if (msg.has_announcement) {
1058  call.set_announcement(msg.announcement);
1059  }
1060  call.perform();
1061 }
1062 #endif
1063 
1064 #ifdef USE_ESP32_CAMERA
1065 void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
1066  if (!this->state_subscription_)
1067  return;
1068  if (this->image_reader_.available())
1069  return;
1070  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
1071  image->was_requested_by(esphome::esp32_camera::IDLE))
1072  this->image_reader_.set_image(std::move(image));
1073 }
1076  msg.key = camera->get_object_id_hash();
1077  msg.object_id = camera->get_object_id();
1078  if (camera->has_own_name())
1079  msg.name = camera->get_name();
1080  msg.unique_id = get_default_unique_id("camera", camera);
1082  msg.icon = camera->get_icon();
1083  msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
1084  return this->send_list_entities_camera_response(msg);
1085 }
1087  if (esp32_camera::global_esp32_camera == nullptr)
1088  return;
1089 
1090  if (msg.single)
1092  if (msg.stream) {
1094 
1095  App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
1097  });
1098  }
1099 }
1100 #endif
1101 
1102 #ifdef USE_HOMEASSISTANT_TIME
1106 }
1107 #endif
1108 
1109 #ifdef USE_BLUETOOTH_PROXY
1112 }
1115 }
1117  if (this->client_api_version_major_ < 1 || this->client_api_version_minor_ < 7) {
1119  for (auto &service : resp.service_data) {
1120  service.legacy_data.assign(service.data.begin(), service.data.end());
1121  service.data.clear();
1122  }
1123  for (auto &manufacturer_data : resp.manufacturer_data) {
1124  manufacturer_data.legacy_data.assign(manufacturer_data.data.begin(), manufacturer_data.data.end());
1125  manufacturer_data.data.clear();
1126  }
1127  return this->send_bluetooth_le_advertisement_response(resp);
1128  }
1129  return this->send_bluetooth_le_advertisement_response(msg);
1130 }
1133 }
1136 }
1139 }
1142 }
1145 }
1148 }
1149 
1152 }
1153 
1159  return resp;
1160 }
1161 #endif
1162 
1163 #ifdef USE_VOICE_ASSISTANT
1165  if (voice_assistant::global_voice_assistant != nullptr) {
1167  }
1168 }
1170  if (voice_assistant::global_voice_assistant != nullptr) {
1171  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1172  return;
1173  }
1174 
1175  if (msg.error) {
1177  return;
1178  }
1179  if (msg.port == 0) {
1180  // Use API Audio
1182  } else {
1183  struct sockaddr_storage storage;
1184  socklen_t len = sizeof(storage);
1185  this->helper_->getpeername((struct sockaddr *) &storage, &len);
1187  }
1188  }
1189 };
1191  if (voice_assistant::global_voice_assistant != nullptr) {
1192  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1193  return;
1194  }
1195 
1197  }
1198 }
1200  if (voice_assistant::global_voice_assistant != nullptr) {
1201  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1202  return;
1203  }
1204 
1206  }
1207 };
1209  if (voice_assistant::global_voice_assistant != nullptr) {
1210  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1211  return;
1212  }
1213 
1215  }
1216 };
1217 
1219  if (voice_assistant::global_voice_assistant != nullptr) {
1220  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1221  return;
1222  }
1223 
1225  }
1226 }
1227 
1231  if (voice_assistant::global_voice_assistant != nullptr) {
1232  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1233  return resp;
1234  }
1235 
1237  for (auto &wake_word : config.available_wake_words) {
1238  VoiceAssistantWakeWord resp_wake_word;
1239  resp_wake_word.id = wake_word.id;
1240  resp_wake_word.wake_word = wake_word.wake_word;
1241  for (const auto &lang : wake_word.trained_languages) {
1242  resp_wake_word.trained_languages.push_back(lang);
1243  }
1244  resp.available_wake_words.push_back(std::move(resp_wake_word));
1245  }
1246  for (auto &wake_word_id : config.active_wake_words) {
1247  resp.active_wake_words.push_back(wake_word_id);
1248  }
1249  resp.max_active_wake_words = config.max_active_wake_words;
1250  }
1251  return resp;
1252 }
1253 
1255  if (voice_assistant::global_voice_assistant != nullptr) {
1256  if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
1257  return;
1258  }
1259 
1261  }
1262 }
1263 
1264 #endif
1265 
1266 #ifdef USE_ALARM_CONTROL_PANEL
1268  if (!this->state_subscription_)
1269  return false;
1270 
1272  resp.key = a_alarm_control_panel->get_object_id_hash();
1273  resp.state = static_cast<enums::AlarmControlPanelState>(a_alarm_control_panel->get_state());
1274  return this->send_alarm_control_panel_state_response(resp);
1275 }
1278  msg.key = a_alarm_control_panel->get_object_id_hash();
1279  msg.object_id = a_alarm_control_panel->get_object_id();
1280  msg.name = a_alarm_control_panel->get_name();
1281  msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel);
1282  msg.icon = a_alarm_control_panel->get_icon();
1283  msg.disabled_by_default = a_alarm_control_panel->is_disabled_by_default();
1284  msg.entity_category = static_cast<enums::EntityCategory>(a_alarm_control_panel->get_entity_category());
1285  msg.supported_features = a_alarm_control_panel->get_supported_features();
1286  msg.requires_code = a_alarm_control_panel->get_requires_code();
1287  msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm();
1289 }
1292  if (a_alarm_control_panel == nullptr)
1293  return;
1294 
1295  auto call = a_alarm_control_panel->make_call();
1296  switch (msg.command) {
1298  call.disarm();
1299  break;
1301  call.arm_away();
1302  break;
1304  call.arm_home();
1305  break;
1307  call.arm_night();
1308  break;
1310  call.arm_vacation();
1311  break;
1313  call.arm_custom_bypass();
1314  break;
1316  call.pending();
1317  break;
1318  }
1319  call.set_code(msg.code);
1320  call.perform();
1321 }
1322 #endif
1323 
1324 #ifdef USE_EVENT
1325 bool APIConnection::send_event(event::Event *event, std::string event_type) {
1326  EventResponse resp{};
1327  resp.key = event->get_object_id_hash();
1328  resp.event_type = std::move(event_type);
1329  return this->send_event_response(resp);
1330 }
1333  msg.key = event->get_object_id_hash();
1334  msg.object_id = event->get_object_id();
1335  if (event->has_own_name())
1336  msg.name = event->get_name();
1337  msg.unique_id = get_default_unique_id("event", event);
1338  msg.icon = event->get_icon();
1339  msg.disabled_by_default = event->is_disabled_by_default();
1340  msg.entity_category = static_cast<enums::EntityCategory>(event->get_entity_category());
1341  msg.device_class = event->get_device_class();
1342  for (const auto &event_type : event->get_event_types())
1343  msg.event_types.push_back(event_type);
1344  return this->send_list_entities_event_response(msg);
1345 }
1346 #endif
1347 
1348 #ifdef USE_UPDATE
1350  if (!this->state_subscription_)
1351  return false;
1352 
1353  UpdateStateResponse resp{};
1354  resp.key = update->get_object_id_hash();
1355  resp.missing_state = !update->has_state();
1356  if (update->has_state()) {
1357  resp.in_progress = update->state == update::UpdateState::UPDATE_STATE_INSTALLING;
1358  if (update->update_info.has_progress) {
1359  resp.has_progress = true;
1360  resp.progress = update->update_info.progress;
1361  }
1362  resp.current_version = update->update_info.current_version;
1363  resp.latest_version = update->update_info.latest_version;
1364  resp.title = update->update_info.title;
1365  resp.release_summary = update->update_info.summary;
1366  resp.release_url = update->update_info.release_url;
1367  }
1368 
1369  return this->send_update_state_response(resp);
1370 }
1373  msg.key = update->get_object_id_hash();
1374  msg.object_id = update->get_object_id();
1375  if (update->has_own_name())
1376  msg.name = update->get_name();
1377  msg.unique_id = get_default_unique_id("update", update);
1378  msg.icon = update->get_icon();
1380  msg.entity_category = static_cast<enums::EntityCategory>(update->get_entity_category());
1381  msg.device_class = update->get_device_class();
1382  return this->send_list_entities_update_response(msg);
1383 }
1386  if (update == nullptr)
1387  return;
1388 
1389  switch (msg.command) {
1391  update->perform();
1392  break;
1394  update->check();
1395  break;
1397  ESP_LOGE(TAG, "UPDATE_COMMAND_NONE not handled. Check client is sending the correct command");
1398  break;
1399  default:
1400  ESP_LOGW(TAG, "Unknown update command: %" PRIu32, msg.command);
1401  break;
1402  }
1403 }
1404 #endif
1405 
1406 bool APIConnection::send_log_message(int level, const char *tag, const char *line) {
1407  if (this->log_subscription_ < level)
1408  return false;
1409 
1410  // Send raw so that we don't copy too much
1411  auto buffer = this->create_buffer();
1412  // LogLevel level = 1;
1413  buffer.encode_uint32(1, static_cast<uint32_t>(level));
1414  // string message = 3;
1415  buffer.encode_string(3, line, strlen(line));
1416  // SubscribeLogsResponse - 29
1417  return this->send_buffer(buffer, 29);
1418 }
1419 
1421  this->client_info_ = msg.client_info;
1422  this->client_peername_ = this->helper_->getpeername();
1423  this->client_combined_info_ = this->client_info_ + " (" + this->client_peername_ + ")";
1424  this->helper_->set_log_info(this->client_combined_info_);
1427  ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
1428  this->client_peername_.c_str(), this->client_api_version_major_, this->client_api_version_minor_);
1429 
1430  HelloResponse resp;
1431  resp.api_version_major = 1;
1432  resp.api_version_minor = 10;
1433  resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
1434  resp.name = App.get_name();
1435 
1436  this->connection_state_ = ConnectionState::CONNECTED;
1437  return resp;
1438 }
1440  bool correct = this->parent_->check_password(msg.password);
1441 
1442  ConnectResponse resp;
1443  // bool invalid_password = 1;
1444  resp.invalid_password = !correct;
1445  if (correct) {
1446  ESP_LOGD(TAG, "%s: Connected successfully", this->client_combined_info_.c_str());
1447  this->connection_state_ = ConnectionState::AUTHENTICATED;
1449 #ifdef USE_HOMEASSISTANT_TIME
1451  this->send_time_request();
1452  }
1453 #endif
1454  }
1455  return resp;
1456 }
1458  DeviceInfoResponse resp{};
1459  resp.uses_password = this->parent_->uses_password();
1460  resp.name = App.get_name();
1461  resp.friendly_name = App.get_friendly_name();
1462  resp.suggested_area = App.get_area();
1463  resp.mac_address = get_mac_address_pretty();
1464  resp.esphome_version = ESPHOME_VERSION;
1465  resp.compilation_time = App.get_compilation_time();
1466 #if defined(USE_ESP8266) || defined(USE_ESP32)
1467  resp.manufacturer = "Espressif";
1468 #elif defined(USE_RP2040)
1469  resp.manufacturer = "Raspberry Pi";
1470 #elif defined(USE_BK72XX)
1471  resp.manufacturer = "Beken";
1472 #elif defined(USE_RTL87XX)
1473  resp.manufacturer = "Realtek";
1474 #elif defined(USE_HOST)
1475  resp.manufacturer = "Host";
1476 #endif
1477  resp.model = ESPHOME_BOARD;
1478 #ifdef USE_DEEP_SLEEP
1479  resp.has_deep_sleep = deep_sleep::global_has_deep_sleep;
1480 #endif
1481 #ifdef ESPHOME_PROJECT_NAME
1482  resp.project_name = ESPHOME_PROJECT_NAME;
1483  resp.project_version = ESPHOME_PROJECT_VERSION;
1484 #endif
1485 #ifdef USE_WEBSERVER
1486  resp.webserver_port = USE_WEBSERVER_PORT;
1487 #endif
1488 #ifdef USE_BLUETOOTH_PROXY
1489  resp.legacy_bluetooth_proxy_version = bluetooth_proxy::global_bluetooth_proxy->get_legacy_version();
1490  resp.bluetooth_proxy_feature_flags = bluetooth_proxy::global_bluetooth_proxy->get_feature_flags();
1491 #endif
1492 #ifdef USE_VOICE_ASSISTANT
1493  resp.legacy_voice_assistant_version = voice_assistant::global_voice_assistant->get_legacy_version();
1494  resp.voice_assistant_feature_flags = voice_assistant::global_voice_assistant->get_feature_flags();
1495 #endif
1496  return resp;
1497 }
1499  for (auto &it : this->parent_->get_state_subs()) {
1500  if (it.entity_id == msg.entity_id && it.attribute.value() == msg.attribute) {
1501  it.callback(msg.state);
1502  }
1503  }
1504 }
1506  bool found = false;
1507  for (auto *service : this->parent_->get_user_services()) {
1508  if (service->execute_service(msg)) {
1509  found = true;
1510  }
1511  }
1512  if (!found) {
1513  ESP_LOGV(TAG, "Could not find matching service!");
1514  }
1515 }
1517  state_subs_at_ = 0;
1518 }
1519 bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) {
1520  if (this->remove_)
1521  return false;
1522  if (!this->helper_->can_write_without_blocking()) {
1523  delay(0);
1524  APIError err = this->helper_->loop();
1525  if (err != APIError::OK) {
1526  on_fatal_error();
1527  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
1528  api_error_to_str(err), errno);
1529  return false;
1530  }
1531  if (!this->helper_->can_write_without_blocking()) {
1532  // SubscribeLogsResponse
1533  if (message_type != 29) {
1534  ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
1535  }
1536  delay(0);
1537  return false;
1538  }
1539  }
1540 
1541  APIError err = this->helper_->write_packet(message_type, buffer.get_buffer()->data(), buffer.get_buffer()->size());
1542  if (err == APIError::WOULD_BLOCK)
1543  return false;
1544  if (err != APIError::OK) {
1545  on_fatal_error();
1546  if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
1547  ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
1548  } else {
1549  ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
1550  errno);
1551  }
1552  return false;
1553  }
1554  // Do not set last_traffic_ on send
1555  return true;
1556 }
1558  this->on_fatal_error();
1559  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_combined_info_.c_str());
1560 }
1562  this->on_fatal_error();
1563  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_combined_info_.c_str());
1564 }
1566  this->helper_->close();
1567  this->remove_ = true;
1568 }
1569 
1570 } // namespace api
1571 } // namespace esphome
1572 #endif
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:63
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:124
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:149
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:39
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:126
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:183
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:17
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)
std::vector< std::string > active_wake_words
Definition: api_pb2.h:1877
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:41
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:293
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:375
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:148
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:106
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:712
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:38
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)