def build_conditions(config, arg_type): conditions = [] for conf in config: for condition in build_condition(conf, arg_type): yield None conditions.append(condition) yield ArrayInitializer(*conditions, multiline=False)
def setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config): if CONF_INTERNAL in config: add(binary_sensor_var.set_internal(CONF_INTERNAL)) 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])) if CONF_FILTERS in config: filters = None for filters in setup_filters(config[CONF_FILTERS]): yield add(binary_sensor_var.add_filters(filters)) for conf in config.get(CONF_ON_PRESS, []): rhs = binary_sensor_var.make_press_trigger() trigger = Pvariable(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(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(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(conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_MULTI_CLICK, []): timings = [] for tim in conf[CONF_TIMING]: timings.append( StructInitializer( MultiClickTriggerEvent, ('state', tim[CONF_STATE]), ('min_length', tim[CONF_MIN_LENGTH]), ('max_length', tim.get(CONF_MAX_LENGTH, 4294967294)), )) timings = ArrayInitializer(*timings, multiline=False) rhs = App.register_component( binary_sensor_var.make_multi_click_trigger(timings)) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) if CONF_INVALID_COOLDOWN in conf: add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN])) automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_STATE, []): rhs = binary_sensor_var.make_state_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) automation.build_automation(trigger, bool_, conf) setup_mqtt_component(mqtt_var, config)
def build_actions(config, arg_type): actions = [] for conf in config: action = None for action in build_action(conf, arg_type): yield None actions.append(action) yield ArrayInitializer(*actions, multiline=False)
def setup_filters(config): filters = [] for conf in config: filter = None for filter in setup_filter(conf): yield None filters.append(filter) yield ArrayInitializer(*filters)
def to_code(config): for uart_ in get_variable(config[CONF_UART_ID]): yield data = config[CONF_DATA] if isinstance(data, str): data = [HexInt(ord(x)) for x in data] rhs = App.make_uart_switch(uart_, config[CONF_NAME], ArrayInitializer(*data, multiline=False)) restart = variable(config[CONF_MAKE_ID], rhs) switch.setup_switch(restart.Puart, restart.Pmqtt, config)
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): 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.EsphomeyamlError( 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 raw_data = MockObj(config[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 config[CONF_GLYPHS]: glyphs.append(Glyph(glyph, raw_data, *glyph_args[glyph])) rhs = App.make_font(ArrayInitializer(*glyphs), ascent, ascent + descent) Pvariable(config[CONF_ID], rhs)
def homeassistant_service_to_code(config, action_id, arg_type, template_arg): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_home_assistant_service_call_action(template_arg) type = HomeAssistantServiceCallAction.template(arg_type) 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(ArrayInitializer(*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(ArrayInitializer(*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(ArrayInitializer(*datas))) yield act
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(ArrayInitializer(*seconds, multiline=False))) minutes = conf.get(CONF_MINUTES, [x for x in range(0, 60)]) add(trigger.add_minutes(ArrayInitializer(*minutes, multiline=False))) hours = conf.get(CONF_HOURS, [x for x in range(0, 24)]) add(trigger.add_hours(ArrayInitializer(*hours, multiline=False))) days_of_month = conf.get(CONF_DAYS_OF_MONTH, [x for x in range(1, 32)]) add( trigger.add_days_of_month( ArrayInitializer(*days_of_month, multiline=False))) months = conf.get(CONF_MONTHS, [x for x in range(1, 13)]) add(trigger.add_months(ArrayInitializer(*months, multiline=False))) days_of_week = conf.get(CONF_DAYS_OF_WEEK, [x for x in range(1, 8)]) add( trigger.add_days_of_week( ArrayInitializer(*days_of_week, multiline=False))) automation.build_automation(trigger, NoArg, conf)
def transmitter_base(full_config): name = full_config[CONF_NAME] key, config = next( (k, v) for k, v in full_config.items() if k in REMOTE_KEYS) if key == CONF_LG: return LGTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS]) if key == CONF_NEC: return NECTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND]) if key == CONF_PANASONIC: return PanasonicTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND]) if key == CONF_SAMSUNG: return SamsungTransmitter.new(name, config[CONF_DATA]) if key == CONF_SONY: return SonyTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS]) if key == CONF_RAW: if isinstance(config, dict): data = config[CONF_DATA] carrier_frequency = config.get(CONF_CARRIER_FREQUENCY) else: data = config carrier_frequency = None return RawTransmitter.new(name, ArrayInitializer(*data, multiline=False), carrier_frequency) if key == CONF_RC_SWITCH_RAW: return RCSwitchRawTransmitter.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), binary_code(config[CONF_CODE]), len(config[CONF_CODE])) if key == CONF_RC_SWITCH_TYPE_A: return RCSwitchTypeATransmitter.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), binary_code(config[CONF_GROUP]), binary_code(config[CONF_DEVICE]), config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_B: return RCSwitchTypeBTransmitter.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), config[CONF_ADDRESS], config[CONF_CHANNEL], config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_C: return RCSwitchTypeCTransmitter.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), ord(config[CONF_FAMILY][0]) - ord('a'), config[CONF_GROUP], config[CONF_DEVICE], config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_D: return RCSwitchTypeDTransmitter.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), ord(config[CONF_GROUP][0]) - ord('a'), config[CONF_DEVICE], config[CONF_STATE]) raise NotImplementedError("Unknown transmitter type {}".format(config))
def wifi_network(config, static_ip): ap = variable(config[CONF_ID], WiFiAP()) if CONF_SSID in config: add(ap.set_ssid(config[CONF_SSID])) if CONF_PASSWORD in config: add(ap.set_password(config[CONF_PASSWORD])) if CONF_BSSID in config: bssid = [HexInt(i) for i in config[CONF_BSSID].parts] add(ap.set_bssid(ArrayInitializer(*bssid, multiline=False))) if CONF_CHANNEL in config: add(ap.set_channel(config[CONF_CHANNEL])) if static_ip is not None: add(ap.set_manual_ip(manual_ip(static_ip))) return ap
def setup_light_core_(light_var, mqtt_var, config): if CONF_INTERNAL in config: add(light_var.set_internal(config[CONF_INTERNAL])) 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])) effects = [] for conf in config.get(CONF_EFFECTS, []): for effect in build_effect(conf): yield effects.append(effect) if effects: add(light_var.add_effects(ArrayInitializer(*effects))) setup_mqtt_component(mqtt_var, config)
def receiver_base(full_config): name = full_config[CONF_NAME] key, config = next( (k, v) for k, v in full_config.items() if k in REMOTE_KEYS) if key == CONF_LG: return LGReceiver.new(name, config[CONF_DATA], config[CONF_NBITS]) if key == CONF_NEC: return NECReceiver.new(name, config[CONF_ADDRESS], config[CONF_COMMAND]) if key == CONF_PANASONIC: return PanasonicReceiver.new(name, config[CONF_ADDRESS], config[CONF_COMMAND]) if key == CONF_SAMSUNG: return SamsungReceiver.new(name, config[CONF_DATA]) if key == CONF_SONY: return SonyReceiver.new(name, config[CONF_DATA], config[CONF_NBITS]) if key == CONF_RAW: data = ArrayInitializer(*config, multiline=False) return RawReceiver.new(name, data) if key == CONF_RC_SWITCH_RAW: return RCSwitchRawReceiver.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), binary_code(config[CONF_CODE]), len(config[CONF_CODE])) if key == CONF_RC_SWITCH_TYPE_A: return RCSwitchTypeAReceiver.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), binary_code(config[CONF_GROUP]), binary_code(config[CONF_DEVICE]), config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_B: return RCSwitchTypeBReceiver.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), config[CONF_ADDRESS], config[CONF_CHANNEL], config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_C: return RCSwitchTypeCReceiver.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), ord(config[CONF_FAMILY][0]) - ord('a'), config[CONF_GROUP], config[CONF_DEVICE], config[CONF_STATE]) if key == CONF_RC_SWITCH_TYPE_D: return RCSwitchTypeDReceiver.new( name, build_rc_switch_protocol(config[CONF_PROTOCOL]), ord(config[CONF_GROUP][0]) - ord('a'), config[CONF_DEVICE], config[CONF_STATE]) raise NotImplementedError("Unknown receiver type {}".format(config))
def to_code(config): from PIL import Image path = CORE.relative_path(config[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 config: image.thumbnail(config[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(config[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(config[CONF_ID], rhs)
def to_code(config): for hub in get_variable(config[CONF_PN532_ID]): yield addr = [HexInt(int(x, 16)) for x in config[CONF_UID].split('-')] rhs = hub.make_tag(config[CONF_NAME], ArrayInitializer(*addr, multiline=False)) binary_sensor.register_binary_sensor(rhs, 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) 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_LEVEL in config: add(mqtt.set_log_level(logger.LOG_LEVELS[config[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( 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 = 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 make_address_array(address): addr = [HexInt(i) for i in address.parts] return ArrayInitializer(*addr, multiline=False)
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_ADDRESSABLE_LAMBDA: args = [(AddressableLightRef, 'it')] for lambda_ in process_lambda(config[CONF_LAMBDA], args): yield None yield AddressableLambdaLightEffect.new(config[CONF_NAME], lambda_, config[CONF_UPDATE_INTERVAL]) elif key == CONF_ADDRESSABLE_RAINBOW: rhs = AddressableRainbowLightEffect.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_ADDRESSABLE_COLOR_WIPE: rhs = AddressableColorWipeEffect.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( AddressableColorWipeEffectColor, ('r', int(round(color[CONF_RED] * 255))), ('g', int(round(color[CONF_GREEN] * 255))), ('b', int(round(color[CONF_BLUE] * 255))), ('w', int(round(color[CONF_WHITE] * 255))), ('random', color[CONF_RANDOM]), ('num_leds', color[CONF_NUM_LEDS]), )) if colors: add(effect.set_colors(ArrayInitializer(*colors))) yield effect elif key == CONF_ADDRESSABLE_SCAN: rhs = AddressableScanEffect.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_ADDRESSABLE_TWINKLE: rhs = AddressableTwinkleEffect.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_ADDRESSABLE_RANDOM_TWINKLE: rhs = AddressableRandomTwinkleEffect.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_ADDRESSABLE_FIREWORKS: rhs = AddressableFireworksEffect.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_ADDRESSABLE_FLICKER: rhs = AddressableFlickerEffect.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())))