ESPHome  2024.10.2
speed_fan.cpp
Go to the documentation of this file.
1 #include "speed_fan.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace speed {
6 
7 static const char *const TAG = "speed.fan";
8 
10  auto restore = this->restore_state_();
11  if (restore.has_value()) {
12  restore->apply(*this);
13  this->write_state_();
14  }
15 
16  // Construct traits
17  this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_);
19 }
20 
21 void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); }
22 
24  if (call.get_state().has_value())
25  this->state = *call.get_state();
26  if (call.get_speed().has_value())
27  this->speed = *call.get_speed();
28  if (call.get_oscillating().has_value())
29  this->oscillating = *call.get_oscillating();
30  if (call.get_direction().has_value())
31  this->direction = *call.get_direction();
32  this->preset_mode = call.get_preset_mode();
33 
34  this->write_state_();
35  this->publish_state();
36 }
37 
39  float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
40  this->output_->set_level(speed);
41  if (this->oscillating_ != nullptr)
42  this->oscillating_->set_state(this->oscillating);
43  if (this->direction_ != nullptr)
45 }
46 
47 } // namespace speed
48 } // namespace esphome
bool state
The current on/off state of the fan.
Definition: fan.h:110
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
optional< FanDirection > get_direction() const
Definition: fan.h:74
void publish_state()
Definition: fan.cpp:117
optional< int > get_speed() const
Definition: fan.h:65
void dump_config() override
Definition: speed_fan.cpp:21
output::FloatOutput * output_
Definition: speed_fan.h:28
int speed
Definition: fan.h:35
virtual void set_state(bool state)
Enable or disable this binary output.
Definition: binary_output.h:34
void control(const fan::FanCall &call) override
Definition: speed_fan.cpp:23
optional< FanRestoreState > restore_state_()
Definition: fan.cpp:140
bool has_value() const
Definition: optional.h:87
void set_level(float state)
Set the level of this float output, this is called from the front-end.
std::string preset_mode
Definition: fan.h:118
int speed
The current fan speed level.
Definition: fan.h:114
optional< bool > get_state() const
Definition: fan.h:49
optional< bool > get_oscillating() const
Definition: fan.h:58
fan::FanTraits traits_
Definition: speed_fan.h:32
std::set< std::string > preset_modes_
Definition: speed_fan.h:33
void set_supported_preset_modes(const std::set< std::string > &preset_modes)
Set the preset modes supported by the fan.
Definition: fan_traits.h:34
output::BinaryOutput * oscillating_
Definition: speed_fan.h:29
void setup() override
Definition: speed_fan.cpp:9
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string get_preset_mode() const
Definition: fan.h:79
output::BinaryOutput * direction_
Definition: speed_fan.h:30