ESPHome  2022.9.1
api_connection.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "api_pb2.h"
6 #include "api_pb2_service.h"
7 #include "api_server.h"
8 #include "api_frame_helper.h"
9 
10 #ifdef USE_BLUETOOTH_PROXY
12 #endif
13 
14 namespace esphome {
15 namespace api {
16 
18  public:
19  APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
20  virtual ~APIConnection() = default;
21 
22  void start();
23  void loop();
24 
27  return this->send_list_entities_done_response(resp);
28  }
29 #ifdef USE_BINARY_SENSOR
32 #endif
33 #ifdef USE_COVER
34  bool send_cover_state(cover::Cover *cover);
35  bool send_cover_info(cover::Cover *cover);
36  void cover_command(const CoverCommandRequest &msg) override;
37 #endif
38 #ifdef USE_FAN
39  bool send_fan_state(fan::Fan *fan);
40  bool send_fan_info(fan::Fan *fan);
41  void fan_command(const FanCommandRequest &msg) override;
42 #endif
43 #ifdef USE_LIGHT
46  void light_command(const LightCommandRequest &msg) override;
47 #endif
48 #ifdef USE_SENSOR
49  bool send_sensor_state(sensor::Sensor *sensor, float state);
50  bool send_sensor_info(sensor::Sensor *sensor);
51 #endif
52 #ifdef USE_SWITCH
53  bool send_switch_state(switch_::Switch *a_switch, bool state);
54  bool send_switch_info(switch_::Switch *a_switch);
55  void switch_command(const SwitchCommandRequest &msg) override;
56 #endif
57 #ifdef USE_TEXT_SENSOR
58  bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state);
60 #endif
61 #ifdef USE_ESP32_CAMERA
62  void send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
64  void camera_image(const CameraImageRequest &msg) override;
65 #endif
66 #ifdef USE_CLIMATE
67  bool send_climate_state(climate::Climate *climate);
68  bool send_climate_info(climate::Climate *climate);
69  void climate_command(const ClimateCommandRequest &msg) override;
70 #endif
71 #ifdef USE_NUMBER
72  bool send_number_state(number::Number *number, float state);
73  bool send_number_info(number::Number *number);
74  void number_command(const NumberCommandRequest &msg) override;
75 #endif
76 #ifdef USE_SELECT
77  bool send_select_state(select::Select *select, std::string state);
78  bool send_select_info(select::Select *select);
79  void select_command(const SelectCommandRequest &msg) override;
80 #endif
81 #ifdef USE_BUTTON
82  bool send_button_info(button::Button *button);
83  void button_command(const ButtonCommandRequest &msg) override;
84 #endif
85 #ifdef USE_LOCK
86  bool send_lock_state(lock::Lock *a_lock, lock::LockState state);
87  bool send_lock_info(lock::Lock *a_lock);
88  void lock_command(const LockCommandRequest &msg) override;
89 #endif
90 #ifdef USE_MEDIA_PLAYER
93  void media_player_command(const MediaPlayerCommandRequest &msg) override;
94 #endif
95  bool send_log_message(int level, const char *tag, const char *line);
97  if (!this->service_call_subscription_)
98  return;
100  }
101 #ifdef USE_BLUETOOTH_PROXY
104  return false;
105  return this->send_bluetooth_le_advertisement_response(call);
106  }
107 #endif
108 #ifdef USE_HOMEASSISTANT_TIME
110  GetTimeRequest req;
111  this->send_get_time_request(req);
112  }
113 #endif
114 
115  void on_disconnect_response(const DisconnectResponse &value) override;
116  void on_ping_response(const PingResponse &value) override {
117  // we initiated ping
118  this->sent_ping_ = false;
119  }
121 #ifdef USE_HOMEASSISTANT_TIME
122  void on_get_time_response(const GetTimeResponse &value) override;
123 #endif
124  HelloResponse hello(const HelloRequest &msg) override;
125  ConnectResponse connect(const ConnectRequest &msg) override;
126  DisconnectResponse disconnect(const DisconnectRequest &msg) override;
127  PingResponse ping(const PingRequest &msg) override { return {}; }
128  DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override;
129  void list_entities(const ListEntitiesRequest &msg) override { this->list_entities_iterator_.begin(); }
130  void subscribe_states(const SubscribeStatesRequest &msg) override {
131  this->state_subscription_ = true;
133  }
134  void subscribe_logs(const SubscribeLogsRequest &msg) override {
135  this->log_subscription_ = msg.level;
136  if (msg.dump_config)
138  }
140  this->service_call_subscription_ = true;
141  }
143  GetTimeResponse get_time(const GetTimeRequest &msg) override {
144  // TODO
145  return {};
146  }
147  void execute_service(const ExecuteServiceRequest &msg) override;
150  }
151  bool is_authenticated() override { return this->connection_state_ == ConnectionState::AUTHENTICATED; }
152  bool is_connection_setup() override {
153  return this->connection_state_ == ConnectionState ::CONNECTED || this->is_authenticated();
154  }
155  void on_fatal_error() override;
156  void on_unauthenticated_access() override;
157  void on_no_setup_connection() override;
159  // FIXME: ensure no recursive writes can happen
160  this->proto_write_buffer_.clear();
161  return {&this->proto_write_buffer_};
162  }
163  bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
164 
165  protected:
166  friend APIServer;
167 
168  bool send_(const void *buf, size_t len, bool force);
169 
170  enum class ConnectionState {
172  CONNECTED,
174  } connection_state_{ConnectionState::WAITING_FOR_HELLO};
175 
176  bool remove_{false};
177 
178  // Buffer used to encode proto messages
179  // Re-use to prevent allocations
180  std::vector<uint8_t> proto_write_buffer_;
181  std::unique_ptr<APIFrameHelper> helper_;
182 
183  std::string client_info_;
184 #ifdef USE_ESP32_CAMERA
186 #endif
187 
188  bool state_subscription_{false};
189  int log_subscription_{ESPHOME_LOG_LEVEL_NONE};
190  uint32_t last_traffic_;
191  bool sent_ping_{false};
194  bool next_close_ = false;
198  int state_subs_at_ = -1;
199 };
200 
201 } // namespace api
202 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state)
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &call)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) override
bool send_climate_info(climate::Climate *climate)
Base class for all cover devices.
Definition: cover.h:111
bool send_cover_info(cover::Cover *cover)
bool send_switch_state(switch_::Switch *a_switch, bool state)
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
InitialStateIterator initial_state_iterator_
Base class for all buttons.
Definition: button.h:22
bool send_camera_info(esp32_camera::ESP32Camera *camera)
bool send_fan_state(fan::Fan *fan)
bool send_climate_state(climate::Climate *climate)
bool send_button_info(button::Button *button)
DisconnectResponse disconnect(const DisconnectRequest &msg) override
bool send_lock_state(lock::Lock *a_lock, lock::LockState state)
std::unique_ptr< APIFrameHelper > helper_
void media_player_command(const MediaPlayerCommandRequest &msg) override
bool send_homeassistant_service_response(const HomeassistantServiceResponse &msg)
void send_camera_state(std::shared_ptr< esp32_camera::CameraImage > image)
void execute_service(const ExecuteServiceRequest &msg) override
bool send_list_entities_done_response(const ListEntitiesDoneResponse &msg)
bool send_number_info(number::Number *number)
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override
void on_no_setup_connection() override
bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor)
bool send_select_info(select::Select *select)
bool send_bluetooth_le_advertisement_response(const BluetoothLEAdvertisementResponse &msg)
bool send_sensor_state(sensor::Sensor *sensor, float state)
bool send_select_state(select::Select *select, std::string state)
std::vector< uint8_t > proto_write_buffer_
bool send_log_message(int level, const char *tag, const char *line)
Base-class for all numbers.
Definition: number.h:29
bool send_media_player_info(media_player::MediaPlayer *media_player)
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state)
bool send_fan_info(fan::Fan *fan)
void subscribe_states(const SubscribeStatesRequest &msg) override
Application App
Global storage of Application pointer - only one Application can exist.
virtual ~APIConnection()=default
void on_disconnect_response(const DisconnectResponse &value) override
void on_get_time_response(const GetTimeResponse &value) override
void begin(bool include_internal=false)
void button_command(const ButtonCommandRequest &msg) override
bool send_cover_state(cover::Cover *cover)
void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override
void on_ping_response(const PingResponse &value) override
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override
esp32_camera::CameraImageReader image_reader_
void switch_command(const SwitchCommandRequest &msg) override
ConnectResponse connect(const ConnectRequest &msg) override
bool send_light_info(light::LightState *light)
void select_command(const SelectCommandRequest &msg) override
bool send_sensor_info(sensor::Sensor *sensor)
std::string size_t len
Definition: helpers.h:281
DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override
GetTimeResponse get_time(const GetTimeRequest &msg) override
bool send_get_time_request(const GetTimeRequest &msg)
void subscribe_logs(const SubscribeLogsRequest &msg) override
Base-class for all selects.
Definition: select.h:24
bool send_light_state(light::LightState *light)
Definition: a4988.cpp:4
void on_unauthenticated_access() override
void lock_command(const LockCommandRequest &msg) override
bool is_authenticated() override
bool send_media_player_state(media_player::MediaPlayer *media_player)
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
LockState
Enum for all states a lock can be in.
Definition: lock.h:26
void light_command(const LightCommandRequest &msg) override
bool send_lock_info(lock::Lock *a_lock)
bool send_(const void *buf, size_t len, bool force)
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)
bool is_connection_setup() override
Base-class for all sensors.
Definition: sensor.h:48
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
HelloResponse hello(const HelloRequest &msg) override
PingResponse ping(const PingRequest &msg) override
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
void cover_command(const CoverCommandRequest &msg) override
void camera_image(const CameraImageRequest &msg) override
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
bool state
Definition: fan.h:34
void list_entities(const ListEntitiesRequest &msg) override
void fan_command(const FanCommandRequest &msg) override
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.