Пример #1
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)

    # TODO Implement your own logic to attach triggers.
    # Generally we suggest to re-use the existing state or event
    # triggers from the automation integration.

    if config[CONF_TYPE] == "turned_on":
        from_state = STATE_OFF
        to_state = STATE_ON
    else:
        from_state = STATE_ON
        to_state = STATE_OFF

    state_config = {
        state.CONF_PLATFORM: "state",
        CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        state.CONF_FROM: from_state,
        state.CONF_TO: to_state,
    }
    state_config = state.TRIGGER_SCHEMA(state_config)
    return await state.async_attach_trigger(hass,
                                            state_config,
                                            action,
                                            automation_info,
                                            platform_type="device")
Пример #2
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Listen for state changes based on configuration."""
    trigger_type = config[CONF_TYPE]
    if trigger_type == CONF_TURNED_ON:
        from_state = "off"
        to_state = "on"
    else:
        from_state = "on"
        to_state = "off"
    state_config = {
        state_automation.CONF_PLATFORM: "state",
        state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        state_automation.CONF_FROM: from_state,
        state_automation.CONF_TO: to_state,
    }
    if CONF_FOR in config:
        state_config[CONF_FOR] = config[CONF_FOR]

    state_config = state_automation.TRIGGER_SCHEMA(state_config)
    return await state_automation.async_attach_trigger(hass,
                                                       state_config,
                                                       action,
                                                       automation_info,
                                                       platform_type="device")
Пример #3
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)

    if config[CONF_TYPE] == "turned_on":
        from_state = STATE_OFF
        to_state = STATE_ON
    else:
        from_state = STATE_ON
        to_state = STATE_OFF

    state_config = {
        state.CONF_PLATFORM: "state",
        CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        state.CONF_FROM: from_state,
        state.CONF_TO: to_state,
    }
    state_config = state.TRIGGER_SCHEMA(state_config)
    return await state.async_attach_trigger(hass,
                                            state_config,
                                            action,
                                            automation_info,
                                            platform_type="device")
Пример #4
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)

    if config[CONF_TYPE] == "cleaning":
        from_state = [state for state in STATES if state != STATE_CLEANING]
        to_state = STATE_CLEANING
    else:
        from_state = [state for state in STATES if state != STATE_DOCKED]
        to_state = STATE_DOCKED

    state_config = {
        state.CONF_PLATFORM: "state",
        CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        state.CONF_FROM: from_state,
        state.CONF_TO: to_state,
    }
    state_config = state.TRIGGER_SCHEMA(state_config)
    return await state.async_attach_trigger(hass,
                                            state_config,
                                            action,
                                            automation_info,
                                            platform_type="device")
Пример #5
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)

    if config[CONF_TYPE] in STATE_TRIGGER_TYPES:
        if config[CONF_TYPE] == "opened":
            to_state = STATE_OPEN
        elif config[CONF_TYPE] == "closed":
            to_state = STATE_CLOSED
        elif config[CONF_TYPE] == "opening":
            to_state = STATE_OPENING
        elif config[CONF_TYPE] == "closing":
            to_state = STATE_CLOSING

        state_config = {
            state_automation.CONF_PLATFORM: "state",
            CONF_ENTITY_ID: config[CONF_ENTITY_ID],
            state_automation.CONF_TO: to_state,
        }
        state_config = state_automation.TRIGGER_SCHEMA(state_config)
        return await state_automation.async_attach_trigger(
            hass,
            state_config,
            action,
            automation_info,
            platform_type="device")

    if config[CONF_TYPE] == "position":
        position = "current_position"
    if config[CONF_TYPE] == "tilt_position":
        position = "current_tilt_position"
    min_pos = config.get(CONF_ABOVE, -1)
    max_pos = config.get(CONF_BELOW, 101)
    value_template = f"{{{{ state.attributes.{position} }}}}"

    numeric_state_config = {
        numeric_state_automation.CONF_PLATFORM: "numeric_state",
        numeric_state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        numeric_state_automation.CONF_BELOW: max_pos,
        numeric_state_automation.CONF_ABOVE: min_pos,
        numeric_state_automation.CONF_VALUE_TEMPLATE: value_template,
    }
    numeric_state_config = numeric_state_automation.TRIGGER_SCHEMA(
        numeric_state_config)
    return await numeric_state_automation.async_attach_trigger(
        hass,
        numeric_state_config,
        action,
        automation_info,
        platform_type="device")
Пример #6
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)
    from_state = None

    if config[CONF_TYPE] == "triggered":
        to_state = STATE_ALARM_TRIGGERED
    elif config[CONF_TYPE] == "disarmed":
        to_state = STATE_ALARM_DISARMED
    elif config[CONF_TYPE] == "arming":
        from_state = STATE_ALARM_DISARMED
        to_state = STATE_ALARM_ARMING
    elif config[CONF_TYPE] == "armed_home":
        from_state = STATE_ALARM_PENDING or STATE_ALARM_ARMING
        to_state = STATE_ALARM_ARMED_HOME
    elif config[CONF_TYPE] == "armed_away":
        from_state = STATE_ALARM_PENDING or STATE_ALARM_ARMING
        to_state = STATE_ALARM_ARMED_AWAY
    elif config[CONF_TYPE] == "armed_night":
        from_state = STATE_ALARM_PENDING or STATE_ALARM_ARMING
        to_state = STATE_ALARM_ARMED_NIGHT

    state_config = {
        state.CONF_PLATFORM: "state",
        CONF_ENTITY_ID: config[CONF_ENTITY_ID],
        state.CONF_TO: to_state,
    }
    if from_state:
        state_config[state.CONF_FROM] = from_state
    state_config = state.TRIGGER_SCHEMA(state_config)
    return await state.async_attach_trigger(
        hass, state_config, action, automation_info, platform_type="device"
    )
Пример #7
0
async def async_attach_trigger(
    hass: HomeAssistant,
    config: ConfigType,
    action: AutomationActionType,
    automation_info: dict,
) -> CALLBACK_TYPE:
    """Attach a trigger."""
    config = TRIGGER_SCHEMA(config)
    trigger_type = config[CONF_TYPE]

    if trigger_type == "hvac_mode_changed":
        state_config = {
            state_automation.CONF_PLATFORM:
            "state",
            state_automation.CONF_ENTITY_ID:
            config[CONF_ENTITY_ID],
            state_automation.CONF_TO:
            config[state_automation.CONF_TO],
            state_automation.CONF_FROM: [
                mode for mode in const.HVAC_MODES
                if mode != config[state_automation.CONF_TO]
            ],
        }
        if CONF_FOR in config:
            state_config[CONF_FOR] = config[CONF_FOR]
        state_config = state_automation.TRIGGER_SCHEMA(state_config)
        return await state_automation.async_attach_trigger(
            hass,
            state_config,
            action,
            automation_info,
            platform_type="device")

    numeric_state_config = {
        numeric_state_automation.CONF_PLATFORM: "numeric_state",
        numeric_state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
    }

    if trigger_type == "current_temperature_changed":
        numeric_state_config[
            numeric_state_automation.
            CONF_VALUE_TEMPLATE] = "{{ state.attributes.current_temperature }}"
    else:
        numeric_state_config[
            numeric_state_automation.
            CONF_VALUE_TEMPLATE] = "{{ state.attributes.current_humidity }}"

    if CONF_ABOVE in config:
        numeric_state_config[CONF_ABOVE] = config[CONF_ABOVE]
    if CONF_BELOW in config:
        numeric_state_config[CONF_BELOW] = config[CONF_BELOW]
    if CONF_FOR in config:
        numeric_state_config[CONF_FOR] = config[CONF_FOR]

    numeric_state_config = numeric_state_automation.TRIGGER_SCHEMA(
        numeric_state_config)
    return await numeric_state_automation.async_attach_trigger(
        hass,
        numeric_state_config,
        action,
        automation_info,
        platform_type="device")