Esempio n. 1
0
def load_config(path):
    try:
        config = yaml_util.load_yaml(path)
    except OSError:
        raise ESPHomeYAMLError(
            u"Could not read configuration file at {}".format(path))
    core.RAW_CONFIG = config

    if CONF_ESPHOMEYAML not in config:
        raise ESPHomeYAMLError(u"No esphomeyaml section in config")
    core_conf = config[CONF_ESPHOMEYAML]
    if CONF_PLATFORM not in core_conf:
        raise ESPHomeYAMLError("esphomeyaml.platform not specified.")
    esp_platform = unicode(core_conf[CONF_PLATFORM])
    esp_platform = esp_platform.upper()
    if '8266' in esp_platform:
        esp_platform = ESP_PLATFORM_ESP8266
    if '32' in esp_platform:
        esp_platform = ESP_PLATFORM_ESP32
    core.ESP_PLATFORM = esp_platform
    if CONF_BOARD not in core_conf:
        raise ESPHomeYAMLError("esphomeyaml.board not specified.")
    core.BOARD = unicode(core_conf[CONF_BOARD])
    core.SIMPLIFY = cv.boolean(core_conf.get(CONF_SIMPLIFY, True))

    try:
        result = validate_config(config)
    except Exception:
        print(u"Unexpected exception while reading configuration:")
        raise

    return result
Esempio n. 2
0
def parse_multi_click_timing_str(value):
    if not isinstance(value, string_types):
        return value

    parts = value.lower().split(' ')
    if len(parts) != 5:
        raise vol.Invalid(
            "Multi click timing grammar consists of exactly 5 words, not {}"
            "".format(len(parts)))
    try:
        state = cv.boolean(parts[0])
    except vol.Invalid:
        raise vol.Invalid(
            u"First word must either be ON or OFF, not {}".format(parts[0]))

    if parts[1] != 'for':
        raise vol.Invalid(u"Second word must be 'for', got {}".format(
            parts[1]))

    if parts[2] == 'at':
        if parts[3] == 'least':
            key = CONF_MIN_LENGTH
        elif parts[3] == 'most':
            key = CONF_MAX_LENGTH
        else:
            raise vol.Invalid(
                u"Third word after at must either be 'least' or 'most', got {}"
                u"".format(parts[3]))
        try:
            length = cv.positive_time_period_milliseconds(parts[4])
        except vol.Invalid as err:
            raise vol.Invalid(
                u"Multi Click Grammar Parsing length failed: {}".format(err))
        return {CONF_STATE: state, key: str(length)}

    if parts[3] != 'to':
        raise vol.Invalid("Multi click grammar: 4th word must be 'to'")

    try:
        min_length = cv.positive_time_period_milliseconds(parts[2])
    except vol.Invalid as err:
        raise vol.Invalid(
            u"Multi Click Grammar Parsing minimum length failed: {}".format(
                err))

    try:
        max_length = cv.positive_time_period_milliseconds(parts[4])
    except vol.Invalid as err:
        raise vol.Invalid(
            u"Multi Click Grammar Parsing minimum length failed: {}".format(
                err))

    return {
        CONF_STATE: state,
        CONF_MIN_LENGTH: str(min_length),
        CONF_MAX_LENGTH: str(max_length)
    }