ESPHome 2026.2.2
Loading...
Searching...
No Matches
esphome::api::CustomAPIDevice Class Reference

#include <custom_api_device.h>

Public Member Functions

bool is_connected () const
 Return if a client (such as Home Assistant) is connected to the native API.
 
template<typename T , typename... Ts>
void register_service (void(T::*callback)(Ts...), const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
 Register a custom native API service that will show up in Home Assistant.
 
template<typename T , typename... Ts>
void register_service (void(T::*callback)(Ts...), const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
 
template<typename T >
void register_service (void(T::*callback)(), const std::string &name)
 Register a custom native API service that will show up in Home Assistant.
 
template<typename T >
void register_service (void(T::*callback)(), const std::string &name)
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(StringRef), const std::string &entity_id, const std::string &attribute="")
 Subscribe to the state (or attribute state) of an entity from Home Assistant.
 
template<typename T >
 ESPDEPRECATED ("Use void callback(StringRef) instead. Will be removed in 2027.1.0.", "2026.1.0") void subscribe_homeassistant_state(void(T
 Subscribe to the state (or attribute state) of an entity from Home Assistant (legacy std::string version).
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(const std::string &, StringRef), const std::string &entity_id, const std::string &attribute="")
 Subscribe to the state (or attribute state) of an entity from Home Assistant.
 
template<typename T >
 ESPDEPRECATED ("Use void callback(const std::string &, StringRef) instead. Will be removed in 2027.1.0.", "2026.1.0") void subscribe_homeassistant_state(void(T
 Subscribe to the state (or attribute state) of an entity from Home Assistant (legacy std::string version).
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(StringRef), const std::string &entity_id, const std::string &attribute="")
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(std::string), const std::string &entity_id, const std::string &attribute="")
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(const std::string &, StringRef), const std::string &entity_id, const std::string &attribute="")
 
template<typename T >
void subscribe_homeassistant_state (void(T::*callback)(std::string, std::string), const std::string &entity_id, const std::string &attribute="")
 
void call_homeassistant_service (const std::string &service_name)
 Call a Home Assistant service from ESPHome.
 
void call_homeassistant_service (const std::string &service_name, const std::map< std::string, std::string > &data)
 Call a Home Assistant service from ESPHome.
 
void fire_homeassistant_event (const std::string &event_name)
 Fire an ESPHome event in Home Assistant.
 
void fire_homeassistant_event (const std::string &service_name, const std::map< std::string, std::string > &data)
 Fire an ESPHome event in Home Assistant.
 
template<typename T = void>
void call_homeassistant_service (const std::string &service_name)
 
template<typename T = void>
void call_homeassistant_service (const std::string &service_name, const std::map< std::string, std::string > &data)
 
template<typename T = void>
void fire_homeassistant_event (const std::string &event_name)
 
template<typename T = void>
void fire_homeassistant_event (const std::string &service_name, const std::map< std::string, std::string > &data)
 

Detailed Description

Definition at line 29 of file custom_api_device.h.

Member Function Documentation

◆ call_homeassistant_service() [1/4]

void esphome::api::CustomAPIDevice::call_homeassistant_service ( const std::string & service_name)
inline

Call a Home Assistant service from ESPHome.

Usage:

call_homeassistant_service("homeassistant.restart");
void call_homeassistant_service(const std::string &service_name)
Call a Home Assistant service from ESPHome.
Parameters
service_nameThe service to call.

Definition at line 241 of file custom_api_device.h.

◆ call_homeassistant_service() [2/4]

template<typename T = void>
void esphome::api::CustomAPIDevice::call_homeassistant_service ( const std::string & service_name)
inline

Definition at line 316 of file custom_api_device.h.

◆ call_homeassistant_service() [3/4]

void esphome::api::CustomAPIDevice::call_homeassistant_service ( const std::string & service_name,
const std::map< std::string, std::string > & data )
inline

Call a Home Assistant service from ESPHome.

Usage:

call_homeassistant_service("light.turn_on", {
{"entity_id", "light.my_light"},
{"brightness", "127"},
});
Parameters
service_nameThe service to call.
dataThe data for the service call, mapping from string to string.

Definition at line 261 of file custom_api_device.h.

◆ call_homeassistant_service() [4/4]

template<typename T = void>
void esphome::api::CustomAPIDevice::call_homeassistant_service ( const std::string & service_name,
const std::map< std::string, std::string > & data )
inline

Definition at line 322 of file custom_api_device.h.

◆ ESPDEPRECATED() [1/2]

template<typename T >
esphome::api::CustomAPIDevice::ESPDEPRECATED ( "Use void callback(const std::string &, StringRef) instead. Will be removed in 2027.1.0." ,
"2026.1.0"  )
inline

Subscribe to the state (or attribute state) of an entity from Home Assistant (legacy std::string version).

Definition at line 188 of file custom_api_device.h.

◆ ESPDEPRECATED() [2/2]

template<typename T >
esphome::api::CustomAPIDevice::ESPDEPRECATED ( "Use void callback(StringRef) instead. Will be removed in 2027.1.0." ,
"2026.1.0"  )
inline

Subscribe to the state (or attribute state) of an entity from Home Assistant (legacy std::string version).

Definition at line 148 of file custom_api_device.h.

◆ fire_homeassistant_event() [1/4]

void esphome::api::CustomAPIDevice::fire_homeassistant_event ( const std::string & event_name)
inline

Fire an ESPHome event in Home Assistant.

Usage:

fire_homeassistant_event("esphome.something_happened");
void fire_homeassistant_event(const std::string &event_name)
Fire an ESPHome event in Home Assistant.
Parameters
event_nameThe event to fire.

Definition at line 283 of file custom_api_device.h.

◆ fire_homeassistant_event() [2/4]

template<typename T = void>
void esphome::api::CustomAPIDevice::fire_homeassistant_event ( const std::string & event_name)
inline

Definition at line 327 of file custom_api_device.h.

◆ fire_homeassistant_event() [3/4]

void esphome::api::CustomAPIDevice::fire_homeassistant_event ( const std::string & service_name,
const std::map< std::string, std::string > & data )
inline

Fire an ESPHome event in Home Assistant.

Usage:

fire_homeassistant_event("esphome.something_happened", {
{"my_value", "500"},
});
Parameters
event_nameThe event to fire.
dataThe data for the event, mapping from string to string.

Definition at line 303 of file custom_api_device.h.

◆ fire_homeassistant_event() [4/4]

template<typename T = void>
void esphome::api::CustomAPIDevice::fire_homeassistant_event ( const std::string & service_name,
const std::map< std::string, std::string > & data )
inline

Definition at line 333 of file custom_api_device.h.

◆ is_connected()

bool esphome::api::CustomAPIDevice::is_connected ( ) const
inline

Return if a client (such as Home Assistant) is connected to the native API.

Definition at line 32 of file custom_api_device.h.

◆ register_service() [1/4]

template<typename T >
void esphome::api::CustomAPIDevice::register_service ( void(T::* callback )(),
const std::string & name )
inline

Register a custom native API service that will show up in Home Assistant.

Usage:

void setup() override {
register_service(&CustomNativeAPI::on_hello_world, "hello_world");
}
void on_hello_world() {
// Hello World service called.
}
void register_service(void(T::*callback)(Ts...), const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
Register a custom native API service that will show up in Home Assistant.
void setup()
Template Parameters
TThe class type creating the service, automatically deduced from the function pointer.
Parameters
callbackThe member function to call when the service is triggered.
nameThe name of the arguments for the service, must match the arguments of the function.

Definition at line 97 of file custom_api_device.h.

◆ register_service() [2/4]

template<typename T >
void esphome::api::CustomAPIDevice::register_service ( void(T::* callback )(),
const std::string & name )
inline

Definition at line 108 of file custom_api_device.h.

◆ register_service() [3/4]

template<typename T , typename... Ts>
void esphome::api::CustomAPIDevice::register_service ( void(T::* callback )(Ts...),
const std::string & name,
const std::array< std::string, sizeof...(Ts)> & arg_names )
inline

Register a custom native API service that will show up in Home Assistant.

Usage:

void setup() override {
register_service(&CustomNativeAPI::on_start_washer_cycle, "start_washer_cycle",
{"cycle_length"});
}
void on_start_washer_cycle(int cycle_length) {
// Start washer cycle.
}
Template Parameters
TThe class type creating the service, automatically deduced from the function pointer.
TsThe argument types for the service, automatically deduced from the function arguments.
Parameters
callbackThe member function to call when the service is triggered.
nameThe name of the service to register.
arg_namesThe name of the arguments for the service, must match the arguments of the function.

Definition at line 57 of file custom_api_device.h.

◆ register_service() [4/4]

template<typename T , typename... Ts>
void esphome::api::CustomAPIDevice::register_service ( void(T::* callback )(Ts...),
const std::string & name,
const std::array< std::string, sizeof...(Ts)> & arg_names )
inline

Definition at line 70 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [1/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(const std::string &, StringRef),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Subscribe to the state (or attribute state) of an entity from Home Assistant.

Usage:

void setup() override {
subscribe_homeassistant_state(&CustomNativeAPI::on_state_changed, "sensor.weather_forecast");
}
void on_state_changed(const std::string &entity_id, StringRef state) {
// State of `entity_id` is `state`
}
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void subscribe_homeassistant_state(void(T::*callback)(StringRef), const std::string &entity_id, const std::string &attribute="")
Subscribe to the state (or attribute state) of an entity from Home Assistant.
bool state
Definition fan.h:2
Template Parameters
TThe class type creating the service, automatically deduced from the function pointer.
Parameters
callbackThe member function to call when the entity state changes (zero-allocation for state).
entity_idThe entity_id to track.
attributeThe entity state attribute to track.

Definition at line 177 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [2/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(const std::string &, StringRef),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Definition at line 214 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [3/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(std::string),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Definition at line 206 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [4/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(std::string, std::string),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Definition at line 222 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [5/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(StringRef),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Subscribe to the state (or attribute state) of an entity from Home Assistant.

Usage:

void setup() override {
subscribe_homeassistant_state(&CustomNativeAPI::on_state_changed, "climate.kitchen", "current_temperature");
}
void on_state_changed(StringRef state) {
// State of climate.kitchen current_temperature is `state`
// Use state.c_str() for C string, state.str() for std::string
}
Template Parameters
TThe class type creating the service, automatically deduced from the function pointer.
Parameters
callbackThe member function to call when the entity state changes (zero-allocation).
entity_idThe entity_id to track.
attributeThe entity state attribute to track.

Definition at line 137 of file custom_api_device.h.

◆ subscribe_homeassistant_state() [6/6]

template<typename T >
void esphome::api::CustomAPIDevice::subscribe_homeassistant_state ( void(T::* callback )(StringRef),
const std::string & entity_id,
const std::string & attribute = "" )
inline

Definition at line 198 of file custom_api_device.h.


The documentation for this class was generated from the following file: