option to show in dump_config

This commit is contained in:
Gábor Poczkodi 2024-11-06 19:51:06 +01:00
parent e2d81b5634
commit 78e620aae6
4 changed files with 35 additions and 19 deletions

View file

@ -8,6 +8,7 @@ from esphome.const import (
)
CODEOWNERS = ["@gabest11"]
CONF_SHOW_IN_LOGCONFIG = "show_in_logconfig"
CONF_SHOW_SECRETS = "show_secrets"
store_yaml_ns = cg.esphome_ns.namespace("store_yaml")
@ -16,6 +17,7 @@ StoreYamlComponent = store_yaml_ns.class_("StoreYamlComponent", cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(StoreYamlComponent),
cv.Optional(CONF_SHOW_IN_LOGCONFIG, default=True): cv.boolean,
cv.Optional(CONF_SHOW_SECRETS, default=False): cv.boolean,
}
)
@ -24,22 +26,23 @@ CONFIG_SCHEMA = cv.Schema(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
store_yaml = await cg.register_component(var, config)
cg.add(store_yaml.set_show_in_logconfig(config[CONF_SHOW_IN_LOGCONFIG]))
yaml = yaml_util.load_yaml(CORE.config_path)
dump = yaml_util.dump(yaml, show_secrets=config[CONF_SHOW_SECRETS])
cg.add(store_yaml.set(dump))
cg.add(store_yaml.set_yaml(dump))
DumpAction = store_yaml_ns.class_("DumpAction", automation.Action)
LogAction = store_yaml_ns.class_("LogAction", automation.Action)
DUMP_ACTION_SCHEMA = automation.maybe_simple_id(
LOG_ACTION_SCHEMA = automation.maybe_simple_id(
{cv.GenerateID(): cv.use_id(StoreYamlComponent)}
)
@automation.register_action(
"store_yaml.dump", DumpAction, DUMP_ACTION_SCHEMA
"store_yaml.log", LogAction, LOG_ACTION_SCHEMA
)
async def dump_yaml_action_to_code(config, action_id, template_arg, args):
async def log_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var

View file

@ -6,11 +6,16 @@ namespace store_yaml {
static const char *const TAG = "store_yaml";
void StoreYamlComponent::set(const std::string &yaml) { this->yaml_ = yaml; }
void StoreYamlComponent::dump_config() {
if (this->show_) {
ESP_LOGCONFIG(TAG, "YAML:");
this->log(true);
}
}
std::string StoreYamlComponent::get() const { return this->yaml_; }
std::string StoreYamlComponent::get_yaml() const { return this->yaml_; }
void StoreYamlComponent::dump() const {
void StoreYamlComponent::log(bool dump_config) const {
const char *s = this->yaml_.c_str();
while (*s) {
const char *e = s;
@ -20,7 +25,12 @@ void StoreYamlComponent::dump() const {
if (e > s) {
if (e[-1] == '\n')
e--;
ESP_LOGI(TAG, "%s", std::string(s, e - s).c_str());
std::string row = std::string(s, e - s);
if (dump_config) {
ESP_LOGCONFIG(TAG, "%s", row.c_str());
} else {
ESP_LOGI(TAG, "%s", row.c_str());
}
}
s = tmp;
}

View file

@ -6,17 +6,20 @@
namespace esphome {
namespace store_yaml {
class StoreYamlComponent : public Component {
bool show_{false};
std::string yaml_;
public:
void set(const std::string &yaml);
std::string get() const;
void dump() const;
void dump_config() override;
void set_show_in_logconfig(bool show) { this->show_ = show; }
void set_yaml(const std::string &yaml) { this->yaml_ = yaml; }
std::string get_yaml() const;
void log(bool dump_config = false) const;
};
template<typename... Ts> class DumpAction : public Action<Ts...>, public Parented<StoreYamlComponent> {
template<typename... Ts> class LogAction : public Action<Ts...>, public Parented<StoreYamlComponent> {
public:
void play(Ts... x) override { this->parent_->dump(); }
void play(Ts... x) override { this->parent_->log(); }
};
} // namespace store_yaml

View file

@ -1,9 +1,9 @@
api:
services:
- service: 'store_yaml_dump'
- service: 'store_yaml_log'
then:
- store_yaml.dump
- store_yaml.log
store_yaml:
show_in_logconfig: True
show_secrets: True