def to_code(config): cg.add_global(cg.global_ns.namespace('esphome').using) cg.add(cg.App.pre_setup(config[CONF_NAME], cg.RawExpression('__DATE__ ", " __TIME__'))) for conf in config.get(CONF_ON_BOOT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf.get(CONF_PRIORITY)) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_LOOP, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) # Build flags if CORE.is_esp8266 and CORE.board in ESP8266_FLASH_SIZES and \ CORE.arduino_version != ARDUINO_VERSION_ESP8266_2_3_0: flash_size = ESP8266_FLASH_SIZES[CORE.board] ld_scripts = ESP8266_LD_SCRIPTS[flash_size] ld_script = None if CORE.arduino_version in ('[email protected]', '[email protected]', '[email protected]'): ld_script = ld_scripts[0] elif CORE.arduino_version in (ARDUINO_VERSION_ESP8266_DEV, ARDUINO_VERSION_ESP8266_2_5_0, ARDUINO_VERSION_ESP8266_2_5_1, ARDUINO_VERSION_ESP8266_2_5_2): ld_script = ld_scripts[1] if ld_script is not None: cg.add_build_flag(f'-Wl,-T{ld_script}') cg.add_build_flag('-fno-exceptions') # Libraries if CORE.is_esp32: cg.add_library('ESPmDNS', None) elif CORE.is_esp8266: cg.add_library('ESP8266WiFi', None) cg.add_library('ESP8266mDNS', None) for lib in config[CONF_LIBRARIES]: if '@' in lib: name, vers = lib.split('@', 1) cg.add_library(name, vers) else: cg.add_library(lib, None) cg.add_build_flag('-Wno-unused-variable') cg.add_build_flag('-Wno-unused-but-set-variable') cg.add_build_flag('-Wno-sign-compare') if config.get(CONF_ESP8266_RESTORE_FROM_FLASH, False): cg.add_define('USE_ESP8266_PREFERENCES_FLASH') if config[CONF_INCLUDES]: CORE.add_job(add_includes, config[CONF_INCLUDES])
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield cover.register_cover(var, config) yield automation.build_automation( var.get_stop_trigger(), [], config[CONF_STOP_ACTION] ) bin = yield cg.get_variable(config[CONF_OPEN_ENDSTOP]) cg.add(var.set_open_endstop(bin)) cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) yield automation.build_automation( var.get_open_trigger(), [], config[CONF_OPEN_ACTION] ) bin = yield cg.get_variable(config[CONF_CLOSE_ENDSTOP]) cg.add(var.set_close_endstop(bin)) cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) yield automation.build_automation( var.get_close_trigger(), [], config[CONF_CLOSE_ACTION] ) if CONF_MAX_DURATION in config: cg.add(var.set_max_duration(config[CONF_MAX_DURATION]))
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield cover.register_cover(var, config) if CONF_LAMBDA in config: template_ = yield cg.process_lambda( config[CONF_LAMBDA], [], return_type=cg.optional.template(float)) cg.add(var.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: yield automation.build_automation(var.get_open_trigger(), [], config[CONF_OPEN_ACTION]) if CONF_CLOSE_ACTION in config: yield automation.build_automation(var.get_close_trigger(), [], config[CONF_CLOSE_ACTION]) if CONF_STOP_ACTION in config: yield automation.build_automation(var.get_stop_trigger(), [], config[CONF_STOP_ACTION]) if CONF_TILT_ACTION in config: yield automation.build_automation(var.get_tilt_trigger(), [(float, 'tilt')], config[CONF_TILT_ACTION]) cg.add(var.set_has_tilt(True)) if CONF_TILT_LAMBDA in config: tilt_template_ = yield cg.process_lambda( config[CONF_TILT_LAMBDA], [], return_type=cg.optional.template(float)) cg.add(var.set_tilt_lambda(tilt_template_)) cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE])) cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
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_automation(trigger, NoArg, conf)
def setup_light_core_(light_var, output_var, config): cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE])) if CONF_INTERNAL in config: cg.add(light_var.set_internal(config[CONF_INTERNAL])) if CONF_DEFAULT_TRANSITION_LENGTH in config: cg.add(light_var.set_default_transition_length(config[CONF_DEFAULT_TRANSITION_LENGTH])) if CONF_GAMMA_CORRECT in config: cg.add(light_var.set_gamma_correct(config[CONF_GAMMA_CORRECT])) effects = yield cg.build_registry_list(EFFECTS_REGISTRY, config.get(CONF_EFFECTS, [])) cg.add(light_var.add_effects(effects)) for conf in config.get(CONF_ON_TURN_ON, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var) yield auto.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_TURN_OFF, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], light_var) yield auto.build_automation(trigger, [], conf) if CONF_COLOR_CORRECT in config: cg.add(output_var.set_correction(*config[CONF_COLOR_CORRECT])) if CONF_POWER_SUPPLY in config: var_ = yield cg.get_variable(config[CONF_POWER_SUPPLY]) cg.add(output_var.set_power_supply(var_)) if CONF_MQTT_ID in config: mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], light_var) yield mqtt.register_mqtt_component(mqtt_, config)
def to_code(config): """Generates the C++ code for the BLE controller configuration""" var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) for cmd in config.get(CONF_BLE_SERVICES, []): yield to_code_service(var, cmd) for cmd in config.get(CONF_BLE_COMMANDS, []): yield to_code_command(var, cmd) security_enabled = SECURTY_MODE_OPTIONS[config[CONF_SECURITY_MODE]] cg.add(var.set_security_enabled(config[CONF_SECURITY_MODE])) for conf in config.get(CONF_ON_SHOW_PASS_KEY, []): if security_enabled: trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [(cg.std_string, 'pass_key')], conf) for conf in config.get(CONF_ON_AUTHENTICATION_COMPLETE, []): if security_enabled: trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [(cg.bool_, 'success')], conf) for conf in config.get(CONF_ON_SERVER_CONNECTED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_SERVER_DISCONNECTED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield climate.register_climate(var, config) sens = yield cg.get_variable(config[CONF_SENSOR]) cg.add(var.set_sensor(sens)) normal_config = BangBangClimateTargetTempConfig( config[CONF_DEFAULT_TARGET_TEMPERATURE_LOW], config[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH]) cg.add(var.set_normal_config(normal_config)) yield automation.build_automation(var.get_idle_trigger(), [], config[CONF_IDLE_ACTION]) if CONF_COOL_ACTION in config: yield automation.build_automation(var.get_cool_trigger(), [], config[CONF_COOL_ACTION]) cg.add(var.set_supports_cool(True)) if CONF_HEAT_ACTION in config: yield automation.build_automation(var.get_heat_trigger(), [], config[CONF_HEAT_ACTION]) cg.add(var.set_supports_heat(True)) if CONF_AWAY_CONFIG in config: away = config[CONF_AWAY_CONFIG] away_config = BangBangClimateTargetTempConfig( away[CONF_DEFAULT_TARGET_TEMPERATURE_LOW], away[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH]) cg.add(var.set_away_config(away_config))
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add_library('CountUpDownTimer', None) yield cg.register_component(var, config) yield text_sensor.register_text_sensor(var, config) parent = yield cg.get_variable(config[CONF_MQTT_PARENT_ID]) cg.add(var.set_parent(parent)) cg.add(var.set_topic(config[CONF_TOPIC])) if CONF_QOS in config: cg.add(var.set_qos(config[CONF_QOS])) for conf in config.get(CONF_ON_END, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield cg.register_parented(trigger, var) cg.add(trigger.set_testState(True)) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_START, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield cg.register_parented(trigger, var) cg.add(trigger.set_testState(False)) yield automation.build_automation(trigger, [], conf)
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_automation(trigger, NoArg, conf)
def setup_time_core_(time_var, config): cg.add(time_var.set_timezone(config[CONF_TIMEZONE])) for conf in config.get(CONF_ON_TIME, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var) seconds = conf.get(CONF_SECONDS, list(range(0, 61))) cg.add(trigger.add_seconds(seconds)) minutes = conf.get(CONF_MINUTES, list(range(0, 60))) cg.add(trigger.add_minutes(minutes)) hours = conf.get(CONF_HOURS, list(range(0, 24))) cg.add(trigger.add_hours(hours)) days_of_month = conf.get(CONF_DAYS_OF_MONTH, list(range(1, 32))) cg.add(trigger.add_days_of_month(days_of_month)) months = conf.get(CONF_MONTHS, list(range(1, 13))) cg.add(trigger.add_months(months)) days_of_week = conf.get(CONF_DAYS_OF_WEEK, list(range(1, 8))) cg.add(trigger.add_days_of_week(days_of_week)) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_TIME_SYNC, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield setup_input(config, CONF_TIME_ID, var.set_time) # inputs yield setup_input(config, CONF_TOTAL, var.set_total) if CONF_TIME_OFFSET in config: cg.add(var.set_time_offset(config[CONF_TIME_OFFSET])) if CONF_TIME_OFFSET_SERVICE in config: cg.add(var.set_time_offset_service(config[CONF_TIME_OFFSET_SERVICE])) # exposed sensors for conf in config.get(CONF_TARIFFS, []): sens = cg.new_Pvariable(conf[CONF_ID]) yield sensor.register_sensor(sens, conf) for tm in conf.get(CONF_TIME, []): parts = tm.split("-") t = [time_period(parts[0]), time_period(parts[1])] cg.add(sens.add_time(t[0].total_minutes, t[1].total_minutes)) cg.add(var.add_tariff(sens)) if CONF_SERVICE in conf: cg.add(sens.set_service(conf[CONF_SERVICE])) for conf in config.get(CONF_ON_TARIFF, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [(sensor.SensorPtr, "x")], conf) for conf in config.get(CONF_ON_BEFORE_TARIFF, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) if config[CONF_TYPE] == CONF_BINARY: yield automation.build_automation(var.get_trigger(), [(bool, 'state')], config[CONF_WRITE_ACTION]) else: yield automation.build_automation(var.get_trigger(), [(float, 'state')], config[CONF_WRITE_ACTION]) yield output.register_output(var, config)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield esp32_ble_tracker.register_client(var, config) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) for conf in config.get(CONF_ON_CONNECT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_DISCONNECT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf)
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_automation(trigger, std_string, conf) setup_mqtt_component(text_sensor_var.get_mqtt(), config)
def setup_pn532(var, config): yield cg.register_component(var, config) for conf in config.get(CONF_ON_TAG, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) cg.add(var.register_trigger(trigger)) yield automation.build_automation(trigger, [(cg.std_string, 'x'), (nfc.NfcTag, 'tag')], conf) for conf in config.get(CONF_ON_FINISHED_WRITE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield cover.register_cover(var, config) yield automation.build_automation(var.get_stop_trigger(), [], config[CONF_STOP_ACTION]) cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) yield automation.build_automation(var.get_open_trigger(), [], config[CONF_OPEN_ACTION]) cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) yield automation.build_automation(var.get_close_trigger(), [], config[CONF_CLOSE_ACTION])
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_automation(trigger, std_string, conf_) setup_component(pn532, config)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield cover.register_cover(var, config) yield automation.build_automation(var.get_stop_trigger(), [], config[CONF_STOP_ACTION]) cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) yield automation.build_automation(var.get_open_trigger(), [], config[CONF_OPEN_ACTION]) cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) yield automation.build_automation(var.get_close_trigger(), [], config[CONF_CLOSE_ACTION]) cg.add(var.set_has_built_in_endstop(config[CONF_HAS_BUILT_IN_ENDSTOP])) cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE]))
def _add_automations(config): for conf in config.get(CONF_ON_BOOT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf.get(CONF_PRIORITY)) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_LOOP, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield uart.register_uart_device(var, config) for conf in config.get(CONF_ON_CODE_RECEIVED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [(RFBridgeData, 'data')], conf) for conf in config.get(CONF_ON_ADVANCED_CODE_RECEIVED, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [(RFBridgeAdvancedData, 'data')], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) params = config[CONF_SCAN_PARAMETERS] cg.add(var.set_scan_duration(params[CONF_DURATION])) cg.add( var.set_scan_interval( int(params[CONF_INTERVAL].total_milliseconds / 0.625))) cg.add( var.set_scan_window(int(params[CONF_WINDOW].total_milliseconds / 0.625))) cg.add(var.set_scan_active(params[CONF_ACTIVE])) for conf in config.get(CONF_ON_BLE_ADVERTISE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) if CONF_MAC_ADDRESS in conf: cg.add(trigger.set_address(conf[CONF_MAC_ADDRESS].as_hex)) yield automation.build_automation(trigger, [(ESPBTDeviceConstRef, 'x')], conf) for conf in config.get(CONF_ON_BLE_SERVICE_DATA_ADVERTISE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) if len(conf[CONF_SERVICE_UUID]) == len(bt_uuid16_format): cg.add(trigger.set_service_uuid16(as_hex(conf[CONF_SERVICE_UUID]))) elif len(conf[CONF_SERVICE_UUID]) == len(bt_uuid32_format): cg.add(trigger.set_service_uuid32(as_hex(conf[CONF_SERVICE_UUID]))) elif len(conf[CONF_SERVICE_UUID]) == len(bt_uuid128_format): uuid128 = as_hex_array(conf[CONF_SERVICE_UUID]) cg.add(trigger.set_service_uuid128(uuid128)) if CONF_MAC_ADDRESS in conf: cg.add(trigger.set_address(conf[CONF_MAC_ADDRESS].as_hex)) yield automation.build_automation(trigger, [(adv_data_t_const_ref, 'x')], conf) for conf in config.get(CONF_ON_BLE_MANUFACTURER_DATA_ADVERTISE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) if len(conf[CONF_MANUFACTURER_ID]) == len(bt_uuid16_format): cg.add( trigger.set_manufacturer_uuid16( as_hex(conf[CONF_MANUFACTURER_ID]))) elif len(conf[CONF_MANUFACTURER_ID]) == len(bt_uuid32_format): cg.add( trigger.set_manufacturer_uuid32( as_hex(conf[CONF_MANUFACTURER_ID]))) elif len(conf[CONF_MANUFACTURER_ID]) == len(bt_uuid128_format): uuid128 = as_hex_array(conf[CONF_MANUFACTURER_ID]) cg.add(trigger.set_manufacturer_uuid128(uuid128)) if CONF_MAC_ADDRESS in conf: cg.add(trigger.set_address(conf[CONF_MAC_ADDRESS].as_hex)) yield automation.build_automation(trigger, [(adv_data_t_const_ref, 'x')], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) cg.add(var.set_port(config[CONF_PORT])) cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) for conf in config.get(CONF_SERVICES, []): template_args = [] func_args = [] service_arg_names = [] for name, var_ in conf[CONF_VARIABLES].items(): native = SERVICE_ARG_NATIVE_TYPES[var_] template_args.append(native) func_args.append((native, name)) service_arg_names.append(name) templ = cg.TemplateArguments(*template_args) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], templ, conf[CONF_SERVICE], service_arg_names) cg.add(var.register_user_service(trigger)) yield automation.build_automation(trigger, func_args, conf) cg.add_define('USE_API') cg.add_global(api_ns.using)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) cg.add(var.set_port(config[CONF_PORT])) cg.add(var.set_password(config[CONF_PASSWORD])) cg.add(var.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_])) templ = cg.TemplateArguments(*template_args) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], templ, conf[CONF_SERVICE], service_type_args) cg.add(var.register_user_service(trigger)) yield automation.build_automation(trigger, func_args, conf) cg.add_define('USE_API') if CORE.is_esp32: cg.add_library('AsyncTCP', '1.0.3') elif CORE.is_esp8266: cg.add_library('ESPAsyncTCP', '1.2.0')
def to_code(config): for conf in config: var = cg.new_Pvariable(conf[CONF_ID]) yield cg.register_component(var, conf) yield automation.build_automation(var, [], conf) cg.add(var.set_update_interval(conf[CONF_INTERVAL]))
def http_request_action_to_code(config, action_id, template_arg, args): paren = yield cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, paren) template_ = yield cg.templatable(config[CONF_URL], args, cg.std_string) cg.add(var.set_url(template_)) cg.add(var.set_method(config[CONF_METHOD])) if CONF_BODY in config: template_ = yield cg.templatable(config[CONF_BODY], args, cg.std_string) cg.add(var.set_body(template_)) if CONF_JSON in config: json_ = config[CONF_JSON] if isinstance(json_, Lambda): args_ = args + [(cg.JsonObjectRef, "root")] lambda_ = yield cg.process_lambda(json_, args_, return_type=cg.void) cg.add(var.set_json(lambda_)) else: for key in json_: template_ = yield cg.templatable(json_[key], args, cg.std_string) cg.add(var.add_json(key, template_)) for key in config.get(CONF_HEADERS, []): template_ = yield cg.templatable( config[CONF_HEADERS][key], args, cg.const_char_ptr ) cg.add(var.add_header(key, template_)) for conf in config.get(CONF_ON_RESPONSE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) cg.add(var.register_response_trigger(trigger)) yield automation.build_automation(trigger, [(int, "status_code")], conf) yield var
def to_code(config): cg.add_global(cg.global_ns.namespace('esphome').using) cg.add(cg.App.pre_setup(config[CONF_NAME], cg.RawExpression('__DATE__ ", " __TIME__'))) for conf in config.get(CONF_ON_BOOT, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf.get(CONF_PRIORITY)) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_LOOP, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) # Set LWIP build constants for ESP8266 if CORE.is_esp8266: CORE.add_job(_esp8266_add_lwip_type) cg.add_build_flag('-fno-exceptions') # Libraries if CORE.is_esp32: cg.add_library('ESPmDNS', None) elif CORE.is_esp8266: cg.add_library('ESP8266WiFi', None) cg.add_library('ESP8266mDNS', None) for lib in config[CONF_LIBRARIES]: if '@' in lib: name, vers = lib.split('@', 1) cg.add_library(name, vers) else: cg.add_library(lib, None) cg.add_build_flag('-Wno-unused-variable') cg.add_build_flag('-Wno-unused-but-set-variable') cg.add_build_flag('-Wno-sign-compare') if config.get(CONF_ESP8266_RESTORE_FROM_FLASH, False): cg.add_define('USE_ESP8266_PREFERENCES_FLASH') if config[CONF_INCLUDES]: CORE.add_job(add_includes, config[CONF_INCLUDES])
def to_code(config): baud_rate = config[CONF_BAUD_RATE] rhs = Logger.new( baud_rate, config[CONF_TX_BUFFER_SIZE], HARDWARE_UART_TO_UART_SELECTION[config[CONF_HARDWARE_UART]]) log = cg.Pvariable(config[CONF_ID], rhs) cg.add(log.pre_setup()) for tag, level in config[CONF_LOGS].items(): cg.add(log.set_log_level(tag, LOG_LEVELS[level])) level = config[CONF_LEVEL] cg.add_define('USE_LOGGER') this_severity = LOG_LEVEL_SEVERITY.index(level) cg.add_build_flag('-DESPHOME_LOG_LEVEL={}'.format(LOG_LEVELS[level])) verbose_severity = LOG_LEVEL_SEVERITY.index('VERBOSE') very_verbose_severity = LOG_LEVEL_SEVERITY.index('VERY_VERBOSE') is_at_least_verbose = this_severity >= verbose_severity is_at_least_very_verbose = this_severity >= very_verbose_severity has_serial_logging = baud_rate != 0 if CORE.is_esp8266 and has_serial_logging and is_at_least_verbose: debug_serial_port = HARDWARE_UART_TO_SERIAL[config.get( CONF_HARDWARE_UART)] cg.add_build_flag(f"-DDEBUG_ESP_PORT={debug_serial_port}") cg.add_build_flag("-DLWIP_DEBUG") DEBUG_COMPONENTS = { 'HTTP_CLIENT', 'HTTP_SERVER', 'HTTP_UPDATE', 'OTA', 'SSL', 'TLS_MEM', 'UPDATER', 'WIFI', # Spams logs too much: # 'MDNS_RESPONDER', } for comp in DEBUG_COMPONENTS: cg.add_build_flag(f"-DDEBUG_ESP_{comp}") if CORE.is_esp32 and is_at_least_verbose: cg.add_build_flag('-DCORE_DEBUG_LEVEL=5') if CORE.is_esp32 and is_at_least_very_verbose: cg.add_build_flag('-DENABLE_I2C_DEBUG_BUFFER') if config.get(CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH): cg.add_build_flag('-DUSE_STORE_LOG_STR_IN_FLASH') # Register at end for safe mode yield cg.register_component(log, config) for conf in config.get(CONF_ON_MESSAGE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], log, LOG_LEVEL_SEVERITY.index(conf[CONF_LEVEL])) yield automation.build_automation(trigger, [(cg.int_, 'level'), (cg.const_char_ptr, 'tag'), (cg.const_char_ptr, 'message')], conf)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) yield cg.register_component(var, config) yield uart.register_uart_device(var, config) for conf in config.get(CONF_ON_FINISHED_PLAYBACK, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield automation.build_automation(trigger, [], conf)
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_automation(trigger, NoArg, 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_automation(trigger, NoArg, conf) setup_mqtt_component(switch_var.Pget_mqtt(), config)
def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) if len(config[CONF_SERVICE_UUID]) == len( esp32_ble_tracker.bt_uuid16_format): cg.add( var.set_service_uuid16( esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]))) elif len(config[CONF_SERVICE_UUID]) == len( esp32_ble_tracker.bt_uuid32_format): cg.add( var.set_service_uuid32( esp32_ble_tracker.as_hex(config[CONF_SERVICE_UUID]))) elif len(config[CONF_SERVICE_UUID]) == len( esp32_ble_tracker.bt_uuid128_format): uuid128 = esp32_ble_tracker.as_hex_array(config[CONF_SERVICE_UUID]) cg.add(var.set_service_uuid128(uuid128)) if len(config[CONF_CHARACTERISTIC_UUID]) == len( esp32_ble_tracker.bt_uuid16_format): cg.add( var.set_char_uuid16( esp32_ble_tracker.as_hex(config[CONF_CHARACTERISTIC_UUID]))) elif len(config[CONF_CHARACTERISTIC_UUID]) == len( esp32_ble_tracker.bt_uuid32_format): cg.add( var.set_char_uuid32( esp32_ble_tracker.as_hex(config[CONF_CHARACTERISTIC_UUID]))) elif len(config[CONF_CHARACTERISTIC_UUID]) == len( esp32_ble_tracker.bt_uuid128_format): uuid128 = esp32_ble_tracker.as_hex_array( config[CONF_CHARACTERISTIC_UUID]) cg.add(var.set_char_uuid128(uuid128)) if CONF_DESCRIPTOR_UUID in config: if len(config[CONF_DESCRIPTOR_UUID]) == len( esp32_ble_tracker.bt_uuid16_format): cg.add( var.set_descr_uuid16( esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]))) elif len(config[CONF_DESCRIPTOR_UUID]) == len( esp32_ble_tracker.bt_uuid32_format): cg.add( var.set_descr_uuid32( esp32_ble_tracker.as_hex(config[CONF_DESCRIPTOR_UUID]))) elif len(config[CONF_DESCRIPTOR_UUID]) == len( esp32_ble_tracker.bt_uuid128_format): uuid128 = esp32_ble_tracker.as_hex_array( config[CONF_DESCRIPTOR_UUID]) cg.add(var.set_descr_uuid128(uuid128)) yield cg.register_component(var, config) yield ble_client.register_ble_node(var, config) cg.add(var.set_enable_notify(config[CONF_NOTIFY])) yield sensor.register_sensor(var, config) for conf in config.get(CONF_ON_NOTIFY, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) yield ble_client.register_ble_node(trigger, config) yield automation.build_automation(trigger, [(float, "x")], conf)