ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
homeassistant_number.cpp
Go to the documentation of this file.
2
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace homeassistant {
9
10static const char *const TAG = "homeassistant.number";
11
13 auto number_value = parse_number<float>(state);
14 if (!number_value.has_value()) {
15 ESP_LOGW(TAG, "'%s': Can't convert '%s' to number!", this->entity_id_.c_str(), 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_.c_str(), state.c_str());
23 this->publish_state(number_value.value());
24}
25
26void HomeassistantNumber::min_retrieved_(const std::string &min) {
27 auto min_value = parse_number<float>(min);
28 if (!min_value.has_value()) {
29 ESP_LOGE(TAG, "'%s': Can't convert 'min' value '%s' to number!", this->entity_id_.c_str(), 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
36void HomeassistantNumber::max_retrieved_(const std::string &max) {
37 auto max_value = parse_number<float>(max);
38 if (!max_value.has_value()) {
39 ESP_LOGE(TAG, "'%s': Can't convert 'max' value '%s' to number!", this->entity_id_.c_str(), 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
46void HomeassistantNumber::step_retrieved_(const std::string &step) {
47 auto step_value = parse_number<float>(step);
48 if (!step_value.has_value()) {
49 ESP_LOGE(TAG, "'%s': Can't convert 'step' value '%s' to number!", this->entity_id_.c_str(), 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
70
72 LOG_NUMBER("", "Homeassistant Number", this);
73 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
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 this->publish_state(value);
85
86 static constexpr auto SERVICE_NAME = StringRef::from_lit("number.set_value");
87 static constexpr auto ENTITY_ID_KEY = StringRef::from_lit("entity_id");
88 static constexpr auto VALUE_KEY = StringRef::from_lit("value");
89
91 resp.set_service(SERVICE_NAME);
92
93 resp.data.emplace_back();
94 auto &entity_id = resp.data.back();
95 entity_id.set_key(ENTITY_ID_KEY);
96 entity_id.value = this->entity_id_;
97
98 resp.data.emplace_back();
99 auto &entity_value = resp.data.back();
100 entity_value.set_key(VALUE_KEY);
101 entity_value.value = to_string(value);
102
104}
105
106} // namespace homeassistant
107} // namespace esphome
const StringRef & get_name() const
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:46
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void get_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void set_service(const StringRef &ref)
Definition api_pb2.h:1095
std::vector< HomeassistantServiceMap > data
Definition api_pb2.h:1096
void publish_state(float state)
Definition number.cpp:9
NumberTraits traits
Definition number.h:49
void set_min_value(float min_value)
void set_max_value(float max_value)
bool state
Definition fan.h:0
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:58
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:291
const nullopt_t nullopt((nullopt_t::init()))