ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
speed_fan.cpp
Go to the documentation of this file.
1#include "speed_fan.h"
2#include "esphome/core/log.h"
3
4namespace esphome::speed {
5
6static const char *const TAG = "speed.fan";
7
9 // Construct traits before restore so preset modes can be looked up by index
10 this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
11
12 auto restore = this->restore_state_();
13 if (restore.has_value()) {
14 restore->apply(*this);
15 this->write_state_();
16 }
17}
18
19void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); }
20
22 auto call_state = call.get_state();
23 if (call_state.has_value())
24 this->state = *call_state;
25 auto call_speed = call.get_speed();
26 if (call_speed.has_value())
27 this->speed = *call_speed;
28 auto call_oscillating = call.get_oscillating();
29 if (call_oscillating.has_value())
30 this->oscillating = *call_oscillating;
31 auto call_direction = call.get_direction();
32 if (call_direction.has_value())
33 this->direction = *call_direction;
34 this->apply_preset_mode_(call);
35
36 this->write_state_();
37 this->publish_state();
38}
39
41 float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
42 this->output_->set_level(speed);
43 if (this->oscillating_ != nullptr)
45 if (this->direction_ != nullptr)
47}
48
49} // namespace esphome::speed
void publish_state()
Definition fan.cpp:223
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:214
FanDirection direction
The current direction of the fan.
Definition fan.h:116
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
bool state
The current on/off state of the fan.
Definition fan.h:110
int speed
The current fan speed level.
Definition fan.h:114
optional< FanRestoreState > restore_state_()
Definition fan.cpp:251
virtual void set_state(bool state)
Enable or disable this binary output.
void set_level(float state)
Set the level of this float output, this is called from the front-end.
void control(const fan::FanCall &call) override
Definition speed_fan.cpp:21
fan::FanTraits traits_
Definition speed_fan.h:32
output::BinaryOutput * oscillating_
Definition speed_fan.h:29
output::FloatOutput * output_
Definition speed_fan.h:28
void dump_config() override
Definition speed_fan.cpp:19
output::BinaryOutput * direction_
Definition speed_fan.h:30
void setup() override
Definition speed_fan.cpp:8