1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 06:04:01 +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

@@ -301,12 +301,17 @@ message ListEntitiesFanResponse {
bool supports_oscillation = 5;
bool supports_speed = 6;
bool supports_direction = 7;
}
enum FanSpeed {
FAN_SPEED_LOW = 0;
FAN_SPEED_MEDIUM = 1;
FAN_SPEED_HIGH = 2;
}
enum FanDirection {
FAN_DIRECTION_FORWARD = 0;
FAN_DIRECTION_REVERSE = 1;
}
message FanStateResponse {
option (id) = 23;
option (source) = SOURCE_SERVER;
@@ -317,6 +322,7 @@ message FanStateResponse {
bool state = 2;
bool oscillating = 3;
FanSpeed speed = 4;
FanDirection direction = 5;
}
message FanCommandRequest {
option (id) = 31;
@@ -331,6 +337,8 @@ message FanCommandRequest {
FanSpeed speed = 5;
bool has_oscillating = 6;
bool oscillating = 7;
bool has_direction = 8;
FanDirection direction = 9;
}
// ==================== LIGHT ====================

View File

@@ -248,6 +248,8 @@ bool APIConnection::send_fan_state(fan::FanState *fan) {
resp.oscillating = fan->oscillating;
if (traits.supports_speed())
resp.speed = static_cast<enums::FanSpeed>(fan->speed);
if (traits.supports_direction())
resp.direction = static_cast<enums::FanDirection>(fan->direction);
return this->send_fan_state_response(resp);
}
bool APIConnection::send_fan_info(fan::FanState *fan) {
@@ -259,6 +261,7 @@ bool APIConnection::send_fan_info(fan::FanState *fan) {
msg.unique_id = get_default_unique_id("fan", fan);
msg.supports_oscillation = traits.supports_oscillation();
msg.supports_speed = traits.supports_speed();
msg.supports_direction = traits.supports_direction();
return this->send_list_entities_fan_response(msg);
}
void APIConnection::fan_command(const FanCommandRequest &msg) {
@@ -273,6 +276,8 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
call.set_oscillating(msg.oscillating);
if (msg.has_speed)
call.set_speed(static_cast<fan::FanSpeed>(msg.speed));
if (msg.has_direction)
call.set_direction(static_cast<fan::FanDirection>(msg.direction));
call.perform();
}
#endif

View File

@@ -52,6 +52,16 @@ template<> const char *proto_enum_to_string<enums::FanSpeed>(enums::FanSpeed val
return "UNKNOWN";
}
}
template<> const char *proto_enum_to_string<enums::FanDirection>(enums::FanDirection value) {
switch (value) {
case enums::FAN_DIRECTION_FORWARD:
return "FAN_DIRECTION_FORWARD";
case enums::FAN_DIRECTION_REVERSE:
return "FAN_DIRECTION_REVERSE";
default:
return "UNKNOWN";
}
}
template<> const char *proto_enum_to_string<enums::LogLevel>(enums::LogLevel value) {
switch (value) {
case enums::LOG_LEVEL_NONE:
@@ -760,6 +770,10 @@ bool ListEntitiesFanResponse::decode_varint(uint32_t field_id, ProtoVarInt value
this->supports_speed = value.as_bool();
return true;
}
case 7: {
this->supports_direction = value.as_bool();
return true;
}
default:
return false;
}
@@ -799,6 +813,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(4, this->unique_id);
buffer.encode_bool(5, this->supports_oscillation);
buffer.encode_bool(6, this->supports_speed);
buffer.encode_bool(7, this->supports_direction);
}
void ListEntitiesFanResponse::dump_to(std::string &out) const {
char buffer[64];
@@ -827,6 +842,10 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
out.append(" supports_speed: ");
out.append(YESNO(this->supports_speed));
out.append("\n");
out.append(" supports_direction: ");
out.append(YESNO(this->supports_direction));
out.append("\n");
out.append("}");
}
bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
@@ -843,6 +862,10 @@ bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
this->speed = value.as_enum<enums::FanSpeed>();
return true;
}
case 5: {
this->direction = value.as_enum<enums::FanDirection>();
return true;
}
default:
return false;
}
@@ -862,6 +885,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(2, this->state);
buffer.encode_bool(3, this->oscillating);
buffer.encode_enum<enums::FanSpeed>(4, this->speed);
buffer.encode_enum<enums::FanDirection>(5, this->direction);
}
void FanStateResponse::dump_to(std::string &out) const {
char buffer[64];
@@ -882,6 +906,10 @@ void FanStateResponse::dump_to(std::string &out) const {
out.append(" speed: ");
out.append(proto_enum_to_string<enums::FanSpeed>(this->speed));
out.append("\n");
out.append(" direction: ");
out.append(proto_enum_to_string<enums::FanDirection>(this->direction));
out.append("\n");
out.append("}");
}
bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
@@ -910,6 +938,14 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
this->oscillating = value.as_bool();
return true;
}
case 8: {
this->has_direction = value.as_bool();
return true;
}
case 9: {
this->direction = value.as_enum<enums::FanDirection>();
return true;
}
default:
return false;
}
@@ -932,6 +968,8 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::FanSpeed>(5, this->speed);
buffer.encode_bool(6, this->has_oscillating);
buffer.encode_bool(7, this->oscillating);
buffer.encode_bool(8, this->has_direction);
buffer.encode_enum<enums::FanDirection>(9, this->direction);
}
void FanCommandRequest::dump_to(std::string &out) const {
char buffer[64];
@@ -964,6 +1002,14 @@ void FanCommandRequest::dump_to(std::string &out) const {
out.append(" oscillating: ");
out.append(YESNO(this->oscillating));
out.append("\n");
out.append(" has_direction: ");
out.append(YESNO(this->has_direction));
out.append("\n");
out.append(" direction: ");
out.append(proto_enum_to_string<enums::FanDirection>(this->direction));
out.append("\n");
out.append("}");
}
bool ListEntitiesLightResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {

View File

@@ -28,6 +28,10 @@ enum FanSpeed : uint32_t {
FAN_SPEED_MEDIUM = 1,
FAN_SPEED_HIGH = 2,
};
enum FanDirection : uint32_t {
FAN_DIRECTION_FORWARD = 0,
FAN_DIRECTION_REVERSE = 1,
};
enum LogLevel : uint32_t {
LOG_LEVEL_NONE = 0,
LOG_LEVEL_ERROR = 1,
@@ -279,6 +283,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
std::string unique_id{}; // NOLINT
bool supports_oscillation{false}; // NOLINT
bool supports_speed{false}; // NOLINT
bool supports_direction{false}; // NOLINT
void encode(ProtoWriteBuffer buffer) const override;
void dump_to(std::string &out) const override;
@@ -289,10 +294,11 @@ class ListEntitiesFanResponse : public ProtoMessage {
};
class FanStateResponse : public ProtoMessage {
public:
uint32_t key{0}; // NOLINT
bool state{false}; // NOLINT
bool oscillating{false}; // NOLINT
enums::FanSpeed speed{}; // NOLINT
uint32_t key{0}; // NOLINT
bool state{false}; // NOLINT
bool oscillating{false}; // NOLINT
enums::FanSpeed speed{}; // NOLINT
enums::FanDirection direction{}; // NOLINT
void encode(ProtoWriteBuffer buffer) const override;
void dump_to(std::string &out) const override;
@@ -302,13 +308,15 @@ class FanStateResponse : public ProtoMessage {
};
class FanCommandRequest : public ProtoMessage {
public:
uint32_t key{0}; // NOLINT
bool has_state{false}; // NOLINT
bool state{false}; // NOLINT
bool has_speed{false}; // NOLINT
enums::FanSpeed speed{}; // NOLINT
bool has_oscillating{false}; // NOLINT
bool oscillating{false}; // NOLINT
uint32_t key{0}; // NOLINT
bool has_state{false}; // NOLINT
bool state{false}; // NOLINT
bool has_speed{false}; // NOLINT
enums::FanSpeed speed{}; // NOLINT
bool has_oscillating{false}; // NOLINT
bool oscillating{false}; // NOLINT
bool has_direction{false}; // NOLINT
enums::FanDirection direction{}; // NOLINT
void encode(ProtoWriteBuffer buffer) const override;
void dump_to(std::string &out) const override;