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_DATA_PIN], config[CONF_CLOCK_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, make.Pmqtt, config) setup_component(fast_led, config)
def to_code(config): hub = get_variable(config.get(CONF_ADS1115_ID), u'sensor::ADS1115Component') mux = RawExpression(MUX[config[CONF_MULTIPLEXER]]) gain = RawExpression(GAIN[config[CONF_GAIN]]) rhs = hub.get_sensor(config[CONF_NAME], mux, gain, config.get(CONF_UPDATE_INTERVAL)) sensor_ = Pvariable('sensor::ADS1115Sensor', config[CONF_ID], rhs) sensor.register_sensor(sensor_, config)
def to_code(config): for conf in config: type_ = RawExpression(conf[CONF_TYPE]) template_args = TemplateArguments(type_) res_type = GlobalVariableComponent.template(template_args) initial_value = None if CONF_INITIAL_VALUE in conf: initial_value = RawExpression(conf[CONF_INITIAL_VALUE]) rhs = App.Pmake_global_variable(template_args, initial_value) glob = Pvariable(conf[CONF_ID], rhs, type=res_type) if conf.get(CONF_RESTORE_VALUE, False): hash_ = hash(conf[CONF_ID].id) % 2**32 add(glob.set_restore_value(hash_)) setup_component(glob, conf)
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 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 to_code(config): rhs = App.make_pulse_counter_sensor(config[CONF_NAME], config[CONF_PIN], config.get(CONF_UPDATE_INTERVAL)) make = variable('Application::MakePulseCounterSensor', config[CONF_ID], rhs) pcnt = make.Ppcnt if CONF_PULL_MODE in config: pull_mode = GPIO_PULL_MODES[config[CONF_PULL_MODE]] add(pcnt.set_pull_mode(RawExpression(pull_mode))) if CONF_COUNT_MODE in config: count_mode = config[CONF_COUNT_MODE] rising_edge = COUNT_MODES[count_mode[CONF_RISING_EDGE]] falling_edge = COUNT_MODES[count_mode[CONF_FALLING_EDGE]] add(pcnt.set_edge_mode(RawExpression(rising_edge), RawExpression(falling_edge))) if CONF_INTERNAL_FILTER in config: add(pcnt.set_filter(config[CONF_INTERNAL_FILTER])) sensor.setup_sensor(pcnt, config) sensor.setup_mqtt_sensor_component(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('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): 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): 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(ArrayInitializer(*uuid_arr, multiline=False)) 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 to_code(config): rhs = App.make_fast_led_light(config[CONF_NAME]) make = variable(MakeFastLEDLight, 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: power_supply = get_variable(config[CONF_POWER_SUPPLY]) add(fast_led.set_power_supply(power_supply)) light.setup_light(make.Pstate, make.Pmqtt, config)
def logger_log_action_to_code(config, action_id, arg_type): template_arg = TemplateArguments(arg_type) esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] args = [RawExpression(unicode(x)) for x in config[CONF_ARGS]] text = unicode( statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args))) for lambda_ in process_lambda(Lambda(text), [(arg_type, 'x')]): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) yield Pvariable(action_id, rhs, type=type)
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) if not config.get(CONF_DISCOVERY, True): add(mqtt.disable_discovery()) elif CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config: discovery_retain = config.get(CONF_DISCOVERY_RETAIN, True) discovery_prefix = config.get(CONF_DISCOVERY_PREFIX, 'homeassistant') 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_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(ArrayInitializer(*arr, multiline=False))) 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 = mqtt.make_message_trigger(conf[CONF_TOPIC], conf[CONF_QOS]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, std_string, conf)
def to_code(config): rhs = App.make_dht_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], config[CONF_PIN], config.get(CONF_UPDATE_INTERVAL)) dht = variable('Application::MakeDHTSensor', config[CONF_ID], rhs) if CONF_MODEL in config: model = RawExpression('DHT::{}'.format(config[CONF_MODEL])) add(dht.Pdht.set_dht_model(model)) sensor.setup_sensor(dht.Pdht.Pget_temperature_sensor(), config[CONF_TEMPERATURE]) sensor.setup_mqtt_sensor_component(dht.Pmqtt_temperature, config[CONF_TEMPERATURE]) sensor.setup_sensor(dht.Pdht.Pget_humidity_sensor(), config[CONF_HUMIDITY]) sensor.setup_mqtt_sensor_component(dht.Pmqtt_humidity, config[CONF_HUMIDITY])
def to_code(config): from PIL import ImageFont for conf in config: path = relative_path(conf[CONF_FILE]) try: font = ImageFont.truetype(path, conf[CONF_SIZE]) except Exception as e: raise core.ESPHomeYAMLError( u"Could not load truetype file {}: {}".format(path, e)) ascent, descent = font.getmetrics() glyph_args = {} data = [] for glyph in conf[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 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)))) glyphs = [] for glyph in conf[CONF_GLYPHS]: glyphs.append(Glyph(glyph, raw_data, *glyph_args[glyph])) rhs = App.make_font(ArrayInitializer(*glyphs), ascent, ascent + descent) Pvariable(conf[CONF_ID], rhs)
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_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new()) automation.build_automation(trigger, const_char_p, conf) for conf in config.get(CONF_ON_LOOP, []): rhs = App.register_component(LoopTrigger.new()) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, conf) update_esphomelib_repo(config) add(App.set_compilation_datetime(RawExpression('__DATE__ ", " __TIME__')))
def setup_filter(config): if CONF_OFFSET in config: return OffsetFilter(config[CONF_OFFSET]) if CONF_MULTIPLY in config: return MultiplyFilter(config[CONF_MULTIPLY]) if CONF_FILTER_OUT in config: return FilterOutValueFilter(config[CONF_FILTER_OUT]) if CONF_FILTER_NAN in config: return FilterOutNANFilter() if CONF_SLIDING_WINDOW_MOVING_AVERAGE in config: conf = config[CONF_SLIDING_WINDOW_MOVING_AVERAGE] return SlidingWindowMovingAverageFilter(conf[CONF_WINDOW_SIZE], conf[CONF_SEND_EVERY]) if CONF_EXPONENTIAL_MOVING_AVERAGE in config: conf = config[CONF_EXPONENTIAL_MOVING_AVERAGE] return ExponentialMovingAverageFilter(conf[CONF_ALPHA], conf[CONF_SEND_EVERY]) if CONF_LAMBDA in config: s = u'[](float x) -> Optional<float> {{ return {}; }}'.format( config[CONF_LAMBDA]) return LambdaFilter(RawExpression(s)) raise ValueError(u"Filter unsupported: {}".format(config))
def to_code(config): rhs = App.init_mqtt(config[CONF_BROKER], config[CONF_PORT], config[CONF_USERNAME], config[CONF_PASSWORD]) mqtt = Pvariable('mqtt::MQTTClientComponent', config[CONF_ID], rhs) if not config.get(CONF_DISCOVERY, True): add(mqtt.disable_discovery()) if CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config: discovery_retain = config.get(CONF_DISCOVERY_RETAIN, True) discovery_prefix = config.get(CONF_DISCOVERY_PREFIX, 'homeassistant') 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 birth_message is None: 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 will_message is None: add(mqtt.disable_last_will()) else: add(mqtt.set_last_will(exp_mqtt_message(will_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 log_topic is None: add(mqtt.disable_log_message()) else: add(mqtt.set_log_topic(exp_mqtt_message(log_topic))) 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(ArrayInitializer(*arr, multiline=False))) if CONF_KEEPALIVE in config: add(mqtt.set_keep_alive(config[CONF_KEEPALIVE]))
def exp_log_level(level): return RawExpression(esphomelib_log_level(level))
def build_effect(full_config): key, config = next(iter(full_config.items())) if key == CONF_LAMBDA: lambda_ = None for lambda_ in process_lambda(config[CONF_LAMBDA], []): yield None yield LambdaLightEffect.new(config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]) elif key == CONF_RANDOM: rhs = RandomLightEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_TRANSITION_LENGTH in config: add(effect.set_transition_length(config[CONF_TRANSITION_LENGTH])) if CONF_UPDATE_INTERVAL in config: add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL])) yield effect elif key == CONF_STROBE: rhs = StrobeLightEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) colors = [] for color in config.get(CONF_COLORS, []): colors.append(StructInitializer( StrobeLightEffectColor, ('color', LightColorValues(color[CONF_STATE], color[CONF_BRIGHTNESS], color[CONF_RED], color[CONF_GREEN], color[CONF_BLUE], color[CONF_WHITE])), ('duration', color[CONF_DURATION]), )) if colors: add(effect.set_colors(ArrayInitializer(*colors))) yield effect elif key == CONF_FLICKER: rhs = FlickerLightEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_ALPHA in config: add(effect.set_alpha(config[CONF_ALPHA])) if CONF_INTENSITY in config: add(effect.set_intensity(config[CONF_INTENSITY])) yield effect elif key == CONF_FASTLED_LAMBDA: lambda_ = None args = [(RawExpression('FastLEDLightOutputComponent &'), 'it')] for lambda_ in process_lambda(config[CONF_LAMBDA], args): yield None yield FastLEDLambdaLightEffect.new(config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]) elif key == CONF_FASTLED_RAINBOW: rhs = FastLEDRainbowLightEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_SPEED in config: add(effect.set_speed(config[CONF_SPEED])) if CONF_WIDTH in config: add(effect.set_width(config[CONF_WIDTH])) yield effect elif key == CONF_FASTLED_COLOR_WIPE: rhs = FastLEDColorWipeEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_ADD_LED_INTERVAL in config: add(effect.set_add_led_interval(config[CONF_ADD_LED_INTERVAL])) if CONF_REVERSE in config: add(effect.set_reverse(config[CONF_REVERSE])) colors = [] for color in config.get(CONF_COLORS, []): colors.append(StructInitializer( FastLEDColorWipeEffectColor, ('r', color[CONF_RED]), ('g', color[CONF_GREEN]), ('b', color[CONF_BLUE]), ('random', color[CONF_RANDOM]), ('num_leds', color[CONF_NUM_LEDS]), )) if colors: add(effect.set_colors(ArrayInitializer(*colors))) yield effect elif key == CONF_FASTLED_SCAN: rhs = FastLEDScanEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_MOVE_INTERVAL in config: add(effect.set_move_interval(config[CONF_MOVE_INTERVAL])) yield effect elif key == CONF_FASTLED_TWINKLE: rhs = FastLEDTwinkleEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_TWINKLE_PROBABILITY in config: add(effect.set_twinkle_probability(config[CONF_TWINKLE_PROBABILITY])) if CONF_PROGRESS_INTERVAL in config: add(effect.set_progress_interval(config[CONF_PROGRESS_INTERVAL])) yield effect elif key == CONF_FASTLED_RANDOM_TWINKLE: rhs = FastLEDRandomTwinkleEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_TWINKLE_PROBABILITY in config: add(effect.set_twinkle_probability(config[CONF_TWINKLE_PROBABILITY])) if CONF_PROGRESS_INTERVAL in config: add(effect.set_progress_interval(config[CONF_PROGRESS_INTERVAL])) yield effect elif key == CONF_FASTLED_FIREWORKS: rhs = FastLEDFireworksEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_UPDATE_INTERVAL in config: add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL])) if CONF_SPARK_PROBABILITY in config: add(effect.set_spark_probability(config[CONF_SPARK_PROBABILITY])) if CONF_USE_RANDOM_COLOR in config: add(effect.set_spark_probability(config[CONF_USE_RANDOM_COLOR])) if CONF_FADE_OUT_RATE in config: add(effect.set_spark_probability(config[CONF_FADE_OUT_RATE])) yield effect elif key == CONF_FASTLED_FLICKER: rhs = FastLEDFlickerEffect.new(config[CONF_NAME]) effect = Pvariable(config[CONF_EFFECT_ID], rhs) if CONF_UPDATE_INTERVAL in config: add(effect.set_update_interval(config[CONF_UPDATE_INTERVAL])) if CONF_INTENSITY in config: add(effect.set_intensity(config[CONF_INTENSITY])) yield effect else: raise NotImplementedError("Effect {} not implemented".format(next(config.keys())))