Files
esphome-wake-up-light-ii/components/uart/switch/__init__.py
T
mangavalkandClaude Opus 5.5 a8f6bb3ca5 Fit this lamp: 4 alarms, full data model, knock snooze, server use
Decode DP127 as 48 bytes (4 alarms), plus DP128, DP107 and DP126, from Tuya's
data model. Add day switches, alarm and sunset controls, a knock sensor on PA18,
display schedule, bedtime fade, radio timer and grouped web UI. Pin LibreTiny
v1.14.0 for the OTA restart fix. Ship the patched UART as an external component
so a server needs only a short YAML.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 12:27:54 +01:00

60 lines
1.9 KiB
Python

import esphome.codegen as cg
from esphome.components import switch, uart
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_SEND_EVERY
from esphome.core import HexInt
from .. import uart_ns, validate_raw_data
DEPENDENCIES = ["uart"]
UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component)
CONF_TURN_OFF = "turn_off"
CONF_TURN_ON = "turn_on"
CONFIG_SCHEMA = (
switch.switch_schema(UARTSwitch, block_inverted=True)
.extend(
{
cv.Required(CONF_DATA): cv.Any(
validate_raw_data,
cv.Schema(
{
cv.Optional(CONF_TURN_OFF): validate_raw_data,
cv.Optional(CONF_TURN_ON): validate_raw_data,
}
),
),
cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
},
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await switch.new_switch(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
data = config[CONF_DATA]
if isinstance(data, dict):
if data_on := data.get(CONF_TURN_ON):
if isinstance(data_on, bytes):
data_on = [HexInt(x) for x in data_on]
cg.add(var.set_data_on(cg.ArrayInitializer(*data_on)))
if data_off := data.get(CONF_TURN_OFF):
if isinstance(data_off, bytes):
data_off = [HexInt(x) for x in data_off]
cg.add(var.set_data_off(cg.ArrayInitializer(*data_off)))
else:
data = config[CONF_DATA]
if isinstance(data, bytes):
data = [HexInt(x) for x in data]
cg.add(var.set_data_on(cg.ArrayInitializer(*data)))
cg.add(var.set_single_state(True))
if CONF_SEND_EVERY in config:
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))