ESPHome  2023.5.5
api_connection.cpp
Go to the documentation of this file.
1 #include "api_connection.h"
2 #include <cerrno>
3 #include <cinttypes>
6 #include "esphome/core/hal.h"
7 #include "esphome/core/log.h"
8 #include "esphome/core/version.h"
9 
10 #ifdef USE_DEEP_SLEEP
12 #endif
13 #ifdef USE_HOMEASSISTANT_TIME
15 #endif
16 #ifdef USE_BLUETOOTH_PROXY
18 #endif
19 #ifdef USE_VOICE_ASSISTANT
21 #endif
22 
23 namespace esphome {
24 namespace api {
25 
26 static const char *const TAG = "api.connection";
27 static const int ESP32_CAMERA_STOP_STREAM = 5000;
28 
29 APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
30  : parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
31  this->proto_write_buffer_.reserve(64);
32 
33 #if defined(USE_API_PLAINTEXT)
34  helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
35 #elif defined(USE_API_NOISE)
36  helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
37 #else
38 #error "No frame helper defined"
39 #endif
40 }
42  this->last_traffic_ = millis();
43 
44  APIError err = helper_->init();
45  if (err != APIError::OK) {
47  ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
48  return;
49  }
50  client_info_ = helper_->getpeername();
51  helper_->set_log_info(client_info_);
52 }
53 
55  if (this->remove_)
56  return;
57 
58  if (!network::is_connected()) {
59  // when network is disconnected force disconnect immediately
60  // don't wait for timeout
61  this->on_fatal_error();
62  ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", client_info_.c_str());
63  return;
64  }
65  if (this->next_close_) {
66  // requested a disconnect
67  this->helper_->close();
68  this->remove_ = true;
69  return;
70  }
71 
72  APIError err = helper_->loop();
73  if (err != APIError::OK) {
75  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
76  return;
77  }
78  ReadPacketBuffer buffer;
79  err = helper_->read_packet(&buffer);
80  if (err == APIError::WOULD_BLOCK) {
81  // pass
82  } else if (err != APIError::OK) {
84  if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) {
85  ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
86  } else if (err == APIError::CONNECTION_CLOSED) {
87  ESP_LOGW(TAG, "%s: Connection closed", client_info_.c_str());
88  } else {
89  ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
90  }
91  return;
92  } else {
93  this->last_traffic_ = millis();
94  // read a packet
95  this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]);
96  if (this->remove_)
97  return;
98  }
99 
102 
103  const uint32_t keepalive = 60000;
104  const uint32_t now = millis();
105  if (this->sent_ping_) {
106  // Disconnect if not responded within 2.5*keepalive
107  if (now - this->last_traffic_ > (keepalive * 5) / 2) {
108  on_fatal_error();
109  ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_info_.c_str());
110  }
111  } else if (now - this->last_traffic_ > keepalive) {
112  ESP_LOGVV(TAG, "Sending keepalive PING...");
113  this->sent_ping_ = true;
115  }
116 
117 #ifdef USE_ESP32_CAMERA
118  if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) {
119  uint32_t to_send = std::min((size_t) 1024, this->image_reader_.available());
120  auto buffer = this->create_buffer();
121  // fixed32 key = 1;
122  buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
123  // bytes data = 2;
124  buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send);
125  // bool done = 3;
126  bool done = this->image_reader_.available() == to_send;
127  buffer.encode_bool(3, done);
128  bool success = this->send_buffer(buffer, 44);
129 
130  if (success) {
131  this->image_reader_.consume_data(to_send);
132  }
133  if (success && done) {
134  this->image_reader_.return_image();
135  }
136  }
137 #endif
138 
139  if (state_subs_at_ != -1) {
140  const auto &subs = this->parent_->get_state_subs();
141  if (state_subs_at_ >= (int) subs.size()) {
142  state_subs_at_ = -1;
143  } else {
144  auto &it = subs[state_subs_at_];
146  resp.entity_id = it.entity_id;
147  resp.attribute = it.attribute.value();
149  state_subs_at_++;
150  }
151  }
152  }
153 }
154 
155 std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
156  return App.get_name() + component_type + entity->get_object_id();
157 }
158 
160  // remote initiated disconnect_client
161  // don't close yet, we still need to send the disconnect response
162  // close will happen on next loop
163  ESP_LOGD(TAG, "%s requested disconnected", client_info_.c_str());
164  this->next_close_ = true;
165  DisconnectResponse resp;
166  return resp;
167 }
169  // pass
170 }
171 
172 #ifdef USE_BINARY_SENSOR
174  if (!this->state_subscription_)
175  return false;
176 
178  resp.key = binary_sensor->get_object_id_hash();
179  resp.state = state;
180  resp.missing_state = !binary_sensor->has_state();
181  return this->send_binary_sensor_state_response(resp);
182 }
185  msg.object_id = binary_sensor->get_object_id();
186  msg.key = binary_sensor->get_object_id_hash();
187  if (binary_sensor->has_own_name())
188  msg.name = binary_sensor->get_name();
189  msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
190  msg.device_class = binary_sensor->get_device_class();
191  msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
192  msg.disabled_by_default = binary_sensor->is_disabled_by_default();
193  msg.icon = binary_sensor->get_icon();
194  msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
196 }
197 #endif
198 
199 #ifdef USE_COVER
201  if (!this->state_subscription_)
202  return false;
203 
204  auto traits = cover->get_traits();
205  CoverStateResponse resp{};
206  resp.key = cover->get_object_id_hash();
207  resp.legacy_state =
209  resp.position = cover->position;
210  if (traits.get_supports_tilt())
211  resp.tilt = cover->tilt;
212  resp.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
213  return this->send_cover_state_response(resp);
214 }
216  auto traits = cover->get_traits();
218  msg.key = cover->get_object_id_hash();
219  msg.object_id = cover->get_object_id();
220  if (cover->has_own_name())
221  msg.name = cover->get_name();
222  msg.unique_id = get_default_unique_id("cover", cover);
223  msg.assumed_state = traits.get_is_assumed_state();
224  msg.supports_position = traits.get_supports_position();
225  msg.supports_tilt = traits.get_supports_tilt();
226  msg.supports_stop = traits.get_supports_stop();
227  msg.device_class = cover->get_device_class();
229  msg.icon = cover->get_icon();
230  msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
231  return this->send_list_entities_cover_response(msg);
232 }
234  cover::Cover *cover = App.get_cover_by_key(msg.key);
235  if (cover == nullptr)
236  return;
237 
238  auto call = cover->make_call();
239  if (msg.has_legacy_command) {
240  switch (msg.legacy_command) {
242  call.set_command_open();
243  break;
245  call.set_command_close();
246  break;
248  call.set_command_stop();
249  break;
250  }
251  }
252  if (msg.has_position)
253  call.set_position(msg.position);
254  if (msg.has_tilt)
255  call.set_tilt(msg.tilt);
256  if (msg.stop)
257  call.set_command_stop();
258  call.perform();
259 }
260 #endif
261 
262 #ifdef USE_FAN
264  if (!this->state_subscription_)
265  return false;
266 
267  auto traits = fan->get_traits();
268  FanStateResponse resp{};
269  resp.key = fan->get_object_id_hash();
270  resp.state = fan->state;
271  if (traits.supports_oscillation())
272  resp.oscillating = fan->oscillating;
273  if (traits.supports_speed()) {
274  resp.speed_level = fan->speed;
275  }
276  if (traits.supports_direction())
277  resp.direction = static_cast<enums::FanDirection>(fan->direction);
278  return this->send_fan_state_response(resp);
279 }
281  auto traits = fan->get_traits();
283  msg.key = fan->get_object_id_hash();
284  msg.object_id = fan->get_object_id();
285  if (fan->has_own_name())
286  msg.name = fan->get_name();
287  msg.unique_id = get_default_unique_id("fan", fan);
288  msg.supports_oscillation = traits.supports_oscillation();
289  msg.supports_speed = traits.supports_speed();
290  msg.supports_direction = traits.supports_direction();
291  msg.supported_speed_count = traits.supported_speed_count();
293  msg.icon = fan->get_icon();
294  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
295  return this->send_list_entities_fan_response(msg);
296 }
298  fan::Fan *fan = App.get_fan_by_key(msg.key);
299  if (fan == nullptr)
300  return;
301 
302  auto call = fan->make_call();
303  if (msg.has_state)
304  call.set_state(msg.state);
305  if (msg.has_oscillating)
306  call.set_oscillating(msg.oscillating);
307  if (msg.has_speed_level) {
308  // Prefer level
309  call.set_speed(msg.speed_level);
310  }
311  if (msg.has_direction)
312  call.set_direction(static_cast<fan::FanDirection>(msg.direction));
313  call.perform();
314 }
315 #endif
316 
317 #ifdef USE_LIGHT
319  if (!this->state_subscription_)
320  return false;
321 
322  auto traits = light->get_traits();
323  auto values = light->remote_values;
324  auto color_mode = values.get_color_mode();
325  LightStateResponse resp{};
326 
327  resp.key = light->get_object_id_hash();
328  resp.state = values.is_on();
329  resp.color_mode = static_cast<enums::ColorMode>(color_mode);
330  resp.brightness = values.get_brightness();
331  resp.color_brightness = values.get_color_brightness();
332  resp.red = values.get_red();
333  resp.green = values.get_green();
334  resp.blue = values.get_blue();
335  resp.white = values.get_white();
336  resp.color_temperature = values.get_color_temperature();
337  resp.cold_white = values.get_cold_white();
338  resp.warm_white = values.get_warm_white();
339  if (light->supports_effects())
340  resp.effect = light->get_effect_name();
341  return this->send_light_state_response(resp);
342 }
344  auto traits = light->get_traits();
346  msg.key = light->get_object_id_hash();
347  msg.object_id = light->get_object_id();
348  if (light->has_own_name())
349  msg.name = light->get_name();
350  msg.unique_id = get_default_unique_id("light", light);
351 
353  msg.icon = light->get_icon();
354  msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
355 
356  for (auto mode : traits.get_supported_color_modes())
357  msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
358 
359  msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS);
360  msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB);
362  msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) ||
363  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE));
364  msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
365  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE);
366 
368  msg.min_mireds = traits.get_min_mireds();
369  msg.max_mireds = traits.get_max_mireds();
370  }
371  if (light->supports_effects()) {
372  msg.effects.emplace_back("None");
373  for (auto *effect : light->get_effects())
374  msg.effects.push_back(effect->get_name());
375  }
376  return this->send_list_entities_light_response(msg);
377 }
380  if (light == nullptr)
381  return;
382 
383  auto call = light->make_call();
384  if (msg.has_state)
385  call.set_state(msg.state);
386  if (msg.has_brightness)
387  call.set_brightness(msg.brightness);
388  if (msg.has_color_mode)
389  call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
390  if (msg.has_color_brightness)
392  if (msg.has_rgb) {
393  call.set_red(msg.red);
394  call.set_green(msg.green);
395  call.set_blue(msg.blue);
396  }
397  if (msg.has_white)
398  call.set_white(msg.white);
399  if (msg.has_color_temperature)
401  if (msg.has_cold_white)
402  call.set_cold_white(msg.cold_white);
403  if (msg.has_warm_white)
404  call.set_warm_white(msg.warm_white);
405  if (msg.has_transition_length)
407  if (msg.has_flash_length)
408  call.set_flash_length(msg.flash_length);
409  if (msg.has_effect)
410  call.set_effect(msg.effect);
411  call.perform();
412 }
413 #endif
414 
415 #ifdef USE_SENSOR
417  if (!this->state_subscription_)
418  return false;
419 
420  SensorStateResponse resp{};
421  resp.key = sensor->get_object_id_hash();
422  resp.state = state;
423  resp.missing_state = !sensor->has_state();
424  return this->send_sensor_state_response(resp);
425 }
428  msg.key = sensor->get_object_id_hash();
429  msg.object_id = sensor->get_object_id();
430  if (sensor->has_own_name())
431  msg.name = sensor->get_name();
432  msg.unique_id = sensor->unique_id();
433  if (msg.unique_id.empty())
434  msg.unique_id = get_default_unique_id("sensor", sensor);
435  msg.icon = sensor->get_icon();
438  msg.force_update = sensor->get_force_update();
439  msg.device_class = sensor->get_device_class();
440  msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
442  msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
443  return this->send_list_entities_sensor_response(msg);
444 }
445 #endif
446 
447 #ifdef USE_SWITCH
449  if (!this->state_subscription_)
450  return false;
451 
452  SwitchStateResponse resp{};
453  resp.key = a_switch->get_object_id_hash();
454  resp.state = state;
455  return this->send_switch_state_response(resp);
456 }
459  msg.key = a_switch->get_object_id_hash();
460  msg.object_id = a_switch->get_object_id();
461  if (a_switch->has_own_name())
462  msg.name = a_switch->get_name();
463  msg.unique_id = get_default_unique_id("switch", a_switch);
464  msg.icon = a_switch->get_icon();
465  msg.assumed_state = a_switch->assumed_state();
467  msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
468  msg.device_class = a_switch->get_device_class();
469  return this->send_list_entities_switch_response(msg);
470 }
472  switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
473  if (a_switch == nullptr)
474  return;
475 
476  if (msg.state) {
477  a_switch->turn_on();
478  } else {
479  a_switch->turn_off();
480  }
481 }
482 #endif
483 
484 #ifdef USE_TEXT_SENSOR
486  if (!this->state_subscription_)
487  return false;
488 
490  resp.key = text_sensor->get_object_id_hash();
491  resp.state = std::move(state);
492  resp.missing_state = !text_sensor->has_state();
493  return this->send_text_sensor_state_response(resp);
494 }
497  msg.key = text_sensor->get_object_id_hash();
498  msg.object_id = text_sensor->get_object_id();
499  msg.name = text_sensor->get_name();
500  msg.unique_id = text_sensor->unique_id();
501  if (msg.unique_id.empty())
502  msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
503  msg.icon = text_sensor->get_icon();
504  msg.disabled_by_default = text_sensor->is_disabled_by_default();
505  msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
506  return this->send_list_entities_text_sensor_response(msg);
507 }
508 #endif
509 
510 #ifdef USE_CLIMATE
512  if (!this->state_subscription_)
513  return false;
514 
515  auto traits = climate->get_traits();
516  ClimateStateResponse resp{};
517  resp.key = climate->get_object_id_hash();
518  resp.mode = static_cast<enums::ClimateMode>(climate->mode);
519  resp.action = static_cast<enums::ClimateAction>(climate->action);
520  if (traits.get_supports_current_temperature())
521  resp.current_temperature = climate->current_temperature;
522  if (traits.get_supports_two_point_target_temperature()) {
523  resp.target_temperature_low = climate->target_temperature_low;
524  resp.target_temperature_high = climate->target_temperature_high;
525  } else {
526  resp.target_temperature = climate->target_temperature;
527  }
528  if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
529  resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
530  if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value())
531  resp.custom_fan_mode = climate->custom_fan_mode.value();
532  if (traits.get_supports_presets() && climate->preset.has_value()) {
533  resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
534  }
535  if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value())
536  resp.custom_preset = climate->custom_preset.value();
537  if (traits.get_supports_swing_modes())
538  resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
539  return this->send_climate_state_response(resp);
540 }
542  auto traits = climate->get_traits();
544  msg.key = climate->get_object_id_hash();
545  msg.object_id = climate->get_object_id();
546  if (climate->has_own_name())
547  msg.name = climate->get_name();
548  msg.unique_id = get_default_unique_id("climate", climate);
549 
551  msg.icon = climate->get_icon();
552  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
553 
554  msg.supports_current_temperature = traits.get_supports_current_temperature();
555  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
556 
557  for (auto mode : traits.get_supported_modes())
558  msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
559 
560  msg.visual_min_temperature = traits.get_visual_min_temperature();
561  msg.visual_max_temperature = traits.get_visual_max_temperature();
562  msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
563  msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
564 
565  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
566  msg.supports_action = traits.get_supports_action();
567 
568  for (auto fan_mode : traits.get_supported_fan_modes())
569  msg.supported_fan_modes.push_back(static_cast<enums::ClimateFanMode>(fan_mode));
570  for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
572  for (auto preset : traits.get_supported_presets())
573  msg.supported_presets.push_back(static_cast<enums::ClimatePreset>(preset));
574  for (auto const &custom_preset : traits.get_supported_custom_presets())
576  for (auto swing_mode : traits.get_supported_swing_modes())
577  msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
578  return this->send_list_entities_climate_response(msg);
579 }
581  climate::Climate *climate = App.get_climate_by_key(msg.key);
582  if (climate == nullptr)
583  return;
584 
585  auto call = climate->make_call();
586  if (msg.has_mode)
587  call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
588  if (msg.has_target_temperature)
594  if (msg.has_fan_mode)
595  call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
596  if (msg.has_custom_fan_mode)
597  call.set_fan_mode(msg.custom_fan_mode);
598  if (msg.has_preset)
599  call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
600  if (msg.has_custom_preset)
601  call.set_preset(msg.custom_preset);
602  if (msg.has_swing_mode)
603  call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
604  call.perform();
605 }
606 #endif
607 
608 #ifdef USE_NUMBER
610  if (!this->state_subscription_)
611  return false;
612 
613  NumberStateResponse resp{};
614  resp.key = number->get_object_id_hash();
615  resp.state = state;
616  resp.missing_state = !number->has_state();
617  return this->send_number_state_response(resp);
618 }
621  msg.key = number->get_object_id_hash();
622  msg.object_id = number->get_object_id();
623  if (number->has_own_name())
624  msg.name = number->get_name();
625  msg.unique_id = get_default_unique_id("number", number);
626  msg.icon = number->get_icon();
628  msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
630  msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
631  msg.device_class = number->traits.get_device_class();
632 
633  msg.min_value = number->traits.get_min_value();
634  msg.max_value = number->traits.get_max_value();
635  msg.step = number->traits.get_step();
636 
637  return this->send_list_entities_number_response(msg);
638 }
640  number::Number *number = App.get_number_by_key(msg.key);
641  if (number == nullptr)
642  return;
643 
644  auto call = number->make_call();
645  call.set_value(msg.state);
646  call.perform();
647 }
648 #endif
649 
650 #ifdef USE_SELECT
652  if (!this->state_subscription_)
653  return false;
654 
655  SelectStateResponse resp{};
656  resp.key = select->get_object_id_hash();
657  resp.state = std::move(state);
658  resp.missing_state = !select->has_state();
659  return this->send_select_state_response(resp);
660 }
663  msg.key = select->get_object_id_hash();
664  msg.object_id = select->get_object_id();
665  if (select->has_own_name())
666  msg.name = select->get_name();
667  msg.unique_id = get_default_unique_id("select", select);
668  msg.icon = select->get_icon();
670  msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
671 
672  for (const auto &option : select->traits.get_options())
673  msg.options.push_back(option);
674 
675  return this->send_list_entities_select_response(msg);
676 }
678  select::Select *select = App.get_select_by_key(msg.key);
679  if (select == nullptr)
680  return;
681 
682  auto call = select->make_call();
683  call.set_option(msg.state);
684  call.perform();
685 }
686 #endif
687 
688 #ifdef USE_BUTTON
691  msg.key = button->get_object_id_hash();
692  msg.object_id = button->get_object_id();
693  if (button->has_own_name())
694  msg.name = button->get_name();
695  msg.unique_id = get_default_unique_id("button", button);
696  msg.icon = button->get_icon();
698  msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
699  msg.device_class = button->get_device_class();
700  return this->send_list_entities_button_response(msg);
701 }
703  button::Button *button = App.get_button_by_key(msg.key);
704  if (button == nullptr)
705  return;
706 
707  button->press();
708 }
709 #endif
710 
711 #ifdef USE_LOCK
713  if (!this->state_subscription_)
714  return false;
715 
716  LockStateResponse resp{};
717  resp.key = a_lock->get_object_id_hash();
718  resp.state = static_cast<enums::LockState>(state);
719  return this->send_lock_state_response(resp);
720 }
723  msg.key = a_lock->get_object_id_hash();
724  msg.object_id = a_lock->get_object_id();
725  if (a_lock->has_own_name())
726  msg.name = a_lock->get_name();
727  msg.unique_id = get_default_unique_id("lock", a_lock);
728  msg.icon = a_lock->get_icon();
729  msg.assumed_state = a_lock->traits.get_assumed_state();
731  msg.entity_category = static_cast<enums::EntityCategory>(a_lock->get_entity_category());
732  msg.supports_open = a_lock->traits.get_supports_open();
733  msg.requires_code = a_lock->traits.get_requires_code();
734  return this->send_list_entities_lock_response(msg);
735 }
737  lock::Lock *a_lock = App.get_lock_by_key(msg.key);
738  if (a_lock == nullptr)
739  return;
740 
741  switch (msg.command) {
742  case enums::LOCK_UNLOCK:
743  a_lock->unlock();
744  break;
745  case enums::LOCK_LOCK:
746  a_lock->lock();
747  break;
748  case enums::LOCK_OPEN:
749  a_lock->open();
750  break;
751  }
752 }
753 #endif
754 
755 #ifdef USE_MEDIA_PLAYER
757  if (!this->state_subscription_)
758  return false;
759 
761  resp.key = media_player->get_object_id_hash();
762  resp.state = static_cast<enums::MediaPlayerState>(media_player->state);
763  resp.volume = media_player->volume;
764  resp.muted = media_player->is_muted();
765  return this->send_media_player_state_response(resp);
766 }
769  msg.key = media_player->get_object_id_hash();
770  msg.object_id = media_player->get_object_id();
771  if (media_player->has_own_name())
772  msg.name = media_player->get_name();
773  msg.unique_id = get_default_unique_id("media_player", media_player);
774  msg.icon = media_player->get_icon();
775  msg.disabled_by_default = media_player->is_disabled_by_default();
776  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
777 
778  auto traits = media_player->get_traits();
779  msg.supports_pause = traits.get_supports_pause();
780 
782 }
785  if (media_player == nullptr)
786  return;
787 
788  auto call = media_player->make_call();
789  if (msg.has_command) {
790  call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
791  }
792  if (msg.has_volume) {
793  call.set_volume(msg.volume);
794  }
795  if (msg.has_media_url) {
796  call.set_media_url(msg.media_url);
797  }
798  call.perform();
799 }
800 #endif
801 
802 #ifdef USE_ESP32_CAMERA
803 void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
804  if (!this->state_subscription_)
805  return;
806  if (this->image_reader_.available())
807  return;
808  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
809  image->was_requested_by(esphome::esp32_camera::IDLE))
810  this->image_reader_.set_image(std::move(image));
811 }
814  msg.key = camera->get_object_id_hash();
815  msg.object_id = camera->get_object_id();
816  if (camera->has_own_name())
817  msg.name = camera->get_name();
818  msg.unique_id = get_default_unique_id("camera", camera);
820  msg.icon = camera->get_icon();
821  msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
822  return this->send_list_entities_camera_response(msg);
823 }
825  if (esp32_camera::global_esp32_camera == nullptr)
826  return;
827 
828  if (msg.single)
830  if (msg.stream) {
832 
833  App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
835  });
836  }
837 }
838 #endif
839 
840 #ifdef USE_HOMEASSISTANT_TIME
844 }
845 #endif
846 
847 #ifdef USE_BLUETOOTH_PROXY
850  return false;
851  if (this->client_api_version_major_ < 1 || this->client_api_version_minor_ < 7) {
853  for (auto &service : resp.service_data) {
854  service.legacy_data.assign(service.data.begin(), service.data.end());
855  service.data.clear();
856  }
857  for (auto &manufacturer_data : resp.manufacturer_data) {
858  manufacturer_data.legacy_data.assign(manufacturer_data.data.begin(), manufacturer_data.data.end());
859  manufacturer_data.data.clear();
860  }
861  return this->send_bluetooth_le_advertisement_response(resp);
862  }
864 }
867 }
870 }
873 }
876 }
879 }
882 }
883 
886 }
887 
893  return resp;
894 }
895 #endif
896 
897 #ifdef USE_VOICE_ASSISTANT
900  return false;
902  msg.start = start;
903  return this->send_voice_assistant_request(msg);
904 }
907  struct sockaddr_storage storage;
908  socklen_t len = sizeof(storage);
909  this->helper_->getpeername((struct sockaddr *) &storage, &len);
911  }
912 };
916  }
917 }
918 
919 #endif
920 
921 bool APIConnection::send_log_message(int level, const char *tag, const char *line) {
922  if (this->log_subscription_ < level)
923  return false;
924 
925  // Send raw so that we don't copy too much
926  auto buffer = this->create_buffer();
927  // LogLevel level = 1;
928  buffer.encode_uint32(1, static_cast<uint32_t>(level));
929  // string message = 3;
930  buffer.encode_string(3, line, strlen(line));
931  // SubscribeLogsResponse - 29
932  return this->send_buffer(buffer, 29);
933 }
934 
936  this->client_info_ = msg.client_info + " (" + this->helper_->getpeername() + ")";
937  this->helper_->set_log_info(client_info_);
940  ESP_LOGV(TAG, "Hello from client: '%s' | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
942 
943  HelloResponse resp;
944  resp.api_version_major = 1;
945  resp.api_version_minor = 8;
946  resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
947  resp.name = App.get_name();
948 
949  this->connection_state_ = ConnectionState::CONNECTED;
950  return resp;
951 }
953  bool correct = this->parent_->check_password(msg.password);
954 
955  ConnectResponse resp;
956  // bool invalid_password = 1;
957  resp.invalid_password = !correct;
958  if (correct) {
959  ESP_LOGD(TAG, "%s: Connected successfully", this->client_info_.c_str());
960  this->connection_state_ = ConnectionState::AUTHENTICATED;
961 
962 #ifdef USE_HOMEASSISTANT_TIME
964  this->send_time_request();
965  }
966 #endif
967  }
968  return resp;
969 }
971  DeviceInfoResponse resp{};
972  resp.uses_password = this->parent_->uses_password();
973  resp.name = App.get_name();
974  resp.friendly_name = App.get_friendly_name();
975  resp.mac_address = get_mac_address_pretty();
976  resp.esphome_version = ESPHOME_VERSION;
977  resp.compilation_time = App.get_compilation_time();
978 #if defined(USE_ESP8266) || defined(USE_ESP32)
979  resp.manufacturer = "Espressif";
980 #elif defined(USE_RP2040)
981  resp.manufacturer = "Raspberry Pi";
982 #elif defined(USE_HOST)
983  resp.manufacturer = "Host";
984 #endif
985  resp.model = ESPHOME_BOARD;
986 #ifdef USE_DEEP_SLEEP
987  resp.has_deep_sleep = deep_sleep::global_has_deep_sleep;
988 #endif
989 #ifdef ESPHOME_PROJECT_NAME
990  resp.project_name = ESPHOME_PROJECT_NAME;
991  resp.project_version = ESPHOME_PROJECT_VERSION;
992 #endif
993 #ifdef USE_WEBSERVER
994  resp.webserver_port = USE_WEBSERVER_PORT;
995 #endif
996 #ifdef USE_BLUETOOTH_PROXY
997  resp.bluetooth_proxy_version = bluetooth_proxy::global_bluetooth_proxy->has_active()
998  ? bluetooth_proxy::ACTIVE_CONNECTIONS_VERSION
999  : bluetooth_proxy::PASSIVE_ONLY_VERSION;
1000 #endif
1001 #ifdef USE_VOICE_ASSISTANT
1002  resp.voice_assistant_version = voice_assistant::global_voice_assistant->get_version();
1003 #endif
1004  return resp;
1005 }
1007  for (auto &it : this->parent_->get_state_subs()) {
1008  if (it.entity_id == msg.entity_id && it.attribute.value() == msg.attribute) {
1009  it.callback(msg.state);
1010  }
1011  }
1012 }
1014  bool found = false;
1015  for (auto *service : this->parent_->get_user_services()) {
1016  if (service->execute_service(msg)) {
1017  found = true;
1018  }
1019  }
1020  if (!found) {
1021  ESP_LOGV(TAG, "Could not find matching service!");
1022  }
1023 }
1025  state_subs_at_ = 0;
1026 }
1027 bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) {
1028  if (this->remove_)
1029  return false;
1030  if (!this->helper_->can_write_without_blocking()) {
1031  delay(0);
1032  APIError err = helper_->loop();
1033  if (err != APIError::OK) {
1034  on_fatal_error();
1035  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
1036  return false;
1037  }
1038  if (!this->helper_->can_write_without_blocking()) {
1039  // SubscribeLogsResponse
1040  if (message_type != 29) {
1041  ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
1042  }
1043  delay(0);
1044  return false;
1045  }
1046  }
1047 
1048  APIError err = this->helper_->write_packet(message_type, buffer.get_buffer()->data(), buffer.get_buffer()->size());
1049  if (err == APIError::WOULD_BLOCK)
1050  return false;
1051  if (err != APIError::OK) {
1052  on_fatal_error();
1053  if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
1054  ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
1055  } else {
1056  ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
1057  }
1058  return false;
1059  }
1060  // Do not set last_traffic_ on send
1061  return true;
1062 }
1064  this->on_fatal_error();
1065  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_info_.c_str());
1066 }
1068  this->on_fatal_error();
1069  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_info_.c_str());
1070 }
1072  this->helper_->close();
1073  this->remove_ = true;
1074 }
1075 
1076 } // namespace api
1077 } // 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:32
value_type const & value() const
Definition: optional.h:89
bool send_list_entities_binary_sensor_response(const ListEntitiesBinarySensorResponse &msg)
bool state
The current on/off state of the fan.
Definition: fan.h:103
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:185
bool has_own_name() const
Definition: entity_base.h:23
enums::EntityCategory entity_category
Definition: api_pb2.h:591
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
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:105
bool has_state() const
Return whether this number has gotten a full state yet.
Definition: number.h:52
bool send_fan_state_response(const FanStateResponse &msg)
std::vector< uint8_t > * get_buffer() const
Definition: proto.h:269
void request_image(CameraRequester requester)
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:291
bool send_climate_info(climate::Climate *climate)
MediaPlayerCall & set_command(MediaPlayerCommand command)
FanDirection direction
The current direction of the fan.
Definition: fan.h:109
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:114
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:1119
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 bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override
bool send_cover_info(cover::Cover *cover)
enums::EntityCategory entity_category
Definition: api_pb2.h:1171
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)
float target_temperature
The target temperature of the climate device.
Definition: climate.h:172
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:255
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:273
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition: cover.cpp:149
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
BluetoothConnectionsFreeResponse subscribe_bluetooth_connections_free(const SubscribeBluetoothConnectionsFreeRequest &msg) override
std::string get_default_unique_id(const std::string &component_type, EntityBase *entity)
InitialStateIterator initial_state_iterator_
const char * api_error_to_str(APIError err)
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: text_sensor.cpp:68
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition: cover.h:116
std::vector< enums::ClimatePreset > supported_presets
Definition: api_pb2.h:938
Base class for all buttons.
Definition: button.h:29
enums::EntityCategory entity_category
Definition: api_pb2.h:1072
bool send_camera_info(esp32_camera::ESP32Camera *camera)
std::vector< std::string > options
Definition: api_pb2.h:1070
bool send_fan_state(fan::Fan *fan)
virtual FanTraits get_traits()=0
bool send_climate_state_response(const ClimateStateResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:625
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:282
bool send_binary_sensor_state_response(const BinarySensorStateResponse &msg)
bool check_password(const std::string &password) const
Definition: api_server.cpp:147
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:164
bool send_button_info(button::Button *button)
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:201
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)
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:146
std::unique_ptr< APIFrameHelper > helper_
SelectTraits traits
Definition: select.h:27
enums::ClimateSwingMode swing_mode
Definition: api_pb2.h:995
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:177
void media_player_command(const MediaPlayerCommandRequest &msg) override
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:168
bool send_list_entities_select_response(const ListEntitiesSelectResponse &msg)
Color temperature can be controlled.
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:546
void send_camera_state(std::shared_ptr< esp32_camera::CameraImage > image)
std::vector< BluetoothServiceData > service_data
Definition: api_pb2.h:1277
NumberCall & set_value(float value)
Definition: number_call.cpp:10
void start(struct sockaddr_storage *addr, uint16_t port)
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:939
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
Definition: light_call.cpp:560
virtual bool is_muted() const
Definition: media_player.h:80
ClimateCall & set_swing_mode(ClimateSwingMode swing_mode)
Set the swing mode of the climate device.
Definition: climate.cpp:232
bool send_number_info(number::Number *number)
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:27
virtual MediaPlayerTraits get_traits()=0
ClimateSwingMode swing_mode
Definition: climate.h:539
void on_no_setup_connection() override
bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor)
enums::ClimateFanMode fan_mode
Definition: api_pb2.h:993
LockTraits traits
Definition: lock.h:124
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
Definition: climate.h:188
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:237
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:255
bool send_select_info(select::Select *select)
virtual std::string unique_id()
Override this method to set the unique ID of this sensor.
Definition: sensor.cpp:88
bool send_bluetooth_le_advertisement_response(const BluetoothLEAdvertisementResponse &msg)
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition: climate.cpp:443
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:228
void lock()
Turn this lock on.
Definition: lock.cpp:30
enums::EntityCategory entity_category
Definition: api_pb2.h:883
std::string get_icon() const
Definition: entity_base.cpp:30
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:37
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:251
enums::ClimatePreset preset
Definition: api_pb2.h:999
bool send_list_entities_text_sensor_response(const ListEntitiesTextSensorResponse &msg)
FanCall & set_speed(int speed)
Definition: fan.h:59
bool send_sensor_state(sensor::Sensor *sensor, float state)
Brightness of cold and warm white output can be controlled.
void bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg)
void press()
Press this button.
Definition: button.cpp:9
bool send_select_state(select::Select *select, std::string state)
std::vector< std::string > get_options() const
bool send_list_entities_light_response(const ListEntitiesLightResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:377
ESP32Camera * global_esp32_camera
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:191
enums::FanDirection direction
Definition: api_pb2.h:474
std::vector< std::string > effects
Definition: api_pb2.h:499
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:537
std::vector< uint8_t > proto_write_buffer_
bool send_list_entities_cover_response(const ListEntitiesCoverResponse &msg)
bool send_log_message(int level, const char *tag, const char *line)
Base-class for all numbers.
Definition: number.h:39
Brightness of white channel can be controlled separately from other channels.
int speed
The current fan speed level.
Definition: fan.h:107
bool send_list_entities_camera_response(const ListEntitiesCameraResponse &msg)
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition: climate.cpp:196
std::vector< std::string > supported_custom_fan_modes
Definition: api_pb2.h:937
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
enums::LockCommand command
Definition: api_pb2.h:1150
bool send_list_entities_button_response(const ListEntitiesButtonResponse &msg)
ClimateCall & set_fan_mode(ClimateFanMode fan_mode)
Set the fan mode of the climate device.
Definition: climate.cpp:154
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:416
void turn_on()
Turn this switch on.
Definition: switch.cpp:11
bool send_voice_assistant_request(const VoiceAssistantRequest &msg)
SelectCall make_call()
Instantiate a SelectCall object to modify this select component&#39;s state.
Definition: select.h:35
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)
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
Application App
Global storage of Application pointer - only one Application can exist.
void bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg)
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:33
enums::EntityCategory entity_category
Definition: api_pb2.h:1023
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:246
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:143
bool send_list_entities_number_response(const ListEntitiesNumberResponse &msg)
bool send_switch_state_response(const SwitchStateResponse &msg)
bool send_cover_state(cover::Cover *cover)
bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) override
LightCall & set_warm_white(optional< float > warm_white)
Set the warm white value of the light from 0.0 to 1.0.
Definition: light_call.cpp:648
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:192
bool send_list_entities_lock_response(const ListEntitiesLockResponse &msg)
ClimateFanMode fan_mode
Definition: climate.h:531
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override
NumberTraits traits
Definition: number.h:49
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
std::string client_info
Definition: api_pb2.h:184
void bluetooth_gatt_get_services(const BluetoothGATTGetServicesRequest &msg) override
bool send_list_entities_sensor_response(const ListEntitiesSensorResponse &msg)
const std::string & get_compilation_time() const
Definition: application.h:152
std::vector< enums::ClimateFanMode > supported_fan_modes
Definition: api_pb2.h:935
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:194
LightCall & set_effect(optional< std::string > effect)
Set the effect of the light by its name.
Definition: light_call.cpp:656
ClimateCall & set_target_temperature_high(float target_temperature_high)
Set the high point target temperature of the climate device.
Definition: climate.cpp:259
esp32_camera::CameraImageReader image_reader_
void bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg)
bool request_voice_assistant(bool start)
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:182
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:122
void switch_command(const SwitchCommandRequest &msg) override
ConnectResponse connect(const ConnectRequest &msg) override
bool send_light_info(light::LightState *light)
EntityCategory get_entity_category() const
Definition: entity_base.cpp:39
NumberCall make_call()
Definition: number.h:45
FanCall & set_state(bool binary_state)
Definition: fan.h:41
void select_command(const SelectCommandRequest &msg) override
bool send_light_state_response(const LightStateResponse &msg)
bool send_sensor_info(sensor::Sensor *sensor)
std::string size_t len
Definition: helpers.h:286
void open()
Open (unlatch) this lock.
Definition: lock.cpp:40
DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override
bool send_media_player_state_response(const MediaPlayerStateResponse &msg)
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:130
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:409
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:264
enums::EntityCategory entity_category
Definition: api_pb2.h:436
bool send_list_entities_switch_response(const ListEntitiesSwitchResponse &msg)
FanCall make_call()
Definition: fan.cpp:86
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 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:146
Base-class for all selects.
Definition: select.h:24
bool send_light_state(light::LightState *light)
std::vector< enums::ColorMode > supported_color_modes
Definition: api_pb2.h:492
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:22
Definition: a4988.cpp:4
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)
bool has_state() const
Return whether this select component has gotten a full state yet.
Definition: select.h:32
void lock_command(const LockCommandRequest &msg) override
std::vector< BluetoothServiceData > manufacturer_data
Definition: api_pb2.h:1278
void unlock()
Turn this lock off.
Definition: lock.cpp:35
bool send_media_player_state(media_player::MediaPlayer *media_player)
bool send_sensor_state_response(const SensorStateResponse &msg)
bool send_cover_state_response(const CoverStateResponse &msg)
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
Color can be controlled using RGB format (includes a brightness control for the color).
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:77
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
void light_command(const LightCommandRequest &msg) override
bool send_lock_info(lock::Lock *a_lock)
NumberMode get_mode() const
Definition: number_traits.h:29
bool send_list_entities_climate_response(const ListEntitiesClimateResponse &msg)
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:25
LightCall & set_white(optional< float > white)
Set the white value value of the light from 0.0 to 1.0 for RGBW[W] lights.
Definition: light_call.cpp: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
void number_command(const NumberCommandRequest &msg) override
ListEntitiesIterator list_entities_iterator_
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:409
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:485
bool send_lock_state_response(const LockStateResponse &msg)
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
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:936
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
enums::SensorStateClass state_class
Definition: api_pb2.h:588
HelloResponse hello(const HelloRequest &msg) override
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &msg)
enums::EntityCategory entity_category
Definition: api_pb2.h:502
enums::EntityCategory entity_category
Definition: api_pb2.h:942
std::vector< uint8_t > container
enums::MediaPlayerCommand command
Definition: api_pb2.h:1233
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:532
bool get_supports_open() const
Definition: lock.h:40
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:175
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:929
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:536
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:166
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:161
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:28
void fan_command(const FanCommandRequest &msg) override