ESPHome 2025.5.0
Loading...
Searching...
No Matches
copy_fan.cpp
Go to the documentation of this file.
1#include "copy_fan.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace copy {
6
7static const char *const TAG = "copy.fan";
8
11 this->state = source_->state;
13 this->speed = source_->speed;
16 this->publish_state();
17 });
18
19 this->state = source_->state;
21 this->speed = source_->speed;
24 this->publish_state();
25}
26
27void CopyFan::dump_config() { LOG_FAN("", "Copy Fan", this); }
28
30 fan::FanTraits traits;
31 auto base = source_->get_traits();
32 // copy traits manually so it doesn't break when new options are added
33 // but the control() method hasn't implemented them yet.
34 traits.set_oscillation(base.supports_oscillation());
35 traits.set_speed(base.supports_speed());
36 traits.set_supported_speed_count(base.supported_speed_count());
37 traits.set_direction(base.supports_direction());
38 traits.set_supported_preset_modes(base.supported_preset_modes());
39 return traits;
40}
41
42void CopyFan::control(const fan::FanCall &call) {
43 auto call2 = source_->make_call();
44 if (call.get_state().has_value())
45 call2.set_state(*call.get_state());
46 if (call.get_oscillating().has_value())
47 call2.set_oscillating(*call.get_oscillating());
48 if (call.get_speed().has_value())
49 call2.set_speed(*call.get_speed());
50 if (call.get_direction().has_value())
51 call2.set_direction(*call.get_direction());
52 if (!call.get_preset_mode().empty())
53 call2.set_preset_mode(call.get_preset_mode());
54 call2.perform();
55}
56
57} // namespace copy
58} // namespace esphome
void control(const fan::FanCall &call) override
Definition copy_fan.cpp:42
void dump_config() override
Definition copy_fan.cpp:27
fan::FanTraits get_traits() override
Definition copy_fan.cpp:29
void setup() override
Definition copy_fan.cpp:9
FanCall & set_oscillating(bool oscillating)
Definition fan.h:50
FanCall & set_direction(FanDirection direction)
Definition fan.h:66
FanCall & set_speed(int speed)
Definition fan.h:59
FanCall & set_state(bool binary_state)
Definition fan.h:41
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.h:75
void publish_state()
Definition fan.cpp:117
FanCall make_call()
Definition fan.cpp:114
virtual FanTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:116
FanDirection direction
The current direction of the fan.
Definition fan.h:116
std::string preset_mode
Definition fan.h:118
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
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:30
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:22
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:26
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:18
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7