示例#1
0
def setup_time_core_(time_var, config):
    add(time_var.set_timezone(config[CONF_TIMEZONE]))

    for conf in config.get(CONF_ON_TIME, []):
        rhs = App.register_component(time_var.Pmake_cron_trigger())
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)

        seconds = conf.get(CONF_SECONDS, [x for x in range(0, 61)])
        add(trigger.add_seconds(seconds))

        minutes = conf.get(CONF_MINUTES, [x for x in range(0, 60)])
        add(trigger.add_minutes(minutes))

        hours = conf.get(CONF_HOURS, [x for x in range(0, 24)])
        add(trigger.add_hours(hours))

        days_of_month = conf.get(CONF_DAYS_OF_MONTH, [x for x in range(1, 32)])
        add(trigger.add_days_of_month(days_of_month))

        months = conf.get(CONF_MONTHS, [x for x in range(1, 13)])
        add(trigger.add_months(months))

        days_of_week = conf.get(CONF_DAYS_OF_WEEK, [x for x in range(1, 8)])
        add(trigger.add_days_of_week(days_of_week))

        automation.build_automations(trigger, [], conf)
示例#2
0
文件: interval.py 项目: khzd/pi4home
def to_code(config):
    for conf in config:
        rhs = App.register_component(IntervalTrigger.new(conf[CONF_INTERVAL]))
        trigger = Pvariable(conf[CONF_ID], rhs)
        setup_component(trigger, conf)

        automation.build_automations(trigger, [], conf)
示例#3
0
文件: template.py 项目: khzd/pi4home
def to_code(config):
    rhs = App.make_template_switch(config[CONF_NAME])
    template = Pvariable(config[CONF_ID], rhs)

    switch.setup_switch(template, config)

    if CONF_LAMBDA in config:
        for template_ in process_lambda(config[CONF_LAMBDA], [],
                                        return_type=optional.template(bool_)):
            yield
        add(template.set_state_lambda(template_))
    if CONF_TURN_OFF_ACTION in config:
        automation.build_automations(template.get_turn_off_trigger(), [],
                                     config[CONF_TURN_OFF_ACTION])
    if CONF_TURN_ON_ACTION in config:
        automation.build_automations(template.get_turn_on_trigger(), [],
                                     config[CONF_TURN_ON_ACTION])
    if CONF_OPTIMISTIC in config:
        add(template.set_optimistic(config[CONF_OPTIMISTIC]))
    if CONF_ASSUMED_STATE in config:
        add(template.set_assumed_state(config[CONF_ASSUMED_STATE]))

    if CONF_RESTORE_STATE in config:
        add(template.set_restore_state(config[CONF_RESTORE_STATE]))

    setup_component(template, config)
示例#4
0
文件: api.py 项目: khzd/pi4home
def to_code(config):
    rhs = App.init_api_server()
    api = Pvariable(config[CONF_ID], rhs)

    if config[CONF_PORT] != 6053:
        add(api.set_port(config[CONF_PORT]))
    if config.get(CONF_PASSWORD):
        add(api.set_password(config[CONF_PASSWORD]))
    if CONF_REBOOT_TIMEOUT in config:
        add(api.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

    for conf in config.get(CONF_SERVICES, []):
        template_args = []
        func_args = []
        service_type_args = []
        for name, var_ in conf[CONF_VARIABLES].items():
            native = SERVICE_ARG_NATIVE_TYPES[var_]
            template_args.append(native)
            func_args.append((native, name))
            service_type_args.append(ServiceTypeArgument(name, SERVICE_ARG_TYPES[var_]))
        func = api.make_user_service_trigger.template(*template_args)
        rhs = func(conf[CONF_SERVICE], service_type_args)
        type_ = UserService.template(*template_args)
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs, type=type_)
        automation.build_automations(trigger, func_args, conf)

    setup_component(api, config)
示例#5
0
def setup_text_sensor_core_(text_sensor_var, config):
    if CONF_INTERNAL in config:
        add(text_sensor_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config:
        add(text_sensor_var.set_icon(config[CONF_ICON]))

    for conf in config.get(CONF_ON_VALUE, []):
        rhs = text_sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(std_string, 'x')], conf)

    setup_mqtt_component(text_sensor_var.get_mqtt(), config)
示例#6
0
文件: pn532.py 项目: khzd/pi4home
def to_code(config):
    for spi_ in get_variable(config[CONF_SPI_ID]):
        yield
    for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
        yield
    rhs = App.make_pn532_component(spi_, cs, config.get(CONF_UPDATE_INTERVAL))
    pn532 = Pvariable(config[CONF_ID], rhs)

    for conf_ in config.get(CONF_ON_TAG, []):
        trigger = Pvariable(conf_[CONF_TRIGGER_ID], pn532.make_trigger())
        automation.build_automations(trigger, [(std_string, 'x')], conf_)

    setup_component(pn532, config)
