Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Auto & Sleep from switches to presets #8

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ An ESPHome component for the Winix C545 air purifier.
- Full local control of the air purifier via Home Assistant or MQTT.
- Physical device controls remain functional and changes are immediately reflected in the frontend.
- AQI, AQI indicator, filter age, filter lifetime and light intensity sensors.
- Switches to control Plasmawave, Auto and Sleep modes.
- Switch to control Plasmawave.
- Auto and Sleep modes are implemented as fan presets.
- Piggybacks on the OEM protocol with minimal hardware modifications required.
- The OEM app can (theoretically) remain functional.

Expand Down Expand Up @@ -105,8 +106,4 @@ switch:
- platform: winix_c545
plasmawave:
name: Plasmawave
auto:
name: Auto
sleep:
name: Sleep
```
27 changes: 0 additions & 27 deletions components/winix_c545/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@

WinixC545PlasmawaveSwitch = winix_c545_ns.class_(
"WinixC545PlasmawaveSwitch", switch.Switch)
WinixC545AutoSwitch = winix_c545_ns.class_(
"WinixC545AutoSwitch", switch.Switch)
WinixC545SleepSwitch = winix_c545_ns.class_(
"WinixC545SleepSwitch", switch.Switch)

# TODO auto and sleep should be presets not switches but fan doesn't support presets currently
CONF_PLASMAWAVE = "plasmawave"
CONF_AUTO = "auto"
CONF_SLEEP = "sleep"

CONFIG_SCHEMA = cv.Schema(
{
Expand All @@ -26,16 +19,6 @@
WinixC545PlasmawaveSwitch,
device_class=DEVICE_CLASS_SWITCH,
icon="mdi:lightning-bolt-outline",
),
cv.Optional(CONF_AUTO): switch.switch_schema(
WinixC545AutoSwitch,
device_class=DEVICE_CLASS_SWITCH,
icon="mdi:auto-mode",
),
cv.Optional(CONF_SLEEP): switch.switch_schema(
WinixC545SleepSwitch,
device_class=DEVICE_CLASS_SWITCH,
icon="mdi:sleep",
)
}
)
Expand All @@ -48,13 +31,3 @@ async def to_code(config) -> None:
sw = await switch.new_switch(switch_config)
await cg.register_parented(sw, config[CONF_WINIX_C545_ID])
cg.add(component.set_plasmawave_switch(sw))

if switch_config := config.get(CONF_AUTO):
sw = await switch.new_switch(switch_config)
await cg.register_parented(sw, config[CONF_WINIX_C545_ID])
cg.add(component.set_auto_switch(sw))

if switch_config := config.get(CONF_SLEEP):
sw = await switch.new_switch(switch_config)
await cg.register_parented(sw, config[CONF_WINIX_C545_ID])
cg.add(component.set_sleep_switch(sw))
59 changes: 35 additions & 24 deletions components/winix_c545/winix_c545.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,6 @@ void WinixC545Component::publish_state_() {
this->plasmawave_switch_->publish_state(state);
break;
}

case StateKey::Auto: {
// Auto
if (this->auto_switch_ == nullptr)
continue;

bool state = value == 1;
if (state != this->auto_switch_->state)
this->auto_switch_->publish_state(state);
break;
}

case StateKey::Speed: {
// Sleep is a speed value
if (this->sleep_switch_ == nullptr)
continue;

bool state = value == 6;
if (state != this->sleep_switch_->state)
this->sleep_switch_->publish_state(state);
break;
}
}
}

Expand Down Expand Up @@ -438,8 +416,6 @@ void WinixC545Component::dump_config() {

#ifdef USE_SWITCH
LOG_SWITCH(" ", "Plasmawave Switch", this->plasmawave_switch_);
LOG_SWITCH(" ", "Auto Switch", this->auto_switch_);
LOG_SWITCH(" ", "Sleep Switch", this->sleep_switch_);
#endif
}

Expand Down Expand Up @@ -503,6 +479,29 @@ void WinixC545Fan::update_state(const WinixStateMap &states) {

// Speed has changed, publish
this->speed = speed;

// Set preset mode to Sleep if speed indicates sleep and Auto is not enabled
if (this->preset_mode != PRESET_AUTO)
this->preset_mode = (value == 6) ? PRESET_SLEEP : PRESET_NONE;

publish = true;

break;
}

case StateKey::Auto: {
// Auto
std::string preset_mode = this->preset_mode;
if (value == 1)
preset_mode = PRESET_AUTO;
else if (this->preset_mode == PRESET_AUTO)
preset_mode = PRESET_NONE;

if (preset_mode == this->preset_mode)
continue;

// Preset has changed, publish
this->preset_mode = preset_mode;
publish = true;

break;
Expand All @@ -529,6 +528,18 @@ void WinixC545Fan::control(const fan::FanCall &call) {
states.emplace(StateKey::Speed, this->speed == 4 ? 5 : this->speed);
}

if (this->preset_mode != call.get_preset_mode()) {
this->preset_mode = call.get_preset_mode();

// Update auto mode
if (this->preset_mode == PRESET_AUTO)
states.emplace(StateKey::Auto, 1);

// Set sleep mode
if (this->preset_mode == PRESET_SLEEP)
states.emplace(StateKey::Speed, 6);
}

this->parent_->write_state(states);
this->publish_state();
}
Expand Down
27 changes: 11 additions & 16 deletions components/winix_c545/winix_c545.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ class WinixC545Component : public uart::UARTDevice, public Component {

#ifdef USE_SWITCH
SUB_SWITCH(plasmawave)
// TODO the following belong as presets, not switches
SUB_SWITCH(auto)
SUB_SWITCH(sleep)
#endif

public:
Expand Down Expand Up @@ -121,16 +118,25 @@ class WinixC545Component : public uart::UARTDevice, public Component {

class WinixC545Fan : public fan::Fan, public Parented<WinixC545Component> {
public:
fan::FanTraits get_traits() override {
WinixC545Fan() {
// Only support speed control with 4 levels: Low, Med, High, Turbo
return fan::FanTraits(false, true, false, 4);
this->traits_ = fan::FanTraits(false, true, false, 4);
// Add presets
this->traits_.set_supported_preset_modes({PRESET_AUTO, PRESET_SLEEP});
}

fan::FanTraits get_traits() override { return this->traits_; }

void dump_config();
void update_state(const WinixStateMap &);

protected:
const std::string PRESET_NONE{""};
const std::string PRESET_SLEEP{"Sleep"};
const std::string PRESET_AUTO{"Auto"};

void control(const fan::FanCall &call) override;
fan::FanTraits traits_;
};

class WinixC545Switch : public switch_::Switch, public Parented<WinixC545Component> {
Expand All @@ -150,16 +156,5 @@ class WinixC545PlasmawaveSwitch : public WinixC545Switch {
WinixC545PlasmawaveSwitch() : WinixC545Switch(StateKey::Plasmawave) {}
};

class WinixC545AutoSwitch : public WinixC545Switch {
public:
WinixC545AutoSwitch() : WinixC545Switch(StateKey::Auto, 1, 2) {}
};

class WinixC545SleepSwitch : public WinixC545Switch {
public:
// Sleep switch operates on fan speed, switch to low when turned off
WinixC545SleepSwitch() : WinixC545Switch(StateKey::Speed, 6, 1) {}
};

} // namespace winix_c545
} // namespace esphome
6 changes: 1 addition & 5 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ text_sensor:
switch:
- platform: winix_c545
plasmawave:
name: Plasmawave
auto:
name: Auto
sleep:
name: Sleep
name: Plasmawave