import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import uart
from esphome.const import CONF_ID

DEPENDENCIES = ["uart"]

CONF_HAUSLANE_ID = "hauslane_id"
CONF_PIN_TIMER = "pin_timer"
CONF_PIN_UP = "pin_up"
CONF_PIN_DOWN = "pin_down"
CONF_PIN_LIGHT = "pin_light"
CONF_PIN_POWER = "pin_power"

MULTI_CONF = True


hauslane_ns = cg.esphome_ns.namespace("hauslane")
Hauslane = hauslane_ns.class_(
	"Hauslane", cg.Component, uart.UARTDevice
)
HauslaneChild = hauslane_ns.class_("HauslaneChild")

HAUSLANE_SCHEMA = cv.Schema(
	{
		cv.GenerateID(CONF_HAUSLANE_ID): cv.use_id(Hauslane),
	}
)

CONFIG_SCHEMA = (
	cv.Schema({
		cv.GenerateID(): cv.declare_id(Hauslane),
		cv.Optional(CONF_PIN_TIMER): pins.gpio_output_pin_schema,
		cv.Optional(CONF_PIN_UP): pins.gpio_output_pin_schema,
		cv.Optional(CONF_PIN_DOWN): pins.gpio_output_pin_schema,
		cv.Optional(CONF_PIN_LIGHT): pins.gpio_output_pin_schema,
		cv.Optional(CONF_PIN_POWER): pins.gpio_output_pin_schema,
	})
	.extend(cv.COMPONENT_SCHEMA)
	.extend(uart.UART_DEVICE_SCHEMA)
)

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)

	if CONF_PIN_TIMER in config:
		pin_timer = await cg.gpio_pin_expression(config[CONF_PIN_TIMER])
		cg.add(var.set_pin_timer(pin_timer))
	if CONF_PIN_UP in config:
		pin_up = await cg.gpio_pin_expression(config[CONF_PIN_UP])
		cg.add(var.set_pin_up(pin_up))
	if CONF_PIN_DOWN in config:
		pin_down= await cg.gpio_pin_expression(config[CONF_PIN_DOWN])
		cg.add(var.set_pin_down(pin_down))
	if CONF_PIN_LIGHT in config:
		pin_light = await cg.gpio_pin_expression(config[CONF_PIN_LIGHT])
		cg.add(var.set_pin_light(pin_light))
	if CONF_PIN_POWER in config:
		pin_power = await cg.gpio_pin_expression(config[CONF_PIN_POWER])
		cg.add(var.set_pin_power(pin_power))
