def time_period_str_unit(value): """Validate and transform time period with time unit and integer value.""" if isinstance(value, int): raise vol.Invalid( "Don't know what '{0}' means as it has no time *unit*! Did you mean " "'{0}s'?".format(value)) elif not isinstance(value, string_types): raise vol.Invalid("Expected string for time period with unit.") unit_to_kwarg = { 'us': 'microseconds', 'microseconds': 'microseconds', 'ms': 'milliseconds', 'milliseconds': 'milliseconds', 's': 'seconds', 'sec': 'seconds', 'seconds': 'seconds', 'min': 'minutes', 'minutes': 'minutes', 'h': 'hours', 'hours': 'hours', 'd': 'days', 'days': 'days', } match = re.match(r"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*)$", value) if match is None: raise vol.Invalid(u"Expected time period with unit, " u"got {}".format(value)) kwarg = unit_to_kwarg[one_of(*unit_to_kwarg)(match.group(2))] return TimePeriod(**{kwarg: float(match.group(1))})
def time_period_str_unit(value): """Validate and transform time period with time unit and integer value.""" if isinstance(value, int): value = str(value) elif not isinstance(value, (str, unicode)): raise vol.Invalid("Expected string for time period with unit.") unit_to_kwarg = { 'us': 'microseconds', 'microseconds': 'microseconds', 'ms': 'milliseconds', 'milliseconds': 'milliseconds', 's': 'seconds', 'sec': 'seconds', 'seconds': 'seconds', 'min': 'minutes', 'minutes': 'minutes', 'h': 'hours', 'hours': 'hours', 'd': 'days', 'days': 'days', } match = re.match(r"^([-+]?[0-9]*\.?[0-9]*)\s*(\w*)$", value) if match is None or match.group(2) not in unit_to_kwarg: raise vol.Invalid(u"Expected time period with unit, " u"got {}".format(value)) kwarg = unit_to_kwarg[match.group(2)] return TimePeriod(**{kwarg: float(match.group(1))})
def time_period_str_colon(value): """Validate and transform time offset with format HH:MM[:SS].""" if isinstance(value, int): raise vol.Invalid('Make sure you wrap time values in quotes') elif not isinstance(value, str): raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) try: parsed = [int(x) for x in value.split(':')] except ValueError: raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) if len(parsed) == 2: hour, minute = parsed second = 0 elif len(parsed) == 3: hour, minute, second = parsed else: raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) return TimePeriod(hours=hour, minutes=minute, seconds=second)
TIME_PERIOD_ERROR = "Time period {} should be format number + unit, for example 5ms, 5s, 5min, 5h" time_period_dict = vol.All( dict, vol.Schema({ 'days': float_, 'hours': float_, 'minutes': float_, 'seconds': float_, 'milliseconds': float_, 'microseconds': float_, }), has_at_least_one_key('days', 'hours', 'minutes', 'seconds', 'milliseconds', 'microseconds'), lambda value: TimePeriod(**value)) def time_period_str_colon(value): """Validate and transform time offset with format HH:MM[:SS].""" if isinstance(value, int): raise vol.Invalid('Make sure you wrap time values in quotes') elif not isinstance(value, str): raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) try: parsed = [int(x) for x in value.split(':')] except ValueError: raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) if len(parsed) == 2:
'0.5V': global_ns.TOUCH_HVOLT_ATTEN_0V5, '0V': global_ns.TOUCH_HVOLT_ATTEN_0V, } ESP32TouchComponent = binary_sensor.binary_sensor_ns.ESP32TouchComponent CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(ESP32TouchComponent), vol.Optional(CONF_SETUP_MODE): cv.boolean, vol.Optional(CONF_IIR_FILTER): cv.positive_time_period_milliseconds, vol.Optional(CONF_SLEEP_DURATION): vol.All(cv.positive_time_period, vol.Range(max=TimePeriod(microseconds=436906))), vol.Optional(CONF_MEASUREMENT_DURATION): vol.All(cv.positive_time_period, vol.Range(max=TimePeriod(microseconds=8192))), vol.Optional(CONF_LOW_VOLTAGE_REFERENCE): validate_voltage(LOW_VOLTAGE_REFERENCE), vol.Optional(CONF_HIGH_VOLTAGE_REFERENCE): validate_voltage(HIGH_VOLTAGE_REFERENCE), vol.Optional(CONF_VOLTAGE_ATTENUATION): validate_voltage(VOLTAGE_ATTENUATION), }) def to_code(config): rhs = App.make_esp32_touch_component() touch = Pvariable(config[CONF_ID], rhs)