From b04fd228bc9ef34c3d4c02ce0074b84f52bc2f9a Mon Sep 17 00:00:00 2001 From: tsunglung Date: Sun, 27 Aug 2023 14:32:28 +0800 Subject: [PATCH] Add feature to make climate mode configurable --- components/daikin_s21/climate/__init__.py | 24 ++++++++++++++- .../daikin_s21/climate/daikin_s21_climate.cpp | 30 +++++++++---------- .../daikin_s21/climate/daikin_s21_climate.h | 4 +++ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/components/daikin_s21/climate/__init__.py b/components/daikin_s21/climate/__init__.py index 9a096f3..233af42 100644 --- a/components/daikin_s21/climate/__init__.py +++ b/components/daikin_s21/climate/__init__.py @@ -5,13 +5,14 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import climate, sensor -from esphome.const import CONF_ID +from esphome.const import CONF_ID, CONF_SUPPORTED_MODES from .. import ( daikin_s21_ns, CONF_S21_ID, S21_CLIENT_SCHEMA, DaikinS21Client, ) +from esphome.components.climate import ClimateMode CONF_ROOM_TEMPERATURE_SENSOR = "room_temperature_sensor" CONF_SETPOINT_INTERVAL = "setpoint_interval" @@ -22,6 +23,15 @@ uart_ns = cg.esphome_ns.namespace("uart") UARTComponent = uart_ns.class_("UARTComponent") +SUPPORTED_CLIMATE_MODES_OPTIONS = { + "OFF": ClimateMode.CLIMATE_MODE_OFF, # always available + "AUTO": ClimateMode.CLIMATE_MODE_AUTO, # always available + "COOL": ClimateMode.CLIMATE_MODE_COOL, + "HEAT": ClimateMode.CLIMATE_MODE_HEAT, + "DRY": ClimateMode.CLIMATE_MODE_DRY, + "FAN_ONLY": ClimateMode.CLIMATE_MODE_FAN_ONLY, +} + CONFIG_SCHEMA = cv.All( climate.CLIMATE_SCHEMA.extend( { @@ -30,6 +40,9 @@ cv.Optional( CONF_SETPOINT_INTERVAL, default="300s" ): cv.positive_time_period_seconds, + cv.Optional(CONF_SUPPORTED_MODES): cv.ensure_list( + cv.enum(SUPPORTED_CLIMATE_MODES_OPTIONS, upper=True) + ), } ) .extend(cv.polling_component_schema("5s")) @@ -49,3 +62,12 @@ async def to_code(config): cg.add(var.set_room_sensor(sens)) if CONF_SETPOINT_INTERVAL in config: cg.add(var.set_setpoint_interval(config[CONF_SETPOINT_INTERVAL])) + + if CONF_SUPPORTED_MODES in config: + cg.add(var.set_supported_modes(config[CONF_SUPPORTED_MODES])) else: + cg.add(var.set_supported_modes([ + ClimateMode.CLIMATE_MODE_COOL, + ClimateMode.CLIMATE_MODE_HEAT, + ClimateMode.CLIMATE_MODE_DRY, + ClimateMode.CLIMATE_MODE_FAN_ONLY, + ])) \ No newline at end of file diff --git a/components/daikin_s21/climate/daikin_s21_climate.cpp b/components/daikin_s21/climate/daikin_s21_climate.cpp index cc3cc30..143c325 100644 --- a/components/daikin_s21/climate/daikin_s21_climate.cpp +++ b/components/daikin_s21/climate/daikin_s21_climate.cpp @@ -38,32 +38,32 @@ void DaikinS21Climate::dump_config() { this->dump_traits_(TAG); } -climate::ClimateTraits DaikinS21Climate::traits() { - auto traits = climate::ClimateTraits(); +void DaikinS21Climate::set_supported_modes(const std::set &modes) { + this->traits_.set_supported_modes(modes); + this->traits_.add_supported_mode(climate::CLIMATE_MODE_OFF); // Always available + this->traits_.add_supported_mode(climate::CLIMATE_MODE_AUTO); // Always available +} - traits.set_supports_action(true); +climate::ClimateTraits DaikinS21Climate::traits() { - traits.set_supports_current_temperature(true); - traits.set_visual_min_temperature(SETPOINT_MIN); - traits.set_visual_max_temperature(SETPOINT_MAX); - traits.set_visual_temperature_step(SETPOINT_STEP); - traits.set_supports_two_point_target_temperature(false); + this->traits_.set_supports_action(true); - traits.set_supported_modes( - {climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_HEAT_COOL, - climate::CLIMATE_MODE_COOL, climate::CLIMATE_MODE_HEAT, - climate::CLIMATE_MODE_FAN_ONLY, climate::CLIMATE_MODE_DRY}); + this->traits_.set_supports_current_temperature(true); + this->traits_.set_visual_min_temperature(SETPOINT_MIN); + this->traits_.set_visual_max_temperature(SETPOINT_MAX); + this->traits_.set_visual_temperature_step(SETPOINT_STEP); + this->traits_.set_supports_two_point_target_temperature(false); - traits.set_supported_custom_fan_modes({"Automatic", "1", "2", "3", "4", "5"}); + this->traits_.set_supported_custom_fan_modes({"Automatic", "1", "2", "3", "4", "5"}); - traits.set_supported_swing_modes({ + this->traits_.set_supported_swing_modes({ climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_BOTH, climate::CLIMATE_SWING_VERTICAL, climate::CLIMATE_SWING_HORIZONTAL, }); - return traits; + return this->traits_; } bool DaikinS21Climate::use_room_sensor() { diff --git a/components/daikin_s21/climate/daikin_s21_climate.h b/components/daikin_s21/climate/daikin_s21_climate.h index 837ed02..e8c6f96 100644 --- a/components/daikin_s21/climate/daikin_s21_climate.h +++ b/components/daikin_s21/climate/daikin_s21_climate.h @@ -49,7 +49,11 @@ class DaikinS21Climate : public climate::Climate, bool e2d_swing_v(climate::ClimateSwingMode mode); bool e2d_swing_h(climate::ClimateSwingMode mode); + void set_supported_modes(const std::set &modes); + protected: + esphome::climate::ClimateTraits traits_; + sensor::Sensor *room_sensor_{nullptr}; float expected_s21_setpoint; uint8_t skip_setpoint_checks = 0;