ESPHome 2026.10.2
Loading...
Searching...
No Matches
homeassistant_number.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.number";
13
15 auto number_value = parse_number<float>(state.c_str());
16 if (!number_value.has_value()) {
17 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
18 this->publish_state(NAN);
19 return;
20 }
21 if (this->state == number_value.value()) {
22 return;
23 }
24 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, state.c_str());
25 this->publish_state(number_value.value());
26}
27
29 auto min_value = parse_number<float>(min.c_str());
30 if (!min_value.has_value()) {
31 ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_, min.c_str());
32 return;
33 }
34 ESP_LOGD(TAG, "'%s': Min retrieved: %s", get_name().c_str(), min.c_str());
35 this->traits.set_min_value(min_value.value());
36}
37
39 auto max_value = parse_number<float>(max.c_str());
40 if (!max_value.has_value()) {
41 ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_, max.c_str());
42 return;
43 }
44 ESP_LOGD(TAG, "'%s': Max retrieved: %s", get_name().c_str(), max.c_str());
45 this->traits.set_max_value(max_value.value());
46}
47
49 auto step_value = parse_number<float>(step.c_str());
50 if (!step_value.has_value()) {
51 ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_, step.c_str());
52 return;
53 }
54 ESP_LOGD(TAG, "'%s': Step Retrieved %s", get_name().c_str(), step.c_str());
55 this->traits.set_step(step_value.value());
56}
57
60 [this](StringRef state) { this->state_changed_(state); });
61
63 [this](StringRef min) { this->min_retrieved_(min); });
65 [this](StringRef max) { this->max_retrieved_(max); });
67 [this](StringRef step) { this->step_retrieved_(step); });
68}
69
71 LOG_NUMBER("", "Homeassistant Number", this);
72 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
73}
74
76
78 if (!api::global_api_server->is_connected()) {
79 ESP_LOGE(TAG, "No clients connected to API server");
80 return;
81 }
82
83#ifdef USE_API_WIZARD_LINKED_INPUTS
84 if (this->entity_id_[0] == '\0') {
85 ESP_LOGW(TAG, "'%s': No entity ID set yet", this->get_name().c_str());
86 return;
87 }
88#endif
89
90 this->publish_state(value);
91
92 static constexpr auto NUMBER_SERVICE_NAME = StringRef::from_lit("number.set_value");
93 static constexpr auto INPUT_NUMBER_SERVICE_NAME = StringRef::from_lit("input_number.set_value");
94 static constexpr char INPUT_PREFIX[] = "input_";
95 static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
96 static constexpr auto VALUE_KEY = StringRef::from_lit("value");
97
99 // input_number entities have their own set_value action
100 if (strncmp(this->entity_id_, INPUT_PREFIX, sizeof(INPUT_PREFIX) - 1) == 0) {
101 resp.service = INPUT_NUMBER_SERVICE_NAME;
102 } else {
103 resp.service = NUMBER_SERVICE_NAME;
104 }
105
106 resp.data.init(2);
107 auto &entity_id = resp.data.emplace_back();
108 entity_id.key = ENTITY_ID_KEY;
109 entity_id.value = StringRef(this->entity_id_);
110
111 auto &entity_value = resp.data.emplace_back();
112 entity_value.key = VALUE_KEY;
113 // Stack buffer - no heap allocation; %g produces shortest representation
114 char value_buf[16];
115 buf_append_printf(value_buf, sizeof(value_buf), 0, "%g", value);
116 entity_value.value = StringRef(value_buf);
117
119}
120
121} // 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
constexpr const char * c_str() const
Definition string_ref.h:77
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:54
void get_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void send_homeassistant_action(const HomeassistantActionRequest &call)
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1414
void publish_state(float state)
Definition number.cpp:22
NumberTraits traits
Definition number.h:41
void set_min_value(float min_value)
void set_max_value(float max_value)
bool state
Definition fan.h:2
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
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1273