Refactor Marlin2 component and add new features

- Updated __init__.py to include max_sd_files configuration option.
- Refactored Marlin2 class to use new namespaces and improved structure.
- Added support for binary sensors and select components in binary_sensor.py and select.py.
- Enhanced sensor.py and text_sensor.py to include new configuration options for SD card file count and selected file.
- Improved code readability and organization across multiple files.
This commit is contained in:
JonatanRek
2026-04-07 08:19:25 +02:00
parent cf3039439f
commit 270a92ba06
8 changed files with 492 additions and 410 deletions

View File

@@ -4,35 +4,36 @@
#include "esphome/core/component.h"
#include "marlin2.h"
namespace esphome {
static const char *TAG = "marlin2";
namespace esphome::marlin2 {
template<typename... Ts> class WriteAction : public Action<Ts...> {
public:
explicit WriteAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, value)
public:
explicit WriteAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, value)
void play(Ts... x) override {
this->marlin2_->write(this->value_.value(x...));
}
void play(Ts... x) override {
this->marlin2_->write(this->value_.value(x...));
}
protected:
Marlin2 *marlin2_;
protected:
Marlin2 *marlin2_;
};
template<typename... Ts> class PrintFileAction : public Action<Ts...> {
public:
explicit PrintFileAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, value)
public:
explicit PrintFileAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
TEMPLATABLE_VALUE(std::string, value)
void play(Ts... x) override {
std::string file_name = this->marlin2_->to_dos_name(this->value_.value(x...));
ESP_LOGD(TAG, "->FILE: %s", file_name.c_str());
this->marlin2_->write(str_sprintf("M32 P !%s#", file_name.c_str()));
}
void play(Ts... x) override {
static const char *const TAG = "marlin2";
std::string file_name = this->marlin2_->to_dos_name(this->value_.value(x...));
ESP_LOGD(TAG, "->FILE: %s", file_name.c_str());
this->marlin2_->write(str_sprintf("M32 P !%s#", file_name.c_str()));
}
protected:
Marlin2 *marlin2_;
protected:
Marlin2 *marlin2_;
};
} // namespace esphome
} // namespace esphome::marlin2