1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 22:53:59 +00:00

Add support for controlling fan direction (#1051)

* Fix fan oscillation trait not being used

* Add fan direction support to SpeedFan

* Add fan direction to API

* Add fan direction support to BinaryFan

* Fix CI errors

* Fix python format

* Change some ordering to trigger CI

* Add test for the configuration
This commit is contained in:
Jim Persson
2020-06-14 21:54:31 +02:00
committed by GitHub
parent 35a2258f12
commit 0bb81e5b2d
16 changed files with 156 additions and 17 deletions

View File

@@ -22,6 +22,7 @@ struct FanStateRTCState {
bool state;
FanSpeed speed;
bool oscillating;
FanDirection direction;
};
void FanState::setup() {
@@ -34,6 +35,7 @@ void FanState::setup() {
call.set_state(recovered.state);
call.set_speed(recovered.speed);
call.set_oscillating(recovered.oscillating);
call.set_direction(recovered.direction);
call.perform();
}
float FanState::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
@@ -46,6 +48,9 @@ void FanStateCall::perform() const {
if (this->oscillating_.has_value()) {
this->state_->oscillating = *this->oscillating_;
}
if (this->direction_.has_value()) {
this->state_->direction = *this->direction_;
}
if (this->speed_.has_value()) {
switch (*this->speed_) {
case FAN_SPEED_LOW:
@@ -63,6 +68,7 @@ void FanStateCall::perform() const {
saved.state = this->state_->state;
saved.speed = this->state_->speed;
saved.oscillating = this->state_->oscillating;
saved.direction = this->state_->direction;
this->state_->rtc_.save(&saved);
this->state_->state_callback_.call();

View File

@@ -15,6 +15,9 @@ enum FanSpeed {
FAN_SPEED_HIGH = 2 ///< The fan is running on high/full speed.
};
/// Simple enum to represent the direction of a fan
enum FanDirection { FAN_DIRECTION_FORWARD = 0, FAN_DIRECTION_REVERSE = 1 };
class FanState;
class FanStateCall {
@@ -46,6 +49,14 @@ class FanStateCall {
return *this;
}
FanStateCall &set_speed(const char *speed);
FanStateCall &set_direction(FanDirection direction) {
this->direction_ = direction;
return *this;
}
FanStateCall &set_direction(optional<FanDirection> direction) {
this->direction_ = direction;
return *this;
}
void perform() const;
@@ -54,6 +65,7 @@ class FanStateCall {
optional<bool> binary_state_;
optional<bool> oscillating_{};
optional<FanSpeed> speed_{};
optional<FanDirection> direction_{};
};
class FanState : public Nameable, public Component {
@@ -76,6 +88,8 @@ class FanState : public Nameable, public Component {
bool oscillating{false};
/// The current fan speed.
FanSpeed speed{FAN_SPEED_HIGH};
/// The current direction of the fan
FanDirection direction{FAN_DIRECTION_FORWARD};
FanStateCall turn_on();
FanStateCall turn_off();

View File

@@ -6,7 +6,8 @@ namespace fan {
class FanTraits {
public:
FanTraits() = default;
FanTraits(bool oscillation, bool speed) : oscillation_(oscillation), speed_(speed) {}
FanTraits(bool oscillation, bool speed, bool direction)
: oscillation_(oscillation), speed_(speed), direction_(direction) {}
/// Return if this fan supports oscillation.
bool supports_oscillation() const { return this->oscillation_; }
@@ -16,10 +17,15 @@ class FanTraits {
bool supports_speed() const { return this->speed_; }
/// Set whether this fan supports speed modes.
void set_speed(bool speed) { this->speed_ = speed; }
/// Return if this fan supports changing direction
bool supports_direction() const { return this->direction_; }
/// Set whether this fan supports changing direction
void set_direction(bool direction) { this->direction_ = direction; }
protected:
bool oscillation_{false};
bool speed_{false};
bool direction_{false};
};
} // namespace fan