Files
ESPHomeMarlin2/__init__.py
JonatanRek 270a92ba06 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.
2026-04-07 08:19:25 +02:00

67 lines
2.1 KiB
Python

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import (
CONF_ID,
CONF_VALUE,
)
from esphome import automation
from esphome.core import coroutine_with_priority
CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ["uart"]
MULTI_CONF = False
CONF_MARLIN2_ID = "marlin2_id"
CONF_MAX_SD_FILES = "max_sd_files"
marlin2_ns = cg.esphome_ns.namespace("marlin2")
Marlin2 = marlin2_ns.class_("Marlin2", cg.PollingComponent, uart.UARTDevice)
WriteAction = marlin2_ns.class_("WriteAction", automation.Action)
PrintFileAction = marlin2_ns.class_("PrintFileAction", automation.Action)
CONFIG_SCHEMA = cv.All(
cv.Schema({
cv.GenerateID(): cv.declare_id(Marlin2),
cv.Optional(CONF_MAX_SD_FILES, default=20): cv.int_range(min=1, max=255),
})
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA),
)
OPERATION_BASE_SCHEMA = cv.Schema({
cv.GenerateID(): cv.use_id(Marlin2),
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
})
@automation.register_action(
"marlin2.write",
WriteAction,
OPERATION_BASE_SCHEMA,
)
async def marlin2_write_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
cg.add(var.set_value(template_))
return var
@automation.register_action(
"marlin2.print_file",
PrintFileAction,
OPERATION_BASE_SCHEMA,
)
async def marlin2_print_file_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
cg.add(var.set_value(template_))
return var
@coroutine_with_priority(100.0)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
cg.add(var.set_max_sd_files(config[CONF_MAX_SD_FILES]))