ESPHome  2023.5.5
api_connection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "api_frame_helper.h"
4 #include "api_pb2.h"
5 #include "api_pb2_service.h"
6 #include "api_server.h"
9 #include "esphome/core/defines.h"
10 
11 #include <vector>
12 
13 namespace esphome {
14 namespace api {
15 
17  public:
18  APIConnection(std::unique_ptr<socket::Socket> socket, APIServer *parent);
19  virtual ~APIConnection() = default;
20 
21  void start();
22  void loop();
23 
26  return this->send_list_entities_done_response(resp);
27  }
28 #ifdef USE_BINARY_SENSOR
31 #endif
32 #ifdef USE_COVER
33  bool send_cover_state(cover::Cover *cover);
34  bool send_cover_info(cover::Cover *cover);
35  void cover_command(const CoverCommandRequest &msg) override;
36 #endif
37 #ifdef USE_FAN
38  bool send_fan_state(fan::Fan *fan);
39  bool send_fan_info(fan::Fan *fan);
40  void fan_command(const FanCommandRequest &msg) override;
41 #endif
42 #ifdef USE_LIGHT
45  void light_command(const LightCommandRequest &msg) override;
46 #endif
47 #ifdef USE_SENSOR
48  bool send_sensor_state(sensor::Sensor *sensor, float state);
49  bool send_sensor_info(sensor::Sensor *sensor);
50 #endif
51 #ifdef USE_SWITCH
52  bool send_switch_state(switch_::Switch *a_switch, bool state);
53  bool send_switch_info(switch_::Switch *a_switch);
54  void switch_command(const SwitchCommandRequest &msg) override;
55 #endif
56 #ifdef USE_TEXT_SENSOR
57  bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state);
59 #endif
60 #ifdef USE_ESP32_CAMERA
61  void send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
63  void camera_image(const CameraImageRequest &msg) override;
64 #endif
65 #ifdef USE_CLIMATE
66  bool send_climate_state(climate::Climate *climate);
67  bool send_climate_info(climate::Climate *climate);
68  void climate_command(const ClimateCommandRequest &msg) override;
69 #endif
70 #ifdef USE_NUMBER
71  bool send_number_state(number::Number *number, float state);
72  bool send_number_info(number::Number *number);
73  void number_command(const NumberCommandRequest &msg) override;
74 #endif
75 #ifdef USE_SELECT
76  bool send_select_state(select::Select *select, std::string state);
77  bool send_select_info(select::Select *select);
78  void select_command(const SelectCommandRequest &msg) override;
79 #endif
80 #ifdef USE_BUTTON
81  bool send_button_info(button::Button *button);
82  void button_command(const ButtonCommandRequest &msg) override;
83 #endif
84 #ifdef USE_LOCK
85  bool send_lock_state(lock::Lock *a_lock, lock::LockState state);
86  bool send_lock_info(lock::Lock *a_lock);
87  void lock_command(const LockCommandRequest &msg) override;
88 #endif
89 #ifdef USE_MEDIA_PLAYER
92  void media_player_command(const MediaPlayerCommandRequest &msg) override;
93 #endif
94  bool send_log_message(int level, const char *tag, const char *line);
96  if (!this->service_call_subscription_)
97  return;
99  }
100 #ifdef USE_BLUETOOTH_PROXY
103  }
106  }
108 
109  void bluetooth_device_request(const BluetoothDeviceRequest &msg) override;
110  void bluetooth_gatt_read(const BluetoothGATTReadRequest &msg) override;
111  void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override;
115  void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override;
117  const SubscribeBluetoothConnectionsFreeRequest &msg) override;
118 
119 #endif
120 #ifdef USE_HOMEASSISTANT_TIME
122  GetTimeRequest req;
123  this->send_get_time_request(req);
124  }
125 #endif
126 
127 #ifdef USE_VOICE_ASSISTANT
130  }
131  bool request_voice_assistant(bool start);
132  void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
134 #endif
135 
136  void on_disconnect_response(const DisconnectResponse &value) override;
137  void on_ping_response(const PingResponse &value) override {
138  // we initiated ping
139  this->sent_ping_ = false;
140  }
142 #ifdef USE_HOMEASSISTANT_TIME
143  void on_get_time_response(const GetTimeResponse &value) override;
144 #endif
145  HelloResponse hello(const HelloRequest &msg) override;
146  ConnectResponse connect(const ConnectRequest &msg) override;
147  DisconnectResponse disconnect(const DisconnectRequest &msg) override;
148  PingResponse ping(const PingRequest &msg) override { return {}; }
149  DeviceInfoResponse device_info(const DeviceInfoRequest &msg) override;
150  void list_entities(const ListEntitiesRequest &msg) override { this->list_entities_iterator_.begin(); }
151  void subscribe_states(const SubscribeStatesRequest &msg) override {
152  this->state_subscription_ = true;
154  }
155  void subscribe_logs(const SubscribeLogsRequest &msg) override {
156  this->log_subscription_ = msg.level;
157  if (msg.dump_config)
159  }
161  this->service_call_subscription_ = true;
162  }
164  GetTimeResponse get_time(const GetTimeRequest &msg) override {
165  // TODO
166  return {};
167  }
168  void execute_service(const ExecuteServiceRequest &msg) override;
169 
170  bool is_authenticated() override { return this->connection_state_ == ConnectionState::AUTHENTICATED; }
171  bool is_connection_setup() override {
172  return this->connection_state_ == ConnectionState ::CONNECTED || this->is_authenticated();
173  }
174  void on_fatal_error() override;
175  void on_unauthenticated_access() override;
176  void on_no_setup_connection() override;
178  // FIXME: ensure no recursive writes can happen
179  this->proto_write_buffer_.clear();
180  return {&this->proto_write_buffer_};
181  }
182  bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
183 
184  protected:
185  friend APIServer;
186 
187  bool send_(const void *buf, size_t len, bool force);
188 
189  enum class ConnectionState {
191  CONNECTED,
193  } connection_state_{ConnectionState::WAITING_FOR_HELLO};
194 
195  bool remove_{false};
196 
197  // Buffer used to encode proto messages
198  // Re-use to prevent allocations
199  std::vector<uint8_t> proto_write_buffer_;
200  std::unique_ptr<APIFrameHelper> helper_;
201 
202  std::string client_info_;
205 #ifdef USE_ESP32_CAMERA
207 #endif
208 
209  bool state_subscription_{false};
210  int log_subscription_{ESPHOME_LOG_LEVEL_NONE};
211  uint32_t last_traffic_;
212  bool sent_ping_{false};
214 #ifdef USE_BLUETOOTH_PROXY
216 #endif
217 #ifdef USE_VOICE_ASSISTANT
219 #endif
220  bool next_close_ = false;
224  int state_subs_at_ = -1;
225 };
226 
227 } // namespace api
228 } // namespace esphome
Base class for all switches.
Definition: switch.h:32
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
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
void bluetooth_gatt_notify(const BluetoothGATTNotifyRequest &msg) override
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)
void on_voice_assistant_response(const VoiceAssistantResponse &msg) override
BluetoothConnectionsFreeResponse subscribe_bluetooth_connections_free(const SubscribeBluetoothConnectionsFreeRequest &msg) override
InitialStateIterator initial_state_iterator_
Base class for all buttons.
Definition: button.h:29
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)
void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override
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_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:39
bool send_media_player_info(media_player::MediaPlayer *media_player)
void bluetooth_gatt_write(const BluetoothGATTWriteRequest &msg) override
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)
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
void bluetooth_gatt_read_descriptor(const BluetoothGATTReadDescriptorRequest &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
void bluetooth_gatt_write_descriptor(const BluetoothGATTWriteDescriptorRequest &msg) override
void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override
void bluetooth_gatt_get_services(const BluetoothGATTGetServicesRequest &msg) override
esp32_camera::CameraImageReader image_reader_
bool request_voice_assistant(bool start)
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:286
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:37
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:57
void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override
void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override
HelloResponse hello(const HelloRequest &msg) override
bool send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &msg)
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 bluetooth_gatt_read(const BluetoothGATTReadRequest &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:161
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.