mirror of
https://github.com/esphome/esphome.git
synced 2025-10-23 20:23:50 +01:00
[mqtt] Update to use new climate API (#11360)
This commit is contained in:
@@ -17,11 +17,11 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
|||||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||||
auto traits = this->device_->get_traits();
|
auto traits = this->device_->get_traits();
|
||||||
// current_temperature_topic
|
// current_temperature_topic
|
||||||
if (traits.get_supports_current_temperature()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE)) {
|
||||||
root[MQTT_CURRENT_TEMPERATURE_TOPIC] = this->get_current_temperature_state_topic();
|
root[MQTT_CURRENT_TEMPERATURE_TOPIC] = this->get_current_temperature_state_topic();
|
||||||
}
|
}
|
||||||
// current_humidity_topic
|
// current_humidity_topic
|
||||||
if (traits.get_supports_current_humidity()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY)) {
|
||||||
root[MQTT_CURRENT_HUMIDITY_TOPIC] = this->get_current_humidity_state_topic();
|
root[MQTT_CURRENT_HUMIDITY_TOPIC] = this->get_current_humidity_state_topic();
|
||||||
}
|
}
|
||||||
// mode_command_topic
|
// mode_command_topic
|
||||||
@@ -45,7 +45,8 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
|||||||
if (traits.supports_mode(CLIMATE_MODE_HEAT_COOL))
|
if (traits.supports_mode(CLIMATE_MODE_HEAT_COOL))
|
||||||
modes.add("heat_cool");
|
modes.add("heat_cool");
|
||||||
|
|
||||||
if (traits.get_supports_two_point_target_temperature()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE |
|
||||||
|
climate::CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE)) {
|
||||||
// temperature_low_command_topic
|
// temperature_low_command_topic
|
||||||
root[MQTT_TEMPERATURE_LOW_COMMAND_TOPIC] = this->get_target_temperature_low_command_topic();
|
root[MQTT_TEMPERATURE_LOW_COMMAND_TOPIC] = this->get_target_temperature_low_command_topic();
|
||||||
// temperature_low_state_topic
|
// temperature_low_state_topic
|
||||||
@@ -61,7 +62,7 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
|||||||
root[MQTT_TEMPERATURE_STATE_TOPIC] = this->get_target_temperature_state_topic();
|
root[MQTT_TEMPERATURE_STATE_TOPIC] = this->get_target_temperature_state_topic();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traits.get_supports_target_humidity()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TARGET_HUMIDITY)) {
|
||||||
// target_humidity_command_topic
|
// target_humidity_command_topic
|
||||||
root[MQTT_TARGET_HUMIDITY_COMMAND_TOPIC] = this->get_target_humidity_command_topic();
|
root[MQTT_TARGET_HUMIDITY_COMMAND_TOPIC] = this->get_target_humidity_command_topic();
|
||||||
// target_humidity_state_topic
|
// target_humidity_state_topic
|
||||||
@@ -109,7 +110,7 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
|||||||
presets.add(preset);
|
presets.add(preset);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traits.get_supports_action()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_ACTION)) {
|
||||||
// action_topic
|
// action_topic
|
||||||
root[MQTT_ACTION_TOPIC] = this->get_action_state_topic();
|
root[MQTT_ACTION_TOPIC] = this->get_action_state_topic();
|
||||||
}
|
}
|
||||||
@@ -174,7 +175,8 @@ void MQTTClimateComponent::setup() {
|
|||||||
call.perform();
|
call.perform();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (traits.get_supports_two_point_target_temperature()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE |
|
||||||
|
climate::CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE)) {
|
||||||
this->subscribe(this->get_target_temperature_low_command_topic(),
|
this->subscribe(this->get_target_temperature_low_command_topic(),
|
||||||
[this](const std::string &topic, const std::string &payload) {
|
[this](const std::string &topic, const std::string &payload) {
|
||||||
auto val = parse_number<float>(payload);
|
auto val = parse_number<float>(payload);
|
||||||
@@ -211,7 +213,7 @@ void MQTTClimateComponent::setup() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traits.get_supports_target_humidity()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TARGET_HUMIDITY)) {
|
||||||
this->subscribe(this->get_target_humidity_command_topic(),
|
this->subscribe(this->get_target_humidity_command_topic(),
|
||||||
[this](const std::string &topic, const std::string &payload) {
|
[this](const std::string &topic, const std::string &payload) {
|
||||||
auto val = parse_number<float>(payload);
|
auto val = parse_number<float>(payload);
|
||||||
@@ -290,12 +292,14 @@ bool MQTTClimateComponent::publish_state_() {
|
|||||||
success = false;
|
success = false;
|
||||||
int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
|
int8_t target_accuracy = traits.get_target_temperature_accuracy_decimals();
|
||||||
int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
|
int8_t current_accuracy = traits.get_current_temperature_accuracy_decimals();
|
||||||
if (traits.get_supports_current_temperature() && !std::isnan(this->device_->current_temperature)) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_TEMPERATURE) &&
|
||||||
|
!std::isnan(this->device_->current_temperature)) {
|
||||||
std::string payload = value_accuracy_to_string(this->device_->current_temperature, current_accuracy);
|
std::string payload = value_accuracy_to_string(this->device_->current_temperature, current_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;
|
||||||
}
|
}
|
||||||
if (traits.get_supports_two_point_target_temperature()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TWO_POINT_TARGET_TEMPERATURE |
|
||||||
|
climate::CLIMATE_REQUIRES_TWO_POINT_TARGET_TEMPERATURE)) {
|
||||||
std::string payload = value_accuracy_to_string(this->device_->target_temperature_low, target_accuracy);
|
std::string payload = value_accuracy_to_string(this->device_->target_temperature_low, target_accuracy);
|
||||||
if (!this->publish(this->get_target_temperature_low_state_topic(), payload))
|
if (!this->publish(this->get_target_temperature_low_state_topic(), payload))
|
||||||
success = false;
|
success = false;
|
||||||
@@ -308,12 +312,14 @@ bool MQTTClimateComponent::publish_state_() {
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traits.get_supports_current_humidity() && !std::isnan(this->device_->current_humidity)) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_CURRENT_HUMIDITY) &&
|
||||||
|
!std::isnan(this->device_->current_humidity)) {
|
||||||
std::string payload = value_accuracy_to_string(this->device_->current_humidity, 0);
|
std::string payload = value_accuracy_to_string(this->device_->current_humidity, 0);
|
||||||
if (!this->publish(this->get_current_humidity_state_topic(), payload))
|
if (!this->publish(this->get_current_humidity_state_topic(), payload))
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
if (traits.get_supports_target_humidity() && !std::isnan(this->device_->target_humidity)) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_TARGET_HUMIDITY) &&
|
||||||
|
!std::isnan(this->device_->target_humidity)) {
|
||||||
std::string payload = value_accuracy_to_string(this->device_->target_humidity, 0);
|
std::string payload = value_accuracy_to_string(this->device_->target_humidity, 0);
|
||||||
if (!this->publish(this->get_target_humidity_state_topic(), payload))
|
if (!this->publish(this->get_target_humidity_state_topic(), payload))
|
||||||
success = false;
|
success = false;
|
||||||
@@ -357,7 +363,7 @@ bool MQTTClimateComponent::publish_state_() {
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (traits.get_supports_action()) {
|
if (traits.has_feature_flags(climate::CLIMATE_SUPPORTS_ACTION)) {
|
||||||
const char *payload;
|
const char *payload;
|
||||||
switch (this->device_->action) {
|
switch (this->device_->action) {
|
||||||
case CLIMATE_ACTION_OFF:
|
case CLIMATE_ACTION_OFF:
|
||||||
|
Reference in New Issue
Block a user