ESPHome  2024.11.1
number.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include "number_call.h"
7 #include "number_traits.h"
8 
9 namespace esphome {
10 namespace number {
11 
12 #define LOG_NUMBER(prefix, type, obj) \
13  if ((obj) != nullptr) { \
14  ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
15  if (!(obj)->get_icon().empty()) { \
16  ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
17  } \
18  if (!(obj)->traits.get_unit_of_measurement().empty()) { \
19  ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, (obj)->traits.get_unit_of_measurement().c_str()); \
20  } \
21  if (!(obj)->traits.get_device_class().empty()) { \
22  ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->traits.get_device_class().c_str()); \
23  } \
24  }
25 
26 #define SUB_NUMBER(name) \
27  protected: \
28  number::Number *name##_number_{nullptr}; \
29 \
30  public: \
31  void set_##name##_number(number::Number *number) { this->name##_number_ = number; }
32 
33 class Number;
34 
39 class Number : public EntityBase {
40  public:
41  float state;
42 
43  void publish_state(float state);
44 
45  NumberCall make_call() { return NumberCall(this); }
46 
47  void add_on_state_callback(std::function<void(float)> &&callback);
48 
50 
52  bool has_state() const { return has_state_; }
53 
54  protected:
55  friend class NumberCall;
56 
63  virtual void control(float value) = 0;
64 
66  bool has_state_{false};
67 };
68 
69 } // namespace number
70 } // namespace esphome
bool has_state() const
Return whether this number has gotten a full state yet.
Definition: number.h:52
void add_on_state_callback(std::function< void(float)> &&callback)
Definition: number.cpp:16
void publish_state(float state)
Definition: number.cpp:9
CallbackManager< void(float)> state_callback_
Definition: number.h:65
virtual void control(float value)=0
Set the value of the number, this is a virtual method that each number integration must implement...
Base-class for all numbers.
Definition: number.h:39
NumberTraits traits
Definition: number.h:49
NumberCall make_call()
Definition: number.h:45
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
friend class NumberCall
Definition: number.h:55