How to use the esphome.config_validation.GenerateID 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 / as3935 / __init__.py View on Github external
from esphome.const import CONF_INDOOR, CONF_WATCHDOG_THRESHOLD, \
    CONF_NOISE_LEVEL, CONF_SPIKE_REJECTION, CONF_LIGHTNING_THRESHOLD, \
    CONF_MASK_DISTURBER, CONF_DIV_RATIO, CONF_CAPACITANCE
from esphome.core import coroutine

AUTO_LOAD = ['sensor', 'binary_sensor']
MULTI_CONF = True

CONF_AS3935_ID = 'as3935_id'

as3935_ns = cg.esphome_ns.namespace('as3935')
AS3935 = as3935_ns.class_('AS3935Component', cg.Component)

CONF_IRQ_PIN = 'irq_pin'
AS3935_SCHEMA = cv.Schema({
    cv.GenerateID(): cv.declare_id(AS3935),
    cv.Required(CONF_IRQ_PIN): pins.gpio_input_pin_schema,

    cv.Optional(CONF_INDOOR, default=True): cv.boolean,
    cv.Optional(CONF_NOISE_LEVEL, default=2): cv.int_range(min=1, max=7),
    cv.Optional(CONF_WATCHDOG_THRESHOLD, default=2): cv.int_range(min=1, max=10),
    cv.Optional(CONF_SPIKE_REJECTION, default=2): cv.int_range(min=1, max=11),
    cv.Optional(CONF_LIGHTNING_THRESHOLD, default=1): cv.one_of(1, 5, 9, 16, int=True),
    cv.Optional(CONF_MASK_DISTURBER, default=False): cv.boolean,
    cv.Optional(CONF_DIV_RATIO, default=0): cv.one_of(0, 16, 22, 64, 128, int=True),
    cv.Optional(CONF_CAPACITANCE, default=0): cv.int_range(min=0, max=15),
})


@coroutine
def setup_as3935(var, config):
    yield cg.register_component(var, config)
github esphome / esphome / esphome / components / mpr121 / __init__.py View on Github external
CONF_TOUCH_THRESHOLD = "touch_threshold"
CONF_RELEASE_THRESHOLD = "release_threshold"
CONF_TOUCH_DEBOUNCE = "touch_debounce"
CONF_RELEASE_DEBOUNCE = "release_debounce"

DEPENDENCIES = ['i2c']
AUTO_LOAD = ['binary_sensor']

mpr121_ns = cg.esphome_ns.namespace('mpr121')
CONF_MPR121_ID = 'mpr121_id'
MPR121Component = mpr121_ns.class_('MPR121Component', cg.Component, i2c.I2CDevice)

MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
    cv.GenerateID(): cv.declare_id(MPR121Component),
    cv.Optional(CONF_RELEASE_DEBOUNCE, default=0): cv.int_range(min=0, max=7),
    cv.Optional(CONF_TOUCH_DEBOUNCE, default=0):  cv.int_range(min=0, max=7),
    cv.Optional(CONF_TOUCH_THRESHOLD, default=0x0b): cv.int_range(min=0x05, max=0x30),
    cv.Optional(CONF_RELEASE_THRESHOLD, default=0x06): cv.int_range(min=0x05, max=0x30),
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x5A))


def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE]))
    cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE]))
    cg.add(var.set_touch_threshold(config[CONF_TOUCH_THRESHOLD]))
    cg.add(var.set_release_threshold(config[CONF_RELEASE_THRESHOLD]))
    yield cg.register_component(var, config)
    yield i2c.register_i2c_device(var, config)
github esphome / esphome / esphome / components / dfplayer / __init__.py View on Github external
    cv.GenerateID(): cv.use_id(DFPlayer),
}))
def dfplayer_sleep_to_code(config, action_id, template_arg, args):
    var = cg.new_Pvariable(action_id, template_arg)
    yield cg.register_parented(var, config[CONF_ID])
    yield var
github esphome / esphome / esphome / components / mqtt_subscribe / sensor / __init__.py View on Github external
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import mqtt, sensor
from esphome.const import CONF_ID, CONF_QOS, CONF_TOPIC, UNIT_EMPTY, ICON_EMPTY
from .. import mqtt_subscribe_ns

DEPENDENCIES = ['mqtt']

CONF_MQTT_PARENT_ID = 'mqtt_parent_id'
MQTTSubscribeSensor = mqtt_subscribe_ns.class_('MQTTSubscribeSensor', sensor.Sensor, cg.Component)

