Make Climate component work over mqtt (#535)

* Fix climate component over MQTT

* Use climate_mode_to_string() in send_discovery()

* remove superfluous mqtt binding
This commit is contained in:
puuu 2019-05-13 18:51:22 +09:00 committed by Otto Winter
parent 02c9ada876
commit b7afb8c887
4 changed files with 11 additions and 19 deletions

View file

@ -207,11 +207,6 @@ ClimateTraits Climate::get_traits() {
return traits; return traits;
} }
#ifdef USE_MQTT_CLIMATE
MQTTClimateComponent *Climate::get_mqtt() const { return this->mqtt_; }
void Climate::set_mqtt(MQTTClimateComponent *mqtt) { this->mqtt_ = mqtt; }
#endif
void Climate::set_visual_min_temperature_override(float visual_min_temperature_override) { void Climate::set_visual_min_temperature_override(float visual_min_temperature_override) {
this->visual_min_temperature_override_ = visual_min_temperature_override; this->visual_min_temperature_override_ = visual_min_temperature_override;
} }

View file

@ -169,11 +169,6 @@ class Climate : public Nameable {
*/ */
ClimateTraits get_traits(); ClimateTraits get_traits();
#ifdef USE_MQTT_COVER
MQTTClimateComponent *get_mqtt() const;
void set_mqtt(MQTTClimateComponent *mqtt);
#endif
void set_visual_min_temperature_override(float visual_min_temperature_override); void set_visual_min_temperature_override(float visual_min_temperature_override);
void set_visual_max_temperature_override(float visual_max_temperature_override); void set_visual_max_temperature_override(float visual_max_temperature_override);
void set_visual_temperature_step_override(float visual_temperature_step_override); void set_visual_temperature_step_override(float visual_temperature_step_override);

View file

@ -6,13 +6,13 @@ namespace climate {
const char *climate_mode_to_string(ClimateMode mode) { const char *climate_mode_to_string(ClimateMode mode) {
switch (mode) { switch (mode) {
case CLIMATE_MODE_OFF: case CLIMATE_MODE_OFF:
return "OFF"; return "off";
case CLIMATE_MODE_AUTO: case CLIMATE_MODE_AUTO:
return "AUTO"; return "auto";
case CLIMATE_MODE_COOL: case CLIMATE_MODE_COOL:
return "COOL"; return "cool";
case CLIMATE_MODE_HEAT: case CLIMATE_MODE_HEAT:
return "HEAT"; return "heat";
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }

View file

@ -24,12 +24,12 @@ void MQTTClimateComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryC
JsonArray &modes = root.createNestedArray("modes"); JsonArray &modes = root.createNestedArray("modes");
// sort array for nice UI in HA // sort array for nice UI in HA
if (traits.supports_mode(CLIMATE_MODE_AUTO)) if (traits.supports_mode(CLIMATE_MODE_AUTO))
modes.add("auto"); modes.add(climate_mode_to_string(CLIMATE_MODE_AUTO));
modes.add("off"); modes.add(climate_mode_to_string(CLIMATE_MODE_OFF));
if (traits.supports_mode(CLIMATE_MODE_COOL)) if (traits.supports_mode(CLIMATE_MODE_COOL))
modes.add("cool"); modes.add(climate_mode_to_string(CLIMATE_MODE_COOL));
if (traits.supports_mode(CLIMATE_MODE_HEAT)) if (traits.supports_mode(CLIMATE_MODE_HEAT))
modes.add("heat"); modes.add(climate_mode_to_string(CLIMATE_MODE_HEAT));
if (traits.get_supports_two_point_target_temperature()) { if (traits.get_supports_two_point_target_temperature()) {
// temperature_low_command_topic // temperature_low_command_topic
@ -60,6 +60,8 @@ void MQTTClimateComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryC
// away_mode_state_topic // away_mode_state_topic
root["away_mode_state_topic"] = this->get_away_state_topic(); root["away_mode_state_topic"] = this->get_away_state_topic();
} }
config.state_topic = false;
config.command_topic = false;
} }
void MQTTClimateComponent::setup() { void MQTTClimateComponent::setup() {
auto traits = this->device_->get_traits(); auto traits = this->device_->get_traits();
@ -144,7 +146,7 @@ bool MQTTClimateComponent::publish_state_() {
if (!this->publish(this->get_mode_state_topic(), mode_s)) if (!this->publish(this->get_mode_state_topic(), mode_s))
success = false; success = false;
int8_t accuracy = traits.get_temperature_accuracy_decimals(); int8_t accuracy = traits.get_temperature_accuracy_decimals();
if (traits.get_supports_current_temperature()) { if (traits.get_supports_current_temperature() && !isnan(this->device_->current_temperature)) {
std::string payload = value_accuracy_to_string(this->device_->current_temperature, accuracy); std::string payload = value_accuracy_to_string(this->device_->current_temperature, accuracy);
if (!this->publish(this->get_current_temperature_state_topic(), payload)) if (!this->publish(this->get_current_temperature_state_topic(), payload))
success = false; success = false;