def validator(value): if isinstance(value, float) and value.is_integer(): value = int(value) value = cv.string(value) if not value.endswith('V'): value += 'V' return cv.one_of(*values)(value)
def validate_platform(value): value = cv.string(value) if value.upper() in ('ESP8266', 'ESPRESSIF8266'): return ESP_PLATFORM_ESP8266 if value.upper() in ('ESP32', 'ESPRESSIF32'): return ESP_PLATFORM_ESP32 raise vol.Invalid(u"Invalid platform '{}'. Only options are ESP8266 and ESP32. Please note " u"the old way to use the latest arduino framework version has been split up " u"into the arduino_version configuration option.".format(value))
def validate_variant(value): value = cv.string(value).upper() if value == 'WS2813': value = 'WS2812X' if value == 'WS2812': value = '800KBPS' if value == 'LC8812': value = 'SK6812' return cv.one_of(*VARIANTS)(value)
def validate_password(value): value = cv.string(value) if not value: return value if len(value) < 8: raise vol.Invalid(u"WPA password must be at least 8 characters long") if len(value) > 63: raise vol.Invalid(u"WPA password must be at most 63 characters long") return value
def validate_rotation(value): value = cv.string(value) if value.endswith(u"°"): value = value[:-1] try: value = int(value) except ValueError: raise vol.Invalid(u"Expected integer for rotation") return cv.one_of(*DISPLAY_ROTATIONS)(value)
def validate_truetype_file(value): value = cv.string(value) path = os.path.join(os.path.dirname(core.CONFIG_PATH), value) if not os.path.isfile(path): raise vol.Invalid(u"Could not find file '{}'. Please make sure it exists.".format(path)) if value.endswith('.zip'): # for Google Fonts downloads raise vol.Invalid(u"Please unzip the font archive '{}' first and then use the .ttf files " u"inside.".format(value)) if not value.endswith('.ttf'): raise vol.Invalid(u"Only truetype (.ttf) files are supported. Please make sure you're " u"using the correct format or rename the extension to .ttf") return value
def validate_type(value): value = cv.string(value).upper() if 'R' not in value: raise vol.Invalid("Must have R in type") if 'G' not in value: raise vol.Invalid("Must have G in type") if 'B' not in value: raise vol.Invalid("Must have B in type") rest = set(value) - set('RGBW') if rest: raise vol.Invalid("Type has invalid color: {}".format(', '.join(rest))) if len(set(value)) != len(value): raise vol.Invalid("Type has duplicate color!") return value
def validate_substitution_key(value): value = cv.string(value) if not value: raise vol.Invalid("Substitution key must not be empty") if value[0] == '$': value = value[1:] if value[0].isdigit(): raise vol.Invalid( "First character in substitutions cannot be a digit.") for char in value: if char not in VALID_SUBSTITUTIONS_CHARACTERS: raise vol.Invalid( u"Substitution must only consist of upper/lowercase characters, the underscore " u"and numbers. The character '{}' cannot be used".format(char)) return value
def validate_cron_raw(value): value = cv.string(value) value = value.split(' ') if len(value) != 6: raise vol.Invalid( "Cron expression must consist of exactly 6 space-separated parts, " "not {}".format(len(value))) seconds, minutes, hours, days_of_month, months, days_of_week = value return { CONF_SECONDS: validate_cron_seconds(seconds), CONF_MINUTES: validate_cron_minutes(minutes), CONF_HOURS: validate_cron_hours(hours), CONF_DAYS_OF_MONTH: validate_cron_days_of_month(days_of_month), CONF_MONTHS: validate_cron_months(months), CONF_DAYS_OF_WEEK: validate_cron_days_of_week(days_of_week), }
def validator(value): if isinstance(value, list): for v in value: if not isinstance(v, int): raise vol.Invalid( "Expected integer for {} '{}', got {}".format( v, name, type(v))) if v < min_value or v > max_value: raise vol.Invalid( "{} {} is out of range (min={} max={}).".format( name, v, min_value, max_value)) return list(sorted(value)) value = cv.string(value) values = set() for part in value.split(','): values |= _parse_cron_part(part, min_value, max_value, special_mapping) return validator(list(values))
def validate_speed(value): value = cv.string(value) for suffix in ('steps/s', 'steps/s'): if value.endswith(suffix): value = value[:-len(suffix)] if value == 'inf': return 1e6 try: value = float(value) except ValueError: raise vol.Invalid("Expected speed as floating point number, got {}".format(value)) if value <= 0: raise vol.Invalid("Speed must be larger than 0 steps/s!") return value
def validate_acceleration(value): value = cv.string(value) for suffix in ('steps/s^2', 'steps/s*s', 'steps/s/s', 'steps/ss', 'steps/(s*s)'): if value.endswith(suffix): value = value[:-len(suffix)] if value == 'inf': return 1e6 try: value = float(value) except ValueError: raise vol.Invalid("Expected acceleration as floating point number, got {}".format(value)) if value <= 0: raise vol.Invalid("Acceleration must be larger than 0 steps/s^2!") return value
def preload_core_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.") if CONF_BOARD not in core_conf: raise EsphomeyamlError("esphomeyaml.board not specified.") if CONF_NAME not in core_conf: raise EsphomeyamlError("esphomeyaml.name not specified.") try: CORE.esp_platform = validate_platform(core_conf[CONF_PLATFORM]) CORE.board = validate_board(core_conf[CONF_BOARD]) CORE.name = cv.valid_name(core_conf[CONF_NAME]) CORE.build_path = CORE.relative_path( cv.string(core_conf.get(CONF_BUILD_PATH, default_build_path()))) except vol.Invalid as e: raise EsphomeyamlError(text_type(e))
def validate_commit(value): value = cv.string(value) if re.match(r"^[0-9a-f]{7,}$", value) is None: raise vol.Invalid("Commit option only accepts commit hashes in hex format.") return value
def validate_image_file(value): value = cv.string(value) path = os.path.join(os.path.dirname(core.CONFIG_PATH), value) if not os.path.isfile(path): raise vol.Invalid(u"Could not find file '{}'. Please make sure it exists.".format(path)) return value
def validate_mux(value): value = cv.string(value).upper() value = value.replace(' ', '_') return cv.one_of(*MUX)(value)
def validate_fingerprint(value): value = cv.string(value) if re.match(r'^[0-9a-f]{40}$', value) is None: raise vol.Invalid(u"fingerprint must be valid SHA1 hash") return value
def validate_range(value): value = cv.string(value) if value.endswith(u'µT') or value.endswith('uT'): value = value[:-2] return cv.one_of(*HMC5883L_RANGES, int=True)(value)