示例#7
0
def setup_switch_core_(switch_var, config):
    if CONF_INTERNAL in config:
        add(switch_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config:
        add(switch_var.set_icon(config[CONF_ICON]))
    if CONF_INVERTED in config:
        add(switch_var.set_inverted(config[CONF_INVERTED]))
    for conf in config.get(CONF_ON_TURN_ON, []):
        rhs = switch_var.make_switch_turn_on_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)
    for conf in config.get(CONF_ON_TURN_OFF, []):
        rhs = switch_var.make_switch_turn_off_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    setup_mqtt_component(switch_var.Pget_mqtt(), config)
示例#8
0
def to_code(config):
    rhs = App.make_template_cover(config[CONF_NAME])
    var = Pvariable(config[CONF_ID], rhs)

    cover.setup_cover(var, config)
    setup_component(var, config)

    if CONF_LAMBDA in config:
        for template_ in process_lambda(config[CONF_LAMBDA], [],
                                        return_type=optional.template(
                                            cover.CoverState)):
            yield
        add(var.set_state_lambda(template_))
    if CONF_OPEN_ACTION in config:
        automation.build_automations(var.get_open_trigger(), [],
                                     config[CONF_OPEN_ACTION])
    if CONF_CLOSE_ACTION in config:
        automation.build_automations(var.get_close_trigger(), [],
                                     config[CONF_CLOSE_ACTION])
    if CONF_STOP_ACTION in config:
        automation.build_automations(var.get_stop_trigger(), [],
                                     config[CONF_STOP_ACTION])
    if CONF_OPTIMISTIC in config:
        add(var.set_optimistic(config[CONF_OPTIMISTIC]))
    if CONF_ASSUMED_STATE in config:
        add(var.set_assumed_state(config[CONF_ASSUMED_STATE]))
示例#9
0
文件: __init__.py 项目: khzd/pi4home
def setup_sensor_core_(sensor_var, config):
    if CONF_INTERNAL in config:
        add(sensor_var.set_internal(config[CONF_INTERNAL]))
    if CONF_UNIT_OF_MEASUREMENT in config:
        add(
            sensor_var.set_unit_of_measurement(
                config[CONF_UNIT_OF_MEASUREMENT]))
    if CONF_ICON in config:
        add(sensor_var.set_icon(config[CONF_ICON]))
    if CONF_ACCURACY_DECIMALS in config:
        add(sensor_var.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
    if CONF_FILTERS in config:
        for filters in setup_filters(config[CONF_FILTERS]):
            yield
        add(sensor_var.set_filters(filters))

    for conf in config.get(CONF_ON_VALUE, []):
        rhs = sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(float_, 'x')], conf)
    for conf in config.get(CONF_ON_RAW_VALUE, []):
        rhs = sensor_var.make_raw_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(float_, 'x')], conf)
    for conf in config.get(CONF_ON_VALUE_RANGE, []):
        rhs = sensor_var.make_value_range_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        add(App.register_component(trigger))
        if CONF_ABOVE in conf:
            for template_ in templatable(conf[CONF_ABOVE], float_, float_):
                yield
            add(trigger.set_min(template_))
        if CONF_BELOW in conf:
            for template_ in templatable(conf[CONF_BELOW], float_, float_):
                yield
            add(trigger.set_max(template_))
        automation.build_automations(trigger, [(float_, 'x')], conf)

    mqtt_ = sensor_var.Pget_mqtt()
    if CONF_EXPIRE_AFTER in config:
        if config[CONF_EXPIRE_AFTER] is None:
            add(mqtt_.disable_expire_after())
        else:
            add(mqtt_.set_expire_after(config[CONF_EXPIRE_AFTER]))
    setup_mqtt_component(mqtt_, config)
示例#10
0
def to_code(config):
    add(App.set_name(config[CONF_NAME]))

    for conf in config.get(CONF_ON_BOOT, []):
        rhs = App.register_component(StartupTrigger.new(conf.get(CONF_PRIORITY)))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_SHUTDOWN, []):
        trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new())
        automation.build_automations(trigger, [(const_char_ptr, 'x')], conf)

    for conf in config.get(CONF_ON_LOOP, []):
        rhs = App.register_component(LoopTrigger.new())
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    add(App.set_compilation_datetime(RawExpression('__DATE__ ", " __TIME__')))
示例#11
0
文件: __init__.py 项目: khzd/pi4home
def setup_binary_sensor_core_(binary_sensor_var, config):
    if CONF_INTERNAL in config:
        add(binary_sensor_var.set_internal(CONF_INTERNAL))
    if CONF_DEVICE_CLASS in config:
        add(binary_sensor_var.set_device_class(config[CONF_DEVICE_CLASS]))
    if CONF_INVERTED in config:
        add(binary_sensor_var.set_inverted(config[CONF_INVERTED]))
    if CONF_FILTERS in config:
        for filters in setup_filters(config[CONF_FILTERS]):
            yield
        add(binary_sensor_var.add_filters(filters))

    for conf in config.get(CONF_ON_PRESS, []):
        rhs = binary_sensor_var.make_press_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_RELEASE, []):
        rhs = binary_sensor_var.make_release_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_CLICK, []):
        rhs = binary_sensor_var.make_click_trigger(conf[CONF_MIN_LENGTH],
                                                   conf[CONF_MAX_LENGTH])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_DOUBLE_CLICK, []):
        rhs = binary_sensor_var.make_double_click_trigger(
            conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_MULTI_CLICK, []):
        timings = []
        for tim in conf[CONF_TIMING]:
            timings.append(
                StructInitializer(
                    MultiClickTriggerEvent,
                    ('state', tim[CONF_STATE]),
                    ('min_length', tim[CONF_MIN_LENGTH]),
                    ('max_length', tim.get(CONF_MAX_LENGTH, 4294967294)),
                ))
        rhs = App.register_component(
            binary_sensor_var.make_multi_click_trigger(timings))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        if CONF_INVALID_COOLDOWN in conf:
            add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN]))
        automation.build_automations(trigger, [], conf)

    for conf in config.get(CONF_ON_STATE, []):
        rhs = binary_sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(bool_, 'x')], conf)

    setup_mqtt_component(binary_sensor_var.Pget_mqtt(), config)
