ESPHome 2025.9.0
Loading...
Searching...
No Matches
number.cpp
Go to the documentation of this file.
1#include "number.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace number {
6
7static const char *const TAG = "number";
8
9// Function implementation of LOG_NUMBER macro to reduce code size
10void log_number(const char *tag, const char *prefix, const char *type, Number *obj) {
11 if (obj == nullptr) {
12 return;
13 }
14
15 ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
16
17 if (!obj->get_icon_ref().empty()) {
18 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, obj->get_icon_ref().c_str());
19 }
20
22 ESP_LOGCONFIG(tag, "%s Unit of Measurement: '%s'", prefix, obj->traits.get_unit_of_measurement_ref().c_str());
23 }
24
25 if (!obj->traits.get_device_class_ref().empty()) {
26 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, obj->traits.get_device_class_ref().c_str());
27 }
28}
29
31 this->set_has_state(true);
32 this->state = state;
33 ESP_LOGD(TAG, "'%s': Sending state %f", this->get_name().c_str(), state);
34 this->state_callback_.call(state);
35}
36
37void Number::add_on_state_callback(std::function<void(float)> &&callback) {
38 this->state_callback_.add(std::move(callback));
39}
40
41} // namespace number
42} // namespace esphome
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:62
void set_has_state(bool state)
Definition entity_base.h:86
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:30
void publish_state(float state)
Definition number.cpp:30
NumberTraits traits
Definition number.h:40
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:37
CallbackManager< void(float)> state_callback_
Definition number.h:53
uint8_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:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7