ESPHome 2026.2.1
Loading...
Searching...
No Matches
homeassistant_number.cpp
Go to the documentation of this file.
2
5#include "esphome/core/log.h"
7
8namespace esphome {
9namespace homeassistant {
10
11static const char *const TAG = "homeassistant.number";
12
14 auto number_value = parse_number<float>(state.c_str());
15 if (!number_value.has_value()) {
16 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
17 this->publish_state(NAN);
18 return;
19 }
20 if (this->state == number_value.value()) {
21 return;
22 }
23 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, state.c_str());
24 this->publish_state(number_value.value());
25}
26
28 auto min_value = parse_number<float>(min.c_str());
29 if (!min_value.has_value()) {
30 ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_, min.c_str());
31 return;
32 }
33 ESP_LOGD(TAG, "'%s': Min retrieved: %s", get_name().c_str(), min.c_str());
34 this->traits.set_min_value(min_value.value());
35}
36
38 auto max_value = parse_number<float>(max.c_str());
39 if (!max_value.has_value()) {
40 ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_, max.c_str());
41 return;
42 }
43 ESP_LOGD(TAG, "'%s': Max retrieved: %s", get_name().c_str(), max.c_str());
44 this->traits.set_max_value(max_value.value());
45}
46
48 auto step_value = parse_number<float>(step.c_str());
49 if (!step_value.has_value()) {
50 ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_, step.c_str());
51 return;
52 }
53 ESP_LOGD(TAG, "'%s': Step Retrieved %s", get_name().c_str(), step.c_str());
54 this->traits.set_step(step_value.value());
55}
56
59 this->entity_id_, nullptr, std::bind(&HomeassistantNumber::state_changed_, this, std::placeholders::_1));
60
62 this->entity_id_, "min", std::bind(&HomeassistantNumber::min_retrieved_, this, std::placeholders::_1));
64 this->entity_id_, "max", std::bind(&HomeassistantNumber::max_retrieved_, this, std::placeholders::_1));
66 this->entity_id_, "step", std::bind(&HomeassistantNumber::step_retrieved_, this, std::placeholders::_1));
67}
68
70 LOG_NUMBER("", "Homeassistant Number", this);
71 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
72}
73
75
77 if (!api::global_api_server->is_connected()) {
78 ESP_LOGE(TAG, "No clients connected to API server");
79 return;
80 }
81
82 this->publish_state(value);
83
84 static constexpr auto SERVICE_NAME = StringRef::from_lit("number.set_value");
85 static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
86 static constexpr auto VALUE_KEY = StringRef::from_lit("value");
87
89 resp.service = SERVICE_NAME;
90
91 resp.data.init(2);
92 auto &entity_id = resp.data.emplace_back();
93 entity_id.key = ENTITY_ID_KEY;
94 entity_id.value = StringRef(this->entity_id_);
95
96 auto &entity_value = resp.data.emplace_back();
97 entity_value.key = VALUE_KEY;
98 // Stack buffer - no heap allocation; %g produces shortest representation
99 char value_buf[16];
100 buf_append_printf(value_buf, sizeof(value_buf), 0, "%g", value);
101 entity_value.value = StringRef(value_buf);
102
104}
105
106} // namespace homeassistant
107} // namespace esphome
const StringRef & get_name() const
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 send_homeassistant_action(const HomeassistantActionRequest &call)
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> f)
void get_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> f)
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1023
void publish_state(float state)
Definition number.cpp:22
NumberTraits traits
Definition number.h:39
void set_min_value(float min_value)
void set_max_value(float max_value)
bool state
Definition fan.h:2
APIServer * global_api_server
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:92
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:785