示例#12
0
文件: mqtt.py 项目: khzd/pi4home
def to_code(config):
    rhs = App.init_mqtt(config[CONF_BROKER], config[CONF_PORT],
                        config[CONF_USERNAME], config[CONF_PASSWORD])
    mqtt = Pvariable(config[CONF_ID], rhs)

    discovery = config.get(CONF_DISCOVERY, True)
    discovery_retain = config.get(CONF_DISCOVERY_RETAIN, True)
    discovery_prefix = config.get(CONF_DISCOVERY_PREFIX, 'homeassistant')

    if not discovery:
        add(mqtt.disable_discovery())
    elif discovery == "CLEAN":
        add(mqtt.set_discovery_info(discovery_prefix, discovery_retain, True))
    elif CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config:
        add(mqtt.set_discovery_info(discovery_prefix, discovery_retain))

    if CONF_TOPIC_PREFIX in config:
        add(mqtt.set_topic_prefix(config[CONF_TOPIC_PREFIX]))

    if CONF_BIRTH_MESSAGE in config:
        birth_message = config[CONF_BIRTH_MESSAGE]
        if not birth_message:
            add(mqtt.disable_birth_message())
        else:
            add(mqtt.set_birth_message(exp_mqtt_message(birth_message)))
    if CONF_WILL_MESSAGE in config:
        will_message = config[CONF_WILL_MESSAGE]
        if not will_message:
            add(mqtt.disable_last_will())
        else:
            add(mqtt.set_last_will(exp_mqtt_message(will_message)))
    if CONF_SHUTDOWN_MESSAGE in config:
        shutdown_message = config[CONF_SHUTDOWN_MESSAGE]
        if not shutdown_message:
            add(mqtt.disable_shutdown_message())
        else:
            add(mqtt.set_shutdown_message(exp_mqtt_message(shutdown_message)))

    if CONF_CLIENT_ID in config:
        add(mqtt.set_client_id(config[CONF_CLIENT_ID]))

    if CONF_LOG_TOPIC in config:
        log_topic = config[CONF_LOG_TOPIC]
        if not log_topic:
            add(mqtt.disable_log_message())
        else:
            add(mqtt.set_log_message_template(exp_mqtt_message(log_topic)))

            if CONF_LEVEL in log_topic:
                add(
                    mqtt.set_log_level(
                        logger.LOG_LEVELS[log_topic[CONF_LEVEL]]))

    if CONF_SSL_FINGERPRINTS in config:
        for fingerprint in config[CONF_SSL_FINGERPRINTS]:
            arr = [
                RawExpression("0x{}".format(fingerprint[i:i + 2]))
                for i in range(0, 40, 2)
            ]
            add(mqtt.add_ssl_fingerprint(arr))

    if CONF_KEEPALIVE in config:
        add(mqtt.set_keep_alive(config[CONF_KEEPALIVE]))

    if CONF_REBOOT_TIMEOUT in config:
        add(mqtt.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

    for conf in config.get(CONF_ON_MESSAGE, []):
        rhs = App.register_component(
            mqtt.make_message_trigger(conf[CONF_TOPIC]))
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        if CONF_QOS in conf:
            add(trigger.set_qos(conf[CONF_QOS]))
        if CONF_PAYLOAD in conf:
            add(trigger.set_payload(conf[CONF_PAYLOAD]))
        automation.build_automations(trigger, [(std_string, 'x')], conf)

    for conf in config.get(CONF_ON_JSON_MESSAGE, []):
        rhs = mqtt.make_json_message_trigger(conf[CONF_TOPIC], conf[CONF_QOS])
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
        automation.build_automations(trigger, [(JsonObjectConstRef, 'x')],
                                     conf)