ESPHome 2026.10.0
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"
9
10namespace esphome::homeassistant {
11
12static const char *const TAG = "homeassistant.button";
13
15 LOG_BUTTON("", "Homeassistant Button", this);
16 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
17}
18
20
22 if (!api::global_api_server->is_connected()) {
23 ESP_LOGE(TAG, "No clients connected to API server");
24 return;
25 }
26
27#ifdef USE_API_WIZARD_LINKED_INPUTS
28 if (this->entity_id_[0] == '\0') {
29 ESP_LOGW(TAG, "'%s': No entity ID set yet", this->get_name().c_str());
30 return;
31 }
32#endif
33
34 static constexpr auto SERVICE_BUTTON = StringRef::from_lit("button.press");
35 static constexpr auto SERVICE_INPUT_BUTTON = StringRef::from_lit("input_button.press");
36 static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
37 static constexpr char INPUT_PREFIX[] = "input_";
38
40 if (strncmp(this->entity_id_, INPUT_PREFIX, sizeof(INPUT_PREFIX) - 1) == 0) {
41 resp.service = SERVICE_INPUT_BUTTON;
42 } else {
43 resp.service = SERVICE_BUTTON;
44 }
45
46 resp.data.init(1);
47 auto &entity_id_kv = resp.data.emplace_back();
48 entity_id_kv.key = ENTITY_ID_KEY;
49 entity_id_kv.value = StringRef(this->entity_id_);
50
52}
53
54} // namespace esphome::homeassistant
const StringRef & get_name() const
Definition entity_base.h:71
StringRef is a reference to a string owned by something else.
Definition string_ref.h:30
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:54
void send_homeassistant_action(const HomeassistantActionRequest &call)
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1414
APIServer * global_api_server
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:57