ESPHome 2025.12.0
Loading...
Searching...
No Matches
number.cpp
Go to the documentation of this file.
1#include "number.h"
4#include "esphome/core/log.h"
5
6namespace esphome::number {
7
8static const char *const TAG = "number";
9
10// Function implementation of LOG_NUMBER macro to reduce code size
11void log_number(const char *tag, const char *prefix, const char *type, Number *obj) {
12 if (obj == nullptr) {
13 return;
14 }
15
16 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
17
18 if (!obj->get_icon_ref().empty()) {
19 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
20 }
21
23 ESP_LOGCONFIG(tag, "%s Unit of Measurement: '%s'", prefix, obj->traits.get_unit_of_measurement_ref().c_str());
24 }
25
26 if (!obj->traits.get_device_class_ref().empty()) {
27 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->traits.get_device_class_ref().c_str());
28 }
29}
30
32 this->set_has_state(true);
33 this->state = state;
34 ESP_LOGD(TAG, "'%s': Sending state %f", this->get_name().c_str(), state);
35 this->state_callback_.call(state);
36#if defined(USE_NUMBER) && defined(USE_CONTROLLER_REGISTRY)
38#endif
39}
40
41void Number::add_on_state_callback(std::function<void(float)> &&callback) {
42 this->state_callback_.add(std::move(callback));
43}
44
45} // namespace esphome::number
static void notify_number_update(number::Number *obj)
StringRef get_device_class_ref() const
Get the device class as StringRef.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
const StringRef & get_name() const
StringRef get_icon_ref() const
Definition entity_base.h:72
void set_has_state(bool state)
Definition entity_base.h:96
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
Base-class for all numbers.
Definition number.h:29
void publish_state(float state)
Definition number.cpp:31
NumberTraits traits
Definition number.h:39
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:41
CallbackManager< void(float)> state_callback_
Definition number.h:52
uint16_t type
bool state
Definition fan.h:0
void log_number(const char *tag, const char *prefix, const char *type, Number *obj)
Definition number.cpp:11