Add luminance select

This commit is contained in:
sqozz 2024-09-17 00:58:39 +02:00
parent 482142094c
commit 189fd539f1
5 changed files with 52 additions and 12 deletions

View file

@ -16,6 +16,9 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional("SOURCE"): select.select_schema( cv.Optional("SOURCE"): select.select_schema(
tw7100Select tw7100Select
), ),
cv.Optional("LUMINANCE"): select.select_schema(
tw7100Select
),
} }
) )
@ -25,7 +28,12 @@ async def to_code(config):
if "SOURCE" in config: if "SOURCE" in config:
select_ = await select.new_select(config["SOURCE"], options=[]) 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_cmd("SOURCE"))
cg.add(select_.set_tw7100_parent(parent)) cg.add(select_.set_tw7100_parent(parent))
cg.add(parent.set_set_source(select_)) 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_))

View file

@ -1,3 +1,4 @@
#include "../tw7100.h"
#include "tw7100_select.h" #include "tw7100_select.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
@ -6,18 +7,26 @@
namespace esphome { namespace esphome {
namespace tw7100 { namespace tw7100 {
std::vector<std::string> get_map_keys(std::map<std::string, uint8_t> map_) { std::vector<std::string> get_map_keys(std::map<std::string, std::string> map_) {
std::vector<std::string> map_keys; std::vector<std::string> map_keys;
for (auto element : map_) { map_keys.push_back(element.first); } for (auto element : map_) { map_keys.push_back(element.first); }
return map_keys; return map_keys;
}; };
std::vector<std::string> get_map_values(std::map<std::string, std::string> map_) {
std::vector<std::string> map_values;
for (auto element : map_) { map_values.push_back(element.second); }
return map_values;
};
void tw7100Select::setup() { void tw7100Select::setup() {
static const char *const TAG = "tw7100Select::setup()"; static const char *const TAG = "tw7100Select::setup()";
if (cmd_ == "SOURCE") { 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() { void tw7100Select::dump_config() {
@ -29,10 +38,19 @@ void tw7100Select::dump_config() {
void tw7100Select::control(const std::string &value) { void tw7100Select::control(const std::string &value) {
static const char *const TAG = "tw7100Select::control()"; static const char *const TAG = "tw7100Select::control()";
std::string param;
if (cmd_ == "SOURCE") { if (cmd_ == "SOURCE") {
uint8_t source_number = sources[value]; for (auto element : sources) { if (element.second == value) { param = element.first; break; } }
ESP_LOGV("", "Source selection: %s, sending code %02x", value.c_str(), source_number); } 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"); ESP_LOGV(TAG, "control done");
}; };

View file

@ -16,13 +16,18 @@ class tw7100Select : public select::Select, public Component {
void set_options(std::vector<std::string> options) { this->traits.set_options(options); } void set_options(std::vector<std::string> options) { this->traits.set_options(options); }
void set_tw7100_parent(tw7100Component *parent) { this->parent_ = parent; } void set_tw7100_parent(tw7100Component *parent) { this->parent_ = parent; }
void set_cmd(std::string cmd) { this->cmd_ = cmd; } void set_cmd(std::string cmd) { this->cmd_ = cmd; }
std::map<std::string, std::string> sources = {
{"30", "HDMI1"},
{"53", "LAN"},
{"A0", "HDMI2"}
};
std::map<std::string, std::string> luminance = {
{"00", "Normal"},
{"01", "Eco"},
{"02", "Medium"}
};
protected: protected:
std::map<std::string, uint8_t> sources = {
{"HDMI1", 0x30},
{"LAN", 0x53},
{"HDMI2", 0xA0}
};
void control(const std::string &value) override; void control(const std::string &value) override;
std::string cmd_; std::string cmd_;
tw7100Component *parent_; tw7100Component *parent_;

View file

@ -1,4 +1,5 @@
#include "tw7100.h" #include "tw7100.h"
#include "select/tw7100_select.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
@ -21,7 +22,9 @@ std::vector<std::string> pwr_on_query_cmds{
"QC?", "QC?",
"CORRECTMET?", "CORRECTMET?",
"ASPECT?", "ASPECT?",
*/
"LUMINANCE?", "LUMINANCE?",
/*
"OVSCAN?", "OVSCAN?",
*/ */
// //
@ -95,6 +98,7 @@ void tw7100Component::setup() {
static const char *const TAG = "setup()"; static const char *const TAG = "setup()";
ESP_LOGV(TAG, "SETUP"); ESP_LOGV(TAG, "SETUP");
source_select_->setup(); source_select_->setup();
luminance_select_->setup();
}; };
void tw7100Component::update() { void tw7100Component::update() {
@ -199,6 +203,9 @@ void tw7100Component::update_sensor(std::pair<std::string, std::string> data) {
ESP_LOGV(TAG, "updating luminance sensors"); ESP_LOGV(TAG, "updating luminance sensors");
int value = std::stoi(value_string); int value = std::stoi(value_string);
luminance_level_->publish_state(value); 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") { } else if (cmd == "SOURCELIST") {
std::string unparsed_result = value_string; std::string unparsed_result = value_string;
int first_token_end; int first_token_end;
@ -231,7 +238,7 @@ void tw7100Component::update_sensor(std::pair<std::string, std::string> data) {
} else if (cmd == "SOURCE") { } else if (cmd == "SOURCE") {
ESP_LOGV(TAG, "updating source select"); ESP_LOGV(TAG, "updating source select");
auto call = source_select_->make_call(); auto call = source_select_->make_call();
call.set_option(value_string); call.set_option(source_select_->sources[value_string]);
call.perform(); call.perform();
} else { } else {
ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str()); ESP_LOGW(TAG, "Command %s unknown, skipping updating sensor with value", cmd.c_str());

View file

@ -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_power(switch_::Switch *switch_) { this->power_switch_ = switch_; }
void set_set_btaudio(switch_::Switch *switch_) { this->btaudio_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_source(tw7100::tw7100Select *source_) { this->source_select_ = source_; }
void set_set_luminance(tw7100::tw7100Select *luminance_) { this->luminance_select_ = luminance_; }
protected: protected:
binary_sensor::BinarySensor *powered_{nullptr}; binary_sensor::BinarySensor *powered_{nullptr};
@ -54,6 +55,7 @@ class tw7100Component : public PollingComponent, public uart::UARTDevice {
switch_::Switch *power_switch_{nullptr}; switch_::Switch *power_switch_{nullptr};
switch_::Switch *btaudio_switch_{nullptr}; switch_::Switch *btaudio_switch_{nullptr};
tw7100::tw7100Select *source_select_{nullptr}; tw7100::tw7100Select *source_select_{nullptr};
tw7100::tw7100Select *luminance_select_{nullptr};
}; };
} // namespace tw7100 } // namespace tw7100