def to_code(config): rhs = App.register_component(ESP32Camera.new(config[CONF_NAME])) cam = Pvariable(config[CONF_ID], rhs) for key, setter in SETTERS.items(): if key in config: add(getattr(cam, setter)(config[key])) extclk = config[CONF_EXTERNAL_CLOCK] add(cam.set_external_clock(extclk[CONF_PIN], extclk[CONF_FREQUENCY])) i2c_pins = config[CONF_I2C_PINS] add(cam.set_i2c_pins(i2c_pins[CONF_SDA], i2c_pins[CONF_SCL])) add(cam.set_max_update_interval(1000 / config[CONF_MAX_FRAMERATE])) if config[CONF_IDLE_FRAMERATE] == 0: add(cam.set_idle_update_interval(0)) else: add(cam.set_idle_update_interval(1000 / config[CONF_IDLE_FRAMERATE])) add(cam.set_frame_size(FRAME_SIZES[config[CONF_RESOLUTION]]))
def to_code(config): rhs = App.init_wifi() wifi = Pvariable(config[CONF_ID], rhs) add(wifi.set_use_address(config[CONF_USE_ADDRESS])) for network in config.get(CONF_NETWORKS, []): add(wifi.add_sta(wifi_network(network, config.get(CONF_MANUAL_IP)))) if CONF_AP in config: add(wifi.set_ap(wifi_network(config[CONF_AP], config.get(CONF_MANUAL_IP)))) if CONF_REBOOT_TIMEOUT in config: add(wifi.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) if CONF_POWER_SAVE_MODE in config: add(wifi.set_power_save_mode(WIFI_POWER_SAVE_MODES[config[CONF_POWER_SAVE_MODE]])) if CONF_FAST_CONNECT in config: add(wifi.set_fast_connect(config[CONF_FAST_CONNECT]))
def to_code(config): for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_gpio_switch(config[CONF_NAME], pin) gpio = Pvariable(config[CONF_ID], rhs) if CONF_RESTORE_MODE in config: add(gpio.set_restore_mode(RESTORE_MODES[config[CONF_RESTORE_MODE]])) if CONF_INTERLOCK in config: interlock = [] for it in config[CONF_INTERLOCK]: for lock in get_variable(it): yield interlock.append(lock) add(gpio.set_interlock(interlock)) switch.setup_switch(gpio, config) setup_component(gpio, config)
def to_code(config): for uart_ in get_variable(config[CONF_UART_ID]): yield rhs = App.make_sds011(uart_) sds011 = Pvariable(config[CONF_ID], rhs) if CONF_UPDATE_INTERVAL in config: add(sds011.set_update_interval_min(config.get(CONF_UPDATE_INTERVAL))) if CONF_RX_ONLY in config: add(sds011.set_rx_mode_only(config[CONF_RX_ONLY])) if CONF_PM_2_5 in config: conf = config[CONF_PM_2_5] sensor.register_sensor(sds011.make_pm_2_5_sensor(conf[CONF_NAME]), conf) if CONF_PM_10_0 in config: conf = config[CONF_PM_10_0] sensor.register_sensor(sds011.make_pm_10_0_sensor(conf[CONF_NAME]), conf) setup_component(sds011, config)
def to_code(config): rhs = App.make_hmc5883l(config.get(CONF_UPDATE_INTERVAL)) hmc = Pvariable(config[CONF_ID], rhs) if CONF_ADDRESS in config: add(hmc.set_address(config[CONF_ADDRESS])) if CONF_RANGE in config: add(hmc.set_range(HMC5883L_RANGES[config[CONF_RANGE]])) if CONF_FIELD_STRENGTH_X in config: conf = config[CONF_FIELD_STRENGTH_X] sensor.register_sensor(hmc.Pmake_x_sensor(conf[CONF_NAME]), conf) if CONF_FIELD_STRENGTH_Y in config: conf = config[CONF_FIELD_STRENGTH_Y] sensor.register_sensor(hmc.Pmake_y_sensor(conf[CONF_NAME]), conf) if CONF_FIELD_STRENGTH_Z in config: conf = config[CONF_FIELD_STRENGTH_Z] sensor.register_sensor(hmc.Pmake_z_sensor(conf[CONF_NAME]), conf) if CONF_HEADING in config: conf = config[CONF_HEADING] sensor.register_sensor(hmc.Pmake_heading_sensor(conf[CONF_NAME]), conf) setup_component(hmc, config)
def to_code(config): for hub in get_variable(config[CONF_ESP32_BLE_ID]): yield rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS])) dev = Pvariable(config[CONF_ID], rhs) if CONF_TEMPERATURE in config: conf = config[CONF_TEMPERATURE] sensor.register_sensor(dev.Pmake_temperature_sensor(conf[CONF_NAME]), conf) if CONF_MOISTURE in config: conf = config[CONF_MOISTURE] sensor.register_sensor(dev.Pmake_moisture_sensor(conf[CONF_NAME]), conf) if CONF_ILLUMINANCE in config: conf = config[CONF_ILLUMINANCE] sensor.register_sensor(dev.Pmake_illuminance_sensor(conf[CONF_NAME]), conf) if CONF_CONDUCTIVITY in config: conf = config[CONF_CONDUCTIVITY] sensor.register_sensor(dev.Pmake_conductivity_sensor(conf[CONF_NAME]), conf) if CONF_BATTERY_LEVEL in config: conf = config[CONF_BATTERY_LEVEL] sensor.register_sensor(dev.Pmake_battery_level_sensor(conf[CONF_NAME]), conf)
def to_code(config): for pin_a in gpio_output_pin_expression(config[CONF_PIN_A]): yield for pin_b in gpio_output_pin_expression(config[CONF_PIN_B]): yield for pin_c in gpio_output_pin_expression(config[CONF_PIN_C]): yield for pin_d in gpio_output_pin_expression(config[CONF_PIN_D]): yield rhs = App.make_uln2003(pin_a, pin_b, pin_c, pin_d) uln = Pvariable(config[CONF_ID], rhs) if CONF_SLEEP_WHEN_DONE in config: add(uln.set_sleep_when_done(config[CONF_SLEEP_WHEN_DONE])) if CONF_STEP_MODE in config: add(uln.set_step_mode(STEP_MODES[config[CONF_STEP_MODE]])) stepper.setup_stepper(uln, config) setup_component(uln, config)
def mqtt_publish_json_action_to_code(config, action_id, arg_type, template_arg): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_publish_json_action(template_arg) type = MQTTPublishJsonAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): yield None add(action.set_topic(template_)) for lambda_ in process_lambda(config[CONF_PAYLOAD], [(arg_type, 'x'), (JsonObjectRef, 'root')], return_type=void): yield None add(action.set_payload(lambda_)) if CONF_QOS in config: add(action.set_qos(config[CONF_QOS])) if CONF_RETAIN in config: add(action.set_retain(config[CONF_RETAIN])) yield action
def to_code(config): ssd = Pvariable(config[CONF_ID], App.make_i2c_ssd1306()) add(ssd.set_model(ssd1306_spi.MODELS[config[CONF_MODEL]])) if CONF_RESET_PIN in config: for reset in gpio_output_pin_expression(config[CONF_RESET_PIN]): yield add(ssd.set_reset_pin(reset)) if CONF_EXTERNAL_VCC in config: add(ssd.set_external_vcc(config[CONF_EXTERNAL_VCC])) if CONF_ADDRESS in config: add(ssd.set_address(config[CONF_ADDRESS])) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=void): yield add(ssd.set_writer(lambda_)) display.setup_display(ssd, config) setup_component(ssd, config)
def homeassistant_service_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_home_assistant_service_call_action(template_arg) type = HomeAssistantServiceCallAction.template(template_arg) act = Pvariable(action_id, rhs, type=type) add(act.set_service(config[CONF_SERVICE])) if CONF_DATA in config: datas = [KeyValuePair(k, v) for k, v in config[CONF_DATA].items()] add(act.set_data(datas)) if CONF_DATA_TEMPLATE in config: datas = [KeyValuePair(k, v) for k, v in config[CONF_DATA_TEMPLATE].items()] add(act.set_data_template(datas)) if CONF_VARIABLES in config: datas = [] for key, value in config[CONF_VARIABLES].items(): for value_ in process_lambda(value, []): yield None datas.append(TemplatableKeyValuePair(key, value_)) add(act.set_variables(datas)) yield act
def to_code(config): rhs = App.make_bmp280_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_PRESSURE][CONF_NAME], config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL)) bmp280 = Pvariable(config[CONF_ID], rhs) if CONF_OVERSAMPLING in config[CONF_TEMPERATURE]: constant = OVERSAMPLING_OPTIONS[config[CONF_TEMPERATURE] [CONF_OVERSAMPLING]] add(bmp280.set_temperature_oversampling(constant)) if CONF_OVERSAMPLING in config[CONF_PRESSURE]: constant = OVERSAMPLING_OPTIONS[config[CONF_PRESSURE] [CONF_OVERSAMPLING]] add(bmp280.set_pressure_oversampling(constant)) if CONF_IIR_FILTER in config: constant = IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]] add(bmp280.set_iir_filter(constant)) sensor.setup_sensor(bmp280.Pget_temperature_sensor(), config[CONF_TEMPERATURE]) sensor.setup_sensor(bmp280.Pget_pressure_sensor(), config[CONF_PRESSURE]) setup_component(bmp280, config)
def mqtt_publish_action_to_code(config, action_id, arg_type, template_arg): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_publish_action(template_arg) type = MQTTPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): yield None add(action.set_topic(template_)) for template_ in templatable(config[CONF_PAYLOAD], arg_type, std_string): yield None add(action.set_payload(template_)) if CONF_QOS in config: for template_ in templatable(config[CONF_QOS], arg_type, uint8): yield add(action.set_qos(template_)) if CONF_RETAIN in config: for template_ in templatable(config[CONF_RETAIN], arg_type, bool_): yield None add(action.set_retain(template_)) yield 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_max7219(spi_, cs) max7219 = Pvariable(config[CONF_ID], rhs) if CONF_NUM_CHIPS in config: add(max7219.set_num_chips(config[CONF_NUM_CHIPS])) if CONF_INTENSITY in config: add(max7219.set_intensity(config[CONF_INTENSITY])) if CONF_LAMBDA in config: for lambda_ in process_lambda(config[CONF_LAMBDA], [(MAX7219ComponentRef, 'it')], return_type=void): yield add(max7219.set_writer(lambda_)) display.setup_display(max7219, config) setup_component(max7219, config)
def to_code(config): from PIL import ImageFont path = CORE.relative_path(config[CONF_FILE]) try: font = ImageFont.truetype(path, config[CONF_SIZE]) except Exception as e: raise core.EsphomeError(u"Could not load truetype file {}: {}".format( path, e)) ascent, descent = font.getmetrics() glyph_args = {} data = [] for glyph in config[CONF_GLYPHS]: mask = font.getmask(glyph, mode='1') _, (offset_x, offset_y) = font.font.getsize(glyph) width, height = mask.size width8 = ((width + 7) // 8) * 8 glyph_data = [0 for _ in range(height * width8 // 8)] # noqa: F812 for y in range(height): for x in range(width): if not mask.getpixel((x, y)): continue pos = x + y * width8 glyph_data[pos // 8] |= 0x80 >> (pos % 8) glyph_args[glyph] = (len(data), offset_x, offset_y, width, height) data += glyph_data rhs = safe_exp([HexInt(x) for x in data]) prog_arr = progmem_array(config[CONF_RAW_DATA_ID], rhs) glyphs = [] for glyph in config[CONF_GLYPHS]: glyphs.append(Glyph(glyph, prog_arr, *glyph_args[glyph])) rhs = App.make_font(glyphs, ascent, ascent + descent) Pvariable(config[CONF_ID], rhs)
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 to_code(config): rhs = App.make_bme680_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_PRESSURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], config[CONF_GAS_RESISTANCE][CONF_NAME], config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL)) bme680 = Pvariable(config[CONF_ID], rhs) if CONF_OVERSAMPLING in config[CONF_TEMPERATURE]: constant = OVERSAMPLING_OPTIONS[config[CONF_TEMPERATURE] [CONF_OVERSAMPLING]] add(bme680.set_temperature_oversampling(constant)) if CONF_OVERSAMPLING in config[CONF_PRESSURE]: constant = OVERSAMPLING_OPTIONS[config[CONF_PRESSURE] [CONF_OVERSAMPLING]] add(bme680.set_pressure_oversampling(constant)) if CONF_OVERSAMPLING in config[CONF_HUMIDITY]: constant = OVERSAMPLING_OPTIONS[config[CONF_HUMIDITY] [CONF_OVERSAMPLING]] add(bme680.set_humidity_oversampling(constant)) if CONF_IIR_FILTER in config: constant = IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]] add(bme680.set_iir_filter(constant)) if CONF_HEATER in config: conf = config[CONF_HEATER] if not conf: add(bme680.set_heater(0, 0)) else: add(bme680.set_heater(conf[CONF_TEMPERATURE], conf[CONF_DURATION])) sensor.setup_sensor(bme680.Pget_temperature_sensor(), config[CONF_TEMPERATURE]) sensor.setup_sensor(bme680.Pget_pressure_sensor(), config[CONF_PRESSURE]) sensor.setup_sensor(bme680.Pget_humidity_sensor(), config[CONF_HUMIDITY]) sensor.setup_sensor(bme680.Pget_gas_resistance_sensor(), config[CONF_GAS_RESISTANCE]) setup_component(bme680, config)
def to_code(config): rhs = App.make_esp32_touch_component() touch = Pvariable(config[CONF_ID], rhs) if CONF_SETUP_MODE in config: add(touch.set_setup_mode(config[CONF_SETUP_MODE])) if CONF_IIR_FILTER in config: add(touch.set_iir_filter(config[CONF_IIR_FILTER])) if CONF_SLEEP_DURATION in config: sleep_duration = int(config[CONF_SLEEP_DURATION].total_microseconds * 0.15) add(touch.set_sleep_duration(sleep_duration)) if CONF_MEASUREMENT_DURATION in config: measurement_duration = int(config[CONF_MEASUREMENT_DURATION].total_microseconds * 0.125) add(touch.set_measurement_duration(measurement_duration)) if CONF_LOW_VOLTAGE_REFERENCE in config: value = LOW_VOLTAGE_REFERENCE[config[CONF_LOW_VOLTAGE_REFERENCE]] add(touch.set_low_voltage_reference(value)) if CONF_HIGH_VOLTAGE_REFERENCE in config: value = HIGH_VOLTAGE_REFERENCE[config[CONF_HIGH_VOLTAGE_REFERENCE]] add(touch.set_high_voltage_reference(value)) if CONF_VOLTAGE_ATTENUATION in config: value = VOLTAGE_ATTENUATION[config[CONF_VOLTAGE_ATTENUATION]] add(touch.set_voltage_attenuation(value)) setup_component(touch, config)
def to_code(config): for sel in gpio_output_pin_expression(config[CONF_SEL_PIN]): yield rhs = App.make_hlw8012(sel, config[CONF_CF_PIN], config[CONF_CF1_PIN], config.get(CONF_UPDATE_INTERVAL)) hlw = Pvariable(config[CONF_ID], rhs) if CONF_VOLTAGE in config: conf = config[CONF_VOLTAGE] sensor.register_sensor(hlw.make_voltage_sensor(conf[CONF_NAME]), conf) if CONF_CURRENT in config: conf = config[CONF_CURRENT] sensor.register_sensor(hlw.make_current_sensor(conf[CONF_NAME]), conf) if CONF_POWER in config: conf = config[CONF_POWER] sensor.register_sensor(hlw.make_power_sensor(conf[CONF_NAME]), conf) if CONF_CURRENT_RESISTOR in config: add(hlw.set_current_resistor(config[CONF_CURRENT_RESISTOR])) if CONF_VOLTAGE_DIVIDER in config: add(hlw.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER])) if CONF_CHANGE_MODE_EVERY in config: add(hlw.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY])) setup_component(hlw, config)
def to_code(config): rhs = App.make_status_binary_sensor(config[CONF_NAME]) status = Pvariable(config[CONF_ID], rhs) binary_sensor.setup_binary_sensor(status, config) setup_component(status, config)
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 api_connected_to_code(config, condition_id, template_arg, args): rhs = APIConnectedCondition.new(template_arg) type = APIConnectedCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type)
def setup_conf(config, key, klass): if key in config: conf = config[key] rhs = App.register_component(klass.new(conf[CONF_NAME])) sensor_ = Pvariable(conf[CONF_ID], rhs) text_sensor.register_text_sensor(sensor_, conf)
def to_code(config): rhs = App.make_ads1115_component(config[CONF_ADDRESS]) var = Pvariable(config[CONF_ID], rhs) setup_component(var, config)
def register_text_sensor(var, config): if not CORE.has_id(config[CONF_ID]): var = Pvariable(config[CONF_ID], var, has_side_effects=True) add(App.register_text_sensor(var)) CORE.add_job(setup_text_sensor_core_, var, config)
def setup_text_sensor(text_sensor_obj, config): if not CORE.has_id(config[CONF_ID]): text_sensor_obj = Pvariable(config[CONF_ID], text_sensor_obj, has_side_effects=True) CORE.add_job(setup_text_sensor_core_, text_sensor_obj, config)
def to_code(config): for uart_ in get_variable(config[CONF_UART_ID]): yield rhs = App.make_rdm6300_component(uart_) var = Pvariable(config[CONF_ID], rhs) setup_component(var, config)
def to_code(config): rhs = App.make_dallas_component(config[CONF_PIN], config.get(CONF_UPDATE_INTERVAL)) var = Pvariable(config[CONF_ID], rhs) setup_component(var, config)
def fan_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) yield Pvariable(action_id, rhs, type=type)
def to_code(config): rhs = App.make_pca9685_component(config.get(CONF_FREQUENCY)) pca9685 = Pvariable(config[CONF_ID], rhs) if CONF_ADDRESS in config: add(pca9685.set_address(config[CONF_ADDRESS])) setup_component(pca9685, config)
def setup_fan(fan_obj, config): fan_var = Pvariable(config[CONF_ID], fan_obj, has_side_effects=False) CORE.add_job(setup_fan_core_, fan_var, config)