Skip to content

Commit d54c1b1

Browse files
committed
Support restore_mode for active mode switch
For #17
1 parent 1ca2362 commit d54c1b1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

esphome/components/MhiAcCtrl/mhi_ac_ctrl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ class MhiPower : public Sensor {
9090

9191
#ifdef USE_SWITCH
9292
class MhiActiveMode : public switch_::Switch {
93+
public:
94+
void setup() {
95+
ESP_LOGCONFIG(TAG, "Setting up MHI Active Mode Switch '%s'...", this->name_.c_str());
96+
97+
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
98+
99+
if (initial_state) {
100+
this->turn_on();
101+
} else {
102+
this->turn_off();
103+
}
104+
}
105+
93106
protected:
94107
virtual void write_state(bool state) {
95108
mhi_ac::active_mode_set(state);
@@ -256,6 +269,11 @@ class MhiAcCtrl : public climate::Climate,
256269
opdatas->energy_used_.enabled = this->energy_used_sensor_;
257270
#ifdef USE_BINARY_SENSOR
258271
opdatas->defrosting_.enabled = this->defrosting_binary_sensor_;
272+
#endif
273+
#ifdef USE_SWITCH
274+
if(this->active_mode_switch_) {
275+
this->active_mode_switch_->setup();
276+
}
259277
#endif
260278
}
261279

@@ -595,6 +613,9 @@ class MhiAcCtrl : public climate::Climate,
595613
MhiIntegratedTotalEnergy *integrated_total_energy_sensor_ = nullptr;
596614
MhiPower *power_sensor_ = nullptr;
597615
mhi_ac::Config ac_config_;
616+
#ifdef USE_SWITCH
617+
MhiActiveMode *active_mode_switch_ = nullptr;
618+
#endif
598619

599620
public:
600621
#ifdef USE_SELECT
@@ -618,6 +639,11 @@ class MhiAcCtrl : public climate::Climate,
618639
void set_power_sensor(MhiPower *sensor) {
619640
this->power_sensor_ = sensor;
620641
}
642+
#ifdef USE_SWITCH
643+
void set_active_mode_switch(MhiActiveMode *swi) {
644+
this->active_mode_switch_ = swi;
645+
}
646+
#endif
621647

622648

623649
};

esphome/components/MhiAcCtrl/switch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
async def setup_conf(config, key, hub):
3333
if key in config:
3434
conf = config[key]
35-
sens = await switch.new_switch(conf)
35+
swi = await switch.new_switch(conf)
36+
cg.add(getattr(hub, f"set_{key}_switch")(swi))
3637

3738

3839
async def to_code(config):

0 commit comments

Comments
 (0)