Example #1
0
        return convert_tz(pytz.timezone(value))
    except Exception:  # pylint: disable=broad-except
        return value


TIME_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Optional(CONF_TIMEZONE, default=detect_tz):
    validate_tz,
    vol.Optional(CONF_ON_TIME):
    automation.validate_automation(
        {
            cv.GenerateID(CONF_TRIGGER_ID):
            cv.declare_variable_id(CronTrigger),
            vol.Optional(CONF_SECONDS): validate_cron_seconds,
            vol.Optional(CONF_MINUTES): validate_cron_minutes,
            vol.Optional(CONF_HOURS): validate_cron_hours,
            vol.Optional(CONF_DAYS_OF_MONTH): validate_cron_days_of_month,
            vol.Optional(CONF_MONTHS): validate_cron_months,
            vol.Optional(CONF_DAYS_OF_WEEK): validate_cron_days_of_week,
            vol.Optional(CONF_CRON): validate_cron_raw,
        }, validate_cron_keys),
})


def setup_time_core_(time_var, config):
    add(time_var.set_timezone(config[CONF_TIMEZONE]))

    for conf in config.get(CONF_ON_TIME, []):
        rhs = App.register_component(time_var.Pmake_cron_trigger())
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
Example #2
0
import voluptuous as vol

from pi4home import automation
import pi4home.config_validation as cv
from pi4home.const import CONF_ID, CONF_INTERVAL
from pi4home.cpp_generator import Pvariable
from pi4home.cpp_helpers import setup_component
from pi4home.cpp_types import App, PollingComponent, Trigger, pi4home_ns

IntervalTrigger = pi4home_ns.class_('IntervalTrigger', Trigger.template(),
                                    PollingComponent)

CONFIG_SCHEMA = automation.validate_automation(
    cv.Schema({
        cv.GenerateID():
        cv.declare_variable_id(IntervalTrigger),
        vol.Required(CONF_INTERVAL):
        cv.positive_time_period_milliseconds,
    }).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    for conf in config:
        rhs = App.register_component(IntervalTrigger.new(conf[CONF_INTERVAL]))
        trigger = Pvariable(conf[CONF_ID], rhs)
        setup_component(trigger, conf)

        automation.build_automations(trigger, [], conf)
Example #3
0
import pi4home.config_validation as cv
from pi4home.const import CONF_ASSUMED_STATE, CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_OPTIMISTIC, \
    CONF_RESTORE_STATE, CONF_STATE, CONF_TURN_OFF_ACTION, CONF_TURN_ON_ACTION
from pi4home.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable
from pi4home.cpp_helpers import setup_component
from pi4home.cpp_types import Action, App, Component, bool_, optional

TemplateSwitch = switch.switch_ns.class_('TemplateSwitch', switch.Switch, Component)
SwitchPublishAction = switch.switch_ns.class_('SwitchPublishAction', Action)

PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(TemplateSwitch),
    vol.Optional(CONF_LAMBDA): cv.lambda_,
    vol.Optional(CONF_OPTIMISTIC): cv.boolean,
    vol.Optional(CONF_ASSUMED_STATE): cv.boolean,
    vol.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(single=True),
    vol.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(single=True),
    vol.Optional(CONF_RESTORE_STATE): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    rhs = App.make_template_switch(config[CONF_NAME])
    template = Pvariable(config[CONF_ID], rhs)

    switch.setup_switch(template, config)

    if CONF_LAMBDA in config:
        for template_ in process_lambda(config[CONF_LAMBDA], [],
                                        return_type=optional.template(bool_)):
            yield
Example #4
0
    vol.Required(CONF_NAME): cv.valid_name,
    vol.Required(CONF_PLATFORM): cv.one_of('ESP8266', 'ESPRESSIF8266', 'ESP32', 'ESPRESSIF32',
                                           upper=True),
    vol.Required(CONF_BOARD): validate_board,
    vol.Optional(CONF_PI4HOME_CORE_VERSION, default='latest'): PI4HOME_CORE_VERSION_SCHEMA,
    vol.Optional(CONF_ARDUINO_VERSION, default='recommended'): validate_arduino_version,
    vol.Optional(CONF_USE_CUSTOM_CODE, default=False): cv.boolean,
    vol.Optional(CONF_BUILD_PATH, default=default_build_path): cv.string,
    vol.Optional(CONF_PLATFORMIO_OPTIONS): cv.Schema({
        cv.string_strict: vol.Any([cv.string], cv.string),
    }),
    vol.Optional(CONF_ESP8266_RESTORE_FROM_FLASH): vol.All(cv.only_on_esp8266, cv.boolean),

    vol.Optional(CONF_BOARD_FLASH_MODE, default='dout'): cv.one_of(*BUILD_FLASH_MODES, lower=True),
    vol.Optional(CONF_ON_BOOT): automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(StartupTrigger),
        vol.Optional(CONF_PRIORITY): cv.float_,
    }),
    vol.Optional(CONF_ON_SHUTDOWN): automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(ShutdownTrigger),
    }),
    vol.Optional(CONF_ON_LOOP): automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(LoopTrigger),
    }),
    vol.Optional(CONF_INCLUDES): cv.ensure_list(cv.file_),
    vol.Optional(CONF_LIBRARIES): cv.ensure_list(cv.string_strict),

    vol.Optional('pi4homelib_version'): cv.invalid("The pi4homelib_version has been renamed to "
                                                   "pi4home_core_version in 1.11.0"),
})

Example #5
0
TemplateCover = cover.cover_ns.class_('TemplateCover', cover.Cover)
CoverPublishAction = cover.cover_ns.class_('CoverPublishAction', Action)

PLATFORM_SCHEMA = cv.nameable(
    cover.COVER_PLATFORM_SCHEMA.extend({
        cv.GenerateID():
        cv.declare_variable_id(TemplateCover),
        vol.Optional(CONF_LAMBDA):
        cv.lambda_,
        vol.Optional(CONF_OPTIMISTIC):
        cv.boolean,
        vol.Optional(CONF_ASSUMED_STATE):
        cv.boolean,
        vol.Optional(CONF_OPEN_ACTION):
        automation.validate_automation(single=True),
        vol.Optional(CONF_CLOSE_ACTION):
        automation.validate_automation(single=True),
        vol.Optional(CONF_STOP_ACTION):
        automation.validate_automation(single=True),
    }).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    rhs = App.make_template_cover(config[CONF_NAME])
    var = Pvariable(config[CONF_ID], rhs)

    cover.setup_cover(var, config)
    setup_component(var, config)

    if CONF_LAMBDA in config:
Example #6
0
File: api.py Project: khzd/pi4home
    'bool': bool_,
    'int': int32,
    'float': float_,
    'string': std_string,
}


CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID(): cv.declare_variable_id(APIServer),
    vol.Optional(CONF_PORT, default=6053): cv.port,
    vol.Optional(CONF_PASSWORD, default=''): cv.string_strict,
    vol.Optional(CONF_REBOOT_TIMEOUT): cv.positive_time_period_milliseconds,
    vol.Optional(CONF_SERVICES): automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(UserService),
        vol.Required(CONF_SERVICE): cv.valid_name,
        vol.Optional(CONF_VARIABLES, default={}): cv.Schema({
            cv.validate_id_name: cv.one_of(*SERVICE_ARG_TYPES, lower=True),
        }),
    }),
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    rhs = App.init_api_server()
    api = Pvariable(config[CONF_ID], rhs)

    if config[CONF_PORT] != 6053:
        add(api.set_port(config[CONF_PORT]))
    if config.get(CONF_PASSWORD):
        add(api.set_password(config[CONF_PASSWORD]))
    if CONF_REBOOT_TIMEOUT in config:
Example #7
0
                                                       spi.SPIDevice)
PN532Trigger = binary_sensor.binary_sensor_ns.class_(
    'PN532Trigger', Trigger.template(std_string))

CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID():
    cv.declare_variable_id(PN532Component),
    cv.GenerateID(CONF_SPI_ID):
    cv.use_variable_id(SPIComponent),
    vol.Required(CONF_CS_PIN):
    pins.gpio_output_pin_schema,
    vol.Optional(CONF_UPDATE_INTERVAL):
    cv.update_interval,
    vol.Optional(CONF_ON_TAG):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(PN532Trigger),
    }),
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    for spi_ in get_variable(config[CONF_SPI_ID]):
        yield
    for cs in gpio_output_pin_expression(config[CONF_CS_PIN]):
        yield
    rhs = App.make_pn532_component(spi_, cs, config.get(CONF_UPDATE_INTERVAL))
    pn532 = Pvariable(config[CONF_ID], rhs)

    for conf_ in config.get(CONF_ON_TAG, []):
        trigger = Pvariable(conf_[CONF_TRIGGER_ID], pn532.make_trigger())
        automation.build_automations(trigger, [(std_string, 'x')], conf_)
Example #8
0
TextSensorPtr = TextSensor.operator('ptr')
MQTTTextSensor = text_sensor_ns.class_('MQTTTextSensor', mqtt.MQTTComponent)

TextSensorStateTrigger = text_sensor_ns.class_('TextSensorStateTrigger',
                                               Trigger.template(std_string))
TextSensorPublishAction = text_sensor_ns.class_('TextSensorPublishAction',
                                                Action)

TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
    cv.GenerateID(CONF_MQTT_ID):
    cv.declare_variable_id(MQTTTextSensor),
    vol.Optional(CONF_ICON):
    cv.icon,
    vol.Optional(CONF_ON_VALUE):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(TextSensorStateTrigger),
    }),
})

TEXT_SENSOR_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(TEXT_SENSOR_SCHEMA.schema)


