Native API

See Application::init_api_server().

API Reference

namespace api

Enums

enum APIMessageType

Values:

HELLO_REQUEST = 1
HELLO_RESPONSE = 2
CONNECT_REQUEST = 3
CONNECT_RESPONSE = 4
DISCONNECT_REQUEST = 5
DISCONNECT_RESPONSE = 6
PING_REQUEST = 7
PING_RESPONSE = 8
DEVICE_INFO_REQUEST = 9
DEVICE_INFO_RESPONSE = 10
LIST_ENTITIES_REQUEST = 11
LIST_ENTITIES_BINARY_SENSOR_RESPONSE = 12
LIST_ENTITIES_COVER_RESPONSE = 13
LIST_ENTITIES_FAN_RESPONSE = 14
LIST_ENTITIES_LIGHT_RESPONSE = 15
LIST_ENTITIES_SENSOR_RESPONSE = 16
LIST_ENTITIES_SWITCH_RESPONSE = 17
LIST_ENTITIES_TEXT_SENSOR_RESPONSE = 18
LIST_ENTITIES_DONE_RESPONSE = 19
SUBSCRIBE_STATES_REQUEST = 20
BINARY_SENSOR_STATE_RESPONSE = 21
COVER_STATE_RESPONSE = 22
FAN_STATE_RESPONSE = 23
LIGHT_STATE_RESPONSE = 24
SENSOR_STATE_RESPONSE = 25
SWITCH_STATE_RESPONSE = 26
TEXT_SENSOR_STATE_RESPONSE = 27
SUBSCRIBE_LOGS_REQUEST = 28
SUBSCRIBE_LOGS_RESPONSE = 29
COVER_COMMAND_REQUEST = 30
FAN_COMMAND_REQUEST = 31
LIGHT_COMMAND_REQUEST = 32
SWITCH_COMMAND_REQUEST = 33
SUBSCRIBE_SERVICE_CALLS_REQUEST = 34
SERVICE_CALL_RESPONSE = 35
GET_TIME_REQUEST = 36
GET_TIME_RESPONSE = 37
SUBSCRIBE_HOME_ASSISTANT_STATES_REQUEST = 38
SUBSCRIBE_HOME_ASSISTANT_STATE_RESPONSE = 39
HOME_ASSISTANT_STATE_RESPONSE = 40
class APIMessage

Subclassed by api::ConnectRequest, api::CoverCommandRequest, api::DeviceInfoRequest, api::DisconnectRequest, api::DisconnectResponse, api::FanCommandRequest, api::HelloRequest, api::HomeAssistantStateResponse, api::LightCommandRequest, api::ListEntitiesRequest, api::PingRequest, api::PingResponse, api::ServiceCallResponse, api::SubscribeHomeAssistantStatesRequest, api::SubscribeLogsRequest, api::SubscribeServiceCallsRequest, api::SubscribeStatesRequest, api::SwitchCommandRequest, time::GetTimeResponse

Public Functions

void decode(uint8_t *buffer, size_t length)
bool decode_varint(uint32_t field_id, uint32_t value)
bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
bool decode_32bit(uint32_t field_id, uint32_t value)
virtual APIMessageType message_type() const = 0
void encode(APIBuffer &buffer)
namespace api
class APIConnection

Public Functions

APIConnection(AsyncClient *client, APIServer *parent)
~APIConnection()
void disconnect_client_()
bool send_buffer(APIMessageType type, APIBuffer &buf)
template <typename T>
bool send_buffer(T func, APIMessageType type, bool resize = true)
bool send_message(APIMessage &msg, bool resize = true)
bool send_empty_message(APIMessageType type)
void loop()
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state)
bool send_cover_state(cover::Cover *cover)
bool send_fan_state(fan::FanState *fan)
bool send_light_state(light::LightState *light)
bool send_sensor_state(sensor::Sensor *sensor, float state)
bool send_switch_state(switch_::Switch *switch_, bool state)
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state)
bool send_log_message(int level, const char *tag, const char *line)
bool send_disconnect_request(const char *reason)
bool send_ping_request()
void send_service_call(ServiceCallResponse &call)

Protected Types

enum ConnectionState

Values:

