Remove deprecated fan speeds (#3397)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Keilin Bickar 2022-05-18 20:50:44 -04:00 committed by GitHub
parent 4f52d43347
commit f0c890f160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2 additions and 149 deletions

View File

@ -12,9 +12,6 @@
#ifdef USE_HOMEASSISTANT_TIME #ifdef USE_HOMEASSISTANT_TIME
#include "esphome/components/homeassistant/time/homeassistant_time.h" #include "esphome/components/homeassistant/time/homeassistant_time.h"
#endif #endif
#ifdef USE_FAN
#include "esphome/components/fan/fan_helpers.h"
#endif
namespace esphome { namespace esphome {
namespace api { namespace api {
@ -253,9 +250,6 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) {
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
// Shut-up about usage of deprecated speed_level_to_enum/speed_enum_to_level functions for a bit.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
bool APIConnection::send_fan_state(fan::Fan *fan) { bool APIConnection::send_fan_state(fan::Fan *fan) {
if (!this->state_subscription_) if (!this->state_subscription_)
return false; return false;
@ -268,7 +262,6 @@ bool APIConnection::send_fan_state(fan::Fan *fan) {
resp.oscillating = fan->oscillating; resp.oscillating = fan->oscillating;
if (traits.supports_speed()) { if (traits.supports_speed()) {
resp.speed_level = fan->speed; resp.speed_level = fan->speed;
resp.speed = static_cast<enums::FanSpeed>(fan::speed_level_to_enum(fan->speed, traits.supported_speed_count()));
} }
if (traits.supports_direction()) if (traits.supports_direction())
resp.direction = static_cast<enums::FanDirection>(fan->direction); resp.direction = static_cast<enums::FanDirection>(fan->direction);
@ -295,8 +288,6 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
if (fan == nullptr) if (fan == nullptr)
return; return;
auto traits = fan->get_traits();
auto call = fan->make_call(); auto call = fan->make_call();
if (msg.has_state) if (msg.has_state)
call.set_state(msg.state); call.set_state(msg.state);
@ -305,14 +296,11 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
if (msg.has_speed_level) { if (msg.has_speed_level) {
// Prefer level // Prefer level
call.set_speed(msg.speed_level); call.set_speed(msg.speed_level);
} else if (msg.has_speed) {
call.set_speed(fan::speed_enum_to_level(static_cast<fan::FanSpeed>(msg.speed), traits.supported_speed_count()));
} }
if (msg.has_direction) if (msg.has_direction)
call.set_direction(static_cast<fan::FanDirection>(msg.direction)); call.set_direction(static_cast<fan::FanDirection>(msg.direction));
call.perform(); call.perform();
} }
#pragma GCC diagnostic pop
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT

View File

@ -1,5 +1,4 @@
#include "fan.h" #include "fan.h"
#include "fan_helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {
@ -61,22 +60,6 @@ void FanCall::validate_() {
} }
} }
// This whole method is deprecated, don't warn about usage of deprecated methods inside of it.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
FanCall &FanCall::set_speed(const char *legacy_speed) {
const auto supported_speed_count = this->parent_.get_traits().supported_speed_count();
if (strcasecmp(legacy_speed, "low") == 0) {
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_LOW, supported_speed_count));
} else if (strcasecmp(legacy_speed, "medium") == 0) {
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_MEDIUM, supported_speed_count));
} else if (strcasecmp(legacy_speed, "high") == 0) {
this->set_speed(fan::speed_enum_to_level(FAN_SPEED_HIGH, supported_speed_count));
}
return *this;
}
#pragma GCC diagnostic pop
FanCall FanRestoreState::to_call(Fan &fan) { FanCall FanRestoreState::to_call(Fan &fan) {
auto call = fan.make_call(); auto call = fan.make_call();
call.set_state(this->state); call.set_state(this->state);

View File

@ -16,13 +16,6 @@ namespace fan {
(obj)->dump_traits_(TAG, prefix); \ (obj)->dump_traits_(TAG, prefix); \
} }
/// Simple enum to represent the speed of a fan. - DEPRECATED - Will be deleted soon
enum ESPDEPRECATED("FanSpeed is deprecated.", "2021.9") FanSpeed {
FAN_SPEED_LOW = 0, ///< The fan is running on low speed.
FAN_SPEED_MEDIUM = 1, ///< The fan is running on medium speed.
FAN_SPEED_HIGH = 2 ///< The fan is running on high/full speed.
};
/// Simple enum to represent the direction of a fan. /// Simple enum to represent the direction of a fan.
enum class FanDirection { FORWARD = 0, REVERSE = 1 }; enum class FanDirection { FORWARD = 0, REVERSE = 1 };

View File

@ -1,23 +0,0 @@
#include <cassert>
#include "fan_helpers.h"
namespace esphome {
namespace fan {
// This whole file is deprecated, don't warn about usage of deprecated types in here.
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels) {
const auto speed_ratio = static_cast<float>(speed_level) / (supported_speed_levels + 1);
const auto legacy_level = clamp<int>(static_cast<int>(ceilf(speed_ratio * 3)), 1, 3);
return static_cast<FanSpeed>(legacy_level - 1);
}
int speed_enum_to_level(FanSpeed speed, int supported_speed_levels) {
const auto enum_level = static_cast<int>(speed) + 1;
const auto speed_level = roundf(enum_level / 3.0f * supported_speed_levels);
return static_cast<int>(speed_level);
}
} // namespace fan
} // namespace esphome