CONFIG_SCHEMA = sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 1).extend({
    cv.GenerateID(): cv.declare_id(MQTTSubscribeSensor),
    cv.GenerateID(CONF_MQTT_PARENT_ID): cv.use_id(mqtt.MQTTClientComponent),
    cv.Required(CONF_TOPIC): cv.subscribe_topic,
    cv.Optional(CONF_QOS, default=0): cv.mqtt_qos,
}).extend(cv.COMPONENT_SCHEMA)


def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    yield cg.register_component(var, config)
    yield sensor.register_sensor(var, config)

    parent = yield cg.get_variable(config[CONF_MQTT_PARENT_ID])
    cg.add(var.set_parent(parent))
    cg.add(var.set_topic(config[CONF_TOPIC]))
    cg.add(var.set_qos(config[CONF_QOS]))
github esphome / esphome / esphome / components / sensor / mhz19.py View on Github external
DEPENDENCIES = ['uart']

MHZ19Component = sensor.sensor_ns.class_('MHZ19Component', PollingComponent, uart.UARTDevice)
MHZ19TemperatureSensor = sensor.sensor_ns.class_('MHZ19TemperatureSensor',
                                                 sensor.EmptyPollingParentSensor)
MHZ19CO2Sensor = sensor.sensor_ns.class_('MHZ19CO2Sensor', sensor.EmptyPollingParentSensor)

PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(MHZ19Component),
    cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent),
    vol.Required(CONF_CO2): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MHZ19CO2Sensor),
    })),
    vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({
        cv.GenerateID(): cv.declare_variable_id(MHZ19TemperatureSensor),
    })),
    vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    uart_ = yield get_variable(config[CONF_UART_ID])
    rhs = App.make_mhz19_sensor(uart_, config[CONF_CO2][CONF_NAME],
                                config.get(CONF_UPDATE_INTERVAL))
    mhz19 = Pvariable(config[CONF_ID], rhs)
    sensor.setup_sensor(mhz19.Pget_co2_sensor(), config[CONF_CO2])

    if CONF_TEMPERATURE in config:
        sensor.register_sensor(mhz19.Pmake_temperature_sensor(config[CONF_TEMPERATURE][CONF_NAME]),
                               config[CONF_TEMPERATURE])
github esphome / esphome / esphome / components / switch / gpio.py View on Github external
from esphome.cpp_generator import Pvariable, add, get_variable
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
from esphome.cpp_types import App, Component

GPIOSwitch = switch.switch_ns.class_('GPIOSwitch', switch.Switch, Component)
GPIOSwitchRestoreMode = switch.switch_ns.enum('GPIOSwitchRestoreMode')

RESTORE_MODES = {
    'RESTORE_DEFAULT_OFF': GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_OFF,
    'RESTORE_DEFAULT_ON': GPIOSwitchRestoreMode.GPIO_SWITCH_RESTORE_DEFAULT_ON,
    'ALWAYS_OFF': GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_OFF,
    'ALWAYS_ON': GPIOSwitchRestoreMode.GPIO_SWITCH_ALWAYS_ON,
}

PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(GPIOSwitch),
    vol.Required(CONF_PIN): pins.gpio_output_pin_schema,
    vol.Optional(CONF_RESTORE_MODE): cv.one_of(*RESTORE_MODES, upper=True, space='_'),
    vol.Optional(CONF_INTERLOCK): cv.ensure_list(cv.use_variable_id(switch.Switch)),
}).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    pin = yield gpio_output_pin_expression(config[CONF_PIN])
    rhs = App.make_gpio_switch(config[CONF_NAME], pin)
    gpio = Pvariable(config[CONF_ID], rhs)

    if CONF_RESTORE_MODE in config:
        add(gpio.set_restore_mode(RESTORE_MODES[config[CONF_RESTORE_MODE]]))

    if CONF_INTERLOCK in config:
        interlock = []
github esphome / esphome / esphome / components / sensor / hx711.py View on Github external
from esphome.cpp_types import App, Application

MakeHX711Sensor = Application.struct('MakeHX711Sensor')
HX711Sensor = sensor.sensor_ns.class_('HX711Sensor', sensor.PollingSensorComponent)

CONF_DOUT_PIN = 'dout_pin'

HX711Gain = sensor.sensor_ns.enum('HX711Gain')
GAINS = {
    128: HX711Gain.HX711_GAIN_128,
    32: HX711Gain.HX711_GAIN_32,
    64: HX711Gain.HX711_GAIN_64,
}

PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({
    cv.GenerateID(): cv.declare_variable_id(HX711Sensor),
    vol.Required(CONF_DOUT_PIN): pins.gpio_input_pin_schema,
    vol.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema,
    vol.Optional(CONF_GAIN): cv.one_of(*GAINS, int=True),
    vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval,
}).extend(cv.COMPONENT_SCHEMA.schema))


