ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
float_output.h
Go to the documentation of this file.
1#pragma once
2
5#include "binary_output.h"
6
7namespace esphome::output {
8
9#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
10#define LOG_FLOAT_OUTPUT(this) \
11 LOG_BINARY_OUTPUT(this) \
12 if (this->max_power_ != 1.0f) { \
13 ESP_LOGCONFIG(TAG, " Max Power: %.1f%%", this->max_power_ * 100.0f); \
14 } \
15 if (this->min_power_ != 0.0f) { \
16 ESP_LOGCONFIG(TAG, " Min Power: %.1f%%", this->min_power_ * 100.0f); \
17 }
18#else
19#define LOG_FLOAT_OUTPUT(this) LOG_BINARY_OUTPUT(this)
20#endif
21
38class FloatOutput : public BinaryOutput {
39 public:
40#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
47 void set_max_power(float max_power);
48
55 void set_min_power(float min_power);
56
61 void set_zero_means_zero(bool zero_means_zero) { this->zero_means_zero_ = zero_means_zero; }
62#else
63 // Compile-time guards for users calling these methods from lambdas (documented usage at
64 // https://esphome.io/components/output/#output-set_min_power_action). When power scaling
65 // is compiled out, these template stubs fail to compile with an actionable error pointing
66 // at the user's lambda. Templating on a default-false bool means static_assert only fires
67 // on instantiation (i.e. when the user actually calls the method), not on every parse.
68 template<bool _use_output_float_power_scaling = false> void set_max_power(float max_power) {
69 static_assert(_use_output_float_power_scaling,
70 "set_max_power() requires USE_OUTPUT_FLOAT_POWER_SCALING. "
71 "To enable it, add 'max_power: 100%' (or any value) to one output entry in your YAML — "
72 "the codegen will then keep the scaling fields. "
73 "See https://esphome.io/components/output/ for details.");
74 }
75 template<bool _use_output_float_power_scaling = false> void set_min_power(float min_power) {
76 static_assert(_use_output_float_power_scaling,
77 "set_min_power() requires USE_OUTPUT_FLOAT_POWER_SCALING. "
78 "To enable it, add 'min_power: 0%' (or any value) to one output entry in your YAML — "
79 "the codegen will then keep the scaling fields. "
80 "See https://esphome.io/components/output/ for details.");
81 }
82 template<bool _use_output_float_power_scaling = false> void set_zero_means_zero(bool zero_means_zero) {
83 static_assert(_use_output_float_power_scaling,
84 "set_zero_means_zero() requires USE_OUTPUT_FLOAT_POWER_SCALING. "
85 "To enable it, add 'zero_means_zero: true' to one output entry in your YAML.");
86 }
87#endif
88
93 void set_level(float state);
94
101 virtual void update_frequency(float frequency) {}
102
103 // ========== INTERNAL METHODS ==========
104 // (In most use cases you won't need these)
105
106#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
108 float get_max_power() const { return this->max_power_; }
109
111 float get_min_power() const { return this->min_power_; }
112#else
114 float get_max_power() const { return 1.0f; }
115
117 float get_min_power() const { return 0.0f; }
118#endif
119
120 protected:
122 void write_state(bool state) override;
123 virtual void write_state(float state) = 0;
124
125#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
126 float max_power_{1.0f};
127 float min_power_{0.0f};
128 bool zero_means_zero_{false};
129#endif
130};
131
132} // namespace esphome::output
uint16_le_t frequency
Definition bl0942.h:6
Base class for all output components that can output a variable level, like PWM.
void set_level(float state)
Set the level of this float output, this is called from the front-end.
void set_zero_means_zero(bool zero_means_zero)
void set_max_power(float max_power)
virtual void update_frequency(float frequency)
Set the frequency of the output for PWM outputs.
float get_min_power() const
Get the minimum power output.
void set_max_power(float max_power)
Set the maximum power output of this component.
virtual void write_state(float state)=0
void set_zero_means_zero(bool zero_means_zero)
Sets this output to ignore min_power for a 0 state.
float get_max_power() const
Get the maximum power output.
void write_state(bool state) override
Implement BinarySensor's write_enabled; this should never be called.
void set_min_power(float min_power)
Set the minimum power output of this component.
void set_min_power(float min_power)
bool state
Definition fan.h:2