Support custom fan modes in mqtt_climate (#1989)

Co-authored-by: Michael Gorven <michael@gorven.za.net>
This commit is contained in:
Michael Gorven 2021-07-09 14:25:27 -07:00 committed by GitHub
parent be61b38a2c
commit 294ba1fca7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 31 deletions

View File

@ -91,7 +91,7 @@ class ClimateTraits {
ESPDEPRECATED("This method is deprecated, use set_supported_fan_modes() instead")
void set_supports_fan_mode_diffuse(bool supported) { set_fan_mode_support_(CLIMATE_FAN_DIFFUSE, supported); }
bool supports_fan_mode(ClimateFanMode fan_mode) const { return supported_fan_modes_.count(fan_mode); }
bool get_supports_fan_modes() const { return !supported_fan_modes_.empty(); }
bool get_supports_fan_modes() const { return !supported_fan_modes_.empty() || !supported_custom_fan_modes_.empty(); }
const std::set<ClimateFanMode> get_supported_fan_modes() const { return supported_fan_modes_; }
void set_supported_custom_fan_modes(std::set<std::string> supported_custom_fan_modes) {

View File

@ -97,6 +97,8 @@ void MQTTClimateComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryC
fan_modes.add("focus");
if (traits.supports_fan_mode(CLIMATE_FAN_DIFFUSE))
fan_modes.add("diffuse");
for (const auto &fan_mode : traits.get_supported_custom_fan_modes())
fan_modes.add(fan_mode);
}
if (traits.get_supports_swing_modes()) {
@ -291,7 +293,8 @@ bool MQTTClimateComponent::publish_state_() {
}
if (traits.get_supports_fan_modes()) {
const char *payload = "";
std::string payload;
if (this->device_->fan_mode.has_value())
switch (this->device_->fan_mode.value()) {
case CLIMATE_FAN_ON:
payload = "on";
@ -321,6 +324,8 @@ bool MQTTClimateComponent::publish_state_() {
payload = "diffuse";
break;
}
if (this->device_->custom_fan_mode.has_value())
payload = this->device_->custom_fan_mode.value();
if (!this->publish(this->get_fan_mode_state_topic(), payload))
success = false;
}