esphome-tw7100/select/tw7100_select.cpp
2024-09-17 00:58:39 +02:00

59 lines
1.7 KiB
C++

#include "../tw7100.h"
#include "tw7100_select.h"
#include "esphome/core/log.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace tw7100 {
std::vector<std::string> get_map_keys(std::map<std::string, std::string> map_) {
std::vector<std::string> map_keys;
for (auto element : map_) { map_keys.push_back(element.first); }
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() {
static const char *const TAG = "tw7100Select::setup()";
if (cmd_ == "SOURCE") {
traits.set_options(get_map_values(sources));
} else if (cmd_ == "LUMINANCE") {
traits.set_options(get_map_values(luminance));
}
ESP_LOGV(TAG, "setup done");
}
void tw7100Select::dump_config() {
static const char *const TAG = "dump_config()";
ESP_LOGCONFIG(TAG, "TW7100:");
LOG_SELECT(TAG, "tw7100Select", this);
}
void tw7100Select::control(const std::string &value) {
static const char *const TAG = "tw7100Select::control()";
std::string param;
if (cmd_ == "SOURCE") {
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");
};
} // namespace tw7100
} // namespace esphome