import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import (
    DEVICE_CLASS_POWER,
    DEVICE_CLASS_CONNECTIVITY,
    CONF_POWER,
    CONF_ID,
)

from . import tw7100

DEPENDENCIES = ["tw7100"]


CONFIG_SCHEMA = cv.Schema(
    {
        cv.GenerateID(): cv.use_id(tw7100),
        cv.Optional("signal_detected"): binary_sensor.binary_sensor_schema(
            device_class=DEVICE_CLASS_CONNECTIVITY
        ),
        cv.Optional(CONF_POWER): binary_sensor.binary_sensor_schema(
            device_class=DEVICE_CLASS_POWER
        ),
    }
)


async def to_code(config):
    parent = await cg.get_variable(config[CONF_ID])

    if "signal_detected" in config:
        sens = await binary_sensor.new_binary_sensor(config["signal_detected"])
        cg.add(parent.set_has_signal(sens))

    if CONF_POWER in config:
        sens = await binary_sensor.new_binary_sensor(config[CONF_POWER])
        cg.add(parent.set_powered(sens))