ESPHome 2026.5.0
Loading...
Searching...
No Matches
homeassistant_number.cpp
Go to the documentation of this file.
2
5#include "esphome/core/log.h"
7
9
10static const char *const TAG = "homeassistant.number";
11
13 auto number_value = parse_number<float>(state.c_str());
14 if (!number_value.has_value()) {
15 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
16 this->publish_state(NAN);
17 return;
18 }
19 if (this->state == number_value.value()) {
20 return;
21 }
22 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, state.c_str());
23 this->publish_state(number_value.value());
24}
25
27 auto min_value = parse_number<float>(min.c_str());
28 if (!min_value.has_value()) {
29 ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_, min.c_str());
30 return;
31 }
32 ESP_LOGD(TAG, "'%s': Min retrieved: %s", get_name().c_str(), min.c_str());
33 this->traits.set_min_value(min_value.value());
34}
35
37 auto max_value = parse_number<float>(max.c_str());
38 if (!max_value.has_value()) {
39 ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_, max.c_str());
40 return;
41 }
42 ESP_LOGD(TAG, "'%s': Max retrieved: %s", get_name().c_str(), max.c_str());
43 this->traits.set_max_value(max_value.value());
44}
45
47 auto step_value = parse_number<float>(step.c_str());
48 if (!step_value.has_value()) {
49 ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_, step.c_str());
50 return;
51 }
52 ESP_LOGD(TAG, "'%s': Step Retrieved %s", get_name().c_str(), step.c_str());
53 this->traits.set_step(step_value.value());
54}
55
58 [this](StringRef state) { this->state_changed_(state); });
59
61 [this](StringRef min) { this->min_retrieved_(min); });
63 [this](StringRef max) { this->max_retrieved_(max); });
65 [this](StringRef step) { this->step_retrieved_(step); });
66}
67
69 LOG_NUMBER("", "Homeassistant Number", this);
70 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
71}
72
74
76 if (!api::global_api_server->is_connected()) {
77 ESP_LOGE(TAG, "No clients connected to API server");
78 return;
79 }
80
81 this->publish_state(value);
82
83 static constexpr auto SERVICE_NAME = StringRef::from_lit("number.set_value");
84 static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
85 static constexpr auto VALUE_KEY = StringRef::from_lit("value");
86
88 resp.service = SERVICE_NAME;
89
90 resp.data.init(2);
91 auto &entity_id = resp.data.emplace_back();
92 entity_id.key = ENTITY_ID_KEY;
93 entity_id.value = StringRef(this->entity_id_);
94
95 auto &entity_value = resp.data.emplace_back();
96 entity_value.key = VALUE_KEY;
97 // Stack buffer - no heap allocation; %g produces shortest representation
98 char value_buf[16];
99 buf_append_printf(value_buf, sizeof(value_buf), 0, "%g", value);
100 entity_value.value = StringRef(value_buf);
101
103}
104
105} // 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:26
constexpr const char * c_str() const
Definition string_ref.h:73
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:50
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:1077
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:55
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1143