ESPHome  2022.9.1
number_call.cpp
Go to the documentation of this file.
1 #include "number_call.h"
2 #include "number.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace number {
7 
8 static const char *const TAG = "number";
9 
11 
13  return this->with_operation(NUMBER_OP_INCREMENT).with_cycle(cycle);
14 }
15 
17  return this->with_operation(NUMBER_OP_DECREMENT).with_cycle(cycle);
18 }
19 
21 
23 
25  this->operation_ = operation;
26  return *this;
27 }
28 
30  this->value_ = value;
31  return *this;
32 }
33 
35  this->cycle_ = cycle;
36  return *this;
37 }
38 
40  auto *parent = this->parent_;
41  const auto *name = parent->get_name().c_str();
42  const auto &traits = parent->traits;
43 
44  if (this->operation_ == NUMBER_OP_NONE) {
45  ESP_LOGW(TAG, "'%s' - NumberCall performed without selecting an operation", name);
46  return;
47  }
48 
49  float target_value = NAN;
50  float min_value = traits.get_min_value();
51  float max_value = traits.get_max_value();
52 
53  if (this->operation_ == NUMBER_OP_SET) {
54  ESP_LOGD(TAG, "'%s' - Setting number value", name);
55  if (!this->value_.has_value() || std::isnan(*this->value_)) {
56  ESP_LOGW(TAG, "'%s' - No value set for NumberCall", name);
57  return;
58  }
59  target_value = this->value_.value();
60  } else if (this->operation_ == NUMBER_OP_TO_MIN) {
61  if (std::isnan(min_value)) {
62  ESP_LOGW(TAG, "'%s' - Can't set to min value through NumberCall: no min_value defined", name);
63  } else {
64  target_value = min_value;
65  }
66  } else if (this->operation_ == NUMBER_OP_TO_MAX) {
67  if (std::isnan(max_value)) {
68  ESP_LOGW(TAG, "'%s' - Can't set to max value through NumberCall: no max_value defined", name);
69  } else {
70  target_value = max_value;
71  }
72  } else if (this->operation_ == NUMBER_OP_INCREMENT) {
73  ESP_LOGD(TAG, "'%s' - Increment number, with%s cycling", name, this->cycle_ ? "" : "out");
74  if (!parent->has_state()) {
75  ESP_LOGW(TAG, "'%s' - Can't increment number through NumberCall: no active state to modify", name);
76  return;
77  }
78  auto step = traits.get_step();
79  target_value = parent->state + (std::isnan(step) ? 1 : step);
80  if (target_value > max_value) {
81  if (this->cycle_ && !std::isnan(min_value)) {
82  target_value = min_value;
83  } else {
84  target_value = max_value;
85  }
86  }
87  } else if (this->operation_ == NUMBER_OP_DECREMENT) {
88  ESP_LOGD(TAG, "'%s' - Decrement number, with%s cycling", name, this->cycle_ ? "" : "out");
89  if (!parent->has_state()) {
90  ESP_LOGW(TAG, "'%s' - Can't decrement number through NumberCall: no active state to modify", name);
91  return;
92  }
93  auto step = traits.get_step();
94  target_value = parent->state - (std::isnan(step) ? 1 : step);
95  if (target_value < min_value) {
96  if (this->cycle_ && !std::isnan(max_value)) {
97  target_value = max_value;
98  } else {
99  target_value = min_value;
100  }
101  }
102  }
103 
104  if (target_value < min_value) {
105  ESP_LOGW(TAG, "'%s' - Value %f must not be less than minimum %f", name, target_value, min_value);
106  return;
107  }
108  if (target_value > max_value) {
109  ESP_LOGW(TAG, "'%s' - Value %f must not be greater than maximum %f", name, target_value, max_value);
110  return;
111  }
112 
113  ESP_LOGD(TAG, " New number value: %f", target_value);
114  this->parent_->control(target_value);
115 }
116 
117 } // namespace number
118 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
const char * name
Definition: stm32flash.h:78
NumberCall & with_cycle(bool cycle)
Definition: number_call.cpp:34
NumberCall & with_operation(NumberOperation operation)
Definition: number_call.cpp:24
NumberCall & set_value(float value)
Definition: number_call.cpp:10
bool has_value() const
Definition: optional.h:87
NumberOperation operation_
Definition: number_call.h:37
virtual void control(float value)=0
Set the value of the number, this is a virtual method that each number integration must implement...
NumberCall & number_decrement(bool cycle)
Definition: number_call.cpp:16
NumberCall & number_to_min()
Definition: number_call.cpp:20
NumberCall & with_value(float value)
Definition: number_call.cpp:29
Definition: a4988.cpp:4
NumberCall & number_to_max()
Definition: number_call.cpp:22
NumberCall & number_increment(bool cycle)
Definition: number_call.cpp:12
optional< float > value_
Definition: number_call.h:38