ESPHome  2022.11.3
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 namespace esphome {
16 namespace api {
18 class APIServer : public Component, public Controller {
19  public:
20  APIServer();
21  void setup() override;
22  uint16_t get_port() const;
23  float get_setup_priority() const override;
24  void loop() override;
25  void dump_config() override;
26  void on_shutdown() override;
27  bool check_password(const std::string &password) const;
28  bool uses_password() const;
29  void set_port(uint16_t port);
30  void set_password(const std::string &password);
31  void set_reboot_timeout(uint32_t reboot_timeout);
33 #ifdef USE_API_NOISE
34  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
35  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
36 #endif // USE_API_NOISE
38  void handle_disconnect(APIConnection *conn);
41 #endif
42 #ifdef USE_COVER
43  void on_cover_update(cover::Cover *obj) override;
44 #endif
45 #ifdef USE_FAN
46  void on_fan_update(fan::Fan *obj) override;
47 #endif
48 #ifdef USE_LIGHT
49  void on_light_update(light::LightState *obj) override;
50 #endif
51 #ifdef USE_SENSOR
52  void on_sensor_update(sensor::Sensor *obj, float state) override;
53 #endif
54 #ifdef USE_SWITCH
55  void on_switch_update(switch_::Switch *obj, bool state) override;
56 #endif
58  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
59 #endif
60 #ifdef USE_CLIMATE
61  void on_climate_update(climate::Climate *obj) override;
62 #endif
63 #ifdef USE_NUMBER
64  void on_number_update(number::Number *obj, float state) override;
65 #endif
66 #ifdef USE_SELECT
67  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
68 #endif
69 #ifdef USE_LOCK
70  void on_lock_update(lock::Lock *obj) override;
71 #endif
74 #endif
78  void send_bluetooth_device_connection(uint64_t address, bool connected, uint16_t mtu = 0, esp_err_t error = ESP_OK);
79  void send_bluetooth_connections_free(uint8_t free, uint8_t limit);
85  void send_bluetooth_gatt_services_done(uint64_t address);
86  void send_bluetooth_gatt_error(uint64_t address, uint16_t handle, esp_err_t error);
87 #endif
88  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
90  void request_time();
91 #endif
93  bool is_connected() const;
96  std::string entity_id;
98  std::function<void(std::string)> callback;
99  };
102  std::function<void(std::string)> f);
103  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
104  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
106  protected:
107  std::unique_ptr<socket::Socket> socket_ = nullptr;
108  uint16_t port_{6053};
109  uint32_t reboot_timeout_{300000};
110  uint32_t last_connected_{0};
111  std::vector<std::unique_ptr<APIConnection>> clients_;
112  std::string password_;
113  std::vector<HomeAssistantStateSubscription> state_subs_;
114  std::vector<UserServiceDescriptor *> user_services_;
116 #ifdef USE_API_NOISE
117  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
118 #endif // USE_API_NOISE
119 };
121 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
123 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
124  public:
125  bool check(Ts... x) override { return global_api_server->is_connected(); }
126 };
128 } // namespace api
129 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
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:32
bool is_connected() const
Definition: api_server.cpp:389
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:104
void send_bluetooth_gatt_notify_response(const BluetoothGATTNotifyResponse &call)
Definition: api_server.cpp:337
void send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &call)
Definition: api_server.cpp:295
void send_bluetooth_gatt_notify_data_response(const BluetoothGATTNotifyDataResponse &call)
Definition: api_server.cpp:332
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:98
bool check_password(const std::string &password) const
Definition: api_server.cpp:147
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:113
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:204
void set_noise_psk(psk_t psk)
Definition: api_server.h:34
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:355
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:312
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:117
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:35
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_gatt_services_done(uint64_t address)
Definition: api_server.cpp:347
Base-class for all numbers.
Definition: number.h:29
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:380
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:88
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:114
void send_bluetooth_gatt_services(const BluetoothGATTGetServicesResponse &call)
Definition: api_server.cpp:342
void set_port(uint16_t port)
Definition: api_server.cpp:285
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:376
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:327
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:111
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
bool check(Ts... x) override
Definition: api_server.h:125
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:368
void on_shutdown() override
Definition: api_server.cpp:390
Base-class for all sensors.
Definition: sensor.h:48
void send_bluetooth_gatt_read_response(const BluetoothGATTReadResponse &call)
Definition: api_server.cpp:322
uint16_t get_port() const
Definition: api_server.cpp:379
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:107
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