View File

@ -1,20 +0,0 @@
#pragma once
#include "fan.h"
namespace esphome {
namespace fan {
// Shut-up about usage of deprecated FanSpeed for a bit.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
ESPDEPRECATED("FanSpeed and speed_level_to_enum() are deprecated.", "2021.9")
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels);
ESPDEPRECATED("FanSpeed and speed_enum_to_level() are deprecated.", "2021.9")
int speed_enum_to_level(FanSpeed speed, int supported_speed_levels);
#pragma GCC diagnostic pop
} // namespace fan
} // namespace esphome

View File

@ -1,5 +1,4 @@
#include "hbridge_fan.h" #include "hbridge_fan.h"
#include "esphome/components/fan/fan_helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {

View File

@ -5,7 +5,6 @@
#ifdef USE_MQTT #ifdef USE_MQTT
#ifdef USE_FAN #ifdef USE_FAN
#include "esphome/components/fan/fan_helpers.h"
namespace esphome { namespace esphome {
namespace mqtt { namespace mqtt {
@ -88,17 +87,6 @@ void MQTTFanComponent::setup() {
}); });
} }
if (this->state_->get_traits().supports_speed()) {
this->subscribe(this->get_speed_command_topic(), [this](const std::string &topic, const std::string &payload) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
this->state_->make_call()
.set_speed(payload.c_str()) // NOLINT(clang-diagnostic-deprecated-declarations)
.perform();
#pragma GCC diagnostic pop
});
}
auto f = std::bind(&MQTTFanComponent::publish_state, this); auto f = std::bind(&MQTTFanComponent::publish_state, this);
this->state_->add_on_state_callback([this, f]() { this->defer("send", f); }); this->state_->add_on_state_callback([this, f]() { this->defer("send", f); });
} }
@ -113,8 +101,6 @@ void MQTTFanComponent::dump_config() {
if (this->state_->get_traits().supports_speed()) { if (this->state_->get_traits().supports_speed()) {
ESP_LOGCONFIG(TAG, " Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str()); ESP_LOGCONFIG(TAG, " Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str());
ESP_LOGCONFIG(TAG, " Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str()); ESP_LOGCONFIG(TAG, " Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str());
ESP_LOGCONFIG(TAG, " Speed State Topic: '%s'", this->get_speed_state_topic().c_str());
ESP_LOGCONFIG(TAG, " Speed Command Topic: '%s'", this->get_speed_command_topic().c_str());
} }
} }
@ -126,10 +112,8 @@ void MQTTFanComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig
root[MQTT_OSCILLATION_STATE_TOPIC] = this->get_oscillation_state_topic(); root[MQTT_OSCILLATION_STATE_TOPIC] = this->get_oscillation_state_topic();
} }
if (this->state_->get_traits().supports_speed()) { if (this->state_->get_traits().supports_speed()) {
root["speed_level_command_topic"] = this->get_speed_level_command_topic(); root[MQTT_PERCENTAGE_COMMAND_TOPIC] = this->get_speed_level_command_topic();
root["speed_level_state_topic"] = this->get_speed_level_state_topic(); root[MQTT_PERCENTAGE_STATE_TOPIC] = this->get_speed_level_state_topic();
root[MQTT_SPEED_COMMAND_TOPIC] = this->get_speed_command_topic();
root[MQTT_SPEED_STATE_TOPIC] = this->get_speed_state_topic();
} }
} }
bool MQTTFanComponent::publish_state() { bool MQTTFanComponent::publish_state() {
@ -148,31 +132,6 @@ bool MQTTFanComponent::publish_state() {
bool success = this->publish(this->get_speed_level_state_topic(), payload); bool success = this->publish(this->get_speed_level_state_topic(), payload);
failed = failed || !success; failed = failed || !success;
} }
if (traits.supports_speed()) {
const char *payload;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
switch (fan::speed_level_to_enum(this->state_->speed, traits.supported_speed_count())) {
case FAN_SPEED_LOW: { // NOLINT(clang-diagnostic-deprecated-declarations)
payload = "low";
break;
}
case FAN_SPEED_MEDIUM: { // NOLINT(clang-diagnostic-deprecated-declarations)
payload = "medium";
break;
}
default:
case FAN_SPEED_HIGH: { // NOLINT(clang-diagnostic-deprecated-declarations)
payload = "high";
break;
}
}
#pragma GCC diagnostic pop
bool success = this->publish(this->get_speed_state_topic(), payload);
failed = failed || !success;
}
return !failed; return !failed;
} }