def to_code(config):
    dout_pin = yield gpio_input_pin_expression(config[CONF_DOUT_PIN])
    sck_pin = yield gpio_input_pin_expression(config[CONF_CLK_PIN])

    rhs = App.make_hx711_sensor(config[CONF_NAME], dout_pin, sck_pin,
                                config.get(CONF_UPDATE_INTERVAL))
    hx711 = Pvariable(config[CONF_ID], rhs)

    if CONF_GAIN in config:
github esphome / esphome / esphome / components / output / my9231.py View on Github external
from esphome.components import output
from esphome.components.my9231 import MY9231OutputComponent
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_MY9231_ID, CONF_POWER_SUPPLY
from esphome.cpp_generator import Pvariable, get_variable
from esphome.cpp_helpers import setup_component

DEPENDENCIES = ['my9231']

Channel = MY9231OutputComponent.class_('Channel', output.FloatOutput)

PLATFORM_SCHEMA = output.FLOAT_OUTPUT_PLATFORM_SCHEMA.extend({
    vol.Required(CONF_ID): cv.declare_variable_id(Channel),
    vol.Required(CONF_CHANNEL): vol.All(vol.Coerce(int),
                                        vol.Range(min=0, max=65535)),
    cv.GenerateID(CONF_MY9231_ID): cv.use_variable_id(MY9231OutputComponent),
}).extend(cv.COMPONENT_SCHEMA.schema)


def to_code(config):
    power_supply = None
    if CONF_POWER_SUPPLY in config:
        power_supply = yield get_variable(config[CONF_POWER_SUPPLY])
    my9231 = yield get_variable(config[CONF_MY9231_ID])
    rhs = my9231.create_channel(config[CONF_CHANNEL], power_supply)
    out = Pvariable(config[CONF_ID], rhs)
    output.setup_output_platform(out, config, skip_power_supply=True)
    setup_component(out, config)


BUILD_FLAGS = '-DUSE_MY9231_OUTPUT'
github esphome / esphome / esphome / components / time_based / cover.py View on Github external
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import cover
from esphome.const import CONF_CLOSE_ACTION, CONF_CLOSE_DURATION, CONF_ID, CONF_OPEN_ACTION, \
    CONF_OPEN_DURATION, CONF_STOP_ACTION

time_based_ns = cg.esphome_ns.namespace('time_based')
TimeBasedCover = time_based_ns.class_('TimeBasedCover', cover.Cover, cg.Component)

CONF_HAS_BUILT_IN_ENDSTOP = 'has_built_in_endstop'

CONFIG_SCHEMA = cover.COVER_SCHEMA.extend({
    cv.GenerateID(): cv.declare_id(TimeBasedCover),
    cv.Required(CONF_STOP_ACTION): automation.validate_automation(single=True),

    cv.Required(CONF_OPEN_ACTION): automation.validate_automation(single=True),
    cv.Required(CONF_OPEN_DURATION): cv.positive_time_period_milliseconds,

    cv.Required(CONF_CLOSE_ACTION): automation.validate_automation(single=True),
    cv.Required(CONF_CLOSE_DURATION): cv.positive_time_period_milliseconds,

    cv.Optional(CONF_HAS_BUILT_IN_ENDSTOP, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA)


def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    yield cg.register_component(var, config)
    yield cover.register_cover(var, config)
github esphome / esphome / esphome / components / max7219 / display.py View on Github external
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import display, spi
from esphome.const import CONF_ID, CONF_INTENSITY, CONF_LAMBDA, CONF_NUM_CHIPS

DEPENDENCIES = ['spi']

max7219_ns = cg.esphome_ns.namespace('max7219')
MAX7219Component = max7219_ns.class_('MAX7219Component', cg.PollingComponent, spi.SPIDevice)
MAX7219ComponentRef = MAX7219Component.operator('ref')

CONFIG_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend({
    cv.GenerateID(): cv.declare_id(MAX7219Component),

    cv.Optional(CONF_NUM_CHIPS, default=1): cv.int_range(min=1, max=255),
    cv.Optional(CONF_INTENSITY, default=15): cv.int_range(min=0, max=15),
}).extend(cv.polling_component_schema('1s')).extend(spi.SPI_DEVICE_SCHEMA)


def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    yield cg.register_component(var, config)
    yield spi.register_spi_device(var, config)
    yield display.register_display(var, config)

    cg.add(var.set_num_chips(config[CONF_NUM_CHIPS]))
    cg.add(var.set_intensity(config[CONF_INTENSITY]))

    if CONF_LAMBDA in config: