예제 #1
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)
    }
예제 #2
0
def validate_integration_time(value):
    value = cv.positive_time_period_milliseconds(value).total_milliseconds
    if value not in INTEGRATION_TIMES:
        raise vol.Invalid(u"Unsupported integration time {}.".format(value))
    return value