ESPHome  2022.9.1
api_connection.cpp
Go to the documentation of this file.
1 #include "api_connection.h"
3 #include "esphome/core/log.h"
5 #include "esphome/core/version.h"
6 #include "esphome/core/hal.h"
7 #include <cerrno>
8 
9 #ifdef USE_DEEP_SLEEP
11 #endif
12 #ifdef USE_HOMEASSISTANT_TIME
14 #endif
15 
16 namespace esphome {
17 namespace api {
18 
19 static const char *const TAG = "api.connection";
20 static const int ESP32_CAMERA_STOP_STREAM = 5000;
21 
22 APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
23  : parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
24  this->proto_write_buffer_.reserve(64);
25 
26 #if defined(USE_API_PLAINTEXT)
27  helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
28 #elif defined(USE_API_NOISE)
29  helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
30 #else
31 #error "No frame helper defined"
32 #endif
33 }
35  this->last_traffic_ = millis();
36 
37  APIError err = helper_->init();
38  if (err != APIError::OK) {
40  ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
41  return;
42  }
43  client_info_ = helper_->getpeername();
44  helper_->set_log_info(client_info_);
45 }
46 
48  if (this->remove_)
49  return;
50 
51  if (!network::is_connected()) {
52  // when network is disconnected force disconnect immediately
53  // don't wait for timeout
54  this->on_fatal_error();
55  ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", client_info_.c_str());
56  return;
57  }
58  if (this->next_close_) {
59  // requested a disconnect
60  this->helper_->close();
61  this->remove_ = true;
62  return;
63  }
64 
65  APIError err = helper_->loop();
66  if (err != APIError::OK) {
68  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
69  return;
70  }
71  ReadPacketBuffer buffer;
72  err = helper_->read_packet(&buffer);
73  if (err == APIError::WOULD_BLOCK) {
74  // pass
75  } else if (err != APIError::OK) {
77  if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) {
78  ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
79  } else if (err == APIError::CONNECTION_CLOSED) {
80  ESP_LOGW(TAG, "%s: Connection closed", client_info_.c_str());
81  } else {
82  ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
83  }
84  return;
85  } else {
86  this->last_traffic_ = millis();
87  // read a packet
88  this->read_message(buffer.data_len, buffer.type, &buffer.container[buffer.data_offset]);
89  if (this->remove_)
90  return;
91  }
92 
95 
96  const uint32_t keepalive = 60000;
97  const uint32_t now = millis();
98  if (this->sent_ping_) {
99  // Disconnect if not responded within 2.5*keepalive
100  if (now - this->last_traffic_ > (keepalive * 5) / 2) {
101  on_fatal_error();
102  ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_info_.c_str());
103  }
104  } else if (now - this->last_traffic_ > keepalive) {
105  ESP_LOGVV(TAG, "Sending keepalive PING...");
106  this->sent_ping_ = true;
108  }
109 
110 #ifdef USE_ESP32_CAMERA
111  if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) {
112  uint32_t to_send = std::min((size_t) 1024, this->image_reader_.available());
113  auto buffer = this->create_buffer();
114  // fixed32 key = 1;
115  buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
116  // bytes data = 2;
117  buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send);
118  // bool done = 3;
119  bool done = this->image_reader_.available() == to_send;
120  buffer.encode_bool(3, done);
121  bool success = this->send_buffer(buffer, 44);
122 
123  if (success) {
124  this->image_reader_.consume_data(to_send);
125  }
126  if (success && done) {
127  this->image_reader_.return_image();
128  }
129  }
130 #endif
131 
132  if (state_subs_at_ != -1) {
133  const auto &subs = this->parent_->get_state_subs();
134  if (state_subs_at_ >= (int) subs.size()) {
135  state_subs_at_ = -1;
136  } else {
137  auto &it = subs[state_subs_at_];
139  resp.entity_id = it.entity_id;
140  resp.attribute = it.attribute.value();
142  state_subs_at_++;
143  }
144  }
145  }
146 }
147 
148 std::string get_default_unique_id(const std::string &component_type, EntityBase *entity) {
149  return App.get_name() + component_type + entity->get_object_id();
150 }
151 
153  // remote initiated disconnect_client
154  // don't close yet, we still need to send the disconnect response
155  // close will happen on next loop
156  ESP_LOGD(TAG, "%s requested disconnected", client_info_.c_str());
157  this->next_close_ = true;
158  DisconnectResponse resp;
159  return resp;
160 }
162  // pass
163 }
164 
165 #ifdef USE_BINARY_SENSOR
167  if (!this->state_subscription_)
168  return false;
169 
171  resp.key = binary_sensor->get_object_id_hash();
172  resp.state = state;
173  resp.missing_state = !binary_sensor->has_state();
174  return this->send_binary_sensor_state_response(resp);
175 }
178  msg.object_id = binary_sensor->get_object_id();
179  msg.key = binary_sensor->get_object_id_hash();
180  msg.name = binary_sensor->get_name();
181  msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
182  msg.device_class = binary_sensor->get_device_class();
183  msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
184  msg.disabled_by_default = binary_sensor->is_disabled_by_default();
185  msg.icon = binary_sensor->get_icon();
186  msg.entity_category = static_cast<enums::EntityCategory>(binary_sensor->get_entity_category());
188 }
189 #endif
190 
191 #ifdef USE_COVER
193  if (!this->state_subscription_)
194  return false;
195 
196  auto traits = cover->get_traits();
197  CoverStateResponse resp{};
198  resp.key = cover->get_object_id_hash();
199  resp.legacy_state =
201  resp.position = cover->position;
202  if (traits.get_supports_tilt())
203  resp.tilt = cover->tilt;
204  resp.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
205  return this->send_cover_state_response(resp);
206 }
208  auto traits = cover->get_traits();
210  msg.key = cover->get_object_id_hash();
211  msg.object_id = cover->get_object_id();
212  msg.name = cover->get_name();
213  msg.unique_id = get_default_unique_id("cover", cover);
214  msg.assumed_state = traits.get_is_assumed_state();
215  msg.supports_position = traits.get_supports_position();
216  msg.supports_tilt = traits.get_supports_tilt();
217  msg.device_class = cover->get_device_class();
219  msg.icon = cover->get_icon();
220  msg.entity_category = static_cast<enums::EntityCategory>(cover->get_entity_category());
221  return this->send_list_entities_cover_response(msg);
222 }
224  cover::Cover *cover = App.get_cover_by_key(msg.key);
225  if (cover == nullptr)
226  return;
227 
228  auto call = cover->make_call();
229  if (msg.has_legacy_command) {
230  switch (msg.legacy_command) {
232  call.set_command_open();
233  break;
235  call.set_command_close();
236  break;
238  call.set_command_stop();
239  break;
240  }
241  }
242  if (msg.has_position)
243  call.set_position(msg.position);
244  if (msg.has_tilt)
245  call.set_tilt(msg.tilt);
246  if (msg.stop)
247  call.set_command_stop();
248  call.perform();
249 }
250 #endif
251 
252 #ifdef USE_FAN
254  if (!this->state_subscription_)
255  return false;
256 
257  auto traits = fan->get_traits();
258  FanStateResponse resp{};
259  resp.key = fan->get_object_id_hash();
260  resp.state = fan->state;
261  if (traits.supports_oscillation())
262  resp.oscillating = fan->oscillating;
263  if (traits.supports_speed()) {
264  resp.speed_level = fan->speed;
265  }
266  if (traits.supports_direction())
267  resp.direction = static_cast<enums::FanDirection>(fan->direction);
268  return this->send_fan_state_response(resp);
269 }
271  auto traits = fan->get_traits();
273  msg.key = fan->get_object_id_hash();
274  msg.object_id = fan->get_object_id();
275  msg.name = fan->get_name();
276  msg.unique_id = get_default_unique_id("fan", fan);
277  msg.supports_oscillation = traits.supports_oscillation();
278  msg.supports_speed = traits.supports_speed();
279  msg.supports_direction = traits.supports_direction();
280  msg.supported_speed_count = traits.supported_speed_count();
282  msg.icon = fan->get_icon();
283  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
284  return this->send_list_entities_fan_response(msg);
285 }
287  fan::Fan *fan = App.get_fan_by_key(msg.key);
288  if (fan == nullptr)
289  return;
290 
291  auto call = fan->make_call();
292  if (msg.has_state)
293  call.set_state(msg.state);
294  if (msg.has_oscillating)
295  call.set_oscillating(msg.oscillating);
296  if (msg.has_speed_level) {
297  // Prefer level
298  call.set_speed(msg.speed_level);
299  }
300  if (msg.has_direction)
301  call.set_direction(static_cast<fan::FanDirection>(msg.direction));
302  call.perform();
303 }
304 #endif
305 
306 #ifdef USE_LIGHT
308  if (!this->state_subscription_)
309  return false;
310 
311  auto traits = light->get_traits();
312  auto values = light->remote_values;
313  auto color_mode = values.get_color_mode();
314  LightStateResponse resp{};
315 
316  resp.key = light->get_object_id_hash();
317  resp.state = values.is_on();
318  resp.color_mode = static_cast<enums::ColorMode>(color_mode);
319  resp.brightness = values.get_brightness();
320  resp.color_brightness = values.get_color_brightness();
321  resp.red = values.get_red();
322  resp.green = values.get_green();
323  resp.blue = values.get_blue();
324  resp.white = values.get_white();
325  resp.color_temperature = values.get_color_temperature();
326  resp.cold_white = values.get_cold_white();
327  resp.warm_white = values.get_warm_white();
328  if (light->supports_effects())
329  resp.effect = light->get_effect_name();
330  return this->send_light_state_response(resp);
331 }
333  auto traits = light->get_traits();
335  msg.key = light->get_object_id_hash();
336  msg.object_id = light->get_object_id();
337  msg.name = light->get_name();
338  msg.unique_id = get_default_unique_id("light", light);
339 
341  msg.icon = light->get_icon();
342  msg.entity_category = static_cast<enums::EntityCategory>(light->get_entity_category());
343 
344  for (auto mode : traits.get_supported_color_modes())
345  msg.supported_color_modes.push_back(static_cast<enums::ColorMode>(mode));
346 
347  msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS);
348  msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB);
350  msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) ||
351  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE));
352  msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) ||
353  traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE);
354 
356  msg.min_mireds = traits.get_min_mireds();
357  msg.max_mireds = traits.get_max_mireds();
358  }
359  if (light->supports_effects()) {
360  msg.effects.emplace_back("None");
361  for (auto *effect : light->get_effects())
362  msg.effects.push_back(effect->get_name());
363  }
364  return this->send_list_entities_light_response(msg);
365 }
368  if (light == nullptr)
369  return;
370 
371  auto call = light->make_call();
372  if (msg.has_state)
373  call.set_state(msg.state);
374  if (msg.has_brightness)
375  call.set_brightness(msg.brightness);
376  if (msg.has_color_mode)
377  call.set_color_mode(static_cast<light::ColorMode>(msg.color_mode));
378  if (msg.has_color_brightness)
380  if (msg.has_rgb) {
381  call.set_red(msg.red);
382  call.set_green(msg.green);
383  call.set_blue(msg.blue);
384  }
385  if (msg.has_white)
386  call.set_white(msg.white);
387  if (msg.has_color_temperature)
389  if (msg.has_cold_white)
390  call.set_cold_white(msg.cold_white);
391  if (msg.has_warm_white)
392  call.set_warm_white(msg.warm_white);
393  if (msg.has_transition_length)
395  if (msg.has_flash_length)
396  call.set_flash_length(msg.flash_length);
397  if (msg.has_effect)
398  call.set_effect(msg.effect);
399  call.perform();
400 }
401 #endif
402 
403 #ifdef USE_SENSOR
405  if (!this->state_subscription_)
406  return false;
407 
408  SensorStateResponse resp{};
409  resp.key = sensor->get_object_id_hash();
410  resp.state = state;
411  resp.missing_state = !sensor->has_state();
412  return this->send_sensor_state_response(resp);
413 }
416  msg.key = sensor->get_object_id_hash();
417  msg.object_id = sensor->get_object_id();
418  msg.name = sensor->get_name();
419  msg.unique_id = sensor->unique_id();
420  if (msg.unique_id.empty())
421  msg.unique_id = get_default_unique_id("sensor", sensor);
422  msg.icon = sensor->get_icon();
425  msg.force_update = sensor->get_force_update();
426  msg.device_class = sensor->get_device_class();
427  msg.state_class = static_cast<enums::SensorStateClass>(sensor->get_state_class());
429  msg.entity_category = static_cast<enums::EntityCategory>(sensor->get_entity_category());
430  return this->send_list_entities_sensor_response(msg);
431 }
432 #endif
433 
434 #ifdef USE_SWITCH
436  if (!this->state_subscription_)
437  return false;
438 
439  SwitchStateResponse resp{};
440  resp.key = a_switch->get_object_id_hash();
441  resp.state = state;
442  return this->send_switch_state_response(resp);
443 }
446  msg.key = a_switch->get_object_id_hash();
447  msg.object_id = a_switch->get_object_id();
448  msg.name = a_switch->get_name();
449  msg.unique_id = get_default_unique_id("switch", a_switch);
450  msg.icon = a_switch->get_icon();
451  msg.assumed_state = a_switch->assumed_state();
453  msg.entity_category = static_cast<enums::EntityCategory>(a_switch->get_entity_category());
454  msg.device_class = a_switch->get_device_class();
455  return this->send_list_entities_switch_response(msg);
456 }
458  switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
459  if (a_switch == nullptr)
460  return;
461 
462  if (msg.state) {
463  a_switch->turn_on();
464  } else {
465  a_switch->turn_off();
466  }
467 }
468 #endif
469 
470 #ifdef USE_TEXT_SENSOR
472  if (!this->state_subscription_)
473  return false;
474 
476  resp.key = text_sensor->get_object_id_hash();
477  resp.state = std::move(state);
478  resp.missing_state = !text_sensor->has_state();
479  return this->send_text_sensor_state_response(resp);
480 }
483  msg.key = text_sensor->get_object_id_hash();
484  msg.object_id = text_sensor->get_object_id();
485  msg.name = text_sensor->get_name();
486  msg.unique_id = text_sensor->unique_id();
487  if (msg.unique_id.empty())
488  msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
489  msg.icon = text_sensor->get_icon();
490  msg.disabled_by_default = text_sensor->is_disabled_by_default();
491  msg.entity_category = static_cast<enums::EntityCategory>(text_sensor->get_entity_category());
492  return this->send_list_entities_text_sensor_response(msg);
493 }
494 #endif
495 
496 #ifdef USE_CLIMATE
498  if (!this->state_subscription_)
499  return false;
500 
501  auto traits = climate->get_traits();
502  ClimateStateResponse resp{};
503  resp.key = climate->get_object_id_hash();
504  resp.mode = static_cast<enums::ClimateMode>(climate->mode);
505  resp.action = static_cast<enums::ClimateAction>(climate->action);
506  if (traits.get_supports_current_temperature())
507  resp.current_temperature = climate->current_temperature;
508  if (traits.get_supports_two_point_target_temperature()) {
509  resp.target_temperature_low = climate->target_temperature_low;
510  resp.target_temperature_high = climate->target_temperature_high;
511  } else {
512  resp.target_temperature = climate->target_temperature;
513  }
514  if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
515  resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
516  if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value())
517  resp.custom_fan_mode = climate->custom_fan_mode.value();
518  if (traits.get_supports_presets() && climate->preset.has_value()) {
519  resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
520  resp.legacy_away = resp.preset == enums::CLIMATE_PRESET_AWAY;
521  }
522  if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value())
523  resp.custom_preset = climate->custom_preset.value();
524  if (traits.get_supports_swing_modes())
525  resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
526  return this->send_climate_state_response(resp);
527 }
529  auto traits = climate->get_traits();
531  msg.key = climate->get_object_id_hash();
532  msg.object_id = climate->get_object_id();
533  msg.name = climate->get_name();
534  msg.unique_id = get_default_unique_id("climate", climate);
535 
537  msg.icon = climate->get_icon();
538  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
539 
540  msg.supports_current_temperature = traits.get_supports_current_temperature();
541  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
542 
543  for (auto mode : traits.get_supported_modes())
544  msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
545 
546  msg.visual_min_temperature = traits.get_visual_min_temperature();
547  msg.visual_max_temperature = traits.get_visual_max_temperature();
548  msg.visual_temperature_step = traits.get_visual_temperature_step();
549  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
550  msg.supports_action = traits.get_supports_action();
551 
552  for (auto fan_mode : traits.get_supported_fan_modes())
553  msg.supported_fan_modes.push_back(static_cast<enums::ClimateFanMode>(fan_mode));
554  for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
556  for (auto preset : traits.get_supported_presets())
557  msg.supported_presets.push_back(static_cast<enums::ClimatePreset>(preset));
558  for (auto const &custom_preset : traits.get_supported_custom_presets())
560  for (auto swing_mode : traits.get_supported_swing_modes())
561  msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
562  return this->send_list_entities_climate_response(msg);
563 }
565  climate::Climate *climate = App.get_climate_by_key(msg.key);
566  if (climate == nullptr)
567  return;
568 
569  auto call = climate->make_call();
570  if (msg.has_mode)
571  call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
572  if (msg.has_target_temperature)
578  if (msg.has_legacy_away)
580  if (msg.has_fan_mode)
581  call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
582  if (msg.has_custom_fan_mode)
583  call.set_fan_mode(msg.custom_fan_mode);
584  if (msg.has_preset)
585  call.set_preset(static_cast<climate::ClimatePreset>(msg.preset));
586  if (msg.has_custom_preset)
587  call.set_preset(msg.custom_preset);
588  if (msg.has_swing_mode)
589  call.set_swing_mode(static_cast<climate::ClimateSwingMode>(msg.swing_mode));
590  call.perform();
591 }
592 #endif
593 
594 #ifdef USE_NUMBER
596  if (!this->state_subscription_)
597  return false;
598 
599  NumberStateResponse resp{};
600  resp.key = number->get_object_id_hash();
601  resp.state = state;
602  resp.missing_state = !number->has_state();
603  return this->send_number_state_response(resp);
604 }
607  msg.key = number->get_object_id_hash();
608  msg.object_id = number->get_object_id();
609  msg.name = number->get_name();
610  msg.unique_id = get_default_unique_id("number", number);
611  msg.icon = number->get_icon();
613  msg.entity_category = static_cast<enums::EntityCategory>(number->get_entity_category());
615  msg.mode = static_cast<enums::NumberMode>(number->traits.get_mode());
616 
617  msg.min_value = number->traits.get_min_value();
618  msg.max_value = number->traits.get_max_value();
619  msg.step = number->traits.get_step();
620 
621  return this->send_list_entities_number_response(msg);
622 }
624  number::Number *number = App.get_number_by_key(msg.key);
625  if (number == nullptr)
626  return;
627 
628  auto call = number->make_call();
629  call.set_value(msg.state);
630  call.perform();
631 }
632 #endif
633 
634 #ifdef USE_SELECT
636  if (!this->state_subscription_)
637  return false;
638 
639  SelectStateResponse resp{};
640  resp.key = select->get_object_id_hash();
641  resp.state = std::move(state);
642  resp.missing_state = !select->has_state();
643  return this->send_select_state_response(resp);
644 }
647  msg.key = select->get_object_id_hash();
648  msg.object_id = select->get_object_id();
649  msg.name = select->get_name();
650  msg.unique_id = get_default_unique_id("select", select);
651  msg.icon = select->get_icon();
653  msg.entity_category = static_cast<enums::EntityCategory>(select->get_entity_category());
654 
655  for (const auto &option : select->traits.get_options())
656  msg.options.push_back(option);
657 
658  return this->send_list_entities_select_response(msg);
659 }
661  select::Select *select = App.get_select_by_key(msg.key);
662  if (select == nullptr)
663  return;
664 
665  auto call = select->make_call();
666  call.set_option(msg.state);
667  call.perform();
668 }
669 #endif
670 
671 #ifdef USE_BUTTON
674  msg.key = button->get_object_id_hash();
675  msg.object_id = button->get_object_id();
676  msg.name = button->get_name();
677  msg.unique_id = get_default_unique_id("button", button);
678  msg.icon = button->get_icon();
680  msg.entity_category = static_cast<enums::EntityCategory>(button->get_entity_category());
681  msg.device_class = button->get_device_class();
682  return this->send_list_entities_button_response(msg);
683 }
685  button::Button *button = App.get_button_by_key(msg.key);
686  if (button == nullptr)
687  return;
688 
689  button->press();
690 }
691 #endif
692 
693 #ifdef USE_LOCK
695  if (!this->state_subscription_)
696  return false;
697 
698  LockStateResponse resp{};
699  resp.key = a_lock->get_object_id_hash();
700  resp.state = static_cast<enums::LockState>(state);
701  return this->send_lock_state_response(resp);
702 }
705  msg.key = a_lock->get_object_id_hash();
706  msg.object_id = a_lock->get_object_id();
707  msg.name = a_lock->get_name();
708  msg.unique_id = get_default_unique_id("lock", a_lock);
709  msg.icon = a_lock->get_icon();
710  msg.assumed_state = a_lock->traits.get_assumed_state();
712  msg.entity_category = static_cast<enums::EntityCategory>(a_lock->get_entity_category());
713  msg.supports_open = a_lock->traits.get_supports_open();
714  msg.requires_code = a_lock->traits.get_requires_code();
715  return this->send_list_entities_lock_response(msg);
716 }
718  lock::Lock *a_lock = App.get_lock_by_key(msg.key);
719  if (a_lock == nullptr)
720  return;
721 
722  switch (msg.command) {
723  case enums::LOCK_UNLOCK:
724  a_lock->unlock();
725  break;
726  case enums::LOCK_LOCK:
727  a_lock->lock();
728  break;
729  case enums::LOCK_OPEN:
730  a_lock->open();
731  break;
732  }
733 }
734 #endif
735 
736 #ifdef USE_MEDIA_PLAYER
738  if (!this->state_subscription_)
739  return false;
740 
742  resp.key = media_player->get_object_id_hash();
743  resp.state = static_cast<enums::MediaPlayerState>(media_player->state);
744  resp.volume = media_player->volume;
745  resp.muted = media_player->is_muted();
746  return this->send_media_player_state_response(resp);
747 }
750  msg.key = media_player->get_object_id_hash();
751  msg.object_id = media_player->get_object_id();
752  msg.name = media_player->get_name();
753  msg.unique_id = get_default_unique_id("media_player", media_player);
754  msg.icon = media_player->get_icon();
755  msg.disabled_by_default = media_player->is_disabled_by_default();
756  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
757 
758  auto traits = media_player->get_traits();
759  msg.supports_pause = traits.get_supports_pause();
760 
762 }
765  if (media_player == nullptr)
766  return;
767 
768  auto call = media_player->make_call();
769  if (msg.has_command) {
770  call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
771  }
772  if (msg.has_volume) {
773  call.set_volume(msg.volume);
774  }
775  if (msg.has_media_url) {
776  call.set_media_url(msg.media_url);
777  }
778  call.perform();
779 }
780 #endif
781 
782 #ifdef USE_ESP32_CAMERA
783 void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
784  if (!this->state_subscription_)
785  return;
786  if (this->image_reader_.available())
787  return;
788  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
789  image->was_requested_by(esphome::esp32_camera::IDLE))
790  this->image_reader_.set_image(std::move(image));
791 }
794  msg.key = camera->get_object_id_hash();
795  msg.object_id = camera->get_object_id();
796  msg.name = camera->get_name();
797  msg.unique_id = get_default_unique_id("camera", camera);
799  msg.icon = camera->get_icon();
800  msg.entity_category = static_cast<enums::EntityCategory>(camera->get_entity_category());
801  return this->send_list_entities_camera_response(msg);
802 }
804  if (esp32_camera::global_esp32_camera == nullptr)
805  return;
806 
807  if (msg.single)
809  if (msg.stream) {
811 
812  App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
814  });
815  }
816 }
817 #endif
818 
819 #ifdef USE_HOMEASSISTANT_TIME
823 }
824 #endif
825 
826 bool APIConnection::send_log_message(int level, const char *tag, const char *line) {
827  if (this->log_subscription_ < level)
828  return false;
829 
830  // Send raw so that we don't copy too much
831  auto buffer = this->create_buffer();
832  // LogLevel level = 1;
833  buffer.encode_uint32(1, static_cast<uint32_t>(level));
834  // string message = 3;
835  buffer.encode_string(3, line, strlen(line));
836  // SubscribeLogsResponse - 29
837  return this->send_buffer(buffer, 29);
838 }
839 
841  this->client_info_ = msg.client_info + " (" + this->helper_->getpeername() + ")";
842  this->helper_->set_log_info(client_info_);
843  ESP_LOGV(TAG, "Hello from client: '%s'", this->client_info_.c_str());
844 
845  HelloResponse resp;
846  resp.api_version_major = 1;
847  resp.api_version_minor = 6;
848  resp.server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
849  resp.name = App.get_name();
850 
851  this->connection_state_ = ConnectionState::CONNECTED;
852  return resp;
853 }
855  bool correct = this->parent_->check_password(msg.password);
856 
857  ConnectResponse resp;
858  // bool invalid_password = 1;
859  resp.invalid_password = !correct;
860  if (correct) {
861  ESP_LOGD(TAG, "%s: Connected successfully", this->client_info_.c_str());
862  this->connection_state_ = ConnectionState::AUTHENTICATED;
863 
864 #ifdef USE_HOMEASSISTANT_TIME
866  this->send_time_request();
867  }
868 #endif
869  }
870  return resp;
871 }
873  DeviceInfoResponse resp{};
874  resp.uses_password = this->parent_->uses_password();
875  resp.name = App.get_name();
876  resp.mac_address = get_mac_address_pretty();
877  resp.esphome_version = ESPHOME_VERSION;
878  resp.compilation_time = App.get_compilation_time();
879  resp.model = ESPHOME_BOARD;
880 #ifdef USE_DEEP_SLEEP
881  resp.has_deep_sleep = deep_sleep::global_has_deep_sleep;
882 #endif
883 #ifdef ESPHOME_PROJECT_NAME
884  resp.project_name = ESPHOME_PROJECT_NAME;
885  resp.project_version = ESPHOME_PROJECT_VERSION;
886 #endif
887 #ifdef USE_WEBSERVER
888  resp.webserver_port = USE_WEBSERVER_PORT;
889 #endif
890 #ifdef USE_BLUETOOTH_PROXY
891  resp.has_bluetooth_proxy = true;
892 #endif
893  return resp;
894 }
896  for (auto &it : this->parent_->get_state_subs()) {
897  if (it.entity_id == msg.entity_id && it.attribute.value() == msg.attribute) {
898  it.callback(msg.state);
899  }
900  }
901 }
903  bool found = false;
904  for (auto *service : this->parent_->get_user_services()) {
905  if (service->execute_service(msg)) {
906  found = true;
907  }
908  }
909  if (!found) {
910  ESP_LOGV(TAG, "Could not find matching service!");
911  }
912 }
914  state_subs_at_ = 0;
915 }
916 bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) {
917  if (this->remove_)
918  return false;
919  if (!this->helper_->can_write_without_blocking()) {
920  delay(0);
921  APIError err = helper_->loop();
922  if (err != APIError::OK) {
923  on_fatal_error();
924  ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
925  return false;
926  }
927  if (!this->helper_->can_write_without_blocking()) {
928  // SubscribeLogsResponse
929  if (message_type != 29) {
930  ESP_LOGV(TAG, "Cannot send message because of TCP buffer space");
931  }
932  delay(0);
933  return false;
934  }
935  }
936 
937  APIError err = this->helper_->write_packet(message_type, buffer.get_buffer()->data(), buffer.get_buffer()->size());
938  if (err == APIError::WOULD_BLOCK)
939  return false;
940  if (err != APIError::OK) {
941  on_fatal_error();
942  if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
943  ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
944  } else {
945  ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
946  }
947  return false;
948  }
949  // Do not set last_traffic_ on send
950  return true;
951 }
953  this->on_fatal_error();
954  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_info_.c_str());
955 }
957  this->on_fatal_error();
958  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_info_.c_str());
959 }
961  this->helper_->close();
962  this->remove_ = true;
963 }
964 
965 } // namespace api
966 } // namespace esphome
bool get_force_update() const
Get whether force update mode is enabled.
Definition: sensor.h:80
Base class for all switches.
Definition: switch.h:33
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:107
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:204
enums::EntityCategory entity_category
Definition: api_pb2.h:563
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
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:109
bool has_state() const
Return whether this number has gotten a full state yet.
Definition: number.h:42
bool send_fan_state_response(const FanStateResponse &msg)
std::vector< uint8_t > * get_buffer() const
Definition: proto.h:267
void request_image(CameraRequester requester)
media_player::MediaPlayer * get_media_player_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:278
bool send_climate_info(climate::Climate *climate)
MediaPlayerCall & set_command(MediaPlayerCommand command)
FanDirection direction
The current direction of the fan.
Definition: fan.h:113
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:95
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)
const std::string & get_object_id()
Definition: entity_base.cpp:34
enums::EntityCategory entity_category
Definition: api_pb2.h:1089
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
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
bool send_cover_info(cover::Cover *cover)
enums::EntityCategory entity_category
Definition: api_pb2.h:1141
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:183
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:242
select::Select * get_select_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:260
CoverCall make_call()
Construct a new cover call used to control the cover.
Definition: cover.cpp:149
Device is in home preset.
Definition: climate_mode.h:84
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()
Definition: text_sensor.cpp:71
CoverOperation current_operation
The current operation of the cover (idle, opening, closing).
Definition: cover.h:117
std::vector< enums::ClimatePreset > supported_presets
Definition: api_pb2.h:910
Base class for all buttons.
Definition: button.h:22
enums::EntityCategory entity_category
Definition: api_pb2.h:1042
bool send_camera_info(esp32_camera::ESP32Camera *camera)
std::vector< std::string > options
Definition: api_pb2.h:1040
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:597
lock::Lock * get_lock_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:269
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:175
bool send_button_info(button::Button *button)
button::Button * get_button_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:188
virtual bool assumed_state()
Return whether this switch uses an assumed state - i.e.
Definition: switch.cpp:40
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)
std::unique_ptr< APIFrameHelper > helper_
SelectTraits traits
Definition: select.h:27
enums::ClimateSwingMode swing_mode
Definition: api_pb2.h:966
float target_temperature_high
The maximum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:188
void media_player_command(const MediaPlayerCommandRequest &msg) override
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool send_list_entities_select_response(const ListEntitiesSelectResponse &msg)
Color temperature can be controlled.
HomeassistantTime * global_homeassistant_time
const std::string & get_name() const
Definition: entity_base.cpp:11
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:518
void send_camera_state(std::shared_ptr< esp32_camera::CameraImage > image)
NumberCall & set_value(float value)
Definition: number_call.cpp:10
bool has_value() const
Definition: optional.h:87
void execute_service(const ExecuteServiceRequest &msg) override
std::vector< std::string > supported_custom_presets
Definition: api_pb2.h:911
std::string get_device_class()
Get the device class for this switch.
Definition: switch.cpp:48
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:229
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:125
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
void stop_stream(CameraRequester requester)
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
virtual MediaPlayerTraits get_traits()=0
ClimateSwingMode swing_mode
Definition: climate.h:548
void on_no_setup_connection() override
bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor)
enums::ClimateFanMode fan_mode
Definition: api_pb2.h:964
LockTraits traits
Definition: lock.h:125
optional< std::string > custom_fan_mode
The active custom fan mode of the climate device.
Definition: climate.h:207
cover::Cover * get_cover_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:224
virtual CoverTraits get_traits()=0
Device is in away preset.
Definition: climate_mode.h:86
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: sensor.cpp:26
ClimateCall & set_target_temperature_low(float target_temperature_low)
Set the low point target temperature of the climate device.
Definition: climate.cpp:252
bool send_select_info(select::Select *select)
virtual std::string unique_id()
A unique ID for this sensor, empty for no unique id.
Definition: sensor.cpp:121
ClimateCall make_call()
Make a climate device control call, this is used to control the climate device, see the ClimateCall d...
Definition: climate.cpp:452
fan::Fan * get_fan_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:215
std::string get_device_class()
Get the device class for this button.
Definition: button.cpp:20
void lock()
Turn this lock on.
Definition: lock.cpp:31
enums::EntityCategory entity_category
Definition: api_pb2.h:855
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:35
ClimateCall & set_target_temperature(float target_temperature)
Set the target temperature of the climate device.
Definition: climate.cpp:248
enums::ClimatePreset preset
Definition: api_pb2.h:970
const std::string & get_icon() const
Definition: entity_base.cpp:26
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 press()
Press this button.
Definition: button.cpp:12
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:350
ESP32Camera * global_esp32_camera
optional< ClimatePreset > preset
The active preset of the climate device.
Definition: climate.h:210
enums::FanDirection direction
Definition: api_pb2.h:446
std::vector< std::string > effects
Definition: api_pb2.h:471
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:546
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:29
Brightness of white channel can be controlled separately from other channels.
int speed
The current fan speed level.
Definition: fan.h:111
bool send_list_entities_camera_response(const ListEntitiesCameraResponse &msg)
ClimateCall & set_preset(ClimatePreset preset)
Set the preset of the climate device.
Definition: climate.cpp:193
std::vector< std::string > supported_custom_fan_modes
Definition: api_pb2.h:909
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
enums::LockCommand command
Definition: api_pb2.h:1120
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:153
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)
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition: climate.cpp:423
void turn_on()
Turn this switch on.
Definition: switch.cpp:12
std::string get_device_class()
Definition: cover.cpp:208
SelectCall make_call()
Instantiate a SelectCall object to modify this select component&#39;s state.
Definition: select.h:35
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state)
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.
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:62
enums::EntityCategory entity_category
Definition: api_pb2.h:994
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:233
void on_get_time_response(const GetTimeResponse &value) override
bool get_assumed_state() const
Definition: lock.h:44
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: sensor.cpp:50
void button_command(const ButtonCommandRequest &msg) override
Master brightness of the light can be controlled.
const std::string & get_name() const
Get the name of this Application set by set_name().
Definition: application.h:135
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:179
bool send_list_entities_lock_response(const ListEntitiesLockResponse &msg)
ClimateFanMode fan_mode
Definition: climate.h:540
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override
NumberTraits traits
Definition: number.h:39
std::string client_info
Definition: api_pb2.h:163
bool send_list_entities_sensor_response(const ListEntitiesSensorResponse &msg)
const std::string & get_compilation_time() const
Definition: application.h:139
std::vector< enums::ClimateFanMode > supported_fan_modes
Definition: api_pb2.h:907
optional< std::string > custom_preset
The active custom preset mode of the climate device.
Definition: climate.h:213
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:256
esp32_camera::CameraImageReader image_reader_
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:198
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition: cover.h:123
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:30
NumberCall make_call()
Definition: number.h:35
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)
void open()
Open (unlatch) this lock.
Definition: lock.cpp:41
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:129
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:310
number::Number * get_number_by_key(uint32_t key, bool include_internal=false)
Definition: application.h:251
enums::EntityCategory entity_category
Definition: api_pb2.h:408
bool send_list_entities_switch_response(const ListEntitiesSwitchResponse &msg)
FanCall make_call()
Definition: fan.cpp:85
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:464
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:16
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:130
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
void unlock()
Turn this lock off.
Definition: lock.cpp:36
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:26
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:78
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:33
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:39
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:381
Base-class for all sensors.
Definition: sensor.h:48
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:419
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:908
enums::SensorStateClass state_class
Definition: api_pb2.h:560
HelloResponse hello(const HelloRequest &msg) override
std::string get_device_class()
Get the device class for this binary sensor, using the manual override if specified.
enums::EntityCategory entity_category
Definition: api_pb2.h:474
enums::EntityCategory entity_category
Definition: api_pb2.h:914
std::vector< uint8_t > container
enums::MediaPlayerCommand command
Definition: api_pb2.h:1203
MediaPlayerCall & set_media_url(const std::string &url)
bool is_disabled_by_default() const
Definition: entity_base.cpp:22
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:541
bool get_supports_open() const
Definition: lock.h:40
uint32_t get_object_id_hash()
Definition: entity_base.cpp:42
float target_temperature_low
The minimum target temperature of the climate device, for climate devices with split target temperatu...
Definition: climate.h:186
SelectCall & set_option(const std::string &option)
Definition: select_call.cpp:10
void cover_command(const CoverCommandRequest &msg) override
std::vector< enums::ClimateMode > supported_modes
Definition: api_pb2.h:901
void camera_image(const CameraImageRequest &msg) override
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
ClimatePreset preset
Definition: climate.h:545
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:177
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
bool state
Definition: fan.h:34
void turn_off()
Turn this switch off.
Definition: switch.cpp:16
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27
void fan_command(const FanCommandRequest &msg) override