api_connection.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/core/application.h"
  4. #include "api_pb2.h"
  5. #include "api_pb2_service.h"
  6. #include "api_server.h"
  7. namespace esphome {
  8. namespace api {
  9. class APIConnection : public APIServerConnection {
  10. public:
  11. APIConnection(AsyncClient *client, APIServer *parent);
  12. virtual ~APIConnection();
  13. void disconnect_client();
  14. void loop();
  15. bool send_list_info_done() {
  16. ListEntitiesDoneResponse resp;
  17. return this->send_list_entities_done_response(resp);
  18. }
  19. #ifdef USE_BINARY_SENSOR
  20. bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state);
  21. bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor);
  22. #endif
  23. #ifdef USE_COVER
  24. bool send_cover_state(cover::Cover *cover);
  25. bool send_cover_info(cover::Cover *cover);
  26. void cover_command(const CoverCommandRequest &msg) override;
  27. #endif
  28. #ifdef USE_FAN
  29. bool send_fan_state(fan::FanState *fan);
  30. bool send_fan_info(fan::FanState *fan);
  31. void fan_command(const FanCommandRequest &msg) override;
  32. #endif
  33. #ifdef USE_LIGHT
  34. bool send_light_state(light::LightState *light);
  35. bool send_light_info(light::LightState *light);
  36. void light_command(const LightCommandRequest &msg) override;
  37. #endif
  38. #ifdef USE_SENSOR
  39. bool send_sensor_state(sensor::Sensor *sensor, float state);
  40. bool send_sensor_info(sensor::Sensor *sensor);
  41. #endif
  42. #ifdef USE_SWITCH
  43. bool send_switch_state(switch_::Switch *a_switch, bool state);
  44. bool send_switch_info(switch_::Switch *a_switch);
  45. void switch_command(const SwitchCommandRequest &msg) override;
  46. #endif
  47. #ifdef USE_TEXT_SENSOR
  48. bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state);
  49. bool send_text_sensor_info(text_sensor::TextSensor *text_sensor);
  50. #endif
  51. #ifdef USE_ESP32_CAMERA
  52. void send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
  53. bool send_camera_info(esp32_camera::ESP32Camera *camera);
  54. void camera_image(const CameraImageRequest &msg) override;
  55. #endif
  56. #ifdef USE_CLIMATE
  57. bool send_climate_state(climate::Climate *climate);
  58. bool send_climate_info(climate::Climate *climate);
  59. void climate_command(const ClimateCommandRequest &msg) override;
  60. #endif
  61. bool send_log_message(int level, const char *tag, const char *line);
  62. void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
  63. if (!this->service_call_subscription_)
  64. return;
  65. this->send_homeassistant_service_response(call);
  66. }
  67. #ifdef USE_HOMEASSISTANT_TIME
  68. void send_time_request() {
  69. GetTimeRequest req;
  70. this->send_get_time_request(req);
  71. }
  72. #endif
  73. void on_disconnect_response(const DisconnectResponse &value) override {
  74. // we initiated disconnect_client
  75. this->next_close_ = true;
  76. }
  77. void on_ping_response(const PingResponse &value) override {
  78. // we initiated ping
  79. this->sent_ping_ = false;
  80. }
  81. void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override;
  82. #ifdef USE_HOMEASSISTANT_TIME
  83. void on_get_time_response(const GetTimeResponse &value) override;
  84. #endif
  85. HelloResponse hello(const HelloRequest &msg) override;
  86. ConnectResponse connect(const ConnectRequest &msg) override;
  87. DisconnectResponse disconnect(const DisconnectRequest &msg) override {
  88. // remote initiated disconnect_client
  89. this->next_close_ = true;
  90. DisconnectResponse resp;
  91. return resp;
  92. }
  93. PingResponse ping(const PingRequest &msg) override { return {}; }
  94. DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override;
  95. void list_entities(const ListEntitiesRequest &msg) override { this->list_entities_iterator_.begin(); }
  96. void subscribe_states(const SubscribeStatesRequest &msg) override {
  97. this->state_subscription_ = true;
  98. this->initial_state_iterator_.begin();
  99. }
  100. void subscribe_logs(const SubscribeLogsRequest &msg) override {
  101. this->log_subscription_ = msg.level;
  102. if (msg.dump_config)
  103. App.schedule_dump_config();
  104. }
  105. void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) override {
  106. this->service_call_subscription_ = true;
  107. }
  108. void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override;
  109. GetTimeResponse get_time(const GetTimeRequest &msg) override {
  110. // TODO
  111. return {};
  112. }
  113. void execute_service(const ExecuteServiceRequest &msg) override;
  114. bool is_authenticated() override { return this->connection_state_ == ConnectionState::AUTHENTICATED; }
  115. bool is_connection_setup() override {
  116. return this->connection_state_ == ConnectionState ::CONNECTED || this->is_authenticated();
  117. }
  118. void on_fatal_error() override;
  119. void on_unauthenticated_access() override;
  120. void on_no_setup_connection() override;
  121. ProtoWriteBuffer create_buffer() override {
  122. this->send_buffer_.clear();
  123. return {&this->send_buffer_};
  124. }
  125. bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
  126. protected:
  127. friend APIServer;
  128. void on_error_(int8_t error);
  129. void on_disconnect_();
  130. void on_timeout_(uint32_t time);
  131. void on_data_(uint8_t *buf, size_t len);
  132. void parse_recv_buffer_();
  133. enum class ConnectionState {
  134. WAITING_FOR_HELLO,
  135. CONNECTED,
  136. AUTHENTICATED,
  137. } connection_state_{ConnectionState::WAITING_FOR_HELLO};
  138. bool remove_{false};
  139. std::vector<uint8_t> send_buffer_;
  140. std::vector<uint8_t> recv_buffer_;
  141. std::string client_info_;
  142. #ifdef USE_ESP32_CAMERA
  143. esp32_camera::CameraImageReader image_reader_;
  144. #endif
  145. bool state_subscription_{false};
  146. int log_subscription_{ESPHOME_LOG_LEVEL_NONE};
  147. uint32_t last_traffic_;
  148. bool sent_ping_{false};
  149. bool service_call_subscription_{false};
  150. bool current_nodelay_{false};
  151. bool next_close_{false};
  152. AsyncClient *client_;
  153. APIServer *parent_;
  154. InitialStateIterator initial_state_iterator_;
  155. ListEntitiesIterator list_entities_iterator_;
  156. };
  157. } // namespace api
  158. } // namespace esphome