6#ifdef USE_API_USER_DEFINED_ACTIONS
11#ifdef USE_API_USER_DEFINED_ACTIONS
15 void (T::*callback)(Ts...))
55#ifdef USE_API_USER_DEFINED_ACTIONS
56 template<
typename T,
typename... Ts>
58 const std::array<std::string,
sizeof...(Ts)> &arg_names) {
59#ifdef USE_API_CUSTOM_SERVICES
65 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
69 template<
typename T,
typename... Ts>
71 const std::array<std::string,
sizeof...(Ts)> &arg_names) {
74 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
96#ifdef USE_API_USER_DEFINED_ACTIONS
97 template<
typename T>
void register_service(
void (T::*callback)(),
const std::string &name) {
98#ifdef USE_API_CUSTOM_SERVICES
104 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
108 template<
typename T>
void register_service(
void (T::*callback)(),
const std::string &name) {
111 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
115#ifdef USE_API_HOMEASSISTANT_STATES
138 const std::string &attribute =
"") {
139 auto *obj =
static_cast<T *
>(
this);
149 ESPDEPRECATED(
"Use void callback(StringRef) instead. Will be removed in 2027.1.0.",
"2026.1.0")
151 const std::
string &attribute = "") {
152 auto *obj =
static_cast<T *
>(
this);
155 entity_id, optional<std::string>(attribute),
156 std::function<
void(
const std::string &)>(
157 [obj, callback](
const std::string &
state) { (obj->*callback)(
state); }));
181 const std::string &attribute =
"") {
182 auto *obj =
static_cast<T *
>(
this);
184 entity_id, optional<std::string>(attribute),
193 ESPDEPRECATED(
"Use void callback(const std::string &, StringRef) instead. Will be removed in 2027.1.0.",
"2026.1.0")
195 const std::
string &attribute = "") {
196 auto *obj =
static_cast<T *
>(
this);
199 entity_id, optional<std::string>(attribute),
200 std::function<
void(
const std::string &)>(
201 [obj, callback, entity_id](
const std::string &
state) { (obj->*callback)(entity_id,
state); }));
206 const std::string &attribute =
"") {
207 static_assert(
sizeof(T) == 0,
208 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
209 "of your YAML configuration");
214 const std::string &attribute =
"") {
215 static_assert(
sizeof(T) == 0,
216 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
217 "of your YAML configuration");
222 const std::string &attribute =
"") {
223 static_assert(
sizeof(T) == 0,
224 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
225 "of your YAML configuration");
230 const std::string &attribute =
"") {
231 static_assert(
sizeof(T) == 0,
232 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
233 "of your YAML configuration");
237#ifdef USE_API_HOMEASSISTANT_SERVICES
271 resp.
data.init(data.size());
272 for (
auto &it : data) {
273 auto &kv = resp.
data.emplace_back();
314 resp.
data.init(data.size());
315 for (
auto &it : data) {
316 auto &kv = resp.
data.emplace_back();
324 static_assert(
sizeof(T) == 0,
"call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
325 "section of your YAML configuration");
328 template<
typename T =
void>
330 static_assert(
sizeof(T) == 0,
"call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
331 "section of your YAML configuration");
335 static_assert(
sizeof(T) == 0,
"fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
336 "section of your YAML configuration");
339 template<
typename T =
void>
341 static_assert(
sizeof(T) == 0,
"fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
342 "section of your YAML configuration");
StringRef is a reference to a string owned by something else.
void register_user_service(UserServiceDescriptor *descriptor)
bool is_connected() const
void send_homeassistant_action(const HomeassistantActionRequest &call)
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void subscribe_homeassistant_state(void(T::*callback)(std::string), const std::string &entity_id, const std::string &attribute="")
void fire_homeassistant_event(const std::string &service_name, const std::map< std::string, std::string > &data)
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.
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 vers...
void fire_homeassistant_event(const std::string &event_name)
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.
void call_homeassistant_service(const std::string &service_name)
Call a Home Assistant service from ESPHome.
void register_service(void(T::*callback)(), const std::string &name)
Register a custom native API service that will show up in Home Assistant.
void fire_homeassistant_event(const std::string &event_name)
Fire an ESPHome event in Home Assistant.
bool is_connected() const
Return if a client (such as Home Assistant) is connected to the native API.
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 vers...
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.
void fire_homeassistant_event(const std::string &service_name, const std::map< std::string, std::string > &data)
Fire an ESPHome event in Home Assistant.
void call_homeassistant_service(const std::string &service_name)
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, const std::map< std::string, std::string > &data)
Call a Home Assistant service from ESPHome.
void call_homeassistant_service(const std::string &service_name, const std::map< std::string, std::string > &data)
void execute(uint32_t, bool, Ts... x) override
void(T::* callback_)(Ts...)
CustomAPIDeviceService(const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names, T *obj, void(T::*callback)(Ts...))
FixedVector< HomeassistantServiceMap > data
APIServer * global_api_server