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:
70
__init__.py
70
__init__.py
@@ -2,72 +2,43 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import uart
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_INDEX,
|
||||
CONF_SENSORS,
|
||||
CONF_HUMIDITY,
|
||||
CONF_MODEL,
|
||||
CONF_PIN,
|
||||
CONF_TEMPERATURE,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_PERCENT,
|
||||
UNIT_SECOND,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_DURATION,
|
||||
CONF_DATA,
|
||||
CONF_VALUE
|
||||
CONF_ID,
|
||||
CONF_VALUE,
|
||||
)
|
||||
from esphome import pins, automation
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
from esphome import automation
|
||||
from esphome.core import coroutine_with_priority
|
||||
|
||||
CODEOWNERS = ["@jonatanrek"]
|
||||
DEPENDENCIES = ['uart']
|
||||
CONF_MARLIN2_ID = "marlin2_id"
|
||||
DEPENDENCIES = ["uart"]
|
||||
MULTI_CONF = False
|
||||
|
||||
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
|
||||
WriteAction = cg.esphome_ns.class_("WriteAction", automation.Action)
|
||||
PrintFileAction = cg.esphome_ns.class_("PrintFileAction", automation.Action)
|
||||
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.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),
|
||||
)
|
||||
|
||||
def validate_raw_data(value):
|
||||
if isinstance(value, str):
|
||||
return value.encode("utf-8")
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
if isinstance(value, list):
|
||||
return cv.Schema([cv.hex_uint8_t])(value)
|
||||
raise cv.Invalid("data must either be a string wrapped in quotes or a list of bytes")
|
||||
|
||||
@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)
|
||||
|
||||
OPERATION_BASE_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.use_id(Marlin2),
|
||||
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
|
||||
})
|
||||
|
||||
OPERATION_BASE_SCHEMA_2 = 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)
|
||||
@@ -78,11 +49,18 @@ async def marlin2_write_to_code(config, action_id, template_arg, args):
|
||||
@automation.register_action(
|
||||
"marlin2.print_file",
|
||||
PrintFileAction,
|
||||
OPERATION_BASE_SCHEMA_2,
|
||||
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
|
||||
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]))
|
||||
|
||||
Reference in New Issue
Block a user