ESPHome 2026.11.0-dev
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"
10
11namespace esphome::homeassistant {
12
13ESPHOME_LOG_TAG(TAG, "homeassistant.number");
14
16 auto number_value = parse_number<float>(state.c_str());
17 if (!number_value.has_value()) {
18 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_, state.c_str());
19 this->publish_state(NAN);
20 return;
21 }
22 if (this->state == number_value.value()) {
23 return;
24 }
25 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, state.c_str());
26 this->publish_state(number_value.value());
27}
28
30 auto min_value = parse_number<float>(min.c_str());
31 if (!min_value.has_value()) {
32 ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_, min.c_str());
33 return;
34 }
35 ESP_LOGD(TAG, "'%s': Min retrieved: %s", LOG_STR_ARG(this->get_log_name()), min.c_str());
36 this->traits.set_min_value(min_value.value());
37}
38
40 auto max_value = parse_number<float>(max.c_str());
41 if (!max_value.has_value()) {
42 ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_, max.c_str());
43 return;
44 }
45 ESP_LOGD(TAG, "'%s': Max retrieved: %s", LOG_STR_ARG(this->get_log_name()), max.c_str());
46 this->traits.set_max_value(max_value.value());
47}
48
50 auto step_value = parse_number<float>(step.c_str());
51 if (!step_value.has_value()) {
52 ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_, step.c_str());
53 return;
54 }
55 ESP_LOGD(TAG, "'%s': Step Retrieved %s", LOG_STR_ARG(this->get_log_name()), step.c_str());
56 this->traits.set_step(step_value.value());
57}
58
61 [this](StringRef state) { this->state_changed_(state); });
62
64 [this](StringRef min) { this->min_retrieved_(min); });
66 [this](StringRef max) { this->max_retrieved_(max); });
68 [this](StringRef step) { this->step_retrieved_(step); });
69}
70
72 LOG_NUMBER("", "Homeassistant Number", this);
73 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
74}
75
77
79 if (!api::global_api_server->is_connected()) {
80 ESP_LOGE(TAG, "No clients connected to API server");
81 return;
82 }
83
84#ifdef USE_API_WIZARD_LINKED_INPUTS
85 if (this->entity_id_[0] == '\0') {
86 ESP_LOGW(TAG, "'%s': No entity ID set yet", LOG_STR_ARG(this->get_log_name()));
87 return;
88 }
89#endif
90
91 this->publish_state(value);
92
93 // In flash on the ESP8266 and encoded from there
94 static const char NUMBER_SERVICE_NAME[] PROGMEM = "number.set_value";
95 static const char INPUT_NUMBER_SERVICE_NAME[] PROGMEM = "input_number.set_value";
96 static const char ENTITY_ID_KEY[] PROGMEM = "entity_id";
97 static const char VALUE_KEY[] PROGMEM = "value";
98
100 // An input helper entity id starts with the "input_" its action name starts with
101 const bool input_helper = ESPHOME_strncmp_P(this->entity_id_, INPUT_NUMBER_SERVICE_NAME, sizeof("input_") - 1) == 0;
102 resp.service = input_helper ? ProgmemStringRef::from_lit(INPUT_NUMBER_SERVICE_NAME)
103 : ProgmemStringRef::from_lit(NUMBER_SERVICE_NAME);
104
105 resp.data.init(2);
106 auto &entity_id = resp.data.emplace_back();
107 entity_id.key = ProgmemStringRef::from_lit(ENTITY_ID_KEY);
108 entity_id.value = ProgmemStringRef(StringRef(this->entity_id_));
109
110 auto &entity_value = resp.data.emplace_back();
111 entity_value.key = ProgmemStringRef::from_lit(VALUE_KEY);
112 // Stack buffer - no heap allocation; %g produces shortest representation
113 char value_buf[16];
114 buf_append_printf(value_buf, sizeof(value_buf), 0, "%g", value);
115 entity_value.value = ProgmemStringRef(StringRef(value_buf));
116
118}
119
120} // namespace esphome::homeassistant
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:230
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:236
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
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:1423
void publish_state(float state)
Definition number.cpp:20
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
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
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:1352