mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 15:38:11 +01:00
store_yaml: this component can be used to store the flattened yaml in the firmware, to be retrieved in case the original was lost
This commit is contained in:
parent
01497c891d
commit
751601ce74
3 changed files with 98 additions and 0 deletions
45
esphome/components/store_yaml/__init__.py
Normal file
45
esphome/components/store_yaml/__init__.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation, yaml_util
|
||||
from esphome.core import CORE
|
||||
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@gabest11"]
|
||||
CONF_SHOW_SECRETS = "show_secrets"
|
||||
|
||||
store_yaml_ns = cg.esphome_ns.namespace("store_yaml")
|
||||
StoreYamlComponent = store_yaml_ns.class_("StoreYamlComponent", cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(StoreYamlComponent),
|
||||
cv.Optional(CONF_SHOW_SECRETS, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
store_yaml = await cg.register_component(var, config)
|
||||
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))
|
||||
|
||||
|
||||
DumpAction = store_yaml_ns.class_("DumpAction", automation.Action)
|
||||
|
||||
DUMP_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||
{cv.GenerateID(): cv.use_id(StoreYamlComponent)}
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"store_yaml.dump", DumpAction, DUMP_ACTION_SCHEMA
|
||||
)
|
||||
async def dump_yaml_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
|
30
esphome/components/store_yaml/store_yaml.cpp
Normal file
30
esphome/components/store_yaml/store_yaml.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "store_yaml.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace store_yaml {
|
||||
|
||||
static const char *const TAG = "store_yaml";
|
||||
|
||||
void StoreYamlComponent::set(const std::string &yaml) { this->yaml_ = yaml; }
|
||||
|
||||
std::string StoreYamlComponent::get() const { return this->yaml_; }
|
||||
|
||||
void StoreYamlComponent::dump() const {
|
||||
const char *s = this->yaml_.c_str();
|
||||
while (*s) {
|
||||
const char *e = s;
|
||||
while (*e && *e++ != '\n')
|
||||
;
|
||||
const char *tmp = e;
|
||||
if (e > s) {
|
||||
if (e[-1] == '\n')
|
||||
e--;
|
||||
ESP_LOGI(TAG, "%s", std::string(s, e - s).c_str());
|
||||
}
|
||||
s = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace store_yaml
|
||||
} // namespace esphome
|
23
esphome/components/store_yaml/store_yaml.h
Normal file
23
esphome/components/store_yaml/store_yaml.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace store_yaml {
|
||||
class StoreYamlComponent : public Component {
|
||||
std::string yaml_;
|
||||
|
||||
public:
|
||||
void set(const std::string &yaml);
|
||||
std::string get() const;
|
||||
void dump() const;
|
||||
};
|
||||
|
||||
template<typename... Ts> class DumpAction : public Action<Ts...>, public Parented<StoreYamlComponent> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->dump(); }
|
||||
};
|
||||
|
||||
} // namespace store_yaml
|
||||
} // namespace esphome
|
Loading…
Reference in a new issue