if CONF_LAMBDA in config: for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(float_)): yield add(template.set_template(template_)) BUILD_FLAGS = '-DUSE_TEMPLATE_SENSOR' CONF_SENSOR_TEMPLATE_PUBLISH = 'sensor.template.publish' SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(sensor.Sensor), vol.Required(CONF_STATE): cv.templatable(cv.float_), }) @ACTION_REGISTRY.register(CONF_SENSOR_TEMPLATE_PUBLISH, SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA) def sensor_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_sensor_publish_action(template_arg) type = SensorPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_STATE], args, float_): yield None add(action.set_state(template_)) yield action
setup_mqtt_component(light_var.Pget_mqtt(), config) def setup_light(light_obj, config): light_var = Pvariable(config[CONF_ID], light_obj, has_side_effects=False) CORE.add_job(setup_light_core_, light_var, config) BUILD_FLAGS = '-DUSE_LIGHT' CONF_LIGHT_TOGGLE = 'light.toggle' LIGHT_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.use_variable_id(LightState), vol.Optional(CONF_TRANSITION_LENGTH): cv.templatable(cv.positive_time_period_milliseconds), }) @ACTION_REGISTRY.register(CONF_LIGHT_TOGGLE, LIGHT_TOGGLE_ACTION_SCHEMA) def light_toggle_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = ToggleAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_TRANSITION_LENGTH in config: for template_ in templatable(config[CONF_TRANSITION_LENGTH], args, uint32): yield None add(action.set_transition_length(template_))
add(servo.set_min_level(config[CONF_MIN_LEVEL])) add(servo.set_idle_level(config[CONF_IDLE_LEVEL])) add(servo.set_max_level(config[CONF_MAX_LEVEL])) setup_component(servo, config) BUILD_FLAGS = '-DUSE_SERVO' CONF_SERVO_WRITE = 'servo.write' SERVO_WRITE_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(Servo), vol.Required(CONF_LEVEL): cv.templatable(cv.possibly_negative_percentage), }) @ACTION_REGISTRY.register(CONF_SERVO_WRITE, SERVO_WRITE_ACTION_SCHEMA) def servo_write_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = ServoWriteAction.new(template_arg, var) type = ServoWriteAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_LEVEL], args, float_): yield None add(action.set_value(template_)) yield action
if CONF_LAMBDA in config: for template_ in process_lambda( config[CONF_LAMBDA], [], return_type=optional.template(std_string)): yield add(template.set_template(template_)) BUILD_FLAGS = '-DUSE_TEMPLATE_TEXT_SENSOR' CONF_TEXT_SENSOR_TEMPLATE_PUBLISH = 'text_sensor.template.publish' TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(text_sensor.TextSensor), vol.Required(CONF_STATE): cv.templatable(cv.string_strict), }) @ACTION_REGISTRY.register(CONF_TEXT_SENSOR_TEMPLATE_PUBLISH, TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA) def text_sensor_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_text_sensor_publish_action(template_arg) type = TextSensorPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_STATE], args, std_string): yield None add(action.set_state(template_))
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) BUILD_FLAGS = '-DUSE_TEMPLATE_SWITCH' CONF_SWITCH_TEMPLATE_PUBLISH = 'switch.template.publish' SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(switch.Switch), vol.Required(CONF_STATE): cv.templatable(cv.boolean), }) @ACTION_REGISTRY.register(CONF_SWITCH_TEMPLATE_PUBLISH, SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA) def switch_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_switch_publish_action(template_arg) type = SwitchPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_STATE], args, bool_): yield None add(action.set_state(template_)) yield action
@ACTION_REGISTRY.register(CONF_OUTPUT_TURN_OFF, OUTPUT_TURN_OFF_ACTION) def output_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = TurnOffAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) CONF_OUTPUT_SET_LEVEL = 'output.set_level' OUTPUT_SET_LEVEL_ACTION = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(FloatOutput), vol.Required(CONF_LEVEL): cv.templatable(cv.percentage), }) @ACTION_REGISTRY.register(CONF_OUTPUT_SET_LEVEL, OUTPUT_SET_LEVEL_ACTION) def output_set_level_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_set_level_action(template_arg) type = SetLevelAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_LEVEL], args, float_): yield None add(action.set_level(template_)) yield action
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])) BUILD_FLAGS = '-DUSE_TEMPLATE_COVER' CONF_COVER_TEMPLATE_PUBLISH = 'cover.template.publish' COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(cover.Cover), vol.Required(CONF_STATE): cv.templatable(cover.validate_cover_state), }) @ACTION_REGISTRY.register(CONF_COVER_TEMPLATE_PUBLISH, COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA) def cover_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_cover_publish_action(template_arg) type = CoverPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) state = config[CONF_STATE] if isinstance(state, string_types): template_ = cover.COVER_STATES[state] else:
if CONF_DECELERATION in config: add(stepper_var.set_deceleration(config[CONF_DECELERATION])) if CONF_MAX_SPEED in config: add(stepper_var.set_max_speed(config[CONF_MAX_SPEED])) def setup_stepper(stepper_var, config): CORE.add_job(setup_stepper_core_, stepper_var, config) BUILD_FLAGS = '-DUSE_STEPPER' CONF_STEPPER_SET_TARGET = 'stepper.set_target' STEPPER_SET_TARGET_ACTION_SCHEMA = cv.Schema({ vol.Required(CONF_ID): cv.use_variable_id(Stepper), vol.Required(CONF_TARGET): cv.templatable(cv.int_), }) @ACTION_REGISTRY.register(CONF_STEPPER_SET_TARGET, STEPPER_SET_TARGET_ACTION_SCHEMA) def stepper_set_target_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_set_target_action(template_arg) type = SetTargetAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TARGET], args, int32): yield None add(action.set_target(template_)) yield action
@ACTION_REGISTRY.register(CONF_FAN_TURN_OFF, FAN_TURN_OFF_ACTION_SCHEMA) def fan_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = TurnOffAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) CONF_FAN_TURN_ON = 'fan.turn_on' FAN_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.use_variable_id(FanState), vol.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean), vol.Optional(CONF_SPEED): cv.templatable(cv.one_of(*FAN_SPEEDS, upper=True)), }) @ACTION_REGISTRY.register(CONF_FAN_TURN_ON, FAN_TURN_ON_ACTION_SCHEMA) def fan_turn_on_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = TurnOnAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_OSCILLATING in config: for template_ in templatable(config[CONF_OSCILLATING], args, bool_): yield None
OR_CONDITION_SCHEMA = validate_recursive_condition @CONDITION_REGISTRY.register(CONF_OR, OR_CONDITION_SCHEMA) def or_condition_to_code(config, condition_id, template_arg, args): for conditions in build_conditions(config, template_arg, args): yield rhs = OrCondition.new(template_arg, conditions) type = OrCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) RANGE_CONDITION_SCHEMA = vol.All(cv.Schema({ vol.Optional(CONF_ABOVE): cv.templatable(cv.float_), vol.Optional(CONF_BELOW): cv.templatable(cv.float_), }), cv.has_at_least_one_key(CONF_ABOVE, CONF_BELOW)) @CONDITION_REGISTRY.register(CONF_RANGE, RANGE_CONDITION_SCHEMA) def range_condition_to_code(config, condition_id, template_arg, args): for conditions in build_conditions(config, template_arg, args): yield rhs = RangeCondition.new(template_arg, conditions) type = RangeCondition.template(template_arg) condition = Pvariable(condition_id, rhs, type=type) if CONF_ABOVE in config: for template_ in templatable(config[CONF_ABOVE], args, float_): yield condition.set_min(template_)
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) CONF_MQTT_PUBLISH = 'mqtt.publish' MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema({ cv.GenerateID(): cv.use_variable_id(MQTTClientComponent), vol.Required(CONF_TOPIC): cv.templatable(cv.publish_topic), vol.Required(CONF_PAYLOAD): cv.templatable(cv.mqtt_payload), vol.Optional(CONF_QOS): cv.templatable(cv.mqtt_qos), vol.Optional(CONF_RETAIN): cv.templatable(cv.boolean), }) @ACTION_REGISTRY.register(CONF_MQTT_PUBLISH, MQTT_PUBLISH_ACTION_SCHEMA) def mqtt_publish_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_publish_action(template_arg) type = MQTTPublishAction.template(template_arg)
if CONF_ROTATION in config: add(display_var.set_rotation(DISPLAY_ROTATIONS[config[CONF_ROTATION]])) if CONF_PAGES in config: pages = [] for conf in config[CONF_PAGES]: for lambda_ in process_lambda(conf[CONF_LAMBDA], [(DisplayBufferRef, 'it')], return_type=void): yield var = Pvariable(conf[CONF_ID], DisplayPage.new(lambda_)) pages.append(var) add(display_var.set_pages(pages)) CONF_DISPLAY_PAGE_SHOW = 'display.page.show' DISPLAY_PAGE_SHOW_ACTION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.templatable(cv.use_variable_id(DisplayPage)), }) @ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW, DISPLAY_PAGE_SHOW_ACTION_SCHEMA) def display_page_show_to_code(config, action_id, template_arg, args): type = DisplayPageShowAction.template(template_arg) action = Pvariable(action_id, type.new(), type=type) if isinstance(config[CONF_ID], core.Lambda): for template_ in templatable(config[CONF_ID], args, DisplayPagePtr): yield None add(action.set_page(template_)) else: for var in get_variable(config[CONF_ID]): yield None add(action.set_page(var))