mirror of
https://github.com/esphome/esphome.git
synced 2025-09-27 07:32:22 +01:00
Add humidity support to climate (#5732)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -17,9 +17,12 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
||||
auto traits = this->device_->get_traits();
|
||||
// current_temperature_topic
|
||||
if (traits.get_supports_current_temperature()) {
|
||||
// current_temperature_topic
|
||||
root[MQTT_CURRENT_TEMPERATURE_TOPIC] = this->get_current_temperature_state_topic();
|
||||
}
|
||||
// current_humidity_topic
|
||||
if (traits.get_supports_current_humidity()) {
|
||||
root[MQTT_CURRENT_HUMIDITY_TOPIC] = this->get_current_humidity_state_topic();
|
||||
}
|
||||
// mode_command_topic
|
||||
root[MQTT_MODE_COMMAND_TOPIC] = this->get_mode_command_topic();
|
||||
// mode_state_topic
|
||||
@@ -57,6 +60,13 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
||||
root[MQTT_TEMPERATURE_STATE_TOPIC] = this->get_target_temperature_state_topic();
|
||||
}
|
||||
|
||||
if (traits.get_supports_target_humidity()) {
|
||||
// target_humidity_command_topic
|
||||
root[MQTT_TARGET_HUMIDITY_COMMAND_TOPIC] = this->get_target_humidity_command_topic();
|
||||
// target_humidity_state_topic
|
||||
root[MQTT_TARGET_HUMIDITY_STATE_TOPIC] = this->get_target_humidity_state_topic();
|
||||
}
|
||||
|
||||
// min_temp
|
||||
root[MQTT_MIN_TEMP] = traits.get_visual_min_temperature();
|
||||
// max_temp
|
||||
@@ -66,6 +76,11 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
|
||||
// temperature units are always coerced to Celsius internally
|
||||
root[MQTT_TEMPERATURE_UNIT] = "C";
|
||||
|
||||
// min_humidity
|
||||
root[MQTT_MIN_HUMIDITY] = traits.get_visual_min_humidity();
|
||||
// max_humidity
|
||||
root[MQTT_MAX_HUMIDITY] = traits.get_visual_max_humidity();
|
||||
|
||||
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
|
||||
// preset_mode_command_topic
|
||||
root[MQTT_PRESET_MODE_COMMAND_TOPIC] = this->get_preset_command_topic();
|
||||
@@ -192,6 +207,20 @@ void MQTTClimateComponent::setup() {
|
||||
});
|
||||
}
|
||||
|
||||
if (traits.get_supports_target_humidity()) {
|
||||
this->subscribe(this->get_target_humidity_command_topic(),
|
||||
[this](const std::string &topic, const std::string &payload) {
|
||||
auto val = parse_number<float>(payload);
|
||||
if (!val.has_value()) {
|
||||
ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
|
||||
return;
|
||||
}
|
||||
auto call = this->device_->make_call();
|
||||
call.set_target_humidity(*val);
|
||||
call.perform();
|
||||
});
|
||||
}
|
||||
|
||||
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
|
||||
this->subscribe(this->get_preset_command_topic(), [this](const std::string &topic, const std::string &payload) {
|
||||
auto call = this->device_->make_call();
|
||||
@@ -273,6 +302,17 @@ bool MQTTClimateComponent::publish_state_() {
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (traits.get_supports_current_humidity() && !std::isnan(this->device_->current_humidity)) {
|
||||
std::string payload = value_accuracy_to_string(this->device_->current_humidity, 0);
|
||||
if (!this->publish(this->get_current_humidity_state_topic(), payload))
|
||||
success = false;
|
||||
}
|
||||
if (traits.get_supports_target_humidity() && !std::isnan(this->device_->target_humidity)) {
|
||||
std::string payload = value_accuracy_to_string(this->device_->target_humidity, 0);
|
||||
if (!this->publish(this->get_target_humidity_state_topic(), payload))
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
|
||||
std::string payload;
|
||||
if (this->device_->preset.has_value()) {
|
||||
|
@@ -20,6 +20,7 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
|
||||
void setup() override;
|
||||
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(current_temperature, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(current_humidity, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(mode, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(mode, command)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature, state)
|
||||
@@ -28,6 +29,8 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_low, command)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, command)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_humidity, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(target_humidity, command)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(away, state)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(away, command)
|
||||
MQTT_COMPONENT_CUSTOM_TOPIC(action, state)
|
||||
|
@@ -51,6 +51,8 @@ constexpr const char *const MQTT_CODE_ARM_REQUIRED = "cod_arm_req";
|
||||
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "cod_dis_req";
|
||||
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "curr_temp_t";
|
||||
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "curr_temp_tpl";
|
||||
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "curr_hum_t";
|
||||
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "curr_hum_tpl";
|
||||
constexpr const char *const MQTT_DEVICE = "dev";
|
||||
constexpr const char *const MQTT_DEVICE_CLASS = "dev_cla";
|
||||
constexpr const char *const MQTT_DOCKED_TOPIC = "dock_t";
|
||||
@@ -305,6 +307,8 @@ constexpr const char *const MQTT_CODE_ARM_REQUIRED = "code_arm_required";
|
||||
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "code_disarm_required";
|
||||
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "current_temperature_topic";
|
||||
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "current_temperature_template";
|
||||
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "current_humidity_topic";
|
||||
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "current_humidity_template";
|
||||
constexpr const char *const MQTT_DEVICE = "device";
|
||||
constexpr const char *const MQTT_DEVICE_CLASS = "device_class";
|
||||
constexpr const char *const MQTT_DOCKED_TOPIC = "docked_topic";
|
||||
|
Reference in New Issue
Block a user