ESPHome  2023.5.4
Go to the documentation of this file.
1 #pragma once
5 #include "esphome/core/defines.h"
6 #include "esphome/core/log.h"
8 #include "api_pb2.h"
9 #include "api_pb2_service.h"
10 #include "list_entities.h"
11 #include "subscribe_state.h"
12 #include "user_services.h"
13 #include "api_noise_context.h"
15 #include <vector>
17 namespace esphome {
18 namespace api {
20 class APIServer : public Component, public Controller {
21  public:
22  APIServer();
23  void setup() override;
24  uint16_t get_port() const;
25  float get_setup_priority() const override;
26  void loop() override;
27  void dump_config() override;
28  void on_shutdown() override;
29  bool check_password(const std::string &password) const;
30  bool uses_password() const;
31  void set_port(uint16_t port);
32  void set_password(const std::string &password);
33  void set_reboot_timeout(uint32_t reboot_timeout);
35 #ifdef USE_API_NOISE
36  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
37  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
38 #endif // USE_API_NOISE
40  void handle_disconnect(APIConnection *conn);
43 #endif
44 #ifdef USE_COVER
45  void on_cover_update(cover::Cover *obj) override;
46 #endif
47 #ifdef USE_FAN
48  void on_fan_update(fan::Fan *obj) override;
49 #endif
50 #ifdef USE_LIGHT
51  void on_light_update(light::LightState *obj) override;
52 #endif
53 #ifdef USE_SENSOR
54  void on_sensor_update(sensor::Sensor *obj, float state) override;
55 #endif
56 #ifdef USE_SWITCH
57  void on_switch_update(switch_::Switch *obj, bool state) override;
58 #endif
60  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
61 #endif
62 #ifdef USE_CLIMATE
63  void on_climate_update(climate::Climate *obj) override;
64 #endif
65 #ifdef USE_NUMBER
66  void on_number_update(number::Number *obj, float state) override;
67 #endif
68 #ifdef USE_SELECT
69  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
70 #endif
71 #ifdef USE_LOCK
72  void on_lock_update(lock::Lock *obj) override;
73 #endif
76 #endif
80  void send_bluetooth_device_connection(uint64_t address, bool connected, uint16_t mtu = 0, esp_err_t error = ESP_OK);
81  void send_bluetooth_device_pairing(uint64_t address, bool paired, esp_err_t error = ESP_OK);
82  void send_bluetooth_device_unpairing(uint64_t address, bool success, esp_err_t error = ESP_OK);
83  void send_bluetooth_device_clear_cache(uint64_t address, bool success, esp_err_t error = ESP_OK);
84  void send_bluetooth_connections_free(uint8_t free, uint8_t limit);
90  void send_bluetooth_gatt_services_done(uint64_t address);
91  void send_bluetooth_gatt_error(uint64_t address, uint16_t handle, esp_err_t error);
92 #endif
93  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
95  void request_time();
96 #endif
99  bool start_voice_assistant();
100  void stop_voice_assistant();
101 #endif
103  bool is_connected() const;
106  std::string entity_id;
108  std::function<void(std::string)> callback;
109  };
112  std::function<void(std::string)> f);
113  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
114  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
116  protected:
117  std::unique_ptr<socket::Socket> socket_ = nullptr;
118  uint16_t port_{6053};
119  uint32_t reboot_timeout_{300000};
120  uint32_t last_connected_{0};
121  std::vector<std::unique_ptr<APIConnection>> clients_;
122  std::string password_;
123  std::vector<HomeAssistantStateSubscription> state_subs_;
124  std::vector<UserServiceDescriptor *> user_services_;
126 #ifdef USE_API_NOISE
127  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
128 #endif // USE_API_NOISE
129 };
131 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
133 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
134  public:
135  bool check(Ts... x) override { return global_api_server->is_connected(); }
136 };
138 } // namespace api
139 } // namespace esphome
Base class for all switches.
Definition: switch.h:32
void handle_disconnect(APIConnection *conn)
Definition: api_server.cpp:175
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
bool is_connected() const
Definition: api_server.cpp:422
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:114
void send_bluetooth_gatt_notify_response(const BluetoothGATTNotifyResponse &call)
Definition: api_server.cpp:370
void send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &call)
Definition: api_server.cpp:295
void send_bluetooth_device_pairing(uint64_t address, bool paired, esp_err_t error=ESP_OK)
Definition: api_server.cpp:312
void send_bluetooth_gatt_notify_data_response(const BluetoothGATTNotifyDataResponse &call)
Definition: api_server.cpp:365
void on_media_player_update(media_player::MediaPlayer *obj) override
Definition: api_server.cpp:276
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
Definition: api_server.cpp:258
std::function< void(std::string)> callback
Definition: api_server.h:108
bool check_password(const std::string &password) const
Definition: api_server.cpp:147
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:123
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:204
void set_noise_psk(psk_t psk)
Definition: api_server.h:36
void on_lock_update(lock::Lock *obj) override
Definition: api_server.cpp:267
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:289
void send_bluetooth_gatt_error(uint64_t address, uint16_t handle, esp_err_t error)
Definition: api_server.cpp:388
void on_cover_update(cover::Cover *obj) override
Definition: api_server.cpp:186
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Definition: api_server.cpp:177
void loop() override
Definition: api_server.cpp:94
void on_fan_update(fan::Fan *obj) override
Definition: api_server.cpp:195
void send_bluetooth_device_connection(uint64_t address, bool connected, uint16_t mtu=0, esp_err_t error=ESP_OK)
Definition: api_server.cpp:300
void setup() override
Definition: api_server.cpp:24
void send_bluetooth_connections_free(uint8_t free, uint8_t limit)
Definition: api_server.cpp:345
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:127
void on_switch_update(switch_::Switch *obj, bool state) override
Definition: api_server.cpp:222
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:37
void dump_config() override
Definition: api_server.cpp:137
Base class for all automation conditions.
Definition: automation.h:74
void on_number_update(number::Number *obj, float state) override
Definition: api_server.cpp:249
void send_bluetooth_device_unpairing(uint64_t address, bool success, esp_err_t error=ESP_OK)
Definition: api_server.cpp:323
void send_bluetooth_gatt_services_done(uint64_t address)
Definition: api_server.cpp:380
Base-class for all numbers.
Definition: number.h:39
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:413
void on_climate_update(climate::Climate *obj) override
Definition: api_server.cpp:240
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Definition: api_server.cpp:231
void register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:93
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:124
void send_bluetooth_gatt_services(const BluetoothGATTGetServicesResponse &call)
Definition: api_server.cpp:375
void set_port(uint16_t port)
Definition: api_server.cpp:285
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:409
void on_sensor_update(sensor::Sensor *obj, float state) override
Definition: api_server.cpp:213
void send_bluetooth_gatt_write_response(const BluetoothGATTWriteResponse &call)
Definition: api_server.cpp:360
bool uses_password() const
Definition: api_server.cpp:146
Base-class for all selects.
Definition: select.h:24
std::vector< std::unique_ptr< APIConnection > > clients_
Definition: api_server.h:121
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool check(Ts... x) override
Definition: api_server.h:135
float get_setup_priority() const override
Definition: api_server.cpp:284
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:401
void send_bluetooth_device_clear_cache(uint64_t address, bool success, esp_err_t error=ESP_OK)
Definition: api_server.cpp:334
void on_shutdown() override
Definition: api_server.cpp:423
Base-class for all sensors.
Definition: sensor.h:57
void send_bluetooth_gatt_read_response(const BluetoothGATTReadResponse &call)
Definition: api_server.cpp:355
uint16_t get_port() const
Definition: api_server.cpp:412
std::array< uint8_t, 32 > psk_t
void set_password(const std::string &password)
Definition: api_server.cpp:288
APIServer * global_api_server
Definition: api_server.cpp:286
std::unique_ptr< socket::Socket > socket_
Definition: api_server.h:117
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