21 static const char *
const TAG =
"api";
25 ESP_LOGCONFIG(TAG,
"Setting up Home Assistant API server...");
29 ESP_LOGW(TAG,
"Could not create socket.");
34 int err =
socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
36 ESP_LOGW(TAG,
"Socket unable to set reuseaddr: errno %d", err);
39 err =
socket_->setblocking(
false);
41 ESP_LOGW(TAG,
"Socket unable to set nonblocking mode: errno %d", err);
50 ESP_LOGW(TAG,
"Socket unable to set sockaddr: errno %d", errno);
57 ESP_LOGW(TAG,
"Socket unable to bind: errno %d", errno);
64 ESP_LOGW(TAG,
"Socket unable to listen: errno %d", errno);
74 c->send_log_message(level, tag, message);
82 #ifdef USE_ESP32_CAMERA 85 [
this](
const std::shared_ptr<esp32_camera::CameraImage> &image) {
88 c->send_camera_state(image);
99 auto sock =
socket_->accept((
struct sockaddr *) &source_addr, &addr_len);
102 ESP_LOGD(TAG,
"Accepted %s", sock->getpeername().c_str());
111 [](
const std::unique_ptr<APIConnection> &conn) {
return !conn->remove_; });
113 for (
auto it = new_end; it != this->
clients_.end(); ++it) {
114 ESP_LOGV(TAG,
"Removing connection to %s", (*it)->client_info_.c_str());
119 for (
auto &client : this->
clients_) {
124 const uint32_t now =
millis();
127 ESP_LOGE(TAG,
"No client connected to API. Rebooting...");
138 ESP_LOGCONFIG(TAG,
"API Server:");
141 ESP_LOGCONFIG(TAG,
" Using noise encryption: YES");
143 ESP_LOGCONFIG(TAG,
" Using noise encryption: NO");
150 uint32_t len_a = this->
password_.length();
151 const char *b = password.c_str();
152 uint32_t len_b = password.length();
155 volatile uint32_t length = len_b;
156 volatile const char *left =
nullptr;
157 volatile const char *right = b;
160 if (len_a == length) {
161 left = *((
volatile const char **) &a);
164 if (len_a != length) {
169 for (
size_t i = 0; i < length; i++) {
170 result |= *left++ ^ *right++;
176 #ifdef USE_BINARY_SENSOR 181 c->send_binary_sensor_state(obj, state);
190 c->send_cover_state(obj);
199 c->send_fan_state(obj);
208 c->send_light_state(obj);
217 c->send_sensor_state(obj, state);
226 c->send_switch_state(obj, state);
230 #ifdef USE_TEXT_SENSOR 235 c->send_text_sensor_state(obj, state);
244 c->send_climate_state(obj);
253 c->send_number_state(obj, state);
262 c->send_select_state(obj, state);
271 c->send_lock_state(obj, obj->
state);
275 #ifdef USE_MEDIA_PLAYER 280 c->send_media_player_state(obj);
290 for (
auto &client : this->
clients_) {
291 client->send_homeassistant_service_call(call);
294 #ifdef USE_BLUETOOTH_PROXY 296 for (
auto &client : this->
clients_) {
297 client->send_bluetooth_le_advertisement(call);
307 for (
auto &client : this->
clients_) {
308 client->send_bluetooth_device_connection_response(call);
318 for (
auto &client : this->
clients_) {
319 client->send_bluetooth_device_pairing_response(call);
329 for (
auto &client : this->
clients_) {
330 client->send_bluetooth_device_unpairing_response(call);
340 for (
auto &client : this->
clients_) {
341 client->send_bluetooth_device_clear_cache_response(call);
350 for (
auto &client : this->
clients_) {
351 client->send_bluetooth_connections_free_response(call);
356 for (
auto &client : this->
clients_) {
357 client->send_bluetooth_gatt_read_response(call);
361 for (
auto &client : this->
clients_) {
362 client->send_bluetooth_gatt_write_response(call);
366 for (
auto &client : this->
clients_) {
367 client->send_bluetooth_gatt_notify_data_response(call);
371 for (
auto &client : this->
clients_) {
372 client->send_bluetooth_gatt_notify_response(call);
376 for (
auto &client : this->
clients_) {
377 client->send_bluetooth_gatt_get_services_response(call);
384 for (
auto &client : this->
clients_) {
385 client->send_bluetooth_gatt_get_services_done_response(call);
394 for (
auto &client : this->
clients_) {
395 client->send_bluetooth_gatt_error_response(call);
402 std::function<
void(std::string)> f) {
405 .attribute = std::move(attribute),
406 .callback = std::move(f),
414 #ifdef USE_HOMEASSISTANT_TIME 416 for (
auto &client : this->
clients_) {
418 client->send_time_request();
430 #ifdef USE_VOICE_ASSISTANT 433 if (c->request_voice_assistant(
true))
440 if (c->request_voice_assistant(
false))
Base class for all switches.
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol...
void handle_disconnect(APIConnection *conn)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
bool is_connected() const
void add_on_log_callback(std::function< void(int, const char *, const char *)> &&callback)
Register a callback that will be called for every log message sent.
Base class for all cover devices.
void send_bluetooth_gatt_notify_response(const BluetoothGATTNotifyResponse &call)
void send_bluetooth_le_advertisement(const BluetoothLEAdvertisementResponse &call)
void send_bluetooth_device_pairing(uint64_t address, bool paired, esp_err_t error=ESP_OK)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
std::string get_use_address()
Get the active network hostname.
void send_bluetooth_gatt_notify_data_response(const BluetoothGATTNotifyDataResponse &call)
LockState state
The current reported state of the lock.
void on_media_player_update(media_player::MediaPlayer *obj) override
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
bool check_password(const std::string &password) const
std::vector< HomeAssistantStateSubscription > state_subs_
void on_light_update(light::LightState *obj) override
void stop_voice_assistant()
void on_lock_update(lock::Lock *obj) override
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void send_bluetooth_gatt_error(uint64_t address, uint16_t handle, esp_err_t error)
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&f)
void on_cover_update(cover::Cover *obj) override
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
uint32_t IRAM_ATTR HOT millis()
void on_fan_update(fan::Fan *obj) override
void send_bluetooth_device_connection(uint64_t address, bool connected, uint16_t mtu=0, esp_err_t error=ESP_OK)
void send_bluetooth_connections_free(uint8_t free, uint8_t limit)
void on_switch_update(switch_::Switch *obj, bool state) override
void dump_config() override
ESP32Camera * global_esp32_camera
void on_number_update(number::Number *obj, float state) override
void send_bluetooth_device_unpairing(uint64_t address, bool success, esp_err_t error=ESP_OK)
void send_bluetooth_gatt_services_done(uint64_t address)
Base-class for all numbers.
void status_clear_warning()
void set_reboot_timeout(uint32_t reboot_timeout)
void on_climate_update(climate::Climate *obj) override
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Application App
Global storage of Application pointer - only one Application can exist.
void status_set_warning()
void send_bluetooth_gatt_services(const BluetoothGATTGetServicesResponse &call)
void set_port(uint16_t port)
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
virtual void mark_failed()
Mark this component as failed.
void on_sensor_update(sensor::Sensor *obj, float state) override
void setup_controller(bool include_internal=false)
void send_bluetooth_gatt_write_response(const BluetoothGATTWriteResponse &call)
bool uses_password() const
Base-class for all selects.
std::vector< std::unique_ptr< APIConnection > > clients_
Base class for all binary_sensor-type classes.
float get_setup_priority() const override
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void send_bluetooth_device_clear_cache(uint64_t address, bool success, esp_err_t error=ESP_OK)
void on_shutdown() override
Base-class for all sensors.
void send_bluetooth_gatt_read_response(const BluetoothGATTReadResponse &call)
uint16_t get_port() const
void set_password(const std::string &password)
APIServer * global_api_server
bool start_voice_assistant()
std::unique_ptr< socket::Socket > socket_
Base class for all locks.
ClimateDevice - This is the base class for all climate integrations.
void IRAM_ATTR HOT delay(uint32_t ms)