From 9df49c72b399a58af12ae8aa64689168cf6d1bec Mon Sep 17 00:00:00 2001 From: GrKoR Date: Fri, 8 Apr 2022 01:31:43 +0300 Subject: [PATCH] Display ON, OFF and TOGGLE action Fixes #15 --- components/aux_ac/automation.h | 35 +++++++++++++++++++++++++++++ components/aux_ac/aux_ac.h | 40 +++++++++++++++++++++++++--------- components/aux_ac/climate.py | 25 ++++++++++++++++++++- 3 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 components/aux_ac/automation.h diff --git a/components/aux_ac/automation.h b/components/aux_ac/automation.h new file mode 100644 index 0000000..c41aea3 --- /dev/null +++ b/components/aux_ac/automation.h @@ -0,0 +1,35 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "aux_ac.h" + +namespace esphome { +namespace aux_ac { + + template + class AirConDisplayOffAction : public Action + { + public: + explicit AirConDisplayOffAction(AirCon *ac) : ac_(ac) {} + + void play(Ts... x) override { this->ac_->displaySequence(AC_DISPLAY_OFF); } + + protected: + AirCon *ac_; + }; + + template + class AirConDisplayOnAction : public Action + { + public: + explicit AirConDisplayOnAction(AirCon *ac) : ac_(ac) {} + + void play(Ts... x) override { this->ac_->displaySequence(AC_DISPLAY_ON); } + + protected: + AirCon *ac_; + }; + +} // namespace aux_ac +} // namespace esphome \ No newline at end of file diff --git a/components/aux_ac/aux_ac.h b/components/aux_ac/aux_ac.h index 34c80d2..23150a0 100644 --- a/components/aux_ac/aux_ac.h +++ b/components/aux_ac/aux_ac.h @@ -2352,6 +2352,36 @@ class AirCon : public esphome::Component, public esphome::climate::Climate { return true; } + // загружает на выполнение последовательность команд на включение/выключение табло с температурой + bool displaySequence(ac_display dsp = AC_DISPLAY_ON){ + // нет смысла в последовательности, если нет коннекта с кондиционером + if (!get_has_connection()) { + _debugMsg(F("displaySequence: no pings from HVAC. It seems like no AC connected."), ESPHOME_LOG_LEVEL_ERROR, __LINE__); + return false; + } + if (dsp == AC_DISPLAY_UNTOUCHED) return false; // выходим, чтобы не тратить время + + // формируем команду + ac_command_t cmd; + _clearCommand(&cmd); // не забываем очищать, а то будет мусор + cmd.display = dsp; + // добавляем команду в последовательность + if (!commandSequence(&cmd)) return false; + + _debugMsg(F("displaySequence: loaded (display = %02X)"), ESPHOME_LOG_LEVEL_VERBOSE, __LINE__, dsp); + return true; + } + + // выключает экран + bool displayOffSequence(){ + return displaySequence(AC_DISPLAY_OFF); + } + + // включает экран + bool displayOnSequence(){ + return displaySequence(AC_DISPLAY_ON); + } + void set_period(uint32_t ms) { this->_update_period = ms; }; uint32_t get_period() { return this->_update_period; }; void set_show_action(bool show_action) { this->_show_action = show_action; }; @@ -2401,16 +2431,6 @@ class AirCon : public esphome::Component, public esphome::climate::Climate { if (get_has_connection()) getStatusBigAndSmall(); } - /* - // это экспериментальная секция для отладки функционала - static uint32_t debug_millis = millis(); - if (millis()-debug_millis > 10000){ - debug_millis = millis(); - //_debugMsg(F("Test!"), ESPHOME_LOG_LEVEL_WARN, __LINE__); - //if (_current_ac_state.power == AC_POWER_OFF) powerSequence(AC_POWER_ON); - //else powerSequence(AC_POWER_OFF); - } - */ }; }; diff --git a/components/aux_ac/climate.py b/components/aux_ac/climate.py index d5180c6..b9eec4e 100644 --- a/components/aux_ac/climate.py +++ b/components/aux_ac/climate.py @@ -2,6 +2,8 @@ import logging import esphome.config_validation as cv import esphome.codegen as cg from esphome.components import climate, uart, sensor +from esphome import automation +from esphome.automation import maybe_simple_id from esphome.const import ( CONF_ID, CONF_UART_ID, @@ -19,6 +21,7 @@ from esphome.components.climate import ( ClimatePreset, ClimateSwingMode, ) + _LOGGER = logging.getLogger(__name__) CODEOWNERS = ["@GrKoR"] @@ -35,6 +38,9 @@ aux_ac_ns = cg.esphome_ns.namespace("aux_ac") AirCon = aux_ac_ns.class_("AirCon", climate.Climate, cg.Component) Capabilities = aux_ac_ns.namespace("Constants") +AirConDisplayOffAction = aux_ac_ns.class_("AirConDisplayOffAction", automation.Action) +AirConDisplayOnAction = aux_ac_ns.class_("AirConDisplayOnAction", automation.Action) + ALLOWED_CLIMATE_MODES = { "HEAT_COOL": ClimateMode.CLIMATE_MODE_HEAT_COOL, "COOL": ClimateMode.CLIMATE_MODE_COOL, @@ -130,4 +136,21 @@ async def to_code(config): cg.add(var.set_custom_presets(config[CONF_CUSTOM_PRESETS])) if CONF_CUSTOM_FAN_MODES in config: cg.add(var.set_custom_fan_modes(config[CONF_CUSTOM_FAN_MODES])) - \ No newline at end of file + + + +DISPLAY_ACTION_SCHEMA = maybe_simple_id( + { + cv.Required(CONF_ID): cv.use_id(AirCon), + } +) + +@automation.register_action("aux_ac.display_off", AirConDisplayOffAction, DISPLAY_ACTION_SCHEMA) +async def switch_toggle_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + return cg.new_Pvariable(action_id, template_arg, paren) + +@automation.register_action("aux_ac.display_on", AirConDisplayOnAction, DISPLAY_ACTION_SCHEMA) +async def switch_toggle_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + return cg.new_Pvariable(action_id, template_arg, paren) \ No newline at end of file