ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
homeassistant_button.cpp
Go to the documentation of this file.
2
3#include <cstring>
4
7#include "esphome/core/log.h"
10
11namespace esphome::homeassistant {
12
13ESPHOME_LOG_TAG(TAG, "homeassistant.button");
14
15void press_button_entity([[maybe_unused]] button::Button *button, const char *entity_id) {
16 if (!api::global_api_server->is_connected()) {
17 ESP_LOGE(TAG, "No clients connected to API server");
18 return;
19 }
20
21#ifdef USE_API_WIZARD_LINKED_INPUTS
22 if (entity_id[0] == '\0') {
23 ESP_LOGW(TAG, "'%s': No entity ID set yet", LOG_STR_ARG(button->get_log_name()));
24 return;
25 }
26#endif
27
28 // In flash on the ESP8266 and encoded from there
29 static const char SERVICE_BUTTON[] PROGMEM = "button.press";
30 static const char SERVICE_INPUT_BUTTON[] PROGMEM = "input_button.press";
31 static const char ENTITY_ID_KEY[] PROGMEM = "entity_id";
32
34 // An input helper entity id starts with the "input_" its action name starts with
35 const bool input_helper = ESPHOME_strncmp_P(entity_id, SERVICE_INPUT_BUTTON, sizeof("input_") - 1) == 0;
36 resp.service =
37 input_helper ? ProgmemStringRef::from_lit(SERVICE_INPUT_BUTTON) : ProgmemStringRef::from_lit(SERVICE_BUTTON);
38
39 resp.data.init(1);
40 auto &entity_id_kv = resp.data.emplace_back();
41 entity_id_kv.key = ProgmemStringRef::from_lit(ENTITY_ID_KEY);
42 entity_id_kv.value = ProgmemStringRef(StringRef(entity_id));
43
45}
46
47} // namespace esphome::homeassistant
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:236
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:242
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
bool send_homeassistant_action(const HomeassistantActionRequest &call)
Returns false when no subscribed client took the request, so it was dropped.
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1514
Base class for all buttons.
Definition button.h:25
APIServer * global_api_server
static void const char * entity_id
void press_button_entity(button::Button *button, const char *entity_id)
Ask Home Assistant to press entity_id, with input_button.press for an input_button helper.
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114