def setup_text_sensor_core_(text_sensor_var, config):
    if CONF_INTERNAL in config:
        add(text_sensor_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config:
        add(text_sensor_var.set_icon(config[CONF_ICON]))

    for conf in config.get(CONF_ON_VALUE, []):
        rhs = text_sensor_var.make_state_trigger()
        trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
Example #9
0
        if max_length is not None:
            tim[CONF_MAX_LENGTH] = max_length
        timings.append(tim)
    return timings


BINARY_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({
    cv.GenerateID(CONF_MQTT_ID):
    cv.declare_variable_id(MQTTBinarySensorComponent),
    vol.Optional(CONF_DEVICE_CLASS):
    cv.one_of(*DEVICE_CLASSES, lower=True),
    vol.Optional(CONF_FILTERS):
    FILTERS_SCHEMA,
    vol.Optional(CONF_ON_PRESS):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(PressTrigger),
    }),
    vol.Optional(CONF_ON_RELEASE):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(ReleaseTrigger),
    }),
    vol.Optional(CONF_ON_CLICK):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(ClickTrigger),
        vol.Optional(CONF_MIN_LENGTH, default='50ms'):
        cv.positive_time_period_milliseconds,
        vol.Optional(CONF_MAX_LENGTH, default='350ms'):
        cv.positive_time_period_milliseconds,
    }),
Example #10
0
File: mqtt.py Project: khzd/pi4home
            MQTT_MESSAGE_BASE.extend({
                vol.Optional(CONF_LEVEL):
                logger.is_log_level,
            }), validate_message_just_topic),
        vol.Optional(CONF_SSL_FINGERPRINTS):
        vol.All(cv.only_on_esp8266, cv.ensure_list(validate_fingerprint)),
        vol.Optional(CONF_KEEPALIVE):
        cv.positive_time_period_seconds,
        vol.Optional(CONF_REBOOT_TIMEOUT):
        cv.positive_time_period_milliseconds,
        vol.Optional(CONF_ON_MESSAGE):
        automation.validate_automation({
            cv.GenerateID(CONF_TRIGGER_ID):
            cv.declare_variable_id(MQTTMessageTrigger),
            vol.Required(CONF_TOPIC):
            cv.subscribe_topic,
            vol.Optional(CONF_QOS):
            cv.mqtt_qos,
            vol.Optional(CONF_PAYLOAD):
            cv.string_strict,
        }),
        vol.Optional(CONF_ON_JSON_MESSAGE):
        automation.validate_automation({
            cv.GenerateID(CONF_TRIGGER_ID):
            cv.declare_variable_id(MQTTJsonMessageTrigger),
            vol.Required(CONF_TOPIC):
            cv.subscribe_topic,
            vol.Optional(CONF_QOS, default=0):
            cv.mqtt_qos,
        }),
    }), validate_config)
Example #11
0
 cv.GenerateID(CONF_MQTT_ID):
 cv.declare_variable_id(MQTTSensorComponent),
 vol.Optional(CONF_UNIT_OF_MEASUREMENT):
 cv.string_strict,
 vol.Optional(CONF_ICON):
 cv.icon,
 vol.Optional(CONF_ACCURACY_DECIMALS):
 vol.Coerce(int),
 vol.Optional(CONF_EXPIRE_AFTER):
 vol.All(cv.requires_component('mqtt'),
         vol.Any(None, cv.positive_time_period_milliseconds)),
 vol.Optional(CONF_FILTERS):
 FILTERS_SCHEMA,
 vol.Optional(CONF_ON_VALUE):
 automation.validate_automation({
     cv.GenerateID(CONF_TRIGGER_ID):
     cv.declare_variable_id(SensorStateTrigger),
 }),
 vol.Optional(CONF_ON_RAW_VALUE):
 automation.validate_automation({
     cv.GenerateID(CONF_TRIGGER_ID):
     cv.declare_variable_id(SensorRawStateTrigger),
 }),
 vol.Optional(CONF_ON_VALUE_RANGE):
 automation.validate_automation(
     {
         cv.GenerateID(CONF_TRIGGER_ID):
         cv.declare_variable_id(ValueRangeTrigger),
         vol.Optional(CONF_ABOVE):
         cv.float_,
         vol.Optional(CONF_BELOW):
         cv.float_,
Example #12
0
SwitchCondition = switch_ns.class_('SwitchCondition', Condition)
SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger',
                                       Trigger.template())
SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger',
                                        Trigger.template())

SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
    cv.GenerateID(CONF_MQTT_ID):
    cv.declare_variable_id(MQTTSwitchComponent),
    vol.Optional(CONF_ICON):
    cv.icon,
    vol.Optional(CONF_INVERTED):
    cv.boolean,
    vol.Optional(CONF_ON_TURN_ON):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(SwitchTurnOnTrigger),
    }),
    vol.Optional(CONF_ON_TURN_OFF):
    automation.validate_automation({
        cv.GenerateID(CONF_TRIGGER_ID):
        cv.declare_variable_id(SwitchTurnOffTrigger),
    }),
})

SWITCH_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(SWITCH_SCHEMA.schema)


def setup_switch_core_(switch_var, config):
    if CONF_INTERNAL in config:
        add(switch_var.set_internal(config[CONF_INTERNAL]))
    if CONF_ICON in config: