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))
    if 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))})
Example #2
0
def time_period(value):
    """Validate and transform time with format HH:MM."""
    if not isinstance(value, str):
        raise cv.Invalid(TIME_PERIOD_ERROR.format(value))

    try:
        parsed = [int(x) for x in value.split(":")]
    except ValueError:
        raise cv.Invalid(TIME_PERIOD_ERROR.format(value))

    if len(parsed) == 2:
        hour, minute = parsed
    else:
        raise cv.Invalid(TIME_PERIOD_ERROR.format(value))

    return TimePeriod(hours=hour, minutes=minute)
Example #3
0
def time_period_str_colon(value):
    """Validate and transform time offset with format HH:MM[:SS]."""
    if isinstance(value, int):
        raise Invalid('Make sure you wrap time values in quotes')
    if not isinstance(value, str):
        raise Invalid(TIME_PERIOD_ERROR.format(value))

    try:
        parsed = [int(x) for x in value.split(':')]
    except ValueError:
        raise 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 Invalid(TIME_PERIOD_ERROR.format(value))

    return TimePeriod(hours=hour, minutes=minute, seconds=second)
Example #4
0
def time_period_str_unit(value):
    """Validate and transform time period with time unit and integer value."""
    check_not_templatable(value)

    if isinstance(value, int):
        raise Invalid(
            "Don't know what '{0}' means as it has no time *unit*! Did you mean "
            "'{0}s'?".format(value))
    if isinstance(value, TimePeriod):
        value = str(value)
    if not isinstance(value, str):
        raise 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 Invalid("Expected time period with unit, "
                      "got {}".format(value))
    kwarg = unit_to_kwarg[one_of(*unit_to_kwarg)(match.group(2))]

    return TimePeriod(**{kwarg: float(match.group(1))})
Example #5
0
    return validate


TIME_PERIOD_ERROR = "Time period {} should be format number + unit, for example 5ms, 5s, 5min, 5h"

time_period_dict = All(
    Schema({
        Optional('days'): float_,
        Optional('hours'): float_,
        Optional('minutes'): float_,
        Optional('seconds'): float_,
        Optional('milliseconds'): float_,
        Optional('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 Invalid('Make sure you wrap time values in quotes')
    if not isinstance(value, str):
        raise Invalid(TIME_PERIOD_ERROR.format(value))

    try:
        parsed = [int(x) for x in value.split(':')]
    except ValueError:
        raise Invalid(TIME_PERIOD_ERROR.format(value))

    if len(parsed) == 2:
Example #6
0
    return validate


TIME_PERIOD_ERROR = "Time period {} should be format number + unit, for example 5ms, 5s, 5min, 5h"

time_period_dict = All(
    Schema({
        Optional('days'): float_,
        Optional('hours'): float_,
        Optional('minutes'): float_,
        Optional('seconds'): float_,
        Optional('milliseconds'): float_,
        Optional('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 Invalid('Make sure you wrap time values in quotes')
    if not isinstance(value, str):
        raise Invalid(TIME_PERIOD_ERROR.format(value))

    try:
        parsed = [int(x) for x in value.split(':')]
    except ValueError:
        raise Invalid(TIME_PERIOD_ERROR.format(value))
}
VOLTAGE_ATTENUATION = {
    '1.5V': global_ns.TOUCH_HVOLT_ATTEN_1V5,
    '1V': global_ns.TOUCH_HVOLT_ATTEN_1V,
    '0.5V': global_ns.TOUCH_HVOLT_ATTEN_0V5,
    '0V': global_ns.TOUCH_HVOLT_ATTEN_0V,
}

ESP32TouchComponent = binary_sensor.binary_sensor_ns.class_('ESP32TouchComponent', Component)

CONFIG_SCHEMA = cv.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),
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    rhs = App.make_esp32_touch_component()
    touch = Pvariable(config[CONF_ID], rhs)
    if CONF_SETUP_MODE in config:
        add(touch.set_setup_mode(config[CONF_SETUP_MODE]))
    if CONF_IIR_FILTER in config:
        add(touch.set_iir_filter(config[CONF_IIR_FILTER]))
    '1.5V': cg.global_ns.TOUCH_HVOLT_ATTEN_1V5,
    '1V': cg.global_ns.TOUCH_HVOLT_ATTEN_1V,
    '0.5V': cg.global_ns.TOUCH_HVOLT_ATTEN_0V5,
    '0V': cg.global_ns.TOUCH_HVOLT_ATTEN_0V,
}

CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID():
    cv.declare_id(ESP32RawTouchComponent),
    cv.Optional(CONF_SETUP_MODE, default=False):
    cv.boolean,
    cv.Optional(CONF_IIR_FILTER, default='0ms'):
    cv.positive_time_period_milliseconds,
    cv.Optional(CONF_SLEEP_DURATION, default='27306us'):
    cv.All(cv.positive_time_period,
           cv.Range(max=TimePeriod(microseconds=436906))),
    cv.Optional(CONF_MEASUREMENT_DURATION, default='8192us'):
    cv.All(cv.positive_time_period,
           cv.Range(max=TimePeriod(microseconds=8192))),
    cv.Optional(CONF_LOW_VOLTAGE_REFERENCE, default='0.5V'):
    validate_voltage(LOW_VOLTAGE_REFERENCE),
    cv.Optional(CONF_HIGH_VOLTAGE_REFERENCE, default='2.7V'):
    validate_voltage(HIGH_VOLTAGE_REFERENCE),
    cv.Optional(CONF_VOLTAGE_ATTENUATION, default='0V'):
    validate_voltage(VOLTAGE_ATTENUATION),
}).extend(cv.COMPONENT_SCHEMA)


def to_code(config):
    touch = cg.new_Pvariable(config[CONF_ID])
    yield cg.register_component(touch, config)
Example #9
0
from . import XiaomiRTCGQ02LM

DEPENDENCIES = ["xiaomi_rtcgq02lm"]

CONF_BUTTON = "button"

CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID():
    cv.use_id(XiaomiRTCGQ02LM),
    cv.Optional(CONF_MOTION):
    binary_sensor.binary_sensor_schema(
        device_class=DEVICE_CLASS_MOTION).extend({
            cv.Optional(CONF_TIMEOUT, default="5s"):
            cv.All(
                cv.positive_time_period_milliseconds,
                cv.Range(max=TimePeriod(milliseconds=65535)),
            ),
        }),
    cv.Optional(CONF_LIGHT):
    binary_sensor.binary_sensor_schema(device_class=DEVICE_CLASS_LIGHT),
    cv.Optional(CONF_BUTTON):
    binary_sensor.binary_sensor_schema().extend({
        cv.Optional(CONF_TIMEOUT, default="200ms"):
        cv.All(
            cv.positive_time_period_milliseconds,
            cv.Range(max=TimePeriod(milliseconds=65535)),
        ),
    }),
})