ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
fan_traits.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <vector>
5#include <initializer_list>
6
7namespace esphome {
8
9namespace fan {
10
11class FanTraits {
12 public:
13 FanTraits() = default;
14 FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
15 : oscillation_(oscillation), speed_(speed), direction_(direction), speed_count_(speed_count) {}
16
18 bool supports_oscillation() const { return this->oscillation_; }
20 void set_oscillation(bool oscillation) { this->oscillation_ = oscillation; }
22 bool supports_speed() const { return this->speed_; }
24 void set_speed(bool speed) { this->speed_ = speed; }
26 int supported_speed_count() const { return this->speed_count_; }
28 void set_supported_speed_count(int speed_count) { this->speed_count_ = speed_count; }
30 bool supports_direction() const { return this->direction_; }
34 const std::vector<const char *> &supported_preset_modes() const { return this->preset_modes_; }
36 void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
37 this->preset_modes_ = preset_modes;
38 }
40 void set_supported_preset_modes(const std::vector<const char *> &preset_modes) { this->preset_modes_ = preset_modes; }
41
42 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
43 void set_supported_preset_modes(const std::vector<std::string> &preset_modes) = delete;
44 void set_supported_preset_modes(std::initializer_list<std::string> preset_modes) = delete;
45
47 bool supports_preset_modes() const { return !this->preset_modes_.empty(); }
49 const char *find_preset_mode(const char *preset_mode) const {
50 if (preset_mode == nullptr)
51 return nullptr;
52 for (const char *mode : this->preset_modes_) {
53 if (strcmp(mode, preset_mode) == 0) {
54 return mode; // Return pointer from traits
55 }
56 }
57 return nullptr;
58 }
59
60 protected:
61 bool oscillation_{false};
62 bool speed_{false};
63 bool direction_{false};
65 std::vector<const char *> preset_modes_{};
66};
67
68} // namespace fan
69} // namespace esphome
BedjetMode mode
BedJet operating mode.
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:32
bool supports_preset_modes() const
Return if preset modes are supported.
Definition fan_traits.h:47
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:24
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:26
void set_supported_preset_modes(const std::vector< const char * > &preset_modes)
Set the preset modes supported by the fan (from vector).
Definition fan_traits.h:40
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:28
bool supports_direction() const
Return if this fan supports changing direction.
Definition fan_traits.h:30
std::vector< const char * > preset_modes_
Definition fan_traits.h:65
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:20
void set_supported_preset_modes(const std::vector< std::string > &preset_modes)=delete
bool supports_speed() const
Return if this fan supports speed modes.
Definition fan_traits.h:22
FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
Definition fan_traits.h:14
void set_supported_preset_modes(std::initializer_list< std::string > preset_modes)=delete
const std::vector< const char * > & supported_preset_modes() const
Return the preset modes supported by the fan.
Definition fan_traits.h:34
const char * find_preset_mode(const char *preset_mode) const
Find and return the matching preset mode pointer from supported modes, or nullptr if not found.
Definition fan_traits.h:49
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the preset modes supported by the fan (from initializer list).
Definition fan_traits.h:36
bool supports_oscillation() const
Return if this fan supports oscillation.
Definition fan_traits.h:18
FanDirection direction
Definition fan.h:3
int speed
Definition fan.h:1
uint8_t preset_mode
Definition fan.h:4
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7