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") 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); } 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 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_; } 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) { 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"); fan_modes.add("focus");
if (traits.supports_fan_mode(CLIMATE_FAN_DIFFUSE)) if (traits.supports_fan_mode(CLIMATE_FAN_DIFFUSE))
fan_modes.add("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()) { if (traits.get_supports_swing_modes()) {
@ -291,36 +293,39 @@ bool MQTTClimateComponent::publish_state_() {
} }
if (traits.get_supports_fan_modes()) { if (traits.get_supports_fan_modes()) {
const char *payload = ""; std::string payload;
switch (this->device_->fan_mode.value()) { if (this->device_->fan_mode.has_value())
case CLIMATE_FAN_ON: switch (this->device_->fan_mode.value()) {
payload = "on"; case CLIMATE_FAN_ON:
break; payload = "on";
case CLIMATE_FAN_OFF: break;
payload = "off"; case CLIMATE_FAN_OFF:
break; payload = "off";
case CLIMATE_FAN_AUTO: break;
payload = "auto"; case CLIMATE_FAN_AUTO:
break; payload = "auto";
case CLIMATE_FAN_LOW: break;
payload = "low"; case CLIMATE_FAN_LOW:
break; payload = "low";
case CLIMATE_FAN_MEDIUM: break;
payload = "medium"; case CLIMATE_FAN_MEDIUM:
break; payload = "medium";
case CLIMATE_FAN_HIGH: break;
payload = "high"; case CLIMATE_FAN_HIGH:
break; payload = "high";
case CLIMATE_FAN_MIDDLE: break;
payload = "middle"; case CLIMATE_FAN_MIDDLE:
break; payload = "middle";
case CLIMATE_FAN_FOCUS: break;
payload = "focus"; case CLIMATE_FAN_FOCUS:
break; payload = "focus";
case CLIMATE_FAN_DIFFUSE: break;
payload = "diffuse"; case CLIMATE_FAN_DIFFUSE:
break; 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)) if (!this->publish(this->get_fan_mode_state_topic(), payload))
success = false; success = false;
} }