ESPHome 2026.11.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
16 LOG_BUTTON("", "Homeassistant Button", this);
17 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
18}
19
21
23 if (!api::global_api_server->is_connected()) {
24 ESP_LOGE(TAG, "No clients connected to API server");
25 return;
26 }
27
28#ifdef USE_API_WIZARD_LINKED_INPUTS
29 if (this->entity_id_[0] == '\0') {
30 ESP_LOGW(TAG, "'%s': No entity ID set yet", this->get_name().c_str());
31 return;
32 }
33#endif
34
35 // In flash on the ESP8266 and encoded from there
36 static const char SERVICE_BUTTON[] PROGMEM = "button.press";
37 static const char SERVICE_INPUT_BUTTON[] PROGMEM = "input_button.press";
38 static const char ENTITY_ID_KEY[] PROGMEM = "entity_id";
39
41 // An input helper entity id starts with the "input_" its action name starts with
42 const bool input_helper = ESPHOME_strncmp_P(this->entity_id_, SERVICE_INPUT_BUTTON, sizeof("input_") - 1) == 0;
43 resp.service =
44 input_helper ? ProgmemStringRef::from_lit(SERVICE_INPUT_BUTTON) : ProgmemStringRef::from_lit(SERVICE_BUTTON);
45
46 resp.data.init(1);
47 auto &entity_id_kv = resp.data.emplace_back();
48 entity_id_kv.key = ProgmemStringRef::from_lit(ENTITY_ID_KEY);
49 entity_id_kv.value = ProgmemStringRef(StringRef(this->entity_id_));
50
52}
53
54} // namespace esphome::homeassistant
const StringRef & get_name() const
Definition entity_base.h:71
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:230
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:236
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
void send_homeassistant_action(const HomeassistantActionRequest &call)
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1414
APIServer * global_api_server
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:57