ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
homeassistant_text.cpp
Go to the documentation of this file.
2
3#include "../homeassistant.h"
4
8#include "esphome/core/log.h"
11
12namespace esphome::homeassistant {
13
14ESPHOME_LOG_TAG(TAG, "homeassistant.text");
15
16PROGMEM_STRING_TABLE(TextModeNames, "text", "password");
17static_assert(text::TEXT_MODE_TEXT == 0 && text::TEXT_MODE_PASSWORD == 1 && TextModeNames::COUNT == 2);
18
20 if (state == this->state) {
21 return;
22 }
23 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, state.c_str());
24 this->publish_state(state.c_str(), state.size());
25}
26
27optional<int> HomeassistantText::parse_length_(const char *name, StringRef value) {
28 auto length = parse_number<int>(value.c_str());
29 if (!length.has_value()) {
30 ESP_LOGE(TAG, "'%s': Can't convert '%s' value '%s' to number!", this->entity_id_, name, value.c_str());
31 return nullopt;
32 }
33 ESP_LOGD(TAG, "'%s': Retrieved '%s': %s", this->entity_id_, name, value.c_str());
34 return length;
35}
36
38 const int16_t index = progmem_string_table_find<TextModeNames>(mode.c_str(), mode.size());
39 if (index < 0) {
40 ESP_LOGW(TAG, "'%s': Unknown 'mode' value '%s'", this->entity_id_, mode.c_str());
41 return;
42 }
43 this->traits.set_mode(static_cast<text::TextMode>(index));
44 ESP_LOGD(TAG, "'%s': Mode retrieved: %s", this->entity_id_, mode.c_str());
45}
46
49 [this](StringRef state) { this->state_changed_(state); });
50
52 if (auto min = this->parse_length_("min", value))
53 this->traits.set_min_length(*min);
54 });
56 if (auto max = this->parse_length_("max", value))
57 this->traits.set_max_length(*max);
58 });
60 [this](StringRef mode) { this->mode_retrieved_(mode); });
61 // The "pattern" attribute is not fetched: Home Assistant sends "None" when it is unset,
62 // and a runtime pattern would need its own buffer.
63}
64
66 LOG_TEXT("", "Homeassistant Text", this);
67 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
68}
69
70void HomeassistantText::control(const std::string &value) {
71 if (!can_send_action(TAG, this->entity_id_, this))
72 return;
73
74 // In flash on the ESP8266 and encoded from there
75 static constexpr char SERVICE_TEXT[] PROGMEM = "text.set_value";
76 static constexpr char SERVICE_INPUT_TEXT[] PROGMEM = "input_text.set_value";
77 static constexpr char VALUE_KEY[] PROGMEM = "value";
78
80 resp.service = is_input_helper(this->entity_id_, SERVICE_INPUT_TEXT) ? ProgmemStringRef::from_lit(SERVICE_INPUT_TEXT)
81 : ProgmemStringRef::from_lit(SERVICE_TEXT);
82
83 resp.data.init(2);
84 add_entity_id(resp, this->entity_id_);
85
86 auto &value_kv = resp.data.emplace_back();
87 value_kv.key = ProgmemStringRef::from_lit(VALUE_KEY);
88 value_kv.value = ProgmemStringRef(value.data(), value.size());
89
91}
92
93} // namespace esphome::homeassistant
BedjetMode mode
BedJet operating mode.
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
constexpr const char * c_str() const
Definition string_ref.h:78
bool send_homeassistant_action(const HomeassistantActionRequest &call)
Returns false when no subscribed client took the request, so it was dropped.
void get_home_assistant_state(const char *entity_id, const char *attribute, F &&f)
Definition api_server.h:282
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, F &&f)
For internal components: entity_id and attribute must live as long as the server.
Definition api_server.h:279
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1514
optional< int > parse_length_(const char *name, StringRef value)
void control(const std::string &value) override
void publish_state(const std::string &state)
Definition text.h:25
std::string state
Definition text.h:22
TextTraits traits
Definition text.h:23
void set_mode(TextMode mode)
Definition text_traits.h:29
void set_max_length(int max_length)
Definition text_traits.h:19
void set_min_length(int min_length)
Definition text_traits.h:17
APIServer * global_api_server
PROGMEM_STRING_TABLE(UnsetValues, "None", "unavailable", "unknown")
void add_entity_id(api::HomeassistantActionRequest &resp, const char *entity_id)
Adds the entity_id entry to an action's data.
bool can_send_action(const char *tag, const char *entity_id, EntityBase *entity)
Logs and returns false when no API client is connected or no entity is linked yet.
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
bool is_input_helper(const char *entity_id, const char *input_service)
True if entity_id is an input helper; input_service must start with "input_".
static void const char const char * state
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
uint32_t value
Definition hal.cpp:59
int16_t progmem_string_table_find(const char *value, size_t len)
The index of the string value (len bytes, in RAM) in a PROGMEM_STRING_TABLE, or -1 when it is not one...
Definition progmem.h:188
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1657
uint16_t length
Definition tt21100.cpp:0