diff --git a/number/__init__.py b/number/__init__.py new file mode 100644 index 0000000..aea2681 --- /dev/null +++ b/number/__init__.py @@ -0,0 +1,30 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import number +from esphome.const import CONF_POWER, ENTITY_CATEGORY_CONFIG, CONF_ID + +from .. import tw7100, tw7100_ns, CONF_TW7100_ID + +DEPENDENCIES = ["tw7100"] +CODEOWNERS = ["@sqozz"] + +tw7100Number = tw7100_ns.class_("tw7100Number", number.Number, cg.Component) + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.use_id(tw7100), + cv.Optional("VOL"): number.number_schema( + tw7100Number + ), + } +) + + +async def to_code(config): + parent = await cg.get_variable(config[CONF_ID]) + + if "VOL" in config: + number_ = await number.new_number(config["VOL"], min_value=0, max_value=255, step=1) + cg.add(number_.set_cmd("VOL")) + cg.add(number_.set_tw7100_parent(parent)) + cg.add(parent.set_set_volume(number_)) diff --git a/number/tw7100_number.cpp b/number/tw7100_number.cpp new file mode 100644 index 0000000..da5df5c --- /dev/null +++ b/number/tw7100_number.cpp @@ -0,0 +1,42 @@ +#include "../tw7100.h" +#include "tw7100_number.h" +#include "esphome/core/log.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" + +namespace esphome { +namespace tw7100 { + +void tw7100Number::setup() { + static const char *const TAG = "tw7100Number::setup()"; + if (cmd_ == "VOL") { + ESP_LOGV(TAG, "setup VOL"); + } + ESP_LOGV(TAG, "setup done"); +} + +void tw7100Number::dump_config() { + static const char *const TAG = "dump_config()"; + ESP_LOGCONFIG(TAG, "TW7100:"); + LOG_NUMBER(TAG, "tw7100Number", this); +} + +void tw7100Number::control(float value) { + static const char *const TAG = "tw7100Number::control()"; + + std::string param; + if (cmd_ == "VOL") { + ESP_LOGV(TAG, "VOL: "); + } + + if (param.length() > 0 && state != value) { + parent_->push_cmd(cmd_, param); + ESP_LOGV("", "CMD: %s, PARAM: %s", cmd_.c_str(), param.c_str()); + } + + publish_state(value); + ESP_LOGV(TAG, "control done"); +}; + +} // namespace tw7100 +} // namespace esphome diff --git a/number/tw7100_number.h b/number/tw7100_number.h new file mode 100644 index 0000000..bcfff5a --- /dev/null +++ b/number/tw7100_number.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "esphome/core/component.h" +#include "esphome/components/number/number.h" + +namespace esphome { +namespace tw7100 { + +class tw7100Component; + +class tw7100Number : public number::Number, public Component { + public: + void setup() override; + void dump_config() override; + void set_tw7100_parent(tw7100Component *parent) { this->parent_ = parent; } + void set_cmd(std::string cmd) { this->cmd_ = cmd; } + + protected: + void control(float value) override; + tw7100Component *parent_; + std::string cmd_; +}; + +} // namespace tw7100 +} // namespace esphome diff --git a/tw7100.h b/tw7100.h index 6aa3896..aabdde1 100644 --- a/tw7100.h +++ b/tw7100.h @@ -7,8 +7,10 @@ #include "esphome/components/text_sensor/text_sensor.h" #include "esphome/components/switch/switch.h" #include "esphome/components/select/select.h" +#include "esphome/components/number/number.h" #include "esphome/components/uart/uart.h" #include "select/tw7100_select.h" +#include "number/tw7100_number.h" namespace esphome { namespace tw7100 { @@ -44,6 +46,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_mute(switch_::Switch *switch_) { this->mute_switch_ = switch_; } void set_set_source(tw7100::tw7100Select *source_) { this->source_select_ = source_; } void set_set_luminance(tw7100::tw7100Select *luminance_) { this->luminance_select_ = luminance_; } + void set_set_volume(tw7100::tw7100Number *number_) { this->volume_number_ = number_; } protected: binary_sensor::BinarySensor *powered_{nullptr}; @@ -58,6 +61,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { switch_::Switch *mute_switch_{nullptr}; tw7100::tw7100Select *source_select_{nullptr}; tw7100::tw7100Select *luminance_select_{nullptr}; + tw7100::tw7100Number *volume_number_{nullptr}; }; } // namespace tw7100