Ejemplo n.º 1
0
def final_validate_device_schema(
    name: str,
    *,
    baud_rate: Optional[int] = None,
    require_tx: bool = False,
    require_rx: bool = False,
):
    def validate_baud_rate(value):
        if value != baud_rate:
            raise cv.Invalid(
                f"Component {name} required baud rate {baud_rate} for the uart bus"
            )
        return value

    def validate_pin(opt, device):
        def validator(value):
            if opt in device:
                raise cv.Invalid(
                    f"The uart {opt} is used both by {name} and {device[opt]}, "
                    f"but can only be used by one. Please create a new uart bus for {name}."
                )
            device[opt] = name
            return value

        return validator

    def validate_hub(hub_config):
        hub_schema = {}
        uart_id = hub_config[CONF_ID]
        devices = fv.full_config.get().data.setdefault(KEY_UART_DEVICES, {})
        device = devices.setdefault(uart_id, {})

        if require_tx:
            hub_schema[cv.Required(
                CONF_TX_PIN,
                msg=
                f"Component {name} requires this uart bus to declare a tx_pin",
            )] = validate_pin(CONF_TX_PIN, device)
        if require_rx:
            hub_schema[cv.Required(
                CONF_RX_PIN,
                msg=
                f"Component {name} requires this uart bus to declare a rx_pin",
            )] = validate_pin(CONF_RX_PIN, device)
        if baud_rate is not None:
            hub_schema[cv.Required(CONF_BAUD_RATE)] = validate_baud_rate
        return cv.Schema(hub_schema, extra=cv.ALLOW_EXTRA)(hub_config)

    return cv.Schema(
        {
            cv.Required(CONF_UART_ID):
            fv.id_declaration_match_schema(validate_hub)
        },
        extra=cv.ALLOW_EXTRA,
    )
Ejemplo n.º 2
0
def final_validate_device_schema(name: str, *, require_mosi: bool,
                                 require_miso: bool):
    hub_schema = {}
    if require_miso:
        hub_schema[cv.Required(
            CONF_MISO_PIN,
            msg=f"Component {name} requires this spi bus to declare a miso_pin",
        )] = cv.valid
    if require_mosi:
        hub_schema[cv.Required(
            CONF_MOSI_PIN,
            msg=f"Component {name} requires this spi bus to declare a mosi_pin",
        )] = cv.valid

    return cv.Schema(
        {cv.Required(CONF_SPI_ID): fv.id_declaration_match_schema(hub_schema)},
        extra=cv.ALLOW_EXTRA,
    )
Ejemplo n.º 3
0
    ]

    if platform in PWM_BAD:
        raise cv.Invalid(
            f"Component rtttl cannot use {platform} as output component")

    if platform not in PWM_GOOD:
        _LOGGER.warning(
            "Component rtttl is not known to work with the selected output type. "
            "Make sure this output supports custom frequency output method.")


FINAL_VALIDATE_SCHEMA = cv.Schema(
    {
        cv.Required(CONF_OUTPUT):
        fv.id_declaration_match_schema(validate_parent_output_config)
    },
    extra=cv.ALLOW_EXTRA,
)


async def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    await cg.register_component(var, config)

    out = await cg.get_variable(config[CONF_OUTPUT])
    cg.add(var.set_output(out))

    for conf in config.get(CONF_ON_FINISHED_PLAYBACK, []):
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
        await automation.build_automation(trigger, [], conf)