ESPHome 2026.5.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 {
5namespace speed {
6
7static const char *const TAG = "speed.fan";
8
10 // Construct traits before restore so preset modes can be looked up by index
11 this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
12
13 auto restore = this->restore_state_();
14 if (restore.has_value()) {
15 restore->apply(*this);
16 this->write_state_();
17 }
18}
19
20void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); }
21
23 auto call_state = call.get_state();
24 if (call_state.has_value())
25 this->state = *call_state;
26 auto call_speed = call.get_speed();
27 if (call_speed.has_value())
28 this->speed = *call_speed;
29 auto call_oscillating = call.get_oscillating();
30 if (call_oscillating.has_value())
31 this->oscillating = *call_oscillating;
32 auto call_direction = call.get_direction();
33 if (call_direction.has_value())
34 this->direction = *call_direction;
35 this->apply_preset_mode_(call);
36
37 this->write_state_();
38 this->publish_state();
39}
40
42 float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
43 this->output_->set_level(speed);
44 if (this->oscillating_ != nullptr)
46 if (this->direction_ != nullptr)
48}
49
50} // namespace speed
51} // namespace esphome
void publish_state()
Definition fan.cpp:224
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:215
FanDirection direction
The current direction of the fan.
Definition fan.h:117
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
optional< FanRestoreState > restore_state_()
Definition fan.cpp:252
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:22
fan::FanTraits traits_
Definition speed_fan.h:33
output::BinaryOutput * oscillating_
Definition speed_fan.h:30
output::FloatOutput * output_
Definition speed_fan.h:29
void dump_config() override
Definition speed_fan.cpp:20
output::BinaryOutput * direction_
Definition speed_fan.h:31
void setup() override
Definition speed_fan.cpp:9
int speed
Definition fan.h:3
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7