ESPHome  2024.2.1
api_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "api_noise_context.h"
4 #include "api_pb2.h"
5 #include "api_pb2_service.h"
10 #include "esphome/core/defines.h"
11 #include "esphome/core/log.h"
12 #include "list_entities.h"
13 #include "subscribe_state.h"
14 #include "user_services.h"
15 
16 #include <vector>
17 
18 namespace esphome {
19 namespace api {
20 
21 class APIServer : public Component, public Controller {
22  public:
23  APIServer();
24  void setup() override;
25  uint16_t get_port() const;
26  float get_setup_priority() const override;
27  void loop() override;
28  void dump_config() override;
29  void on_shutdown() override;
30  bool check_password(const std::string &password) const;
31  bool uses_password() const;
32  void set_port(uint16_t port);
33  void set_password(const std::string &password);
34  void set_reboot_timeout(uint32_t reboot_timeout);
35 
36 #ifdef USE_API_NOISE
37  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
38  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
39 #endif // USE_API_NOISE
40 
41  void handle_disconnect(APIConnection *conn);
42 #ifdef USE_BINARY_SENSOR
44 #endif
45 #ifdef USE_COVER
46  void on_cover_update(cover::Cover *obj) override;
47 #endif
48 #ifdef USE_FAN
49  void on_fan_update(fan::Fan *obj) override;
50 #endif
51 #ifdef USE_LIGHT
52  void on_light_update(light::LightState *obj) override;
53 #endif
54 #ifdef USE_SENSOR
55  void on_sensor_update(sensor::Sensor *obj, float state) override;
56 #endif
57 #ifdef USE_SWITCH
58  void on_switch_update(switch_::Switch *obj, bool state) override;
59 #endif
60 #ifdef USE_TEXT_SENSOR
61  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
62 #endif
63 #ifdef USE_CLIMATE
64  void on_climate_update(climate::Climate *obj) override;
65 #endif
66 #ifdef USE_NUMBER
67  void on_number_update(number::Number *obj, float state) override;
68 #endif
69 #ifdef USE_TEXT
70  void on_text_update(text::Text *obj, const std::string &state) override;
71 #endif
72 #ifdef USE_SELECT
73  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
74 #endif
75 #ifdef USE_LOCK
76  void on_lock_update(lock::Lock *obj) override;
77 #endif
78 #ifdef USE_MEDIA_PLAYER
80 #endif
82  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
83 #ifdef USE_HOMEASSISTANT_TIME
84  void request_time();
85 #endif
86 
87 #ifdef USE_ALARM_CONTROL_PANEL
89 #endif
90 
91  bool is_connected() const;
92 
94  std::string entity_id;
96  std::function<void(std::string)> callback;
97  };
98 
100  std::function<void(std::string)> f);
101  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
102  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
103 
106  return this->client_disconnected_trigger_;
107  }
108 
109  protected:
110  std::unique_ptr<socket::Socket> socket_ = nullptr;
111  uint16_t port_{6053};
112  uint32_t reboot_timeout_{300000};
113  uint32_t last_connected_{0};
114  std::vector<std::unique_ptr<APIConnection>> clients_;
115  std::string password_;
116  std::vector<HomeAssistantStateSubscription> state_subs_;
117  std::vector<UserServiceDescriptor *> user_services_;
120 
121 #ifdef USE_API_NOISE
122  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
123 #endif // USE_API_NOISE
124 };
125 
126 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
127 
128 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
129  public:
130  bool check(Ts... x) override { return global_api_server->is_connected(); }
131 };
132 
133 } // namespace api
134 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
void handle_disconnect(APIConnection *conn)
Definition: api_server.cpp:176
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:327
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:102
void on_media_player_update(media_player::MediaPlayer *obj) override
Definition: api_server.cpp:286
uint16_t x
Definition: tt21100.cpp:17
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
Definition: api_server.cpp:268
std::function< void(std::string)> callback
Definition: api_server.h:96
Trigger< std::string, std::string > * client_connected_trigger_
Definition: api_server.h:118
bool check_password(const std::string &password) const
Definition: api_server.cpp:148
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:116
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:205
void set_noise_psk(psk_t psk)
Definition: api_server.h:37
void on_lock_update(lock::Lock *obj) override
Definition: api_server.cpp:277
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:299
void on_cover_update(cover::Cover *obj) override
Definition: api_server.cpp:187
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Definition: api_server.cpp:178
Base-class for all text inputs.
Definition: text.h:24
void loop() override
Definition: api_server.cpp:94
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition: api_server.h:119
void on_fan_update(fan::Fan *obj) override
Definition: api_server.cpp:196
void setup() override
Definition: api_server.cpp:24
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
Definition: api_server.cpp:336
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:122
void on_switch_update(switch_::Switch *obj, bool state) override
Definition: api_server.cpp:223
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:38
void dump_config() override
Definition: api_server.cpp:138
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition: api_server.h:104
Base class for all automation conditions.
Definition: automation.h:74
void on_text_update(text::Text *obj, const std::string &state) override
Definition: api_server.cpp:259
void on_number_update(number::Number *obj, float state) override
Definition: api_server.cpp:250
Base-class for all numbers.
Definition: number.h:39
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:318
void on_climate_update(climate::Climate *obj) override
Definition: api_server.cpp:241
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Definition: api_server.cpp:232
void register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:82
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:117
void set_port(uint16_t port)
Definition: api_server.cpp:295
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:314
void on_sensor_update(sensor::Sensor *obj, float state) override
Definition: api_server.cpp:214
bool uses_password() const
Definition: api_server.cpp:147
Base-class for all selects.
Definition: select.h:31
std::vector< std::unique_ptr< APIConnection > > clients_
Definition: api_server.h:114
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool check(Ts... x) override
Definition: api_server.h:130
float get_setup_priority() const override
Definition: api_server.cpp:294
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:306
void on_shutdown() override
Definition: api_server.cpp:328
Base-class for all sensors.
Definition: sensor.h:57
uint16_t get_port() const
Definition: api_server.cpp:317
std::array< uint8_t, 32 > psk_t
Trigger< std::string, std::string > * get_client_disconnected_trigger() const
Definition: api_server.h:105
void set_password(const std::string &password)
Definition: api_server.cpp:298
APIServer * global_api_server
Definition: api_server.cpp:296
std::unique_ptr< socket::Socket > socket_
Definition: api_server.h:110
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
bool state
Definition: fan.h:34