WAITING_FOR_HELLO
WAITING_FOR_CONNECT
CONNECTED
enum ParseMessageState

Values:

PREAMBLE
LENGTH_FIELD
TYPE_FIELD
MESSAGE_FIELD
SKIP_MESSAGE_FIELD

Protected Functions

void on_error_(int8_t error)
void on_disconnect_()
void on_timeout_(uint32_t time)
void on_data_(uint8_t *buf, size_t len)
void fatal_error_()
bool valid_rx_message_type_()
void read_message_()
void on_hello_request_(const HelloRequest &req)
void on_connect_request_(const ConnectRequest &req)
void on_disconnect_request_(const DisconnectRequest &req)
void on_disconnect_response_(const DisconnectResponse &req)
void on_ping_request_(const PingRequest &req)
void on_ping_response_(const PingResponse &req)
void on_device_info_request_(const DeviceInfoRequest &req)
void on_list_entities_request_(const ListEntitiesRequest &req)
void on_subscribe_states_request_(const SubscribeStatesRequest &req)
void on_subscribe_logs_request_(const SubscribeLogsRequest &req)
void on_cover_command_request_(const CoverCommandRequest &req)
void on_fan_command_request_(const FanCommandRequest &req)
void on_light_command_request_(const LightCommandRequest &req)
void on_switch_command_request_(const SwitchCommandRequest &req)
void on_subscribe_service_calls_request(const SubscribeServiceCallsRequest &req)
void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &req)
void on_home_assistant_state_response(const HomeAssistantStateResponse &req)
void resize_buffer_()

Protected Attributes

api::APIConnection::ConnectionState WAITING_FOR_HELLO
AsyncClient *client_
APIServer *parent_
uint8_t *buffer_
size_t buffer_size_
api::APIConnection::ParseMessageState PREAMBLE
uint32_t rx_buffer_at_ = {0}
APIMessageType message_type_
size_t message_length_
std::string client_info_
ListEntitiesIterator list_entities_iterator_
InitialStateIterator initial_state_iterator_
bool state_subscription_ = {false}
int log_subscription_ = {ESPHOMELIB_LOG_LEVEL_NONE}
uint32_t last_traffic_
bool sent_ping_ = {false}
bool service_call_subscription_ = {false}
class APIServer : public Component, public StoringUpdateListenerController

Public Functions

APIServer()
void setup()
uint16_t get_port() const
float get_setup_priority() const
void loop()
void dump_config()
bool check_password(const std::string &password) const
bool uses_password() const
void set_port(uint16_t port)
void set_password(const std::string &password)
void set_reboot_timeout(uint32_t reboot_timeout)
void handle_disconnect(APIConnection *conn)
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state)
void on_cover_update(cover::Cover *obj)
void on_fan_update(fan::FanState *obj)
void on_light_update(light::LightState *obj)
void on_sensor_update(sensor::Sensor *obj, float state)
void on_switch_update(switch_::Switch *obj, bool state)
void on_text_sensor_update(text_sensor::TextSensor *obj, std::string state)
void send_service_call(ServiceCallResponse &call)
template <typename T>
HomeAssistantServiceCallAction<T> *make_home_assistant_service_call_action()
void subscribe_home_assistant_state(std::string entity_id, std::function<void(std::string)> f)
const std::vector<APIServer::HomeAssistantStateSubscription> &get_state_subs() const

Protected Attributes

AsyncServer server_ = {0}
uint16_t port_ = {6053}
uint32_t reboot_timeout_ = {300000}
uint32_t last_connected_ = {0}
std::vector<APIConnection *> clients_
std::string password_
std::vector<HomeAssistantStateSubscription> state_subs_
struct HomeAssistantStateSubscription

Public Members

std::string entity_id
std::function<void(std::string)> callback
template <typename T>
class HomeAssistantServiceCallAction : public Action<T>

Public Functions

HomeAssistantServiceCallAction(APIServer *parent)
void set_service(const std::string &service)
void set_data(const std::vector<KeyValuePair> &data)
void set_data_template(const std::vector<KeyValuePair> &data_template)
void set_variables(const std::vector<TemplatableKeyValuePair> &variables)
void play(T x)

Protected Attributes

APIServer *parent_
ServiceCallResponse resp_
namespace api
class ConnectRequest : public api::APIMessage