View File

@ -1,5 +1,4 @@
#include "speed_fan.h" #include "speed_fan.h"
#include "esphome/components/fan/fan_helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {

View File

@ -1,5 +1,4 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/components/fan/fan_helpers.h"
#include "tuya_fan.h" #include "tuya_fan.h"
namespace esphome { namespace esphome {

View File

@ -21,10 +21,6 @@
#include "esphome/components/logger/logger.h" #include "esphome/components/logger/logger.h"
#endif #endif
#ifdef USE_FAN
#include "esphome/components/fan/fan_helpers.h"
#endif
#ifdef USE_CLIMATE #ifdef USE_CLIMATE
#include "esphome/components/climate/climate.h" #include "esphome/components/climate/climate.h"
#endif #endif
@ -482,22 +478,6 @@ std::string WebServer::fan_json(fan::Fan *obj, JsonDetail start_config) {
if (traits.supports_speed()) { if (traits.supports_speed()) {
root["speed_level"] = obj->speed; root["speed_level"] = obj->speed;
root["speed_count"] = traits.supported_speed_count(); root["speed_count"] = traits.supported_speed_count();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
switch (fan::speed_level_to_enum(obj->speed, traits.supported_speed_count())) {
case fan::FAN_SPEED_LOW: // NOLINT(clang-diagnostic-deprecated-declarations)
root["speed"] = "low";
break;
case fan::FAN_SPEED_MEDIUM: // NOLINT(clang-diagnostic-deprecated-declarations)
root["speed"] = "medium";
break;
case fan::FAN_SPEED_HIGH: // NOLINT(clang-diagnostic-deprecated-declarations)
root["speed"] = "high";
break;
}
#pragma GCC diagnostic pop
} }
if (obj->get_traits().supports_oscillation()) if (obj->get_traits().supports_oscillation())
root["oscillation"] = obj->oscillating; root["oscillation"] = obj->oscillating;
@ -518,10 +498,6 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc
auto call = obj->turn_on(); auto call = obj->turn_on();
if (request->hasParam("speed")) { if (request->hasParam("speed")) {
String speed = request->getParam("speed")->value(); String speed = request->getParam("speed")->value();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
call.set_speed(speed.c_str()); // NOLINT(clang-diagnostic-deprecated-declarations)
#pragma GCC diagnostic pop
} }
if (request->hasParam("speed_level")) { if (request->hasParam("speed_level")) {
String speed_level = request->getParam("speed_level")->value(); String speed_level = request->getParam("speed_level")->value();