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);
297 std::function<
void(std::string)> f) {
300 .attribute = std::move(attribute),
301 .callback = std::move(f),
309 #ifdef USE_HOMEASSISTANT_TIME 311 for (
auto &client : this->
clients_) {
313 client->send_time_request();
325 #ifdef USE_VOICE_ASSISTANT 328 if (c->request_voice_assistant(
true, conversation_id, use_vad))
335 if (c->request_voice_assistant(
false,
"",
false))
341 #ifdef USE_ALARM_CONTROL_PANEL 346 c->send_alarm_control_panel_state(obj);
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.
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.
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 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 on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
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
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 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)
bool uses_password() const
Base-class for all selects.
std::vector< std::unique_ptr< APIConnection > > clients_
Implementation of SPI Controller mode.
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 on_shutdown() override
Base-class for all sensors.
uint16_t get_port() const
void set_password(const std::string &password)
APIServer * global_api_server
std::unique_ptr< socket::Socket > socket_
Base class for all locks.
bool start_voice_assistant(const std::string &conversation_id, bool use_vad)
ClimateDevice - This is the base class for all climate integrations.
void IRAM_ATTR HOT delay(uint32_t ms)