Skip to content

Commit

Permalink
[haier] text_sensor and button platforms (#6780)
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed May 23, 2024
1 parent 4ab7a5d commit aed0593
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 80 deletions.
4 changes: 4 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ esphome/components/grove_tb6612fng/* @max246
esphome/components/growatt_solar/* @leeuwte
esphome/components/gt911/* @clydebarrow @jesserockz
esphome/components/haier/* @paveldn
esphome/components/haier/binary_sensor/* @paveldn
esphome/components/haier/button/* @paveldn
esphome/components/haier/sensor/* @paveldn
esphome/components/haier/text_sensor/* @paveldn
esphome/components/havells_solar/* @sourabhjaiswal
esphome/components/hbridge/fan/* @WeekendWarrior
esphome/components/hbridge/light/* @DotNetDann
Expand Down
4 changes: 2 additions & 2 deletions esphome/components/haier/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template<typename... Ts> class BeeperOffAction : public Action<Ts...> {
template<typename... Ts> class VerticalAirflowAction : public Action<Ts...> {
public:
VerticalAirflowAction(HonClimate *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(AirflowVerticalDirection, direction)
TEMPLATABLE_VALUE(hon_protocol::VerticalSwingMode, direction)
void play(Ts... x) { this->parent_->set_vertical_airflow(this->direction_.value(x...)); }

protected:
Expand All @@ -56,7 +56,7 @@ template<typename... Ts> class VerticalAirflowAction : public Action<Ts...> {
template<typename... Ts> class HorizontalAirflowAction : public Action<Ts...> {
public:
HorizontalAirflowAction(HonClimate *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(AirflowHorizontalDirection, direction)
TEMPLATABLE_VALUE(hon_protocol::HorizontalSwingMode, direction)
void play(Ts... x) { this->parent_->set_horizontal_airflow(this->direction_.value(x...)); }

protected:
Expand Down
1 change: 1 addition & 0 deletions esphome/components/haier/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
HonClimate,
)

CODEOWNERS = ["@paveldn"]
BinarySensorTypeEnum = HonClimate.enum("SubBinarySensorType", True)

# Haier sensors
Expand Down
41 changes: 41 additions & 0 deletions esphome/components/haier/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button
from ..climate import (
CONF_HAIER_ID,
HonClimate,
haier_ns,
)

CODEOWNERS = ["@paveldn"]
SelfCleaningButton = haier_ns.class_("SelfCleaningButton", button.Button)
SteriCleaningButton = haier_ns.class_("SteriCleaningButton", button.Button)


# Haier buttons
CONF_SELF_CLEANING = "self_cleaning"
CONF_STERI_CLEANING = "steri_cleaning"

# Additional icons
ICON_SPRAY_BOTTLE = "mdi:spray-bottle"

CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate),
cv.Optional(CONF_SELF_CLEANING): button.button_schema(
SelfCleaningButton,
icon=ICON_SPRAY_BOTTLE,
),
cv.Optional(CONF_STERI_CLEANING): button.button_schema(
SteriCleaningButton,
icon=ICON_SPRAY_BOTTLE,
),
}
)


async def to_code(config):
for button_type in [CONF_SELF_CLEANING, CONF_STERI_CLEANING]:
if conf := config.get(button_type):
btn = await button.new_button(conf)
await cg.register_parented(btn, config[CONF_HAIER_ID])
9 changes: 9 additions & 0 deletions esphome/components/haier/button/self_cleaning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "self_cleaning.h"

namespace esphome {
namespace haier {

void SelfCleaningButton::press_action() { this->parent_->start_self_cleaning(); }

} // namespace haier
} // namespace esphome
18 changes: 18 additions & 0 deletions esphome/components/haier/button/self_cleaning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/button/button.h"
#include "../hon_climate.h"

namespace esphome {
namespace haier {

class SelfCleaningButton : public button::Button, public Parented<HonClimate> {
public:
SelfCleaningButton() = default;

protected:
void press_action() override;
};

} // namespace haier
} // namespace esphome
9 changes: 9 additions & 0 deletions esphome/components/haier/button/steri_cleaning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "steri_cleaning.h"

namespace esphome {
namespace haier {

void SteriCleaningButton::press_action() { this->parent_->start_steri_cleaning(); }

} // namespace haier
} // namespace esphome
18 changes: 18 additions & 0 deletions esphome/components/haier/button/steri_cleaning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/button/button.h"
#include "../hon_climate.h"

namespace esphome {
namespace haier {

class SteriCleaningButton : public button::Button, public Parented<HonClimate> {
public:
SteriCleaningButton() = default;

protected:
void press_action() override;
};

} // namespace haier
} // namespace esphome
7 changes: 4 additions & 3 deletions esphome/components/haier/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
PROTOCOL_SMARTAIR2 = "SMARTAIR2"

haier_ns = cg.esphome_ns.namespace("haier")
hon_protocol_ns = haier_ns.namespace("hon_protocol")
HaierClimateBase = haier_ns.class_(
"HaierClimateBase", uart.UARTDevice, climate.Climate, cg.Component
)
Expand All @@ -63,7 +64,7 @@

CONF_HAIER_ID = "haier_id"

AirflowVerticalDirection = haier_ns.enum("AirflowVerticalDirection", True)
AirflowVerticalDirection = hon_protocol_ns.enum("VerticalSwingMode", True)
AIRFLOW_VERTICAL_DIRECTION_OPTIONS = {
"HEALTH_UP": AirflowVerticalDirection.HEALTH_UP,
"MAX_UP": AirflowVerticalDirection.MAX_UP,
Expand All @@ -73,7 +74,7 @@
"HEALTH_DOWN": AirflowVerticalDirection.HEALTH_DOWN,
}

AirflowHorizontalDirection = haier_ns.enum("AirflowHorizontalDirection", True)
AirflowHorizontalDirection = hon_protocol_ns.enum("HorizontalSwingMode", True)
AIRFLOW_HORIZONTAL_DIRECTION_OPTIONS = {
"MAX_LEFT": AirflowHorizontalDirection.MAX_LEFT,
"LEFT": AirflowHorizontalDirection.LEFT,
Expand Down Expand Up @@ -483,4 +484,4 @@ async def to_code(config):
trigger, [(cg.uint8, "code"), (cg.const_char_ptr, "message")], conf
)
# https://github.com/paveldn/HaierProtocol
cg.add_library("pavlodn/HaierProtocol", "0.9.25")
cg.add_library("pavlodn/HaierProtocol", "0.9.28")
3 changes: 2 additions & 1 deletion esphome/components/haier/haier_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void HaierClimateBase::setup() {
this->haier_protocol_.set_default_timeout_handler(
std::bind(&esphome::haier::HaierClimateBase::timeout_default_handler_, this, std::placeholders::_1));
this->set_handlers();
this->initialization();
}

void HaierClimateBase::dump_config() {
Expand Down Expand Up @@ -326,7 +327,7 @@ ClimateTraits HaierClimateBase::traits() { return traits_; }

void HaierClimateBase::control(const ClimateCall &call) {
ESP_LOGD("Control", "Control call");
if (this->protocol_phase_ < ProtocolPhases::IDLE) {
if (!this->valid_connection()) {
ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
return; // cancel the control, we cant do it without a poll answer.
}
Expand Down
3 changes: 2 additions & 1 deletion esphome/components/haier/haier_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class HaierClimateBase : public esphome::Component,
void set_supported_modes(const std::set<esphome::climate::ClimateMode> &modes);
void set_supported_swing_modes(const std::set<esphome::climate::ClimateSwingMode> &modes);
void set_supported_presets(const std::set<esphome::climate::ClimatePreset> &presets);
bool valid_connection() { return this->protocol_phase_ >= ProtocolPhases::IDLE; };
bool valid_connection() const { return this->protocol_phase_ >= ProtocolPhases::IDLE; };
size_t available() noexcept override { return esphome::uart::UARTDevice::available(); };
size_t read_array(uint8_t *data, size_t len) noexcept override {
return esphome::uart::UARTDevice::read_array(data, len) ? len : 0;
Expand Down Expand Up @@ -80,6 +80,7 @@ class HaierClimateBase : public esphome::Component,
virtual void process_phase(std::chrono::steady_clock::time_point now) = 0;
virtual haier_protocol::HaierMessage get_control_message() = 0;
virtual haier_protocol::HaierMessage get_power_message(bool state) = 0;
virtual void initialization(){};
virtual bool prepare_pending_action();
virtual void process_protocol_reset();
esphome::climate::ClimateTraits traits() override;
Expand Down
Loading

0 comments on commit aed0593

Please sign in to comment.