Public Functions

bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
const std::string &get_password() const
void set_password(const std::string &password)
APIMessageType message_type() const

Protected Attributes

std::string password_
class DeviceInfoRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
class DisconnectRequest : public api::APIMessage

Public Functions

bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
void encode(APIBuffer &buffer)
APIMessageType message_type() const
const std::string &get_reason() const
void set_reason(const std::string &reason)

Protected Attributes

std::string reason_
class DisconnectResponse : public api::APIMessage

Public Functions

APIMessageType message_type() const
class HelloRequest : public api::APIMessage

Public Functions

bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
const std::string &get_client_info() const
void set_client_info(const std::string &client_info)
APIMessageType message_type() const

Protected Attributes

std::string client_info_
class PingRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
class PingResponse : public api::APIMessage

Public Functions

APIMessageType message_type() const
namespace api
class CoverCommandRequest : public api::APIMessage

Public Functions

bool decode_varint(uint32_t field_id, uint32_t value)
bool decode_32bit(uint32_t field_id, uint32_t value)
APIMessageType message_type() const
uint32_t get_key() const
optional<cover::CoverCommand> get_command() const

Protected Attributes

uint32_t key_ = {0}
bool has_state_ = {false}
cover::CoverCommand command_ = {cover::COVER_COMMAND_OPEN}
class FanCommandRequest : public api::APIMessage

Public Functions

bool decode_varint(uint32_t field_id, uint32_t value)
bool decode_32bit(uint32_t field_id, uint32_t value)
APIMessageType message_type() const
uint32_t get_key() const
optional<bool> get_state() const
optional<fan::FanSpeed> get_speed() const
optional<bool> get_oscillating() const

Protected Attributes

uint32_t key_ = {0}
bool has_state_ = {false}
bool state_ = {false}
bool has_speed_ = {false}
fan::FanSpeed speed_ = {fan::FAN_SPEED_LOW}
bool has_oscillating_ = {false}
bool oscillating_ = {false}
class LightCommandRequest : public api::APIMessage

Public Functions

bool decode_varint(uint32_t field_id, uint32_t value)
bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
bool decode_32bit(uint32_t field_id, uint32_t value)
APIMessageType message_type() const
uint32_t get_key() const
optional<bool> get_state() const
optional<float> get_brightness() const
optional<float> get_red() const
optional<float> get_green() const
optional<float> get_blue() const
optional<float> get_white() const
optional<float> get_color_temperature() const
optional<uint32_t> get_transition_length() const
optional<uint32_t> get_flash_length() const
optional<std::string> get_effect() const

Protected Attributes

uint32_t key_ = {0}
bool has_state_ = {false}
bool state_ = {false}
bool has_brightness_ = {false}
float brightness_ = {0.0f}
bool has_rgb_ = {false}
float red_ = {0.0f}
float green_ = {0.0f}
float blue_ = {0.0f}
bool has_white_ = {false}
float white_ = {0.0f}
bool has_color_temperature_ = {false}
float color_temperature_ = {0.0f}
bool has_transition_length_ = {false}
uint32_t transition_length_ = {0}
bool has_flash_length_ = {false}
uint32_t flash_length_ = {0}
bool has_effect_ = {false}
std::string effect_ = {}
class SwitchCommandRequest : public api::APIMessage

Public Functions

bool decode_varint(uint32_t field_id, uint32_t value)
bool decode_32bit(uint32_t field_id, uint32_t value)
APIMessageType message_type() const
uint32_t get_key() const
bool get_state() const

Protected Attributes

uint32_t key_ = {0}
bool state_ = {false}
namespace api
class ListEntitiesIterator : public api::ComponentIterator

Public Functions

ListEntitiesIterator(StoringController *controller, APIConnection *client)
bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
bool on_cover(cover::Cover *cover)
bool on_fan(fan::FanState *fan)
bool on_light(light::LightState *light)
bool on_sensor(sensor::Sensor *sensor)
bool on_switch(switch_::Switch *switch_)
bool on_text_sensor(text_sensor::TextSensor *text_sensor)
bool on_end()

Protected Attributes

APIConnection *client_
class ListEntitiesRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
namespace api
class KeyValuePair

