def setup_light_core_(light_var, mqtt_var, config): if CONF_DEFAULT_TRANSITION_LENGTH in config: add(light_var.set_default_transition_length(config[CONF_DEFAULT_TRANSITION_LENGTH])) if CONF_GAMMA_CORRECT in config: add(light_var.set_gamma_correct(config[CONF_GAMMA_CORRECT])) setup_mqtt_component(mqtt_var, config)
def to_code(config): rhs = App.make_esp32_ble_tracker() ble = Pvariable(config[CONF_ID], rhs) if CONF_SCAN_INTERVAL in config: add(ble.set_scan_interval(config[CONF_SCAN_INTERVAL])) setup_component(ble, config)
def setup_switch_core_(switch_var, mqtt_var, config): 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])) setup_mqtt_component(mqtt_var, config)
def to_code(config): rhs = App.make_template_cover(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) if CONF_LAMBDA in config: template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template( cover.CoverState)): yield add(make.Ptemplate_.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_OPEN_ACTION], NoArg): yield add(make.Ptemplate_.add_open_actions(actions)) if CONF_CLOSE_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_CLOSE_ACTION], NoArg): yield add(make.Ptemplate_.add_close_actions(actions)) if CONF_STOP_ACTION in config: actions = None for actions in automation.build_actions(config[CONF_STOP_ACTION], NoArg): yield add(make.Ptemplate_.add_stop_actions(actions)) if CONF_OPTIMISTIC in config: add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) cover.setup_cover(make.Ptemplate_, make.Pmqtt, config)
def to_code(config): from PIL import Image for conf in config: path = relative_path(conf[CONF_FILE]) try: image = Image.open(path) except Exception as e: raise core.ESPHomeYAMLError(u"Could not load image file {}: {}".format(path, e)) if CONF_RESIZE in conf: image.thumbnail(conf[CONF_RESIZE]) image = image.convert('1', dither=Image.NONE) width, height = image.size if width > 500 or height > 500: _LOGGER.warning("The image you requested is very big. Please consider using the resize " "parameter") width8 = ((width + 7) // 8) * 8 data = [0 for _ in range(height * width8 // 8)] for y in range(height): for x in range(width): if image.getpixel((x, y)): continue pos = x + y * width8 data[pos // 8] |= 0x80 >> (pos % 8) raw_data = MockObj(conf[CONF_RAW_DATA_ID]) add(RawExpression('static const uint8_t {}[{}] PROGMEM = {}'.format( raw_data, len(data), ArrayInitializer(*[HexInt(x) for x in data], multiline=False)))) rhs = App.make_image(raw_data, width, height) Pvariable(conf[CONF_ID], rhs)
def setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config): 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])) for conf in config.get(CONF_ON_PRESS, []): rhs = binary_sensor_var.make_press_trigger() trigger = Pvariable(PressTrigger, conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_RELEASE, []): rhs = binary_sensor_var.make_release_trigger() trigger = Pvariable(ReleaseTrigger, conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, 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(ClickTrigger, conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, 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(DoubleClickTrigger, conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, conf) setup_mqtt_component(mqtt_var, config)
def write_cpp(config): _LOGGER.info("Generating C++ source...") add_job(core_to_code, config[CONF_ESPHOMEYAML], domain='esphomeyaml') for domain in PRE_INITIALIZE: if domain == CONF_ESPHOMEYAML or domain not in config: continue add_job(get_component(domain).to_code, config[domain], domain=domain) for domain, component, conf in iter_components(config): if domain in PRE_INITIALIZE or not hasattr(component, 'to_code'): continue add_job(component.to_code, conf, domain=domain) flush_tasks() add(RawStatement('')) add(RawStatement('')) all_code = [] for exp in _EXPRESSIONS: if core.SIMPLIFY: if isinstance(exp, Expression) and not exp.required: continue if isinstance(exp, AssignmentExpression) and not exp.obj.required: if not exp.has_side_effects(): continue exp = exp.rhs all_code.append(unicode(statement(exp))) writer.write_platformio_project(config, get_base_path(config)) code_s = indent('\n'.join(line.rstrip() for line in all_code)) cpp_path = os.path.join(get_base_path(config), 'src', 'main.cpp') writer.write_cpp(code_s, cpp_path) return 0
def to_code(config): rhs = App.make_ina3221(config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL)) ina = Pvariable(config[CONF_ID], rhs) for i, channel in enumerate( [CONF_CHANNEL_1, CONF_CHANNEL_2, CONF_CHANNEL_3]): if channel not in config: continue conf = config[channel] if CONF_SHUNT_RESISTANCE in conf: add(ina.set_shunt_resistance(i, conf[CONF_SHUNT_RESISTANCE])) if CONF_BUS_VOLTAGE in conf: c = conf[CONF_BUS_VOLTAGE] sensor.register_sensor( ina.Pmake_bus_voltage_sensor(i, c[CONF_NAME]), c) if CONF_SHUNT_VOLTAGE in conf: c = conf[CONF_SHUNT_VOLTAGE] sensor.register_sensor( ina.Pmake_shunt_voltage_sensor(i, c[CONF_NAME]), c) if CONF_CURRENT in conf: c = conf[CONF_CURRENT] sensor.register_sensor(ina.Pmake_current_sensor(i, c[CONF_NAME]), c) if CONF_POWER in conf: c = conf[CONF_POWER] sensor.register_sensor(ina.Pmake_power_sensor(i, c[CONF_NAME]), c) setup_component(ina, config)
def setup_mqtt_sensor_component(obj, config): if CONF_EXPIRE_AFTER in config: if config[CONF_EXPIRE_AFTER] is None: add(obj.disable_expire_after()) else: add(obj.set_expire_after(config[CONF_EXPIRE_AFTER])) setup_mqtt_component(obj, config)
def to_code(config): rhs = App.make_gpio_lcd_display(config[CONF_DIMENSIONS][0], config[CONF_DIMENSIONS][1]) lcd = Pvariable(config[CONF_ID], rhs) pins_ = [] for conf in config[CONF_DATA_PINS]: for pin in gpio_output_pin_expression(conf): yield pins_.append(pin) add(lcd.set_data_pins(*pins_)) for enable in gpio_output_pin_expression(config[CONF_ENABLE_PIN]): yield add(lcd.set_enable_pin(enable)) for rs in gpio_output_pin_expression(config[CONF_RS_PIN]): yield add(lcd.set_rs_pin(rs)) if CONF_RW_PIN in config: for rw in gpio_output_pin_expression(config[CONF_RW_PIN]): yield add(lcd.set_rw_pin(rw)) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')]): yield add(lcd.set_writer(lambda_)) display.setup_display(lcd, config)
def to_code(config): for conf in config: address = HexIntLiteral(conf[CONF_ADDRESS]) rhs = App.make_ads1115_component(address) ads1115 = Pvariable(ADS1115_COMPONENT_CLASS, conf[CONF_ID], rhs) if CONF_RATE in conf: add(ads1115.set_rate(RawExpression(RATES[conf[CONF_RATE]])))
def build_automation(trigger, arg_type, config): rhs = App.make_automation(trigger) obj = Pvariable(Automation.template(arg_type), config[CONF_AUTOMATION_ID], rhs) if CONF_IF in config: add(obj.add_conditions(build_conditions(config[CONF_IF], arg_type))) add(obj.add_actions(build_actions(config[CONF_THEN], arg_type)))
def to_code(config): rhs = App.init_web_server(config.get(CONF_PORT)) web_server = Pvariable('WebServer', config[CONF_ID], rhs) if CONF_CSS_URL in config: add(web_server.set_css_url(config[CONF_CSS_URL])) if CONF_JS_URL in config: add(web_server.set_js_url(config[CONF_JS_URL]))
def to_code(config): rhs = App.init_ota() ota = Pvariable(OTAComponent, config[CONF_ID], rhs) if CONF_PASSWORD in config: hash_ = hashlib.md5(config[CONF_PASSWORD].encode()).hexdigest() add(ota.set_auth_password_hash(hash_)) if config[CONF_SAFE_MODE]: add(ota.start_safe_mode())
def to_code(config): rhs = App.init_i2c(config[CONF_SDA], config[CONF_SCL], config.get(CONF_SCAN)) i2c = Pvariable(config[CONF_ID], rhs) if CONF_FREQUENCY in config: add(i2c.set_frequency(config[CONF_FREQUENCY])) setup_component(i2c, config)
def to_code(config): for conf in config: rhs = App.make_power_supply(gpio_output_pin_expression(conf[CONF_PIN])) psu = Pvariable(PowerSupplyComponent, conf[CONF_ID], rhs) if CONF_ENABLE_TIME in conf: add(psu.set_enable_time(conf[CONF_ENABLE_TIME])) if CONF_KEEP_ON_TIME in conf: add(psu.set_keep_on_time(conf[CONF_KEEP_ON_TIME]))
def to_code(config): rhs = App.init_i2c(config[CONF_SDA], config[CONF_SCL], config.get(CONF_SCAN)) i2c = Pvariable(I2CComponent, config[CONF_ID], rhs) if CONF_FREQUENCY in config: add(i2c.set_frequency(config[CONF_FREQUENCY])) if CONF_RECEIVE_TIMEOUT in config: add(i2c.set_receive_timeout(config[CONF_RECEIVE_TIMEOUT]))
def to_code(config): rhs = App.make_sntp_component() sntp = Pvariable(config[CONF_ID], rhs) if CONF_SERVERS in config: add(sntp.set_servers(*config[CONF_SERVERS])) time_.setup_time(sntp, config) setup_component(sntp, config)
def to_code(config): rhs = App.make_mqtt_subscribe_sensor(config[CONF_NAME], config[CONF_TOPIC]) make = variable(config[CONF_MAKE_ID], rhs) if CONF_QOS in config: add(make.Psensor.set_qos(config[CONF_QOS])) sensor.setup_sensor(make.Psensor, make.Pmqtt, config)
def to_code(config): rhs = App.make_adc_sensor(config[CONF_NAME], config[CONF_PIN], config.get(CONF_UPDATE_INTERVAL)) make = variable(MakeADCSensor, config[CONF_MAKE_ID], rhs) adc = make.Padc if CONF_ATTENUATION in config: add(adc.set_attenuation(ATTENUATION_MODES[config[CONF_ATTENUATION]])) sensor.setup_sensor(make.Padc, make.Pmqtt, config)
def to_code(config): rhs = App.make_gpio_binary_sensor(config[CONF_NAME], exp_gpio_input_pin(config[CONF_PIN])) gpio = variable('Application::MakeGPIOBinarySensor', config[CONF_ID], rhs) if CONF_INVERTED in config: add(gpio.Pgpio.set_inverted(config[CONF_INVERTED])) binary_sensor.setup_binary_sensor(gpio.Pgpio, config) binary_sensor.setup_mqtt_binary_sensor(gpio.Pmqtt, config)
def to_code(config): rhs = App.make_bh1750_sensor(config[CONF_NAME], config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL)) make_bh1750 = variable(config[CONF_MAKE_ID], rhs) bh1750 = make_bh1750.Pbh1750 if CONF_RESOLUTION in config: add(bh1750.set_resolution(BH1750_RESOLUTIONS[config[CONF_RESOLUTION]])) sensor.setup_sensor(bh1750, make_bh1750.Pmqtt, config)
def delay_action_to_code(config, action_id, arg_type): template_arg = TemplateArguments(arg_type) rhs = App.register_component(DelayAction.new(template_arg)) type = DelayAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config, arg_type, uint32): yield add(action.set_delay(template_)) yield action
def to_code(config): output = get_variable(config[CONF_OUTPUT]) rhs = App.make_fan(config[CONF_NAME]) fan_struct = variable('Application::MakeFan', config[CONF_ID], rhs) add(fan_struct.Poutput.set_binary(output)) if CONF_OSCILLATION_OUTPUT in config: oscillation_output = get_variable(config[CONF_OSCILLATION_OUTPUT]) add(fan_struct.Poutput.set_oscillation(oscillation_output)) fan.setup_mqtt_fan(fan_struct.Pmqtt, config)
def to_code(config): for conf in config: rhs = App.make_pca9685_component(conf.get(CONF_FREQUENCY)) pca9685 = Pvariable(PCA9685_COMPONENT_TYPE, conf[CONF_ID], rhs) if CONF_ADDRESS in conf: add(pca9685.set_address(HexIntLiteral(conf[CONF_ADDRESS]))) if CONF_PHASE_BALANCER in conf: phase_balancer = RawExpression(u'PCA9685_PhaseBalancer_{}'.format( conf[CONF_PHASE_BALANCER])) add(pca9685.set_phase_balancer(phase_balancer))
def to_code(config): red = get_variable(config[CONF_RED]) green = get_variable(config[CONF_GREEN]) blue = get_variable(config[CONF_BLUE]) rhs = App.make_rgb_light(config[CONF_NAME], red, green, blue) light_struct = variable('Application::MakeLight', config[CONF_ID], rhs) if CONF_GAMMA_CORRECT in config: add(light_struct.Poutput.set_gamma_correct(config[CONF_GAMMA_CORRECT])) setup_mqtt_component(light_struct.Pmqtt, config) light.setup_light_component(light_struct.Pstate, config)
def to_code(config): frequency = config.get(CONF_FREQUENCY) if frequency is None and CONF_BIT_DEPTH in config: frequency = 1000 rhs = App.make_ledc_output(config[CONF_PIN], frequency, config.get(CONF_BIT_DEPTH)) ledc = Pvariable(LEDCOutputComponent, config[CONF_ID], rhs) if CONF_CHANNEL in config: add(ledc.set_channel(config[CONF_CHANNEL])) output.setup_output_platform(ledc, config)
def to_code(config): rhs = App.make_sntp_component() sntp = Pvariable(config[CONF_ID], rhs) if CONF_SERVERS in config: servers = config[CONF_SERVERS] servers += [''] * (3 - len(servers)) add(sntp.set_servers(*servers)) time_.setup_time(sntp, config) setup_component(sntp, config)
def to_code(config): rhs = App.make_adc_sensor(config[CONF_NAME], config[CONF_PIN], config.get(CONF_UPDATE_INTERVAL)) make = variable('Application::MakeADCSensor', config[CONF_ID], rhs) adc = make.Padc if CONF_ATTENUATION in config: attenuation = ATTENUATION_MODES[config[CONF_ATTENUATION]] add(adc.set_attenuation(RawExpression(attenuation))) sensor.setup_sensor(adc, config) sensor.setup_mqtt_sensor_component(make.Pmqtt, config)
def to_code(config): rhs = App.make_template_binary_sensor(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config) template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(bool_)): yield add(make.Ptemplate_.set_template(template_))