ESPHome  2024.4.1
float_output.cpp
Go to the documentation of this file.
1 #include "float_output.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace output {
7 
8 static const char *const TAG = "output.float";
9 
10 void FloatOutput::set_max_power(float max_power) {
11  this->max_power_ = clamp(max_power, this->min_power_, 1.0f); // Clamp to MIN>=MAX>=1.0
12 }
13 
14 float FloatOutput::get_max_power() const { return this->max_power_; }
15 
16 void FloatOutput::set_min_power(float min_power) {
17  this->min_power_ = clamp(min_power, 0.0f, this->max_power_); // Clamp to 0.0>=MIN>=MAX
18 }
19 
20 void FloatOutput::set_zero_means_zero(bool zero_means_zero) { this->zero_means_zero_ = zero_means_zero; }
21 
22 float FloatOutput::get_min_power() const { return this->min_power_; }
23 
25  state = clamp(state, 0.0f, 1.0f);
26 
27 #ifdef USE_POWER_SUPPLY
28  if (state > 0.0f) { // ON
29  this->power_.request();
30  } else { // OFF
31  this->power_.unrequest();
32  }
33 #endif
34 
35  if (!(state == 0.0f && this->zero_means_zero_)) // regardless of min_power_, 0.0 means off
36  state = (state * (this->max_power_ - this->min_power_)) + this->min_power_;
37 
38  if (this->is_inverted())
39  state = 1.0f - state;
40  this->write_state(state);
41 }
42 
43 void FloatOutput::write_state(bool state) { this->set_level(state != this->inverted_ ? 1.0f : 0.0f); }
44 
45 } // namespace output
46 } // namespace esphome
void set_zero_means_zero(bool zero_means_zero)
Sets this output to ignore min_power for a 0 state.
void set_max_power(float max_power)
Set the maximum power output of this component.
float get_max_power() const
Get the maximum power output.
bool is_inverted() const
Return whether this binary output is inverted.
Definition: binary_output.h:61
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:92
void set_min_power(float min_power)
Set the minimum power output of this component.
void set_level(float state)
Set the level of this float output, this is called from the front-end.
power_supply::PowerSupplyRequester power_
Definition: binary_output.h:68
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void write_state(bool state) override
Implement BinarySensor's write_enabled; this should never be called.
float get_min_power() const
Get the minimum power output.
bool state
Definition: fan.h:34