mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 14:43:51 +00:00
Add humidity support to climate (#5732)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import climate, sensor, output
|
||||
from esphome.const import CONF_ID, CONF_SENSOR
|
||||
from esphome.const import CONF_HUMIDITY_SENSOR, CONF_ID, CONF_SENSOR
|
||||
|
||||
pid_ns = cg.esphome_ns.namespace("pid")
|
||||
PIDClimate = pid_ns.class_("PIDClimate", climate.Climate, cg.Component)
|
||||
@@ -45,6 +45,7 @@ CONFIG_SCHEMA = cv.All(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(PIDClimate),
|
||||
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
|
||||
cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
|
||||
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE): cv.temperature,
|
||||
cv.Optional(CONF_COOL_OUTPUT): cv.use_id(output.FloatOutput),
|
||||
cv.Optional(CONF_HEAT_OUTPUT): cv.use_id(output.FloatOutput),
|
||||
@@ -86,6 +87,10 @@ async def to_code(config):
|
||||
sens = await cg.get_variable(config[CONF_SENSOR])
|
||||
cg.add(var.set_sensor(sens))
|
||||
|
||||
if CONF_HUMIDITY_SENSOR in config:
|
||||
sens = await cg.get_variable(config[CONF_HUMIDITY_SENSOR])
|
||||
cg.add(var.set_humidity_sensor(sens))
|
||||
|
||||
if CONF_COOL_OUTPUT in config:
|
||||
out = await cg.get_variable(config[CONF_COOL_OUTPUT])
|
||||
cg.add(var.set_cool_output(out))
|
||||
|
||||
@@ -14,6 +14,16 @@ void PIDClimate::setup() {
|
||||
this->update_pid_();
|
||||
});
|
||||
this->current_temperature = this->sensor_->state;
|
||||
|
||||
// register for humidity values and get initial state
|
||||
if (this->humidity_sensor_ != nullptr) {
|
||||
this->humidity_sensor_->add_on_state_callback([this](float state) {
|
||||
this->current_humidity = state;
|
||||
this->publish_state();
|
||||
});
|
||||
this->current_humidity = this->humidity_sensor_->state;
|
||||
}
|
||||
|
||||
// restore set points
|
||||
auto restore = this->restore_state_();
|
||||
if (restore.has_value()) {
|
||||
@@ -47,6 +57,9 @@ climate::ClimateTraits PIDClimate::traits() {
|
||||
traits.set_supports_current_temperature(true);
|
||||
traits.set_supports_two_point_target_temperature(false);
|
||||
|
||||
if (this->humidity_sensor_ != nullptr)
|
||||
traits.set_supports_current_humidity(true);
|
||||
|
||||
traits.set_supported_modes({climate::CLIMATE_MODE_OFF});
|
||||
if (supports_cool_())
|
||||
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
|
||||
|
||||
@@ -19,6 +19,7 @@ class PIDClimate : public climate::Climate, public Component {
|
||||
void dump_config() override;
|
||||
|
||||
void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; }
|
||||
void set_humidity_sensor(sensor::Sensor *sensor) { humidity_sensor_ = sensor; }
|
||||
void set_cool_output(output::FloatOutput *cool_output) { cool_output_ = cool_output; }
|
||||
void set_heat_output(output::FloatOutput *heat_output) { heat_output_ = heat_output; }
|
||||
void set_kp(float kp) { controller_.kp_ = kp; }
|
||||
@@ -85,6 +86,8 @@ class PIDClimate : public climate::Climate, public Component {
|
||||
|
||||
/// The sensor used for getting the current temperature
|
||||
sensor::Sensor *sensor_;
|
||||
/// The sensor used for getting the current humidity
|
||||
sensor::Sensor *humidity_sensor_{nullptr};
|
||||
output::FloatOutput *cool_output_{nullptr};
|
||||
output::FloatOutput *heat_output_{nullptr};
|
||||
PIDController controller_;
|
||||
|
||||
Reference in New Issue
Block a user