diff --git a/select/__init__.py b/select/__init__.py index 497e8f0..5423bfc 100644 --- a/select/__init__.py +++ b/select/__init__.py @@ -16,6 +16,9 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional("SOURCE"): select.select_schema( tw7100Select ), + cv.Optional("LUMINANCE"): select.select_schema( + tw7100Select + ), } ) @@ -25,7 +28,12 @@ async def to_code(config): if "SOURCE" in config: select_ = await select.new_select(config["SOURCE"], options=[]) - cg.add(select_.set_disabled_by_default(True)) cg.add(select_.set_cmd("SOURCE")) cg.add(select_.set_tw7100_parent(parent)) cg.add(parent.set_set_source(select_)) + + if "LUMINANCE" in config: + select_ = await select.new_select(config["LUMINANCE"], options=[]) + cg.add(select_.set_cmd("LUMINANCE")) + cg.add(select_.set_tw7100_parent(parent)) + cg.add(parent.set_set_luminance(select_)) diff --git a/select/tw7100_select.cpp b/select/tw7100_select.cpp index 500b6df..f197b9d 100644 --- a/select/tw7100_select.cpp +++ b/select/tw7100_select.cpp @@ -1,3 +1,4 @@ +#include "../tw7100.h" #include "tw7100_select.h" #include "esphome/core/log.h" #include "esphome/core/defines.h" @@ -6,18 +7,26 @@ namespace esphome { namespace tw7100 { -std::vector get_map_keys(std::map map_) { +std::vector get_map_keys(std::map map_) { std::vector map_keys; for (auto element : map_) { map_keys.push_back(element.first); } return map_keys; }; +std::vector get_map_values(std::map map_) { + std::vector map_values; + for (auto element : map_) { map_values.push_back(element.second); } + return map_values; +}; + void tw7100Select::setup() { static const char *const TAG = "tw7100Select::setup()"; if (cmd_ == "SOURCE") { - traits.set_options(get_map_keys(sources)); + traits.set_options(get_map_values(sources)); + } else if (cmd_ == "LUMINANCE") { + traits.set_options(get_map_values(luminance)); } - ESP_LOGV(TAG, "control done"); + ESP_LOGV(TAG, "setup done"); } void tw7100Select::dump_config() { @@ -29,10 +38,19 @@ void tw7100Select::dump_config() { void tw7100Select::control(const std::string &value) { static const char *const TAG = "tw7100Select::control()"; + std::string param; if (cmd_ == "SOURCE") { - uint8_t source_number = sources[value]; - ESP_LOGV("", "Source selection: %s, sending code %02x", value.c_str(), source_number); + for (auto element : sources) { if (element.second == value) { param = element.first; break; } } + } else if (cmd_ == "LUMINANCE") { + for (auto element : luminance) { if (element.second == value) { param = element.first; break; } } } + + 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"); }; diff --git a/select/tw7100_select.h b/select/tw7100_select.h index fcb4843..8460223 100644 --- a/select/tw7100_select.h +++ b/select/tw7100_select.h @@ -16,13 +16,18 @@ class tw7100Select : public select::Select, public Component { void set_options(std::vector options) { this->traits.set_options(options); } void set_tw7100_parent(tw7100Component *parent) { this->parent_ = parent; } void set_cmd(std::string cmd) { this->cmd_ = cmd; } + std::map sources = { + {"30", "HDMI1"}, + {"53", "LAN"}, + {"A0", "HDMI2"} + }; + std::map luminance = { + {"00", "Normal"}, + {"01", "Eco"}, + {"02", "Medium"} + }; protected: - std::map sources = { - {"HDMI1", 0x30}, - {"LAN", 0x53}, - {"HDMI2", 0xA0} - }; void control(const std::string &value) override; std::string cmd_; tw7100Component *parent_; diff --git a/tw7100.cpp b/tw7100.cpp index e5e74cf..28f070a 100644 --- a/tw7100.cpp +++ b/tw7100.cpp @@ -1,4 +1,5 @@ #include "tw7100.h" +#include "select/tw7100_select.h" #include "esphome/core/log.h" #include "esphome/core/defines.h" #include "esphome/core/helpers.h" @@ -21,7 +22,9 @@ std::vector pwr_on_query_cmds{ "QC?", "CORRECTMET?", "ASPECT?", +*/ "LUMINANCE?", +/* "OVSCAN?", */ // @@ -95,6 +98,7 @@ void tw7100Component::setup() { static const char *const TAG = "setup()"; ESP_LOGV(TAG, "SETUP"); source_select_->setup(); + luminance_select_->setup(); }; void tw7100Component::update() { @@ -199,6 +203,9 @@ void tw7100Component::update_sensor(std::pair data) { ESP_LOGV(TAG, "updating luminance sensors"); int value = std::stoi(value_string); luminance_level_->publish_state(value); + auto call = luminance_select_->make_call(); + call.set_option(luminance_select_->luminance[value_string]); + call.perform(); } else if (cmd == "SOURCELIST") { std::string unparsed_result = value_string; int first_token_end; @@ -231,7 +238,7 @@ void tw7100Component::update_sensor(std::pair data) { } else if (cmd == "SOURCE") { ESP_LOGV(TAG, "updating source select"); auto call = source_select_->make_call(); - call.set_option(value_string); + call.set_option(source_select_->sources[value_string]); call.perform(); } else { ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str()); diff --git a/tw7100.h b/tw7100.h index 63eb619..3a191e2 100644 --- a/tw7100.h +++ b/tw7100.h @@ -42,6 +42,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { void set_set_power(switch_::Switch *switch_) { this->power_switch_ = switch_; } void set_set_btaudio(switch_::Switch *switch_) { this->btaudio_switch_ = switch_; } void set_set_source(tw7100::tw7100Select *source_) { this->source_select_ = source_; } + void set_set_luminance(tw7100::tw7100Select *luminance_) { this->luminance_select_ = luminance_; } protected: binary_sensor::BinarySensor *powered_{nullptr}; @@ -54,6 +55,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice { switch_::Switch *power_switch_{nullptr}; switch_::Switch *btaudio_switch_{nullptr}; tw7100::tw7100Select *source_select_{nullptr}; + tw7100::tw7100Select *luminance_select_{nullptr}; }; } // namespace tw7100