Skip to content

Commit

Permalink
#21 update presets logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dentra committed Feb 1, 2024
1 parent 8950a5f commit cbfe7fb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
76 changes: 68 additions & 8 deletions components/tion/tion_climate_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,96 @@ climate::ClimateTraits TionClimateComponentBase::traits() {
}

void TionClimateComponentBase::control(const climate::ClimateCall &call) {
auto gate_position = TionGatePosition::NONE;
auto mode = this->mode;
auto fan_speed = fan_mode_to_speed(this->custom_fan_mode);
auto target_temperature = this->target_temperature;

const bool has_changes = call.get_mode().has_value() || call.get_custom_fan_mode().has_value() ||
call.get_target_temperature().has_value();

#ifdef TION_ENABLE_PRESETS
const auto old_preset = this->preset.value_or(climate::CLIMATE_PRESET_NONE);
if (call.get_preset().has_value()) {
if (this->enable_preset_(*call.get_preset())) {
return;
const auto new_preset = *call.get_preset();
auto *preset_data = this->presets_enable_preset_(new_preset, this, this);
if (preset_data) {
ESP_LOGD(TAG, "Set preset %s", LOG_STR_ARG(climate::climate_preset_to_string(new_preset)));
gate_position = preset_data->gate_position;
mode = preset_data->mode;
fan_speed = preset_data->fan_speed;
target_temperature = preset_data->target_temperature;
this->preset = new_preset;
// если буст, то больне ничеего не даем изменять и выходим
if (new_preset == climate::CLIMATE_PRESET_BOOST) {
if (has_changes) {
ESP_LOGW(TAG, "No more changes can be performed");
if (call.get_mode().has_value()) {
ESP_LOGW(TAG, " Change of mode was skipped");
}
if (call.get_custom_fan_mode().has_value()) {
ESP_LOGW(TAG, " Change of fan speed was skipped");
}
if (call.get_target_temperature().has_value()) {
ESP_LOGW(TAG, " Change of target temperature was skipped");
}
}
return;
}
}
} else if (old_preset == climate::CLIMATE_PRESET_BOOST && has_changes) {
auto *preset_data = this->presets_enable_preset_(this->saved_preset_, this, this);
if (preset_data) {
ESP_LOGD(TAG, "Restored preset %s data", LOG_STR_ARG(climate::climate_preset_to_string(this->saved_preset_)));
gate_position = preset_data->gate_position;
mode = preset_data->mode;
fan_speed = preset_data->fan_speed;
target_temperature = preset_data->target_temperature;
}
}
const auto new_preset = this->preset.value_or(climate::CLIMATE_PRESET_NONE);
#else
const auto old_preset = climate::CLIMATE_PRESET_NONE;
const auto new_preset = climate::CLIMATE_PRESET_NONE;
#endif

climate::ClimateMode mode = this->mode;
if (call.get_mode().has_value()) {
mode = *call.get_mode();
ESP_LOGD(TAG, "Set mode %s", LOG_STR_ARG(climate::climate_mode_to_string(mode)));
this->preset = climate::CLIMATE_PRESET_NONE;
}

uint8_t fan_speed = fan_mode_to_speed(this->custom_fan_mode);
if (call.get_custom_fan_mode().has_value()) {
fan_speed = fan_mode_to_speed(*call.get_custom_fan_mode());
ESP_LOGD(TAG, "Set fan speed %u", fan_speed);
this->preset = climate::CLIMATE_PRESET_NONE;
}

float target_temperature = this->target_temperature;
if (call.get_target_temperature().has_value()) {
target_temperature = *call.get_target_temperature();
ESP_LOGD(TAG, "Set target temperature %d °C", int(target_temperature));
}

if (!has_changes && old_preset == new_preset) {
ESP_LOGW(TAG, "No changes was performed");
return;
}

#ifdef TION_ENABLE_PRESETS
// в любом случае сбрасываем пресет если были изменения
if (has_changes) {
this->preset = climate::CLIMATE_PRESET_NONE;
}
#endif

this->control_climate_state(mode, fan_speed, target_temperature, gate_position);
}

this->control_climate_state(mode, fan_speed, target_temperature, TionGatePosition::NONE);
void TionClimateComponentBase::reset_preset_() {
#ifdef TION_ENABLE_PRESETS
const auto preset = this->preset.value_or(climate::CLIMATE_PRESET_NONE);
if (preset == climate::CLIMATE_PRESET_BOOST) {
}
this->preset = climate::CLIMATE_PRESET_NONE;
#endif
}

void TionClimateComponentBase::set_fan_speed_(uint8_t fan_speed) {
Expand Down
7 changes: 6 additions & 1 deletion components/tion/tion_climate_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class TionClimateComponentBase : public climate::Climate, public TionClimatePres
TionClimateComponentBase(const TionClimateComponentBase &) = delete; // non construction-copyable
TionClimateComponentBase &operator=(const TionClimateComponentBase &) = delete; // non copyable

TionClimateComponentBase(TionVPortType vport_type) : vport_type_(vport_type) { this->target_temperature = NAN; }
TionClimateComponentBase(TionVPortType vport_type) : vport_type_(vport_type) {
this->target_temperature = NAN;
this->preset = climate::CLIMATE_PRESET_NONE;
}
void call_setup() override;
void dump_settings(const char *tag, const char *component) const;

Expand All @@ -37,6 +40,8 @@ class TionClimateComponentBase : public climate::Climate, public TionClimatePres

void set_fan_speed_(uint8_t fan_speed);

void reset_preset_();

#ifdef TION_ENABLE_PRESETS
virtual bool enable_boost() { return this->presets_enable_boost_(this, this); }
virtual void cancel_boost() { this->presets_cancel_boost_(this, this); }
Expand Down
2 changes: 1 addition & 1 deletion components/tion/tion_climate_presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void TionClimatePresets::dump_preset_(const char *tag, climate::ClimatePreset in

TionClimatePresetData *TionClimatePresets::presets_enable_preset_(climate::ClimatePreset new_preset,
Component *component, climate::Climate *climate) {
const auto old_preset = *climate->preset;
const auto old_preset = climate->preset.value_or(climate::CLIMATE_PRESET_NONE);
if (new_preset == old_preset) {
ESP_LOGD(TAG, "Preset was not changed");
return nullptr;
Expand Down
7 changes: 4 additions & 3 deletions components/tion_4s/climate/tion_4s_climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ void Tion4sClimate::on_turbo(const tion4s_turbo_t &turbo, uint32_t request_id) {
}

// change preset if turbo changed outside
const auto preset = this->preset.value_or(climate::CLIMATE_PRESET_NONE);
if (turbo.is_active) {
if (*this->preset != climate::CLIMATE_PRESET_BOOST) {
this->saved_preset_ = *this->preset;
if (preset != climate::CLIMATE_PRESET_BOOST) {
this->saved_preset_ = preset;
this->preset = climate::CLIMATE_PRESET_BOOST;
}
} else {
if (*this->preset == climate::CLIMATE_PRESET_BOOST) {
if (preset == climate::CLIMATE_PRESET_BOOST) {
this->enable_preset_(this->saved_preset_);
}
}
Expand Down

0 comments on commit cbfe7fb

Please sign in to comment.