From e4d5c140cccb1b55d9498df740b0fc6a069b1bba Mon Sep 17 00:00:00 2001 From: Pavlo Dudnytskyi Date: Mon, 19 Aug 2024 20:42:29 +0200 Subject: [PATCH] Display and health mode switches for haier platform added --- components/haier/haier_base.cpp | 14 ++++++++++++++ components/haier/haier_base.h | 13 +++++++++++++ components/haier/switch/__init__.py | 11 ++++++----- components/haier/switch/display.cpp | 11 +++++++++++ components/haier/switch/display.h | 18 ++++++++++++++++++ components/haier/switch/health_mode.cpp | 11 +++++++++++ components/haier/switch/health_mode.h | 18 ++++++++++++++++++ 7 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 components/haier/switch/display.cpp create mode 100644 components/haier/switch/display.h create mode 100644 components/haier/switch/health_mode.cpp create mode 100644 components/haier/switch/health_mode.h diff --git a/components/haier/haier_base.cpp b/components/haier/haier_base.cpp index 0bd3863..4f10192 100644 --- a/components/haier/haier_base.cpp +++ b/components/haier/haier_base.cpp @@ -353,6 +353,20 @@ void HaierClimateBase::control(const ClimateCall &call) { } } +void HaierClimateBase::set_display_switch(switch_::Switch *sw) { + this->display_switch_ = sw; + if ((this->display_switch_ != nullptr) && (this->valid_connection())) { + this->display_switch_->publish_state(this->display_status_); + } +} + +void HaierClimateBase::set_health_mode_switch(switch_::Switch *sw) { + this->health_mode_switch_ = sw; + if ((this->health_mode_switch_ != nullptr) && (this->valid_connection())) { + this->health_mode_switch_->publish_state(this->health_mode_); + } +} + void HaierClimateBase::HvacSettings::reset() { this->valid = false; this->mode.reset(); diff --git a/components/haier/haier_base.h b/components/haier/haier_base.h index c0bf878..3ca2337 100644 --- a/components/haier/haier_base.h +++ b/components/haier/haier_base.h @@ -8,6 +8,10 @@ // HaierProtocol #include +#ifdef USE_SWITCH +#include "esphome/components/switch/switch.h" +#endif + namespace esphome { namespace haier { @@ -24,6 +28,15 @@ class HaierClimateBase : public esphome::Component, public esphome::climate::Climate, public esphome::uart::UARTDevice, public haier_protocol::ProtocolStream { +#ifdef USE_SWITCH + public: + void set_display_switch(switch_::Switch *sw); + void set_health_mode_switch(switch_::Switch *sw); + + protected: + switch_::Switch *display_switch_{nullptr}; + switch_::Switch *health_mode_switch_{nullptr}; +#endif public: HaierClimateBase(); HaierClimateBase(const HaierClimateBase &) = delete; diff --git a/components/haier/switch/__init__.py b/components/haier/switch/__init__.py index 979442e..ff8450f 100644 --- a/components/haier/switch/__init__.py +++ b/components/haier/switch/__init__.py @@ -56,15 +56,16 @@ async def to_code(config): full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID]) -# for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]: -# if conf := config.get(switch_type): -# sw_var = await switch.new_switch(conf) -# await cg.register_parented(sw_var, parent) + for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]: + if conf := config.get(switch_type): + sw_var = await switch.new_switch(conf) + await cg.register_parented(sw_var, parent) + cg.add(getattr(parent, f"set_{switch_type}_switch")(sw_var)) if conf := config.get(CONF_BEEPER): if full_id.type is HonClimate: sw_var = await switch.new_switch(conf) await cg.register_parented(sw_var, parent) - cg.add(getattr(parent, f"set_beeper_switch")(sw_var)) + cg.add(getattr(parent, "set_beeper_switch")(sw_var)) else: raise ValueError("Beeper switch is only supported for hon climate") diff --git a/components/haier/switch/display.cpp b/components/haier/switch/display.cpp new file mode 100644 index 0000000..c74e071 --- /dev/null +++ b/components/haier/switch/display.cpp @@ -0,0 +1,11 @@ +#include "display.h" + +namespace esphome { +namespace haier { + +void DisplaySwitch::write_state(bool state) { + this->publish_state(state); +} + +} // namespace haier +} // namespace esphome diff --git a/components/haier/switch/display.h b/components/haier/switch/display.h new file mode 100644 index 0000000..f93ccfc --- /dev/null +++ b/components/haier/switch/display.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/switch/switch.h" +#include "../haier_base.h" + +namespace esphome { +namespace haier { + +class DisplaySwitch : public switch_::Switch, public Parented { + public: + DisplaySwitch() = default; + + protected: + void write_state(bool state) override; +}; + +} // namespace haier +} // namespace esphome diff --git a/components/haier/switch/health_mode.cpp b/components/haier/switch/health_mode.cpp new file mode 100644 index 0000000..ba91272 --- /dev/null +++ b/components/haier/switch/health_mode.cpp @@ -0,0 +1,11 @@ +#include "health_mode.h" + +namespace esphome { +namespace haier { + +void HealthModeSwitch::write_state(bool state) { + this->publish_state(state); +} + +} // namespace haier +} // namespace esphome diff --git a/components/haier/switch/health_mode.h b/components/haier/switch/health_mode.h new file mode 100644 index 0000000..cfd2aa2 --- /dev/null +++ b/components/haier/switch/health_mode.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/switch/switch.h" +#include "../haier_base.h" + +namespace esphome { +namespace haier { + +class HealthModeSwitch : public switch_::Switch, public Parented { + public: + HealthModeSwitch() = default; + + protected: + void write_state(bool state) override; +}; + +} // namespace haier +} // namespace esphome