def to_code(config): rhs = App.make_fast_led_light(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) fast_led = make.Pfast_led rgb_order = None if CONF_RGB_ORDER in config: rgb_order = RawExpression(config[CONF_RGB_ORDER]) template_args = TemplateArguments(RawExpression(config[CONF_CHIPSET]), config[CONF_PIN], rgb_order) add(fast_led.add_leds(template_args, config[CONF_NUM_LEDS])) if CONF_MAX_REFRESH_RATE in config: add(fast_led.set_max_refresh_rate(config[CONF_MAX_REFRESH_RATE])) if CONF_POWER_SUPPLY in config: for power_supply in get_variable(config[CONF_POWER_SUPPLY]): yield add(fast_led.set_power_supply(power_supply)) if CONF_COLOR_CORRECT in config: r, g, b = config[CONF_COLOR_CORRECT] add(fast_led.set_correction(r, g, b)) light.setup_light(make.Pstate, config) setup_component(fast_led, config)
def to_code(config): type_ = RawExpression(config[CONF_TYPE]) template_args = TemplateArguments(type_) res_type = GlobalVariableComponent.template(template_args) initial_value = None if CONF_INITIAL_VALUE in config: initial_value = RawExpression(config[CONF_INITIAL_VALUE]) rhs = App.Pmake_global_variable(template_args, initial_value) glob = Pvariable(config[CONF_ID], rhs, type=res_type) if config.get(CONF_RESTORE_VALUE, False): hash_ = hash(config[CONF_ID].id) % 2**32 add(glob.set_restore_value(hash_)) setup_component(glob, config)
def generic_gpio_pin_expression_(conf, mock_obj, default_mode): if conf is None: return number = conf[CONF_NUMBER] inverted = conf.get(CONF_INVERTED) if CONF_PCF8574 in conf: from esphome.components import pcf8574 for hub in CORE.get_variable(conf[CONF_PCF8574]): yield None if default_mode == u'INPUT': mode = pcf8574.PCF8675_GPIO_MODES[conf.get(CONF_MODE, u'INPUT')] yield hub.make_input_pin(number, mode, inverted) return if default_mode == u'OUTPUT': yield hub.make_output_pin(number, inverted) return raise EsphomeError(u"Unknown default mode {}".format(default_mode)) if len(conf) == 1: yield IntLiteral(number) return mode = RawExpression(conf.get(CONF_MODE, default_mode)) yield mock_obj(number, mode, inverted)
def to_code(config): uuid = config[CONF_UUID].hex uuid_arr = [RawExpression('0x{}'.format(uuid[i:i + 2])) for i in range(0, len(uuid), 2)] rhs = App.make_esp32_ble_beacon(uuid_arr) ble = Pvariable(config[CONF_ID], rhs) if CONF_MAJOR in config: add(ble.set_major(config[CONF_MAJOR])) if CONF_MINOR in config: add(ble.set_minor(config[CONF_MINOR])) setup_component(ble, config)
def logger_log_action_to_code(config, action_id, template_arg, args): esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] args_ = [RawExpression(text_type(x)) for x in config[CONF_ARGS]] text = text_type( statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args_))) for lambda_ in process_lambda(Lambda(text), args, return_type=void): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type)
async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_port(config[CONF_PORT])) cg.add(var.set_auth_password(config[CONF_PASSWORD])) await cg.register_component(var, config) if config[CONF_SAFE_MODE]: condition = var.should_enter_safe_mode(config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT]) cg.add(RawExpression(f"if ({condition}) return")) if CORE.is_esp8266: cg.add_library("Update", None) elif CORE.is_esp32: cg.add_library("Hash", None)
async def gpio_pin_expression(conf): """Generate an expression for the given pin option. This is a coroutine, you must await it with a 'await' expression! """ if conf is None: return from esphome import pins for key, (func, _) in pins.PIN_SCHEMA_REGISTRY.items(): if key in conf: return await coroutine(func)(conf) number = conf[CONF_NUMBER] mode = conf[CONF_MODE] inverted = conf.get(CONF_INVERTED) return GPIOPin.new(number, RawExpression(mode), inverted)
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__')))
async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_port(config[CONF_PORT])) if CONF_PASSWORD in config: cg.add(var.set_auth_password(config[CONF_PASSWORD])) cg.add_define("USE_OTA_PASSWORD") await cg.register_component(var, config) if config[CONF_SAFE_MODE]: condition = var.should_enter_safe_mode( config[CONF_NUM_ATTEMPTS], config[CONF_REBOOT_TIMEOUT] ) cg.add(RawExpression(f"if ({condition}) return")) if CORE.is_esp32 and CORE.using_arduino: cg.add_library("Update", None) use_state_callback = False for conf in config.get(CONF_ON_STATE_CHANGE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(OTAState, "state")], conf) use_state_callback = True for conf in config.get(CONF_ON_BEGIN, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) use_state_callback = True for conf in config.get(CONF_ON_PROGRESS, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(float, "x")], conf) use_state_callback = True for conf in config.get(CONF_ON_END, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [], conf) use_state_callback = True for conf in config.get(CONF_ON_ERROR, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) await automation.build_automation(trigger, [(int, "x")], conf) use_state_callback = True if use_state_callback: cg.add_define("USE_OTA_STATE_CALLBACK")
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_automation(trigger, std_string, 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_automation(trigger, JsonObjectConstRef, conf)
def as_hex(self): from esphome.cpp_generator import RawExpression num = "".join(f"{part:02X}" for part in self.parts) return RawExpression(f"0x{num}ULL")
def as_hex(self): from esphome.cpp_generator import RawExpression num = ''.join(f'{part:02X}' for part in self.parts) return RawExpression(f'0x{num}ULL')
def as_hex(self): from esphome.cpp_generator import RawExpression num = ''.join('{:02X}'.format(part) for part in self.parts) return RawExpression('0x{}ULL'.format(num))
def as_hex(self): num = ''.join(f'{part:02X}' for part in self.parts) return RawExpression(f'0x{num}ULL')