Public Functions

KeyValuePair(const std::string &key, const std::string &value)

Public Members

std::string key
std::string value
class ServiceCallResponse : public api::APIMessage

Public Functions

APIMessageType message_type() const
void encode(APIBuffer &buffer)
void set_service(const std::string &service)
void set_data(const std::vector<KeyValuePair> &data)
void set_data_template(const std::vector<KeyValuePair> &data_template)
void set_variables(const std::vector<TemplatableKeyValuePair> &variables)

Protected Attributes

std::string service_
std::vector<KeyValuePair> data_
std::vector<KeyValuePair> data_template_
std::vector<TemplatableKeyValuePair> variables_
class SubscribeServiceCallsRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
class TemplatableKeyValuePair

Public Functions

template <typename T>
TemplatableKeyValuePair(std::string key, T func)

Public Members

std::string key
std::function<std::string()> value
namespace api
class SubscribeLogsRequest : public api::APIMessage

Public Functions

bool decode_varint(uint32_t field_id, uint32_t value)
APIMessageType message_type() const
uint32_t get_level() const
void set_level(uint32_t level)
bool get_dump_config() const
void set_dump_config(bool dump_config)

Protected Attributes

uint32_t level_ = {6}
bool dump_config_ = {false}
namespace api
class HomeAssistantStateResponse : public api::APIMessage

Public Functions

bool decode_length_delimited(uint32_t field_id, const uint8_t *value, size_t len)
APIMessageType message_type() const
const std::string &get_entity_id() const
const std::string &get_state() const

Protected Attributes

std::string entity_id_
std::string state_
class InitialStateIterator : public api::ComponentIterator

Public Functions

InitialStateIterator(StoringController *controller, APIConnection *client)
bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor)
bool on_cover(cover::Cover *cover)
bool on_fan(fan::FanState *fan)
bool on_light(light::LightState *light)
bool on_sensor(sensor::Sensor *sensor)
bool on_switch(switch_::Switch *switch_)
bool on_text_sensor(text_sensor::TextSensor *text_sensor)

Protected Attributes

APIConnection *client_
class SubscribeHomeAssistantStatesRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
class SubscribeStatesRequest : public api::APIMessage

Public Functions

APIMessageType message_type() const
namespace api
class APIBuffer

Public Functions

APIBuffer(uint8_t *buffer, size_t max_len)
APIBuffer(const APIBuffer&)
size_t get_length() const
bool get_overflow() const
void write(uint8_t value)
void encode_int32(uint32_t field, int32_t value)
void encode_uint32(uint32_t field, uint32_t value)
void encode_sint32(uint32_t field, int32_t value)
void encode_bool(uint32_t field, bool value)
void encode_string(uint32_t field, const std::string &value)
void encode_string(uint32_t field, const char *string, size_t len)
void encode_fixed32(uint32_t field, uint32_t value)
void encode_float(uint32_t field, float value)
void encode_nameable(Nameable *nameable)
size_t begin_nested(uint32_t field)
void end_nested(size_t begin_index)
void encode_field_(uint32_t field, uint32_t type)
void encode_varint_(uint32_t value)
uint32_t varint_length_(uint32_t value)

Protected Attributes

uint8_t *buffer_
size_t at_ = {0}
size_t max_len_
bool overflow_ = {false}
class ComponentIterator

Subclassed by api::InitialStateIterator, api::ListEntitiesIterator

Public Functions

ComponentIterator(StoringController *controller)
void begin()
void advance()
bool on_begin()
virtual bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) = 0
virtual bool on_cover(cover::Cover *cover) = 0
virtual bool on_fan(fan::FanState *fan) = 0
virtual bool on_light(light::LightState *light) = 0
virtual bool on_sensor(sensor::Sensor *sensor) = 0
virtual bool on_switch(switch_::Switch *switch_) = 0
virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor) = 0
bool on_end()

Protected Types

enum IteratorState

Values:

NONE = 0
BEGIN
BINARY_SENSOR
COVER
FAN
LIGHT
SENSOR
SWITCH
TEXT_SENSOR
MAX

Protected Attributes

api::ComponentIterator::IteratorState NONE
size_t at_ = {0}
StoringController *controller_