How to use the esphome.config_validation.declare_variable_id function in esphome

To help you get started, we’ve selected a few esphome examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esphome / esphome / esphome / components / light / rgbw.py View on Github external
import voluptuous as vol

from esphome.components import light, output
import esphome.config_validation as cv
from esphome.const import CONF_BLUE, CONF_GREEN, CONF_MAKE_ID, CONF_NAME, CONF_RED, CONF_WHITE
from esphome.cpp_generator import get_variable, variable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App

PLATFORM_SCHEMA = cv.nameable(light.PLATFORM_SCHEMA.extend({
    cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight),
    vol.Required(CONF_RED): cv.use_variable_id(output.FloatOutput),
    vol.Required(CONF_GREEN): cv.use_variable_id(output.FloatOutput),
    vol.Required(CONF_BLUE): cv.use_variable_id(output.FloatOutput),
    vol.Required(CONF_WHITE): cv.use_variable_id(output.FloatOutput),
}).extend(light.RGB_LIGHT_SCHEMA.schema).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    red = yield get_variable(config[CONF_RED])
    green = yield get_variable(config[CONF_GREEN])
    blue = yield get_variable(config[CONF_BLUE])
    white = yield get_variable(config[CONF_WHITE])
    rhs = App.make_rgbw_light(config[CONF_NAME], red, green, blue, white)
    light_struct = variable(config[CONF_MAKE_ID], rhs)
    light.setup_light(light_struct.Pstate, light_struct.Poutput, config)
    setup_component(light_struct.Pstate, config)
github esphome / esphome / esphome / components / text_sensor / custom.py View on Github external
import voluptuous as vol

from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_TEXT_SENSORS
from esphome.cpp_generator import add, process_lambda, variable
from esphome.cpp_types import std_vector

CustomTextSensorConstructor = text_sensor.text_sensor_ns.class_('CustomTextSensorConstructor')

PLATFORM_SCHEMA = text_sensor.PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(CustomTextSensorConstructor),
    vol.Required(CONF_LAMBDA): cv.lambda_,
    vol.Required(CONF_TEXT_SENSORS):
        cv.ensure_list(text_sensor.TEXT_SENSOR_SCHEMA.extend({
            cv.GenerateID(): cv.declare_variable_id(text_sensor.TextSensor),
        })),
})


def to_code(config):
    template_ = yield process_lambda(config[CONF_LAMBDA], [],
                                     return_type=std_vector.template(text_sensor.TextSensorPtr))

    rhs = CustomTextSensorConstructor(template_)
    custom = variable(config[CONF_ID], rhs)
    for i, conf in enumerate(config[CONF_TEXT_SENSORS]):
        rhs = custom.Pget_text_sensor(i)
        add(rhs.set_name(conf[CONF_NAME]))
        text_sensor.register_text_sensor(rhs, conf)
github esphome / esphome / esphome / components / sensor / tsl2561.py View on Github external
CONF_IS_CS_PACKAGE = 'is_cs_package'


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


TSL2561Sensor = sensor.sensor_ns.class_('TSL2561Sensor', sensor.PollingSensorComponent,
                                        i2c.I2CDevice)

PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(TSL2561Sensor),
    vol.Optional(CONF_ADDRESS, default=0x39): cv.i2c_address,
    vol.Optional(CONF_INTEGRATION_TIME): validate_integration_time,
    vol.Optional(CONF_GAIN): cv.one_of(*GAINS, upper=True),
    vol.Optional(CONF_IS_CS_PACKAGE): cv.boolean,
    vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    rhs = App.make_tsl2561_sensor(config[CONF_NAME], config[CONF_ADDRESS],
                                  config.get(CONF_UPDATE_INTERVAL))
    tsl2561 = Pvariable(config[CONF_ID], rhs)

    if CONF_INTEGRATION_TIME in config:
        add(tsl2561.set_integration_time(INTEGRATION_TIMES[config[CONF_INTEGRATION_TIME]]))
    if CONF_GAIN in config:
github esphome / esphome / esphome / components / esp32_ble_beacon.py View on Github external
from esphome import config_validation as cv
from esphome.const import CONF_ID, CONF_SCAN_INTERVAL, CONF_TYPE, CONF_UUID, ESP_PLATFORM_ESP32
from esphome.cpp_generator import Pvariable, RawExpression, add
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App, Component, esphome_ns

ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
CONFLICTS_WITH = ['esp32_ble_tracker']

ESP32BLEBeacon = esphome_ns.class_('ESP32BLEBeacon', Component)

CONF_MAJOR = 'major'
CONF_MINOR = 'minor'

CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID(): cv.declare_variable_id(ESP32BLEBeacon),
    vol.Required(CONF_TYPE): cv.one_of('IBEACON', upper=True),
    vol.Required(CONF_UUID): cv.uuid,
    vol.Optional(CONF_MAJOR): cv.uint16_t,
    vol.Optional(CONF_MINOR): cv.uint16_t,
    vol.Optional(CONF_SCAN_INTERVAL): cv.positive_time_period_milliseconds,
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    uuid = config[CONF_UUID].hex
    uuid_arr = [RawExpression('0x{}'.format(uuid[i:i + 2])) for i in range(0, len(uuid), 2)]
    rhs = App.make_esp32_ble_beacon(uuid_arr)
    ble = Pvariable(config[CONF_ID], rhs)
    if CONF_MAJOR in config:
        add(ble.set_major(config[CONF_MAJOR]))
    if CONF_MINOR in config:
github esphome / esphome / esphome / components / sensor / mpu6050.py View on Github external
CONF_GYRO_Z = 'gyro_z'

MPU6050Component = sensor.sensor_ns.class_('MPU6050Component', PollingComponent, i2c.I2CDevice)
MPU6050AccelSensor = sensor.sensor_ns.class_('MPU6050AccelSensor', sensor.EmptyPollingParentSensor)
MPU6050GyroSensor = sensor.sensor_ns.class_('MPU6050GyroSensor', sensor.EmptyPollingParentSensor)
MPU6050TemperatureSensor = sensor.sensor_ns.class_('MPU6050TemperatureSensor',
                                                   sensor.EmptyPollingParentSensor)

SENSOR_KEYS = [CONF_ACCEL_X, CONF_ACCEL_Y, CONF_ACCEL_Z,
               CONF_GYRO_X, CONF_GYRO_Y, CONF_GYRO_Z]

PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(MPU6050Component),
    vol.Optional(CONF_ADDRESS, default=0x68): cv.i2c_address,
    vol.Optional(CONF_ACCEL_X): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
    })),
    vol.Optional(CONF_ACCEL_Y): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
    })),
    vol.Optional(CONF_ACCEL_Z): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050AccelSensor),
    })),
    vol.Optional(CONF_GYRO_X): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
    })),
    vol.Optional(CONF_GYRO_Y): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
    })),
    vol.Optional(CONF_GYRO_Z): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MPU6050GyroSensor),
    })),
github esphome / esphome / esphome / components / display / ssd1306_spi.py View on Github external
MODELS = {
    'SSD1306_128X32': SSD1306Model.SSD1306_MODEL_128_32,
    'SSD1306_128X64': SSD1306Model.SSD1306_MODEL_128_64,
    'SSD1306_96X16': SSD1306Model.SSD1306_MODEL_96_16,
    'SSD1306_64X48': SSD1306Model.SSD1306_MODEL_64_48,
    'SH1106_128X32': SSD1306Model.SH1106_MODEL_128_32,
    'SH1106_128X64': SSD1306Model.SH1106_MODEL_128_64,
    'SH1106_96X16': SSD1306Model.SH1106_MODEL_96_16,
    'SH1106_64X48': SSD1306Model.SH1106_MODEL_64_48,
}

SSD1306_MODEL = cv.one_of(*MODELS, upper=True, space="_")

PLATFORM_SCHEMA = vol.All(display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(SPISSD1306),
    cv.GenerateID(CONF_SPI_ID): cv.use_variable_id(SPIComponent),
    vol.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
    vol.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
    vol.Required(CONF_MODEL): SSD1306_MODEL,
    vol.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
    vol.Optional(CONF_EXTERNAL_VCC): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))


def to_code(config):
    spi_ = yield get_variable(config[CONF_SPI_ID])
    cs = yield gpio_output_pin_expression(config[CONF_CS_PIN])
    dc = yield gpio_output_pin_expression(config[CONF_DC_PIN])

    rhs = App.make_spi_ssd1306(spi_, cs, dc)
    ssd = Pvariable(config[CONF_ID], rhs)
github esphome / esphome / esphome / components / sensor / sht3xd.py View on Github external
DEPENDENCIES = ['i2c']

SHT3XDComponent = sensor.sensor_ns.class_('SHT3XDComponent', PollingComponent, i2c.I2CDevice)
SHT3XDTemperatureSensor = sensor.sensor_ns.class_('SHT3XDTemperatureSensor',
                                                  sensor.EmptyPollingParentSensor)
SHT3XDHumiditySensor = sensor.sensor_ns.class_('SHT3XDHumiditySensor',
                                               sensor.EmptyPollingParentSensor)

PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(SHT3XDComponent),
    vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(SHT3XDTemperatureSensor),
    })),
    vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(SHT3XDHumiditySensor),
    })),
    vol.Optional(CONF_ADDRESS, default=0x44): cv.i2c_address,
    vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    rhs = App.make_sht3xd_sensor(config[CONF_TEMPERATURE][CONF_NAME],
                                 config[CONF_HUMIDITY][CONF_NAME],
                                 config[CONF_ADDRESS],
                                 config.get(CONF_UPDATE_INTERVAL))
    sht3xd = Pvariable(config[CONF_ID], rhs)

    sensor.setup_sensor(sht3xd.Pget_temperature_sensor(), config[CONF_TEMPERATURE])
    sensor.setup_sensor(sht3xd.Pget_humidity_sensor(), config[CONF_HUMIDITY])
    setup_component(sht3xd, config)
github esphome / esphome / esphome / components / switch / remote_transmitter.py View on Github external
def validate_raw(value):
    if isinstance(value, dict):
        return cv.Schema({
            cv.GenerateID(): cv.declare_variable_id(int32),
            vol.Required(CONF_DATA): [vol.Any(vol.Coerce(int), cv.time_period_microseconds)],
            vol.Optional(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
        })(value)
    return validate_raw({
        CONF_DATA: value
    })


PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(RemoteTransmitter),
    vol.Optional(CONF_JVC): cv.Schema({
        vol.Required(CONF_DATA): cv.hex_uint32_t,
    }),
    vol.Optional(CONF_LG): cv.Schema({
        vol.Required(CONF_DATA): cv.hex_uint32_t,
        vol.Optional(CONF_NBITS, default=28): cv.one_of(28, 32, int=True),
    }),
    vol.Optional(CONF_NEC): cv.Schema({
        vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
        vol.Required(CONF_COMMAND): cv.hex_uint16_t,
    }),
    vol.Optional(CONF_SAMSUNG): cv.Schema({
        vol.Required(CONF_DATA): cv.hex_uint32_t,
    }),
    vol.Optional(CONF_SONY): cv.Schema({
        vol.Required(CONF_DATA): cv.hex_uint32_t,
github esphome / esphome / esphome / components / sensor / hlw8012.py View on Github external
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import CONF_CF1_PIN, CONF_CF_PIN, CONF_CHANGE_MODE_EVERY, CONF_CURRENT, \
    CONF_CURRENT_RESISTOR, CONF_ID, CONF_NAME, CONF_POWER, CONF_SEL_PIN, CONF_UPDATE_INTERVAL, \
    CONF_VOLTAGE, CONF_VOLTAGE_DIVIDER
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
from esphome.cpp_types import App, PollingComponent

HLW8012Component = sensor.sensor_ns.class_('HLW8012Component', PollingComponent)
HLW8012VoltageSensor = sensor.sensor_ns.class_('HLW8012VoltageSensor', sensor.EmptySensor)
HLW8012CurrentSensor = sensor.sensor_ns.class_('HLW8012CurrentSensor', sensor.EmptySensor)
HLW8012PowerSensor = sensor.sensor_ns.class_('HLW8012PowerSensor', sensor.EmptyPollingParentSensor)

PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(HLW8012Component),
    vol.Required(CONF_SEL_PIN): pins.gpio_output_pin_schema,
    vol.Required(CONF_CF_PIN): pins.input_pin,
    vol.Required(CONF_CF1_PIN): pins.input_pin,

    vol.Optional(CONF_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(HLW8012VoltageSensor),
    })),
    vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(HLW8012CurrentSensor),
    })),
    vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(HLW8012PowerSensor),
    })),

    vol.Optional(CONF_CURRENT_RESISTOR): cv.resistance,
    vol.Optional(CONF_VOLTAGE_DIVIDER): cv.positive_float,
github esphome / esphome / esphome / components / light / binary.py View on Github external
import voluptuous as vol

from esphome.components import light, output
import esphome.config_validation as cv
from esphome.const import CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT
from esphome.cpp_generator import get_variable, variable
from esphome.cpp_helpers import setup_component
from esphome.cpp_types import App

PLATFORM_SCHEMA = cv.nameable(light.PLATFORM_SCHEMA.extend({
    cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight),
    vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput),
}).extend(light.BINARY_LIGHT_SCHEMA.schema).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    output_ = yield get_variable(config[CONF_OUTPUT])
    rhs = App.make_binary_light(config[CONF_NAME], output_)
    light_struct = variable(config[CONF_MAKE_ID], rhs)
    light.setup_light(light_struct.Pstate, light_struct.Poutput, config)
    setup_component(light_struct.Pstate, config)