From 15331edb78a1d8283fbd2927c3d33405536641ed Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 12 Nov 2018 23:30:31 +0100 Subject: [PATCH] Let esphomeyaml know about class inheritance (#229) * Allow overriding setup priority * Add inheritance tree * Global variables * Tests and better validation * Fix * Lint --- esphomeyaml/automation.py | 35 +++---- esphomeyaml/components/ads1115.py | 17 ++-- .../components/binary_sensor/__init__.py | 36 +++++--- .../binary_sensor/esp32_ble_tracker.py | 2 +- .../components/binary_sensor/esp32_touch.py | 3 +- esphomeyaml/components/binary_sensor/gpio.py | 12 ++- .../components/binary_sensor/nextion.py | 3 +- esphomeyaml/components/binary_sensor/pn532.py | 4 +- .../components/binary_sensor/rdm6300.py | 4 +- .../binary_sensor/remote_receiver.py | 25 +++-- .../components/binary_sensor/status.py | 11 ++- .../components/binary_sensor/template.py | 12 ++- esphomeyaml/components/cover/__init__.py | 24 +++-- esphomeyaml/components/cover/template.py | 11 ++- esphomeyaml/components/dallas.py | 9 +- esphomeyaml/components/deep_sleep.py | 25 ++--- esphomeyaml/components/display/__init__.py | 2 +- esphomeyaml/components/display/lcd_gpio.py | 10 +- esphomeyaml/components/display/lcd_pcf8574.py | 12 ++- esphomeyaml/components/display/max7219.py | 13 +-- esphomeyaml/components/display/nextion.py | 17 ++-- esphomeyaml/components/display/ssd1306_i2c.py | 7 +- esphomeyaml/components/display/ssd1306_spi.py | 31 ++++--- .../components/display/waveshare_epaper.py | 34 ++++--- esphomeyaml/components/esp32_ble.py | 5 - esphomeyaml/components/esp32_ble_beacon.py | 13 ++- esphomeyaml/components/esp32_ble_tracker.py | 15 ++- esphomeyaml/components/esp32_touch.py | 8 +- esphomeyaml/components/fan/__init__.py | 33 ++++--- esphomeyaml/components/fan/binary.py | 17 ++-- esphomeyaml/components/fan/speed.py | 16 ++-- esphomeyaml/components/font.py | 4 +- esphomeyaml/components/globals.py | 35 +++++++ esphomeyaml/components/i2c.py | 9 +- esphomeyaml/components/image.py | 2 +- esphomeyaml/components/ir_transmitter.py | 6 -- esphomeyaml/components/light/__init__.py | 66 +++++++------ esphomeyaml/components/light/binary.py | 16 ++-- esphomeyaml/components/light/cwww.py | 11 ++- .../components/light/fastled_clockless.py | 8 +- esphomeyaml/components/light/fastled_spi.py | 8 +- esphomeyaml/components/light/monochromatic.py | 18 ++-- esphomeyaml/components/light/rgb.py | 16 ++-- esphomeyaml/components/light/rgbw.py | 19 ++-- esphomeyaml/components/light/rgbww.py | 17 ++-- esphomeyaml/components/logger.py | 7 +- esphomeyaml/components/mqtt.py | 16 ++-- esphomeyaml/components/ota.py | 5 +- esphomeyaml/components/output/__init__.py | 18 ++-- esphomeyaml/components/output/esp8266_pwm.py | 9 +- esphomeyaml/components/output/gpio.py | 10 +- esphomeyaml/components/output/ledc.py | 7 +- esphomeyaml/components/output/pca9685.py | 3 +- esphomeyaml/components/pca9685.py | 10 +- esphomeyaml/components/pcf8574.py | 21 +++-- esphomeyaml/components/pn532.py | 20 ++-- esphomeyaml/components/power_supply.py | 11 ++- esphomeyaml/components/rdm6300.py | 20 ++-- esphomeyaml/components/remote_receiver.py | 30 +++--- esphomeyaml/components/remote_transmitter.py | 19 ++-- esphomeyaml/components/script.py | 7 +- esphomeyaml/components/sensor/__init__.py | 60 ++++++------ esphomeyaml/components/sensor/adc.py | 11 ++- esphomeyaml/components/sensor/ads1115.py | 32 ++++--- esphomeyaml/components/sensor/bh1750.py | 19 ++-- esphomeyaml/components/sensor/ble_rssi.py | 3 +- esphomeyaml/components/sensor/bme280.py | 49 ++++++---- esphomeyaml/components/sensor/bme680.py | 61 +++++++----- esphomeyaml/components/sensor/bmp085.py | 19 +++- esphomeyaml/components/sensor/bmp280.py | 43 +++++---- esphomeyaml/components/sensor/cse7766.py | 32 +++++-- esphomeyaml/components/sensor/dallas.py | 4 +- esphomeyaml/components/sensor/dht.py | 51 ++++++---- esphomeyaml/components/sensor/dht12.py | 34 +++++-- esphomeyaml/components/sensor/duty_cycle.py | 11 ++- esphomeyaml/components/sensor/esp32_hall.py | 9 +- esphomeyaml/components/sensor/hdc1080.py | 36 +++++--- esphomeyaml/components/sensor/hlw8012.py | 30 +++--- esphomeyaml/components/sensor/hmc5883l.py | 53 +++++++---- esphomeyaml/components/sensor/htu21d.py | 35 +++++-- esphomeyaml/components/sensor/hx711.py | 24 +++-- esphomeyaml/components/sensor/ina219.py | 43 ++++++--- esphomeyaml/components/sensor/ina3221.py | 37 +++++--- esphomeyaml/components/sensor/max6675.py | 22 +++-- esphomeyaml/components/sensor/mhz19.py | 34 ++++--- esphomeyaml/components/sensor/mpu6050.py | 49 +++++++--- .../components/sensor/mqtt_subscribe.py | 14 +-- esphomeyaml/components/sensor/ms5611.py | 35 +++++-- esphomeyaml/components/sensor/pmsx003.py | 39 +++++--- .../components/sensor/pulse_counter.py | 25 +++-- .../components/sensor/rotary_encoder.py | 21 +++-- esphomeyaml/components/sensor/sht3xd.py | 39 +++++--- esphomeyaml/components/sensor/tcs34725.py | 64 ++++++++----- esphomeyaml/components/sensor/template.py | 17 ++-- .../components/sensor/total_daily_energy.py | 24 ++--- esphomeyaml/components/sensor/tsl2561.py | 27 ++++-- esphomeyaml/components/sensor/ultrasonic.py | 12 ++- esphomeyaml/components/sensor/uptime.py | 13 ++- esphomeyaml/components/sensor/wifi_signal.py | 13 ++- .../components/sensor/xiaomi_miflora.py | 27 +++--- esphomeyaml/components/sensor/xiaomi_mijia.py | 17 ++-- esphomeyaml/components/spi.py | 8 +- esphomeyaml/components/status_led.py | 12 ++- esphomeyaml/components/stepper/__init__.py | 13 ++- esphomeyaml/components/stepper/a4988.py | 8 +- esphomeyaml/components/switch/__init__.py | 20 ++-- esphomeyaml/components/switch/gpio.py | 16 ++-- esphomeyaml/components/switch/output.py | 24 ++--- .../components/switch/remote_transmitter.py | 29 +++--- esphomeyaml/components/switch/restart.py | 4 +- esphomeyaml/components/switch/shutdown.py | 4 +- esphomeyaml/components/switch/template.py | 24 ++--- esphomeyaml/components/switch/uart.py | 11 +-- .../components/text_sensor/__init__.py | 10 +- .../components/text_sensor/mqtt_subscribe.py | 17 ++-- .../components/text_sensor/template.py | 16 ++-- esphomeyaml/components/text_sensor/version.py | 10 +- esphomeyaml/components/time/__init__.py | 7 +- esphomeyaml/components/time/sntp.py | 7 +- esphomeyaml/components/uart.py | 15 +-- esphomeyaml/components/web_server.py | 17 ++-- esphomeyaml/components/wifi.py | 19 ++-- esphomeyaml/config.py | 28 +++++- esphomeyaml/config_validation.py | 9 +- esphomeyaml/const.py | 3 + esphomeyaml/helpers.py | 92 +++++++++++++++++-- esphomeyaml/pins.py | 9 +- tests/test1.yaml | 27 +++++- 128 files changed, 1572 insertions(+), 989 deletions(-) delete mode 100644 esphomeyaml/components/esp32_ble.py create mode 100644 esphomeyaml/components/globals.py delete mode 100644 esphomeyaml/components/ir_transmitter.py diff --git a/esphomeyaml/automation.py b/esphomeyaml/automation.py index d7194171d4..de32e7d39c 100644 --- a/esphomeyaml/automation.py +++ b/esphomeyaml/automation.py @@ -10,7 +10,8 @@ from esphomeyaml.const import CONF_ABOVE, CONF_ACTION_ID, CONF_AND, CONF_AUTOMAT CONF_OR, CONF_RANGE, CONF_THEN, CONF_TRIGGER_ID from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, TemplateArguments, add, add_job, \ - esphomelib_ns, float_, process_lambda, templatable, uint32, get_variable + esphomelib_ns, float_, process_lambda, templatable, uint32, get_variable, PollingComponent, \ + Action, Component, Trigger from esphomeyaml.util import ServiceRegistry @@ -49,7 +50,7 @@ def validate_recursive_action(value): u"".format(key, key2)) validator = ACTION_REGISTRY[key][0] value[i] = { - CONF_ACTION_ID: cv.declare_variable_id(None)(item[CONF_ACTION_ID]), + CONF_ACTION_ID: cv.declare_variable_id(Action)(item[CONF_ACTION_ID]), key: validator(item[key]) } return value @@ -58,14 +59,20 @@ def validate_recursive_action(value): ACTION_REGISTRY = ServiceRegistry() # pylint: disable=invalid-name -DelayAction = esphomelib_ns.DelayAction -LambdaAction = esphomelib_ns.LambdaAction -IfAction = esphomelib_ns.IfAction -UpdateComponentAction = esphomelib_ns.UpdateComponentAction -Automation = esphomelib_ns.Automation +DelayAction = esphomelib_ns.class_('DelayAction', Action, Component) +LambdaAction = esphomelib_ns.class_('LambdaAction', Action) +IfAction = esphomelib_ns.class_('IfAction', Action) +UpdateComponentAction = esphomelib_ns.class_('UpdateComponentAction', Action) +Automation = esphomelib_ns.class_('Automation') + +Condition = esphomelib_ns.class_('Condition') +AndCondition = esphomelib_ns.class_('AndCondition', Condition) +OrCondition = esphomelib_ns.class_('OrCondition', Condition) +RangeCondition = esphomelib_ns.class_('RangeCondition', Condition) +LambdaCondition = esphomelib_ns.class_('LambdaCondition', Condition) CONDITIONS_SCHEMA = vol.All(cv.ensure_list, [cv.templatable({ - cv.GenerateID(CONF_CONDITION_ID): cv.declare_variable_id(None), + cv.GenerateID(CONF_CONDITION_ID): cv.declare_variable_id(Condition), vol.Optional(CONF_AND): validate_recursive_condition, vol.Optional(CONF_OR): validate_recursive_condition, vol.Optional(CONF_RANGE): vol.All(vol.Schema({ @@ -75,12 +82,6 @@ CONDITIONS_SCHEMA = vol.All(cv.ensure_list, [cv.templatable({ vol.Optional(CONF_LAMBDA): cv.lambda_, })]) -# pylint: disable=invalid-name -AndCondition = esphomelib_ns.AndCondition -OrCondition = esphomelib_ns.OrCondition -RangeCondition = esphomelib_ns.RangeCondition -LambdaCondition = esphomelib_ns.LambdaCondition - def validate_automation(extra_schema=None, extra_validators=None, single=False): schema = AUTOMATION_SCHEMA.extend(extra_schema or {}) @@ -119,8 +120,8 @@ def validate_automation(extra_schema=None, extra_validators=None, single=False): AUTOMATION_SCHEMA = vol.Schema({ - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(None), - cv.GenerateID(CONF_AUTOMATION_ID): cv.declare_variable_id(None), + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(Trigger), + cv.GenerateID(CONF_AUTOMATION_ID): cv.declare_variable_id(Automation), vol.Optional(CONF_IF): CONDITIONS_SCHEMA, vol.Required(CONF_THEN): validate_recursive_action, }) @@ -228,7 +229,7 @@ def lambda_action_to_code(config, action_id, arg_type): CONF_COMPONENT_UPDATE = 'component.update' COMPONENT_UPDATE_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(PollingComponent), }) diff --git a/esphomeyaml/components/ads1115.py b/esphomeyaml/components/ads1115.py index 3acb95a049..d8d1d9b7cd 100644 --- a/esphomeyaml/components/ads1115.py +++ b/esphomeyaml/components/ads1115.py @@ -1,22 +1,18 @@ import voluptuous as vol +from esphomeyaml.components import sensor, i2c import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor -from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_RATE -from esphomeyaml.helpers import App, Pvariable +from esphomeyaml.const import CONF_ADDRESS, CONF_ID +from esphomeyaml.helpers import App, Pvariable, setup_component, Component DEPENDENCIES = ['i2c'] -ADS1115Component = sensor.sensor_ns.ADS1115Component - -RATE_REMOVE_MESSAGE = """The rate option has been removed in 1.5.0 and is no longer required.""" +ADS1115Component = sensor.sensor_ns.class_('ADS1115Component', Component, i2c.I2CDevice) ADS1115_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(ADS1115Component), vol.Required(CONF_ADDRESS): cv.i2c_address, - - vol.Optional(CONF_RATE): cv.invalid(RATE_REMOVE_MESSAGE) -}) +}).extend(cv.COMPONENT_SCHEMA.schema) CONFIG_SCHEMA = vol.All(cv.ensure_list, [ADS1115_SCHEMA]) @@ -24,7 +20,8 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [ADS1115_SCHEMA]) def to_code(config): for conf in config: rhs = App.make_ads1115_component(conf[CONF_ADDRESS]) - Pvariable(conf[CONF_ID], rhs) + var = Pvariable(conf[CONF_ID], rhs) + setup_component(var, conf) BUILD_FLAGS = '-DUSE_ADS1115_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/__init__.py b/esphomeyaml/components/binary_sensor/__init__.py index 232f6a8dbc..e4cf8a264b 100644 --- a/esphomeyaml/components/binary_sensor/__init__.py +++ b/esphomeyaml/components/binary_sensor/__init__.py @@ -9,7 +9,8 @@ from esphomeyaml.const import CONF_DELAYED_OFF, CONF_DELAYED_ON, CONF_DEVICE_CLA CONF_ON_DOUBLE_CLICK, CONF_ON_MULTI_CLICK, CONF_ON_PRESS, CONF_ON_RELEASE, CONF_STATE, \ CONF_TIMING, CONF_TRIGGER_ID from esphomeyaml.helpers import App, ArrayInitializer, NoArg, Pvariable, StructInitializer, add, \ - add_job, bool_, esphomelib_ns, process_lambda, setup_mqtt_component + add_job, bool_, esphomelib_ns, process_lambda, setup_mqtt_component, Nameable, Trigger, \ + Component DEVICE_CLASSES = [ '', 'battery', 'cold', 'connectivity', 'door', 'garage_door', 'gas', @@ -23,19 +24,25 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) binary_sensor_ns = esphomelib_ns.namespace('binary_sensor') -PressTrigger = binary_sensor_ns.PressTrigger -ReleaseTrigger = binary_sensor_ns.ReleaseTrigger -ClickTrigger = binary_sensor_ns.ClickTrigger -DoubleClickTrigger = binary_sensor_ns.DoubleClickTrigger -MultiClickTrigger = binary_sensor_ns.MultiClickTrigger -MultiClickTriggerEvent = binary_sensor_ns.MultiClickTriggerEvent -BinarySensor = binary_sensor_ns.BinarySensor -InvertFilter = binary_sensor_ns.InvertFilter -LambdaFilter = binary_sensor_ns.LambdaFilter -DelayedOnFilter = binary_sensor_ns.DelayedOnFilter -DelayedOffFilter = binary_sensor_ns.DelayedOffFilter -HeartbeatFilter = binary_sensor_ns.HeartbeatFilter -MQTTBinarySensorComponent = binary_sensor_ns.MQTTBinarySensorComponent +BinarySensor = binary_sensor_ns.class_('BinarySensor', Nameable) +MQTTBinarySensorComponent = binary_sensor_ns.class_('MQTTBinarySensorComponent', mqtt.MQTTComponent) + +# Triggers +PressTrigger = binary_sensor_ns.class_('PressTrigger', Trigger.template(NoArg)) +ReleaseTrigger = binary_sensor_ns.class_('ReleaseTrigger', Trigger.template(NoArg)) +ClickTrigger = binary_sensor_ns.class_('ClickTrigger', Trigger.template(NoArg)) +DoubleClickTrigger = binary_sensor_ns.class_('DoubleClickTrigger', Trigger.template(NoArg)) +MultiClickTrigger = binary_sensor_ns.class_('MultiClickTrigger', Trigger.template(NoArg), Component) +MultiClickTriggerEvent = binary_sensor_ns.struct('MultiClickTriggerEvent') + +# Filters +Filter = binary_sensor_ns.class_('Filter') +DelayedOnFilter = binary_sensor_ns.class_('DelayedOnFilter', Filter, Component) +DelayedOffFilter = binary_sensor_ns.class_('DelayedOffFilter', Filter, Component) +HeartbeatFilter = binary_sensor_ns.class_('HeartbeatFilter', Filter, Component) +InvertFilter = binary_sensor_ns.class_('InvertFilter', Filter) +LambdaFilter = binary_sensor_ns.class_('LambdaFilter', Filter) + FILTER_KEYS = [CONF_INVERT, CONF_DELAYED_ON, CONF_DELAYED_OFF, CONF_LAMBDA, CONF_HEARTBEAT] @@ -142,7 +149,6 @@ def validate_multi_click_timing(value): BINARY_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({ cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTBinarySensorComponent), - cv.GenerateID(): cv.declare_variable_id(BinarySensor), vol.Optional(CONF_DEVICE_CLASS): vol.All(vol.Lower, cv.one_of(*DEVICE_CLASSES)), vol.Optional(CONF_FILTERS): FILTERS_SCHEMA, diff --git a/esphomeyaml/components/binary_sensor/esp32_ble_tracker.py b/esphomeyaml/components/binary_sensor/esp32_ble_tracker.py index 7208afabc6..63e08a0baf 100644 --- a/esphomeyaml/components/binary_sensor/esp32_ble_tracker.py +++ b/esphomeyaml/components/binary_sensor/esp32_ble_tracker.py @@ -8,7 +8,7 @@ from esphomeyaml.const import CONF_MAC_ADDRESS, CONF_NAME from esphomeyaml.helpers import esphomelib_ns, get_variable DEPENDENCIES = ['esp32_ble_tracker'] -ESP32BLEPresenceDevice = esphomelib_ns.ESP32BLEPresenceDevice +ESP32BLEPresenceDevice = esphomelib_ns.class_('ESP32BLEPresenceDevice', binary_sensor.BinarySensor) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ESP32BLEPresenceDevice), diff --git a/esphomeyaml/components/binary_sensor/esp32_touch.py b/esphomeyaml/components/binary_sensor/esp32_touch.py index 7ebde4e93b..fcf01e664c 100644 --- a/esphomeyaml/components/binary_sensor/esp32_touch.py +++ b/esphomeyaml/components/binary_sensor/esp32_touch.py @@ -34,7 +34,8 @@ def validate_touch_pad(value): return value -ESP32TouchBinarySensor = binary_sensor.binary_sensor_ns.ESP32TouchBinarySensor +ESP32TouchBinarySensor = binary_sensor.binary_sensor_ns.class_('ESP32TouchBinarySensor', + binary_sensor.BinarySensor) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ESP32TouchBinarySensor), diff --git a/esphomeyaml/components/binary_sensor/gpio.py b/esphomeyaml/components/binary_sensor/gpio.py index 4d4e880218..80a6d2b487 100644 --- a/esphomeyaml/components/binary_sensor/gpio.py +++ b/esphomeyaml/components/binary_sensor/gpio.py @@ -4,16 +4,19 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import binary_sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_PIN -from esphomeyaml.helpers import App, gpio_input_pin_expression, variable, Application +from esphomeyaml.helpers import App, gpio_input_pin_expression, variable, Application, \ + setup_component, Component -MakeGPIOBinarySensor = Application.MakeGPIOBinarySensor -GPIOBinarySensorComponent = binary_sensor.binary_sensor_ns.GPIOBinarySensorComponent +MakeGPIOBinarySensor = Application.struct('MakeGPIOBinarySensor') +GPIOBinarySensorComponent = binary_sensor.binary_sensor_ns.class_('GPIOBinarySensorComponent', + binary_sensor.BinarySensor, + Component) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(GPIOBinarySensorComponent), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeGPIOBinarySensor), vol.Required(CONF_PIN): pins.gpio_input_pin_schema -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): @@ -23,6 +26,7 @@ def to_code(config): rhs = App.make_gpio_binary_sensor(config[CONF_NAME], pin) gpio = variable(config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(gpio.Pgpio, gpio.Pmqtt, config) + setup_component(gpio.Pgpio, config) BUILD_FLAGS = '-DUSE_GPIO_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/nextion.py b/esphomeyaml/components/binary_sensor/nextion.py index 981e85dbb0..162392d223 100644 --- a/esphomeyaml/components/binary_sensor/nextion.py +++ b/esphomeyaml/components/binary_sensor/nextion.py @@ -10,7 +10,8 @@ DEPENDENCIES = ['display'] CONF_NEXTION_ID = 'nextion_id' -NextionTouchComponent = display.display_ns.NextionTouchComponent +NextionTouchComponent = display.display_ns.class_('NextionTouchComponent', + binary_sensor.BinarySensor) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(NextionTouchComponent), diff --git a/esphomeyaml/components/binary_sensor/pn532.py b/esphomeyaml/components/binary_sensor/pn532.py index fab8482391..38031b650c 100644 --- a/esphomeyaml/components/binary_sensor/pn532.py +++ b/esphomeyaml/components/binary_sensor/pn532.py @@ -27,7 +27,8 @@ def validate_uid(value): return value -PN532BinarySensor = binary_sensor.binary_sensor_ns.PN532BinarySensor +PN532BinarySensor = binary_sensor.binary_sensor_ns.class_('PN532BinarySensor', + binary_sensor.BinarySensor) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(PN532BinarySensor), @@ -37,7 +38,6 @@ PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend def to_code(config): - hub = None for hub in get_variable(config[CONF_PN532_ID]): yield addr = [HexInt(int(x, 16)) for x in config[CONF_UID].split('-')] diff --git a/esphomeyaml/components/binary_sensor/rdm6300.py b/esphomeyaml/components/binary_sensor/rdm6300.py index ff51f973db..c4a11e4f31 100644 --- a/esphomeyaml/components/binary_sensor/rdm6300.py +++ b/esphomeyaml/components/binary_sensor/rdm6300.py @@ -9,7 +9,8 @@ DEPENDENCIES = ['rdm6300'] CONF_RDM6300_ID = 'rdm6300_id' -RDM6300BinarySensor = binary_sensor.binary_sensor_ns.RDM6300BinarySensor +RDM6300BinarySensor = binary_sensor.binary_sensor_ns.class_('RDM6300BinarySensor', + binary_sensor.BinarySensor) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(RDM6300BinarySensor), @@ -19,7 +20,6 @@ PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend def to_code(config): - hub = None for hub in get_variable(config[CONF_RDM6300_ID]): yield rhs = hub.make_card(config[CONF_NAME], config[CONF_UID]) diff --git a/esphomeyaml/components/binary_sensor/remote_receiver.py b/esphomeyaml/components/binary_sensor/remote_receiver.py index e2ef0f43c5..4d66caca44 100644 --- a/esphomeyaml/components/binary_sensor/remote_receiver.py +++ b/esphomeyaml/components/binary_sensor/remote_receiver.py @@ -22,18 +22,18 @@ REMOTE_KEYS = [CONF_NEC, CONF_LG, CONF_SONY, CONF_PANASONIC, CONF_SAMSUNG, CONF_ CONF_REMOTE_RECEIVER_ID = 'remote_receiver_id' CONF_RECEIVER_ID = 'receiver_id' -RemoteReceiver = remote_ns.RemoteReceiver -LGReceiver = remote_ns.LGReceiver -NECReceiver = remote_ns.NECReceiver -PanasonicReceiver = remote_ns.PanasonicReceiver -RawReceiver = remote_ns.RawReceiver -SamsungReceiver = remote_ns.SamsungReceiver -SonyReceiver = remote_ns.SonyReceiver -RCSwitchRawReceiver = remote_ns.RCSwitchRawReceiver -RCSwitchTypeAReceiver = remote_ns.RCSwitchTypeAReceiver -RCSwitchTypeBReceiver = remote_ns.RCSwitchTypeBReceiver -RCSwitchTypeCReceiver = remote_ns.RCSwitchTypeCReceiver -RCSwitchTypeDReceiver = remote_ns.RCSwitchTypeDReceiver +RemoteReceiver = remote_ns.class_('RemoteReceiver', binary_sensor.BinarySensor) +LGReceiver = remote_ns.class_('LGReceiver', RemoteReceiver) +NECReceiver = remote_ns.class_('NECReceiver', RemoteReceiver) +PanasonicReceiver = remote_ns.class_('PanasonicReceiver', RemoteReceiver) +RawReceiver = remote_ns.class_('RawReceiver', RemoteReceiver) +SamsungReceiver = remote_ns.class_('SamsungReceiver', RemoteReceiver) +SonyReceiver = remote_ns.class_('SonyReceiver', RemoteReceiver) +RCSwitchRawReceiver = remote_ns.class_('RCSwitchRawReceiver', RemoteReceiver) +RCSwitchTypeAReceiver = remote_ns.class_('RCSwitchTypeAReceiver', RCSwitchRawReceiver) +RCSwitchTypeBReceiver = remote_ns.class_('RCSwitchTypeBReceiver', RCSwitchRawReceiver) +RCSwitchTypeCReceiver = remote_ns.class_('RCSwitchTypeCReceiver', RCSwitchRawReceiver) +RCSwitchTypeDReceiver = remote_ns.class_('RCSwitchTypeDReceiver', RCSwitchRawReceiver) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(RemoteReceiver), @@ -110,7 +110,6 @@ def receiver_base(full_config): def to_code(config): - remote = None for remote in get_variable(config[CONF_REMOTE_RECEIVER_ID]): yield rhs = receiver_base(config) diff --git a/esphomeyaml/components/binary_sensor/status.py b/esphomeyaml/components/binary_sensor/status.py index af2f0ff66b..5fca06cf93 100644 --- a/esphomeyaml/components/binary_sensor/status.py +++ b/esphomeyaml/components/binary_sensor/status.py @@ -1,23 +1,26 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components import binary_sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.helpers import App, Application, variable, setup_component, Component DEPENDENCIES = ['mqtt'] -MakeStatusBinarySensor = Application.MakeStatusBinarySensor -StatusBinarySensor = binary_sensor.binary_sensor_ns.StatusBinarySensor +MakeStatusBinarySensor = Application.struct('MakeStatusBinarySensor') +StatusBinarySensor = binary_sensor.binary_sensor_ns.class_('StatusBinarySensor', + binary_sensor.BinarySensor, + Component) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeStatusBinarySensor), cv.GenerateID(): cv.declare_variable_id(StatusBinarySensor), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_status_binary_sensor(config[CONF_NAME]) status = variable(config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(status.Pstatus, status.Pmqtt, config) + setup_component(status.Pstatus, config) BUILD_FLAGS = '-DUSE_STATUS_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/template.py b/esphomeyaml/components/binary_sensor/template.py index fb62e24024..3369b50aab 100644 --- a/esphomeyaml/components/binary_sensor/template.py +++ b/esphomeyaml/components/binary_sensor/template.py @@ -3,22 +3,26 @@ import voluptuous as vol from esphomeyaml.components import binary_sensor import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME -from esphomeyaml.helpers import App, Application, add, bool_, optional, process_lambda, variable +from esphomeyaml.helpers import App, Application, add, bool_, optional, process_lambda, variable, \ + setup_component, Component -MakeTemplateBinarySensor = Application.MakeTemplateBinarySensor -TemplateBinarySensor = binary_sensor.binary_sensor_ns.TemplateBinarySensor +MakeTemplateBinarySensor = Application.struct('MakeTemplateBinarySensor') +TemplateBinarySensor = binary_sensor.binary_sensor_ns.class_('TemplateBinarySensor', + binary_sensor.BinarySensor, + Component) PLATFORM_SCHEMA = cv.nameable(binary_sensor.BINARY_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TemplateBinarySensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateBinarySensor), vol.Required(CONF_LAMBDA): cv.lambda_, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_template_binary_sensor(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config) + setup_component(make.Ptemplate_, config) template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], diff --git a/esphomeyaml/components/cover/__init__.py b/esphomeyaml/components/cover/__init__.py index b768804d60..a121ce9b08 100644 --- a/esphomeyaml/components/cover/__init__.py +++ b/esphomeyaml/components/cover/__init__.py @@ -5,21 +5,25 @@ from esphomeyaml.components import mqtt import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ID, CONF_MQTT_ID, CONF_INTERNAL from esphomeyaml.helpers import Pvariable, esphomelib_ns, setup_mqtt_component, add, \ - TemplateArguments, get_variable + TemplateArguments, get_variable, Action, Nameable PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) cover_ns = esphomelib_ns.namespace('cover') -Cover = cover_ns.Cover -MQTTCoverComponent = cover_ns.MQTTCoverComponent -CoverState = cover_ns.CoverState + +Cover = cover_ns.class_('Cover', Nameable) +MQTTCoverComponent = cover_ns.class_('MQTTCoverComponent', mqtt.MQTTComponent) + +CoverState = cover_ns.class_('CoverState') COVER_OPEN = cover_ns.COVER_OPEN COVER_CLOSED = cover_ns.COVER_CLOSED -OpenAction = cover_ns.OpenAction -CloseAction = cover_ns.CloseAction -StopAction = cover_ns.StopAction + +# Actions +OpenAction = cover_ns.class_('OpenAction', Action) +CloseAction = cover_ns.class_('CloseAction', Action) +StopAction = cover_ns.class_('StopAction', Action) COVER_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(Cover), @@ -45,7 +49,7 @@ BUILD_FLAGS = '-DUSE_COVER' CONF_COVER_OPEN = 'cover.open' COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Cover), }) @@ -61,7 +65,7 @@ def cover_open_to_code(config, action_id, arg_type): CONF_COVER_CLOSE = 'cover.close' COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Cover), }) @@ -77,7 +81,7 @@ def cover_close_to_code(config, action_id, arg_type): CONF_COVER_STOP = 'cover.stop' COVER_STOP_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Cover), }) diff --git a/esphomeyaml/components/cover/template.py b/esphomeyaml/components/cover/template.py index 0b20c6c785..564c13ceb4 100644 --- a/esphomeyaml/components/cover/template.py +++ b/esphomeyaml/components/cover/template.py @@ -5,18 +5,21 @@ from esphomeyaml import automation from esphomeyaml.components import cover from esphomeyaml.const import CONF_CLOSE_ACTION, CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, \ CONF_OPEN_ACTION, CONF_STOP_ACTION, CONF_OPTIMISTIC -from esphomeyaml.helpers import App, Application, NoArg, add, process_lambda, variable, optional +from esphomeyaml.helpers import App, Application, NoArg, add, process_lambda, variable, optional, \ + setup_component -MakeTemplateCover = Application.MakeTemplateCover +MakeTemplateCover = Application.struct('MakeTemplateCover') +TemplateCover = cover.cover_ns.class_('TemplateCover', cover.Cover) PLATFORM_SCHEMA = cv.nameable(cover.COVER_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateCover), + cv.GenerateID(): cv.declare_variable_id(TemplateCover), vol.Optional(CONF_LAMBDA): cv.lambda_, vol.Optional(CONF_OPTIMISTIC): 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), -}), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) def to_code(config): @@ -24,9 +27,9 @@ def to_code(config): make = variable(config[CONF_MAKE_ID], rhs) cover.setup_cover(make.Ptemplate_, make.Pmqtt, config) + setup_component(make.Ptemplate_, config) if CONF_LAMBDA in config: - template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(cover.CoverState)): yield diff --git a/esphomeyaml/components/dallas.py b/esphomeyaml/components/dallas.py index 22c4ac3ee3..eae9fa9c82 100644 --- a/esphomeyaml/components/dallas.py +++ b/esphomeyaml/components/dallas.py @@ -4,21 +4,22 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ID, CONF_PIN, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Pvariable +from esphomeyaml.helpers import App, Pvariable, setup_component, PollingComponent -DallasComponent = sensor.sensor_ns.DallasComponent +DallasComponent = sensor.sensor_ns.class_('DallasComponent', PollingComponent) CONFIG_SCHEMA = vol.All(cv.ensure_list, [vol.Schema({ cv.GenerateID(): cv.declare_variable_id(DallasComponent), vol.Required(CONF_PIN): pins.input_output_pin, vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})]) +}).extend(cv.COMPONENT_SCHEMA.schema)]) def to_code(config): for conf in config: rhs = App.make_dallas_component(conf[CONF_PIN], conf.get(CONF_UPDATE_INTERVAL)) - Pvariable(conf[CONF_ID], rhs) + var = Pvariable(conf[CONF_ID], rhs) + setup_component(var, conf) BUILD_FLAGS = '-DUSE_DALLAS_SENSOR' diff --git a/esphomeyaml/components/deep_sleep.py b/esphomeyaml/components/deep_sleep.py index 7dc3ecdf42..bcded6c00a 100644 --- a/esphomeyaml/components/deep_sleep.py +++ b/esphomeyaml/components/deep_sleep.py @@ -1,11 +1,11 @@ import voluptuous as vol from esphomeyaml import config_validation as cv, pins -from esphomeyaml.automation import maybe_simple_id, ACTION_REGISTRY +from esphomeyaml.automation import ACTION_REGISTRY, maybe_simple_id from esphomeyaml.const import CONF_ID, CONF_NUMBER, CONF_RUN_CYCLES, CONF_RUN_DURATION, \ CONF_SLEEP_DURATION, CONF_WAKEUP_PIN -from esphomeyaml.helpers import App, Pvariable, add, gpio_input_pin_expression, esphomelib_ns, \ - TemplateArguments, get_variable +from esphomeyaml.helpers import Action, App, Component, Pvariable, TemplateArguments, add, \ + esphomelib_ns, get_variable, gpio_input_pin_expression, setup_component def validate_pin_number(value): @@ -16,14 +16,15 @@ def validate_pin_number(value): return value -DeepSleepComponent = esphomelib_ns.DeepSleepComponent -EnterDeepSleepAction = esphomelib_ns.EnterDeepSleepAction -PreventDeepSleepAction = esphomelib_ns.PreventDeepSleepAction +DeepSleepComponent = esphomelib_ns.class_('DeepSleepComponent', Component) +EnterDeepSleepAction = esphomelib_ns.class_('EnterDeepSleepAction', Action) +PreventDeepSleepAction = esphomelib_ns.class_('PreventDeepSleepAction', Action) +WakeupPinMode = esphomelib_ns.enum('WakeupPinMode') WAKEUP_PIN_MODES = { - 'IGNORE': esphomelib_ns.WAKEUP_PIN_MODE_IGNORE, - 'KEEP_AWAKE': esphomelib_ns.WAKEUP_PIN_MODE_KEEP_AWAKE, - 'INVERT_WAKEUP': esphomelib_ns.WAKEUP_PIN_MODE_INVERT_WAKEUP, + 'IGNORE': WakeupPinMode.WAKEUP_PIN_MODE_IGNORE, + 'KEEP_AWAKE': WakeupPinMode.WAKEUP_PIN_MODE_KEEP_AWAKE, + 'INVERT_WAKEUP': WakeupPinMode.WAKEUP_PIN_MODE_INVERT_WAKEUP, } CONF_WAKEUP_PIN_MODE = 'wakeup_pin_mode' @@ -37,7 +38,7 @@ CONFIG_SCHEMA = vol.Schema({ cv.one_of(*WAKEUP_PIN_MODES)), vol.Optional(CONF_RUN_CYCLES): cv.positive_int, vol.Optional(CONF_RUN_DURATION): cv.positive_time_period_milliseconds, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -46,7 +47,6 @@ def to_code(config): if CONF_SLEEP_DURATION in config: add(deep_sleep.set_sleep_duration(config[CONF_SLEEP_DURATION])) if CONF_WAKEUP_PIN in config: - pin = None for pin in gpio_input_pin_expression(config[CONF_WAKEUP_PIN]): yield add(deep_sleep.set_wakeup_pin(pin)) @@ -57,10 +57,11 @@ def to_code(config): if CONF_RUN_DURATION in config: add(deep_sleep.set_run_duration(config[CONF_RUN_DURATION])) + setup_component(deep_sleep, config) + BUILD_FLAGS = '-DUSE_DEEP_SLEEP' - CONF_DEEP_SLEEP_ENTER = 'deep_sleep.enter' DEEP_SLEEP_ENTER_ACTION_SCHEMA = maybe_simple_id({ vol.Required(CONF_ID): cv.use_variable_id(DeepSleepComponent), diff --git a/esphomeyaml/components/display/__init__.py b/esphomeyaml/components/display/__init__.py index cada701f87..ffcaabecb6 100644 --- a/esphomeyaml/components/display/__init__.py +++ b/esphomeyaml/components/display/__init__.py @@ -10,7 +10,7 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) display_ns = esphomelib_ns.namespace('display') -DisplayBuffer = display_ns.DisplayBuffer +DisplayBuffer = display_ns.class_('DisplayBuffer') DisplayBufferRef = DisplayBuffer.operator('ref') DISPLAY_ROTATIONS = { diff --git a/esphomeyaml/components/display/lcd_gpio.py b/esphomeyaml/components/display/lcd_gpio.py index 66cc021c5f..d36ac780e8 100644 --- a/esphomeyaml/components/display/lcd_gpio.py +++ b/esphomeyaml/components/display/lcd_gpio.py @@ -5,11 +5,12 @@ from esphomeyaml import pins from esphomeyaml.components import display from esphomeyaml.const import CONF_DATA_PINS, CONF_DIMENSIONS, CONF_ENABLE_PIN, CONF_ID, \ CONF_LAMBDA, CONF_RS_PIN, CONF_RW_PIN -from esphomeyaml.helpers import App, Pvariable, add, gpio_output_pin_expression, process_lambda +from esphomeyaml.helpers import App, Pvariable, add, gpio_output_pin_expression, process_lambda, \ + setup_component, PollingComponent -GPIOLCDDisplay = display.display_ns.GPIOLCDDisplay -LCDDisplay = display.display_ns.LCDDisplay +LCDDisplay = display.display_ns.class_('LCDDisplay', PollingComponent) LCDDisplayRef = LCDDisplay.operator('ref') +GPIOLCDDisplay = display.display_ns.class_('GPIOLCDDisplay', LCDDisplay) def validate_lcd_dimensions(value): @@ -36,7 +37,7 @@ PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ENABLE_PIN): pins.gpio_output_pin_schema, vol.Required(CONF_RS_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_RW_PIN): pins.gpio_output_pin_schema, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -67,6 +68,7 @@ def to_code(config): add(lcd.set_writer(lambda_)) display.setup_display(lcd, config) + setup_component(lcd, config) BUILD_FLAGS = '-DUSE_LCD_DISPLAY' diff --git a/esphomeyaml/components/display/lcd_pcf8574.py b/esphomeyaml/components/display/lcd_pcf8574.py index a2e9e28eac..761203c212 100644 --- a/esphomeyaml/components/display/lcd_pcf8574.py +++ b/esphomeyaml/components/display/lcd_pcf8574.py @@ -1,20 +1,21 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import display -from esphomeyaml.components.display.lcd_gpio import LCDDisplayRef, validate_lcd_dimensions +from esphomeyaml.components import display, i2c +from esphomeyaml.components.display.lcd_gpio import LCDDisplayRef, validate_lcd_dimensions, \ + LCDDisplay from esphomeyaml.const import CONF_ADDRESS, CONF_DIMENSIONS, CONF_ID, CONF_LAMBDA -from esphomeyaml.helpers import App, Pvariable, add, process_lambda +from esphomeyaml.helpers import App, Pvariable, add, process_lambda, setup_component DEPENDENCIES = ['i2c'] -PCF8574LCDDisplay = display.display_ns.PCF8574LCDDisplay +PCF8574LCDDisplay = display.display_ns.class_('PCF8574LCDDisplay', LCDDisplay, i2c.I2CDevice) PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(PCF8574LCDDisplay), vol.Required(CONF_DIMENSIONS): validate_lcd_dimensions, vol.Optional(CONF_ADDRESS): cv.i2c_address, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -30,6 +31,7 @@ def to_code(config): add(lcd.set_writer(lambda_)) display.setup_display(lcd, config) + setup_component(lcd, config) BUILD_FLAGS = ['-DUSE_LCD_DISPLAY', '-DUSE_LCD_DISPLAY_PCF8574'] diff --git a/esphomeyaml/components/display/max7219.py b/esphomeyaml/components/display/max7219.py index 24256bfb32..b5bf380c4d 100644 --- a/esphomeyaml/components/display/max7219.py +++ b/esphomeyaml/components/display/max7219.py @@ -2,16 +2,16 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins -from esphomeyaml.components import display +from esphomeyaml.components import display, spi from esphomeyaml.components.spi import SPIComponent from esphomeyaml.const import CONF_CS_PIN, CONF_ID, CONF_INTENSITY, CONF_LAMBDA, CONF_NUM_CHIPS, \ CONF_SPI_ID from esphomeyaml.helpers import App, Pvariable, add, get_variable, gpio_output_pin_expression, \ - process_lambda + process_lambda, setup_component, PollingComponent DEPENDENCIES = ['spi'] -MAX7219Component = display.display_ns.MAX7219Component +MAX7219Component = display.display_ns.class_('MAX7219Component', PollingComponent, spi.SPIDevice) MAX7219ComponentRef = MAX7219Component.operator('ref') PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({ @@ -21,15 +21,15 @@ PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NUM_CHIPS): vol.All(cv.uint8_t, vol.Range(min=1)), vol.Optional(CONF_INTENSITY): vol.All(cv.uint8_t, vol.Range(min=0, max=15)), -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - for spi in get_variable(config[CONF_SPI_ID]): + 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_max7219(spi, cs) + rhs = App.make_max7219(spi_, cs) max7219 = Pvariable(config[CONF_ID], rhs) if CONF_NUM_CHIPS in config: @@ -43,6 +43,7 @@ def to_code(config): add(max7219.set_writer(lambda_)) display.setup_display(max7219, config) + setup_component(max7219, config) BUILD_FLAGS = '-DUSE_MAX7219' diff --git a/esphomeyaml/components/display/nextion.py b/esphomeyaml/components/display/nextion.py index 44e69c74cf..9f26647f96 100644 --- a/esphomeyaml/components/display/nextion.py +++ b/esphomeyaml/components/display/nextion.py @@ -1,24 +1,26 @@ -import esphomeyaml.config_validation as cv -from esphomeyaml.components import display +from esphomeyaml.components import display, uart from esphomeyaml.components.uart import UARTComponent +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ID, CONF_LAMBDA, CONF_UART_ID -from esphomeyaml.helpers import App, Pvariable, add, get_variable, process_lambda +from esphomeyaml.helpers import App, PollingComponent, Pvariable, add, get_variable, \ + process_lambda, \ + setup_component DEPENDENCIES = ['uart'] -Nextion = display.display_ns.Nextion +Nextion = display.display_ns.class_('Nextion', PollingComponent, uart.UARTDevice) NextionRef = Nextion.operator('ref') PLATFORM_SCHEMA = display.BASIC_DISPLAY_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(Nextion), cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent), -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - for uart in get_variable(config[CONF_UART_ID]): + for uart_ in get_variable(config[CONF_UART_ID]): yield - rhs = App.make_nextion(uart) + rhs = App.make_nextion(uart_) nextion = Pvariable(config[CONF_ID], rhs) if CONF_LAMBDA in config: @@ -27,6 +29,7 @@ def to_code(config): add(nextion.set_writer(lambda_)) display.setup_display(nextion, config) + setup_component(nextion, config) BUILD_FLAGS = '-DUSE_NEXTION' diff --git a/esphomeyaml/components/display/ssd1306_i2c.py b/esphomeyaml/components/display/ssd1306_i2c.py index be19b119c0..a80ba8d5b5 100644 --- a/esphomeyaml/components/display/ssd1306_i2c.py +++ b/esphomeyaml/components/display/ssd1306_i2c.py @@ -7,11 +7,11 @@ from esphomeyaml.components.display import ssd1306_spi from esphomeyaml.const import CONF_ADDRESS, CONF_EXTERNAL_VCC, CONF_ID, \ CONF_MODEL, CONF_RESET_PIN, CONF_LAMBDA from esphomeyaml.helpers import App, Pvariable, add, \ - gpio_output_pin_expression, process_lambda + gpio_output_pin_expression, process_lambda, setup_component DEPENDENCIES = ['i2c'] -I2CSSD1306 = display.display_ns.I2CSSD1306 +I2CSSD1306 = display.display_ns.class_('I2CSSD1306', ssd1306_spi.SSD1306) PLATFORM_SCHEMA = display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(I2CSSD1306), @@ -19,7 +19,7 @@ PLATFORM_SCHEMA = display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_EXTERNAL_VCC): cv.boolean, vol.Optional(CONF_ADDRESS): cv.i2c_address, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -41,6 +41,7 @@ def to_code(config): add(ssd.set_writer(lambda_)) display.setup_display(ssd, config) + setup_component(ssd, config) BUILD_FLAGS = '-DUSE_SSD1306' diff --git a/esphomeyaml/components/display/ssd1306_spi.py b/esphomeyaml/components/display/ssd1306_spi.py index b08e0a3b00..bf47a5e92a 100644 --- a/esphomeyaml/components/display/ssd1306_spi.py +++ b/esphomeyaml/components/display/ssd1306_spi.py @@ -2,27 +2,29 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins -from esphomeyaml.components import display +from esphomeyaml.components import display, spi from esphomeyaml.components.spi import SPIComponent from esphomeyaml.const import CONF_CS_PIN, CONF_DC_PIN, CONF_EXTERNAL_VCC, \ CONF_ID, CONF_MODEL, \ CONF_RESET_PIN, CONF_SPI_ID, CONF_LAMBDA from esphomeyaml.helpers import App, Pvariable, add, get_variable, \ - gpio_output_pin_expression, process_lambda + gpio_output_pin_expression, process_lambda, setup_component, PollingComponent DEPENDENCIES = ['spi'] -SPISSD1306 = display.display_ns.SPISSD1306 +SSD1306 = display.display_ns.class_('SSD1306', PollingComponent, display.DisplayBuffer) +SPISSD1306 = display.display_ns.class_('SPISSD1306', SSD1306, spi.SPIDevice) +SSD1306Model = display.display_ns.enum('SSD1306Model') MODELS = { - 'SSD1306_128X32': display.display_ns.SSD1306_MODEL_128_32, - 'SSD1306_128X64': display.display_ns.SSD1306_MODEL_128_64, - 'SSD1306_96X16': display.display_ns.SSD1306_MODEL_96_16, - 'SSD1306_64X48': display.display_ns.SSD1306_MODEL_64_48, - 'SH1106_128X32': display.display_ns.SH1106_MODEL_128_32, - 'SH1106_128X64': display.display_ns.SH1106_MODEL_128_64, - 'SH1106_96X16': display.display_ns.SH1106_MODEL_96_16, - 'SH1106_64X48': display.display_ns.SH1106_MODEL_64_48, + '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 = vol.All(vol.Upper, vol.Replace(' ', '_'), cv.one_of(*MODELS)) @@ -35,18 +37,18 @@ PLATFORM_SCHEMA = display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({ 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) def to_code(config): - for spi in get_variable(config[CONF_SPI_ID]): + for spi_ in get_variable(config[CONF_SPI_ID]): yield for cs in gpio_output_pin_expression(config[CONF_CS_PIN]): yield for dc in gpio_output_pin_expression(config[CONF_DC_PIN]): yield - rhs = App.make_spi_ssd1306(spi, cs, dc) + rhs = App.make_spi_ssd1306(spi_, cs, dc) ssd = Pvariable(config[CONF_ID], rhs) add(ssd.set_model(MODELS[config[CONF_MODEL]])) @@ -63,6 +65,7 @@ def to_code(config): add(ssd.set_writer(lambda_)) display.setup_display(ssd, config) + setup_component(ssd, config) BUILD_FLAGS = '-DUSE_SSD1306' diff --git a/esphomeyaml/components/display/waveshare_epaper.py b/esphomeyaml/components/display/waveshare_epaper.py index 823f268349..51723440cf 100644 --- a/esphomeyaml/components/display/waveshare_epaper.py +++ b/esphomeyaml/components/display/waveshare_epaper.py @@ -2,25 +2,30 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins -from esphomeyaml.components import display +from esphomeyaml.components import display, spi from esphomeyaml.components.spi import SPIComponent from esphomeyaml.const import CONF_BUSY_PIN, CONF_CS_PIN, CONF_DC_PIN, CONF_FULL_UPDATE_EVERY, \ CONF_ID, CONF_LAMBDA, CONF_MODEL, CONF_RESET_PIN, CONF_SPI_ID from esphomeyaml.helpers import App, Pvariable, add, get_variable, gpio_input_pin_expression, \ - gpio_output_pin_expression, process_lambda + gpio_output_pin_expression, process_lambda, setup_component, PollingComponent DEPENDENCIES = ['spi'] WaveshareEPaperTypeA = display.display_ns.WaveshareEPaperTypeA -WaveshareEPaper = display.display_ns.WaveshareEPaper +WaveshareEPaper = display.display_ns.class_('WaveshareEPaper', + PollingComponent, spi.SPIDevice, display.DisplayBuffer) + + +WaveshareEPaperTypeAModel = display.display_ns.enum('WaveshareEPaperTypeAModel') +WaveshareEPaperTypeBModel = display.display_ns.enum('WaveshareEPaperTypeBModel') MODELS = { - '1.54in': ('a', display.display_ns.WAVESHARE_EPAPER_1_54_IN), - '2.13in': ('a', display.display_ns.WAVESHARE_EPAPER_2_13_IN), - '2.90in': ('a', display.display_ns.WAVESHARE_EPAPER_2_9_IN), - '2.70in': ('b', display.display_ns.WAVESHARE_EPAPER_2_7_IN), - '4.20in': ('b', display.display_ns.WAVESHARE_EPAPER_4_2_IN), - '7.50in': ('b', display.display_ns.WAVESHARE_EPAPER_7_5_IN), + '1.54in': ('a', WaveshareEPaperTypeAModel.WAVESHARE_EPAPER_1_54_IN), + '2.13in': ('a', WaveshareEPaperTypeAModel.WAVESHARE_EPAPER_2_13_IN), + '2.90in': ('a', WaveshareEPaperTypeAModel.WAVESHARE_EPAPER_2_9_IN), + '2.70in': ('b', WaveshareEPaperTypeBModel.WAVESHARE_EPAPER_2_7_IN), + '4.20in': ('b', WaveshareEPaperTypeBModel.WAVESHARE_EPAPER_4_2_IN), + '7.50in': ('b', WaveshareEPaperTypeBModel.WAVESHARE_EPAPER_7_5_IN), } @@ -34,7 +39,7 @@ def validate_full_update_every_only_type_a(value): PLATFORM_SCHEMA = vol.All(display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({ - cv.GenerateID(): cv.declare_variable_id(None), + cv.GenerateID(): cv.declare_variable_id(WaveshareEPaper), 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, @@ -42,11 +47,11 @@ PLATFORM_SCHEMA = vol.All(display.FULL_DISPLAY_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_BUSY_PIN): pins.gpio_input_pin_schema, vol.Optional(CONF_FULL_UPDATE_EVERY): cv.uint32_t, -}), validate_full_update_every_only_type_a) +}).extend(cv.COMPONENT_SCHEMA.schema), validate_full_update_every_only_type_a) def to_code(config): - for spi in get_variable(config[CONF_SPI_ID]): + for spi_ in get_variable(config[CONF_SPI_ID]): yield for cs in gpio_output_pin_expression(config[CONF_CS_PIN]): yield @@ -55,10 +60,10 @@ def to_code(config): model_type, model = MODELS[config[CONF_MODEL]] if model_type == 'a': - rhs = App.make_waveshare_epaper_type_a(spi, cs, dc, model) + rhs = App.make_waveshare_epaper_type_a(spi_, cs, dc, model) epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaperTypeA) elif model_type == 'b': - rhs = App.make_waveshare_epaper_type_b(spi, cs, dc, model) + rhs = App.make_waveshare_epaper_type_b(spi_, cs, dc, model) epaper = Pvariable(config[CONF_ID], rhs, type=WaveshareEPaper) else: raise NotImplementedError() @@ -79,6 +84,7 @@ def to_code(config): add(epaper.set_full_update_every(config[CONF_FULL_UPDATE_EVERY])) display.setup_display(epaper, config) + setup_component(epaper, config) BUILD_FLAGS = '-DUSE_WAVESHARE_EPAPER' diff --git a/esphomeyaml/components/esp32_ble.py b/esphomeyaml/components/esp32_ble.py deleted file mode 100644 index adf60a7290..0000000000 --- a/esphomeyaml/components/esp32_ble.py +++ /dev/null @@ -1,5 +0,0 @@ -from esphomeyaml import config_validation as cv - -CONFIG_SCHEMA = cv.invalid("The 'esp32_ble' component has been renamed to the 'esp32_ble_tracker' " - "component in order to avoid confusion with the new 'esp32_ble_beacon' " - "component.") diff --git a/esphomeyaml/components/esp32_ble_beacon.py b/esphomeyaml/components/esp32_ble_beacon.py index c10b0a0fc7..2d9e8d37a4 100644 --- a/esphomeyaml/components/esp32_ble_beacon.py +++ b/esphomeyaml/components/esp32_ble_beacon.py @@ -1,12 +1,13 @@ import voluptuous as vol from esphomeyaml import config_validation as cv -from esphomeyaml.const import CONF_ID, CONF_SCAN_INTERVAL, ESP_PLATFORM_ESP32, CONF_UUID, CONF_TYPE -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, RawExpression, ArrayInitializer +from esphomeyaml.const import CONF_ID, CONF_SCAN_INTERVAL, CONF_TYPE, CONF_UUID, ESP_PLATFORM_ESP32 +from esphomeyaml.helpers import App, ArrayInitializer, Component, Pvariable, RawExpression, add, \ + esphomelib_ns, setup_component ESP_PLATFORMS = [ESP_PLATFORM_ESP32] -ESP32BLEBeacon = esphomelib_ns.ESP32BLEBeacon +ESP32BLEBeacon = esphomelib_ns.class_('ESP32BLEBeacon', Component) CONF_MAJOR = 'major' CONF_MINOR = 'minor' @@ -18,12 +19,12 @@ CONFIG_SCHEMA = vol.Schema({ 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)] + uuid_arr = [RawExpression('0x{}'.format(uuid[i:i + 2])) for i in range(0, len(uuid), 2)] rhs = App.make_esp32_ble_beacon(ArrayInitializer(*uuid_arr, multiline=False)) ble = Pvariable(config[CONF_ID], rhs) if CONF_MAJOR in config: @@ -31,5 +32,7 @@ def to_code(config): if CONF_MINOR in config: add(ble.set_minor(config[CONF_MINOR])) + setup_component(ble, config) + BUILD_FLAGS = '-DUSE_ESP32_BLE_BEACON' diff --git a/esphomeyaml/components/esp32_ble_tracker.py b/esphomeyaml/components/esp32_ble_tracker.py index 600d31bd97..40be12590a 100644 --- a/esphomeyaml/components/esp32_ble_tracker.py +++ b/esphomeyaml/components/esp32_ble_tracker.py @@ -1,19 +1,26 @@ import voluptuous as vol from esphomeyaml import config_validation as cv +from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ID, CONF_SCAN_INTERVAL, ESP_PLATFORM_ESP32 from esphomeyaml.core import HexInt -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, ArrayInitializer +from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, ArrayInitializer, \ + setup_component, Component ESP_PLATFORMS = [ESP_PLATFORM_ESP32] CONF_ESP32_BLE_ID = 'esp32_ble_id' -ESP32BLETracker = esphomelib_ns.ESP32BLETracker +ESP32BLETracker = esphomelib_ns.class_('ESP32BLETracker', Component) +XiaomiSensor = esphomelib_ns.class_('XiaomiSensor', sensor.Sensor) +XiaomiDevice = esphomelib_ns.class_('XiaomiDevice') +XIAOMI_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(XiaomiSensor) +}) CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(ESP32BLETracker), vol.Optional(CONF_SCAN_INTERVAL): cv.positive_time_period_milliseconds, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def make_address_array(address): @@ -27,5 +34,7 @@ def to_code(config): if CONF_SCAN_INTERVAL in config: add(ble.set_scan_interval(config[CONF_SCAN_INTERVAL])) + setup_component(ble, config) + BUILD_FLAGS = '-DUSE_ESP32_BLE_TRACKER' diff --git a/esphomeyaml/components/esp32_touch.py b/esphomeyaml/components/esp32_touch.py index 5804a0863e..0591e1e92f 100644 --- a/esphomeyaml/components/esp32_touch.py +++ b/esphomeyaml/components/esp32_touch.py @@ -6,7 +6,7 @@ from esphomeyaml.const import CONF_ID, CONF_SETUP_MODE, CONF_IIR_FILTER, \ CONF_SLEEP_DURATION, CONF_MEASUREMENT_DURATION, CONF_LOW_VOLTAGE_REFERENCE, \ CONF_HIGH_VOLTAGE_REFERENCE, CONF_VOLTAGE_ATTENUATION, ESP_PLATFORM_ESP32 from esphomeyaml.core import TimePeriod -from esphomeyaml.helpers import App, Pvariable, add, global_ns +from esphomeyaml.helpers import App, Pvariable, add, global_ns, setup_component, Component ESP_PLATFORMS = [ESP_PLATFORM_ESP32] @@ -41,7 +41,7 @@ VOLTAGE_ATTENUATION = { '0V': global_ns.TOUCH_HVOLT_ATTEN_0V, } -ESP32TouchComponent = binary_sensor.binary_sensor_ns.ESP32TouchComponent +ESP32TouchComponent = binary_sensor.binary_sensor_ns.class_('ESP32TouchComponent', Component) CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(ESP32TouchComponent), @@ -54,7 +54,7 @@ CONFIG_SCHEMA = vol.Schema({ 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): @@ -80,5 +80,7 @@ def to_code(config): value = VOLTAGE_ATTENUATION[config[CONF_VOLTAGE_ATTENUATION]] add(touch.set_voltage_attenuation(value)) + setup_component(touch, config) + BUILD_FLAGS = '-DUSE_ESP32_TOUCH_BINARY_SENSOR' diff --git a/esphomeyaml/components/fan/__init__.py b/esphomeyaml/components/fan/__init__.py index 807c463db5..9874c4114f 100644 --- a/esphomeyaml/components/fan/__init__.py +++ b/esphomeyaml/components/fan/__init__.py @@ -7,24 +7,27 @@ from esphomeyaml.const import CONF_ID, CONF_MQTT_ID, CONF_OSCILLATION_COMMAND_TO CONF_OSCILLATION_STATE_TOPIC, CONF_SPEED_COMMAND_TOPIC, CONF_SPEED_STATE_TOPIC, CONF_INTERNAL, \ CONF_SPEED, CONF_OSCILLATING, CONF_OSCILLATION_OUTPUT, CONF_NAME from esphomeyaml.helpers import Application, Pvariable, add, esphomelib_ns, setup_mqtt_component, \ - TemplateArguments, get_variable, templatable, bool_ + TemplateArguments, get_variable, templatable, bool_, Action, Nameable, Component PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) fan_ns = esphomelib_ns.namespace('fan') -FanState = fan_ns.FanState -MQTTFanComponent = fan_ns.MQTTFanComponent -MakeFan = Application.MakeFan -TurnOnAction = fan_ns.TurnOnAction -TurnOffAction = fan_ns.TurnOffAction -ToggleAction = fan_ns.ToggleAction -FanSpeed = fan_ns.FanSpeed -FAN_SPEED_OFF = fan_ns.FAN_SPEED_OFF -FAN_SPEED_LOW = fan_ns.FAN_SPEED_LOW -FAN_SPEED_MEDIUM = fan_ns.FAN_SPEED_MEDIUM -FAN_SPEED_HIGH = fan_ns.FAN_SPEED_HIGH +FanState = fan_ns.class_('FanState', Nameable, Component) +MQTTFanComponent = fan_ns.class_('MQTTFanComponent', mqtt.MQTTComponent) +MakeFan = Application.struct('MakeFan') + +# Actions +TurnOnAction = fan_ns.class_('TurnOnAction', Action) +TurnOffAction = fan_ns.class_('TurnOffAction', Action) +ToggleAction = fan_ns.class_('ToggleAction', Action) + +FanSpeed = fan_ns.enum('FanSpeed') +FAN_SPEED_OFF = FanSpeed.FAN_SPEED_OFF +FAN_SPEED_LOW = FanSpeed.FAN_SPEED_LOW +FAN_SPEED_MEDIUM = FanSpeed.FAN_SPEED_MEDIUM +FAN_SPEED_HIGH = FanSpeed.FAN_SPEED_HIGH FAN_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(FanState), @@ -74,7 +77,7 @@ BUILD_FLAGS = '-DUSE_FAN' CONF_FAN_TOGGLE = 'fan.toggle' FAN_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(FanState), }) @@ -90,7 +93,7 @@ def fan_toggle_to_code(config, action_id, arg_type): CONF_FAN_TURN_OFF = 'fan.turn_off' FAN_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(FanState), }) @@ -106,7 +109,7 @@ def fan_turn_off_to_code(config, action_id, arg_type): CONF_FAN_TURN_ON = 'fan.turn_on' FAN_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(FanState), vol.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean), vol.Optional(CONF_SPEED): cv.templatable(validate_fan_speed), }) diff --git a/esphomeyaml/components/fan/binary.py b/esphomeyaml/components/fan/binary.py index cf588989a4..1ce51c2a0b 100644 --- a/esphomeyaml/components/fan/binary.py +++ b/esphomeyaml/components/fan/binary.py @@ -1,32 +1,31 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import fan +from esphomeyaml.components import fan, output from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_OSCILLATION_OUTPUT, CONF_OUTPUT -from esphomeyaml.helpers import App, add, get_variable, variable +from esphomeyaml.helpers import App, add, get_variable, variable, setup_component PLATFORM_SCHEMA = cv.nameable(fan.FAN_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(fan.MakeFan), - vol.Required(CONF_OUTPUT): cv.use_variable_id(None), - vol.Optional(CONF_OSCILLATION_OUTPUT): cv.use_variable_id(None), -})) + vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput), + vol.Optional(CONF_OSCILLATION_OUTPUT): cv.use_variable_id(output.BinaryOutput), +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - output = None - for output in get_variable(config[CONF_OUTPUT]): + for output_ in get_variable(config[CONF_OUTPUT]): yield rhs = App.make_fan(config[CONF_NAME]) fan_struct = variable(config[CONF_MAKE_ID], rhs) - add(fan_struct.Poutput.set_binary(output)) + add(fan_struct.Poutput.set_binary(output_)) if CONF_OSCILLATION_OUTPUT in config: - oscillation_output = None for oscillation_output in get_variable(config[CONF_OSCILLATION_OUTPUT]): yield add(fan_struct.Poutput.set_oscillation(oscillation_output)) fan.setup_fan(fan_struct.Pstate, fan_struct.Pmqtt, config) + setup_component(fan_struct.Poutput, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/fan/speed.py b/esphomeyaml/components/fan/speed.py index 468f9be8e8..d1259d3ae1 100644 --- a/esphomeyaml/components/fan/speed.py +++ b/esphomeyaml/components/fan/speed.py @@ -1,7 +1,7 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import fan, mqtt +from esphomeyaml.components import fan, mqtt, output from esphomeyaml.const import CONF_HIGH, CONF_LOW, CONF_MAKE_ID, CONF_MEDIUM, CONF_NAME, \ CONF_OSCILLATION_OUTPUT, CONF_OUTPUT, CONF_SPEED, CONF_SPEED_COMMAND_TOPIC, \ CONF_SPEED_STATE_TOPIC @@ -9,35 +9,33 @@ from esphomeyaml.helpers import App, add, get_variable, variable PLATFORM_SCHEMA = cv.nameable(fan.FAN_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(fan.MakeFan), - vol.Required(CONF_OUTPUT): cv.use_variable_id(None), + vol.Required(CONF_OUTPUT): cv.use_variable_id(output.FloatOutput), vol.Optional(CONF_SPEED_STATE_TOPIC): cv.publish_topic, vol.Optional(CONF_SPEED_COMMAND_TOPIC): cv.subscribe_topic, - vol.Optional(CONF_OSCILLATION_OUTPUT): cv.use_variable_id(None), + vol.Optional(CONF_OSCILLATION_OUTPUT): cv.use_variable_id(output.BinaryOutput), vol.Optional(CONF_SPEED): vol.Schema({ vol.Required(CONF_LOW): cv.percentage, vol.Required(CONF_MEDIUM): cv.percentage, vol.Required(CONF_HIGH): cv.percentage, }), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - output = None - for output in get_variable(config[CONF_OUTPUT]): + for output_ in get_variable(config[CONF_OUTPUT]): yield rhs = App.make_fan(config[CONF_NAME]) fan_struct = variable(config[CONF_MAKE_ID], rhs) if CONF_SPEED in config: speeds = config[CONF_SPEED] - add(fan_struct.Poutput.set_speed(output, + add(fan_struct.Poutput.set_speed(output_, speeds[CONF_LOW], speeds[CONF_MEDIUM], speeds[CONF_HIGH])) else: - add(fan_struct.Poutput.set_speed(output)) + add(fan_struct.Poutput.set_speed(output_)) if CONF_OSCILLATION_OUTPUT in config: - oscillation_output = None for oscillation_output in get_variable(config[CONF_OSCILLATION_OUTPUT]): yield add(fan_struct.Poutput.set_oscillation(oscillation_output)) diff --git a/esphomeyaml/components/font.py b/esphomeyaml/components/font.py index f18336a0a0..1456c3e717 100644 --- a/esphomeyaml/components/font.py +++ b/esphomeyaml/components/font.py @@ -11,8 +11,8 @@ from esphomeyaml.helpers import App, ArrayInitializer, MockObj, Pvariable, RawEx DEPENDENCIES = ['display'] -Font = display.display_ns.Font -Glyph = display.display_ns.Glyph +Font = display.display_ns.class_('Font') +Glyph = display.display_ns.class_('Glyph') def validate_glyphs(value): diff --git a/esphomeyaml/components/globals.py b/esphomeyaml/components/globals.py new file mode 100644 index 0000000000..d9a6cf0978 --- /dev/null +++ b/esphomeyaml/components/globals.py @@ -0,0 +1,35 @@ +import voluptuous as vol + +from esphomeyaml import config_validation as cv +from esphomeyaml.const import CONF_ID, CONF_INITIAL_VALUE, CONF_RESTORE_VALUE, CONF_TYPE +from esphomeyaml.helpers import App, Component, Pvariable, RawExpression, TemplateArguments, add, \ + esphomelib_ns, setup_component + +GlobalVariableComponent = esphomelib_ns.class_('GlobalVariableComponent', Component) + +GLOBAL_VAR_SCHEMA = vol.Schema({ + vol.Required(CONF_ID): cv.declare_variable_id(GlobalVariableComponent), + vol.Required(CONF_TYPE): cv.string_strict, + vol.Optional(CONF_INITIAL_VALUE): cv.string_strict, + vol.Optional(CONF_RESTORE_VALUE): cv.boolean, +}).extend(cv.COMPONENT_SCHEMA.schema) + +CONFIG_SCHEMA = vol.All(cv.ensure_list, [GLOBAL_VAR_SCHEMA]) + + +def to_code(config): + for conf in config: + type_ = RawExpression(conf[CONF_TYPE]) + template_args = TemplateArguments(type_) + res_type = GlobalVariableComponent.template(template_args) + initial_value = None + if CONF_INITIAL_VALUE in conf: + initial_value = RawExpression(conf[CONF_INITIAL_VALUE]) + rhs = App.Pmake_global_variable(template_args, initial_value) + glob = Pvariable(conf[CONF_ID], rhs, type=res_type) + + if conf.get(CONF_RESTORE_VALUE, False): + hash_ = hash(conf[CONF_ID].id) % 2**32 + add(glob.set_restore_value(hash_)) + + setup_component(glob, conf) diff --git a/esphomeyaml/components/i2c.py b/esphomeyaml/components/i2c.py index 647530b25b..133b3031c6 100644 --- a/esphomeyaml/components/i2c.py +++ b/esphomeyaml/components/i2c.py @@ -4,9 +4,10 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.const import CONF_FREQUENCY, CONF_SCL, CONF_SDA, CONF_SCAN, CONF_ID, \ CONF_RECEIVE_TIMEOUT -from esphomeyaml.helpers import App, add, Pvariable, esphomelib_ns +from esphomeyaml.helpers import App, add, Pvariable, esphomelib_ns, setup_component, Component -I2CComponent = esphomelib_ns.I2CComponent +I2CComponent = esphomelib_ns.class_('I2CComponent', Component) +I2CDevice = pins.I2CDevice CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(I2CComponent), @@ -18,7 +19,7 @@ CONFIG_SCHEMA = vol.Schema({ vol.Optional(CONF_RECEIVE_TIMEOUT): cv.invalid("The receive_timeout option has been removed " "because timeouts are already handled by the " "low-level i2c interface.") -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -27,6 +28,8 @@ def to_code(config): if CONF_FREQUENCY in config: add(i2c.set_frequency(config[CONF_FREQUENCY])) + setup_component(i2c, config) + BUILD_FLAGS = '-DUSE_I2C' diff --git a/esphomeyaml/components/image.py b/esphomeyaml/components/image.py index acb99bbb19..ac0c95be2d 100644 --- a/esphomeyaml/components/image.py +++ b/esphomeyaml/components/image.py @@ -15,7 +15,7 @@ _LOGGER = logging.getLogger(__name__) DEPENDENCIES = ['display'] -Image_ = display.display_ns.Image +Image_ = display.display_ns.class_('Image') CONF_RAW_DATA_ID = 'raw_data_id' diff --git a/esphomeyaml/components/ir_transmitter.py b/esphomeyaml/components/ir_transmitter.py deleted file mode 100644 index 6e50f78870..0000000000 --- a/esphomeyaml/components/ir_transmitter.py +++ /dev/null @@ -1,6 +0,0 @@ -import voluptuous as vol - - -def CONFIG_SCHEMA(config): - raise vol.Invalid("The ir_transmitter component has been renamed to " - "remote_transmitter because of 433MHz signal support.") diff --git a/esphomeyaml/components/light/__init__.py b/esphomeyaml/components/light/__init__.py index c75370d239..c74376b2fb 100644 --- a/esphomeyaml/components/light/__init__.py +++ b/esphomeyaml/components/light/__init__.py @@ -11,36 +11,48 @@ from esphomeyaml.const import CONF_ALPHA, CONF_BLUE, CONF_BRIGHTNESS, CONF_COLOR CONF_EFFECT from esphomeyaml.helpers import Application, ArrayInitializer, Pvariable, RawExpression, \ StructInitializer, add, add_job, esphomelib_ns, process_lambda, setup_mqtt_component, \ - get_variable, TemplateArguments, templatable, uint32, float_, std_string + get_variable, TemplateArguments, templatable, uint32, float_, std_string, Nameable, Component, \ + Action PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) +# Base light_ns = esphomelib_ns.namespace('light') -LightState = light_ns.LightState -LightColorValues = light_ns.LightColorValues -MQTTJSONLightComponent = light_ns.MQTTJSONLightComponent -ToggleAction = light_ns.ToggleAction -TurnOffAction = light_ns.TurnOffAction -TurnOnAction = light_ns.TurnOnAction -MakeLight = Application.MakeLight -RandomLightEffect = light_ns.RandomLightEffect -LambdaLightEffect = light_ns.LambdaLightEffect -StrobeLightEffect = light_ns.StrobeLightEffect -StrobeLightEffectColor = light_ns.StrobeLightEffectColor -FlickerLightEffect = light_ns.FlickerLightEffect -FastLEDLambdaLightEffect = light_ns.FastLEDLambdaLightEffect -FastLEDRainbowLightEffect = light_ns.FastLEDRainbowLightEffect -FastLEDColorWipeEffect = light_ns.FastLEDColorWipeEffect -FastLEDColorWipeEffectColor = light_ns.FastLEDColorWipeEffectColor -FastLEDScanEffect = light_ns.FastLEDScanEffect -FastLEDScanEffectColor = light_ns.FastLEDScanEffectColor -FastLEDTwinkleEffect = light_ns.FastLEDTwinkleEffect -FastLEDRandomTwinkleEffect = light_ns.FastLEDRandomTwinkleEffect -FastLEDFireworksEffect = light_ns.FastLEDFireworksEffect -FastLEDFlickerEffect = light_ns.FastLEDFlickerEffect -FastLEDLightOutputComponent = light_ns.FastLEDLightOutputComponent +LightState = light_ns.class_('LightState', Nameable, Component) +MakeLight = Application.struct('MakeLight') +LightOutput = light_ns.class_('LightOutput') +FastLEDLightOutputComponent = light_ns.class_('FastLEDLightOutputComponent', LightOutput) + +# Actions +ToggleAction = light_ns.class_('ToggleAction', Action) +TurnOffAction = light_ns.class_('TurnOffAction', Action) +TurnOnAction = light_ns.class_('TurnOnAction', Action) + +LightColorValues = light_ns.class_('LightColorValues') + + +MQTTJSONLightComponent = light_ns.class_('MQTTJSONLightComponent', mqtt.MQTTComponent) + +# Effects +LightEffect = light_ns.class_('LightEffect') +RandomLightEffect = light_ns.class_('RandomLightEffect', LightEffect) +LambdaLightEffect = light_ns.class_('LambdaLightEffect', LightEffect) +StrobeLightEffect = light_ns.class_('StrobeLightEffect', LightEffect) +StrobeLightEffectColor = light_ns.class_('StrobeLightEffectColor', LightEffect) +FlickerLightEffect = light_ns.class_('FlickerLightEffect', LightEffect) +BaseFastLEDLightEffect = light_ns.class_('BaseFastLEDLightEffect', LightEffect) +FastLEDLambdaLightEffect = light_ns.class_('FastLEDLambdaLightEffect', BaseFastLEDLightEffect) +FastLEDRainbowLightEffect = light_ns.class_('FastLEDRainbowLightEffect', BaseFastLEDLightEffect) +FastLEDColorWipeEffect = light_ns.class_('FastLEDColorWipeEffect', BaseFastLEDLightEffect) +FastLEDColorWipeEffectColor = light_ns.class_('FastLEDColorWipeEffectColor', BaseFastLEDLightEffect) +FastLEDScanEffect = light_ns.class_('FastLEDScanEffect', BaseFastLEDLightEffect) +FastLEDScanEffectColor = light_ns.class_('FastLEDScanEffectColor', BaseFastLEDLightEffect) +FastLEDTwinkleEffect = light_ns.class_('FastLEDTwinkleEffect', BaseFastLEDLightEffect) +FastLEDRandomTwinkleEffect = light_ns.class_('FastLEDRandomTwinkleEffect', BaseFastLEDLightEffect) +FastLEDFireworksEffect = light_ns.class_('FastLEDFireworksEffect', BaseFastLEDLightEffect) +FastLEDFlickerEffect = light_ns.class_('FastLEDFlickerEffect', BaseFastLEDLightEffect) CONF_STROBE = 'strobe' CONF_FLICKER = 'flicker' @@ -345,7 +357,7 @@ BUILD_FLAGS = '-DUSE_LIGHT' CONF_LIGHT_TOGGLE = 'light.toggle' LIGHT_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(LightState), vol.Optional(CONF_TRANSITION_LENGTH): cv.templatable(cv.positive_time_period_milliseconds), }) @@ -367,7 +379,7 @@ def light_toggle_to_code(config, action_id, arg_type): CONF_LIGHT_TURN_OFF = 'light.turn_off' LIGHT_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(LightState), vol.Optional(CONF_TRANSITION_LENGTH): cv.templatable(cv.positive_time_period_milliseconds), }) @@ -389,7 +401,7 @@ def light_turn_off_to_code(config, action_id, arg_type): CONF_LIGHT_TURN_ON = 'light.turn_on' LIGHT_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(LightState), vol.Exclusive(CONF_TRANSITION_LENGTH, 'transformer'): cv.templatable(cv.positive_time_period_milliseconds), vol.Exclusive(CONF_FLASH_LENGTH, 'transformer'): diff --git a/esphomeyaml/components/light/binary.py b/esphomeyaml/components/light/binary.py index 5b2a43e0b9..55beec807a 100644 --- a/esphomeyaml/components/light/binary.py +++ b/esphomeyaml/components/light/binary.py @@ -1,24 +1,24 @@ import voluptuous as vol +from esphomeyaml.components import light, output import esphomeyaml.config_validation as cv -from esphomeyaml.components import light -from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT, CONF_EFFECTS -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.const import CONF_EFFECTS, CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT +from esphomeyaml.helpers import App, get_variable, setup_component, variable PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_OUTPUT): cv.use_variable_id(None), + vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput), vol.Optional(CONF_EFFECTS): light.validate_effects(light.BINARY_EFFECTS), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - output = None - for output in get_variable(config[CONF_OUTPUT]): + for output_ in get_variable(config[CONF_OUTPUT]): yield - rhs = App.make_binary_light(config[CONF_NAME], 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.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/light/cwww.py b/esphomeyaml/components/light/cwww.py index ded1f44767..d21139b5a0 100644 --- a/esphomeyaml/components/light/cwww.py +++ b/esphomeyaml/components/light/cwww.py @@ -1,25 +1,25 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import light +from esphomeyaml.components import light, output from esphomeyaml.components.light.rgbww import validate_cold_white_colder, \ validate_color_temperature from esphomeyaml.const import CONF_COLD_WHITE, CONF_COLD_WHITE_COLOR_TEMPERATURE, \ CONF_DEFAULT_TRANSITION_LENGTH, CONF_EFFECTS, CONF_GAMMA_CORRECT, CONF_MAKE_ID, \ CONF_NAME, CONF_WARM_WHITE, CONF_WARM_WHITE_COLOR_TEMPERATURE -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.helpers import App, get_variable, variable, setup_component PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_COLD_WHITE): cv.use_variable_id(None), - vol.Required(CONF_WARM_WHITE): cv.use_variable_id(None), + vol.Required(CONF_COLD_WHITE): cv.use_variable_id(output.FloatOutput), + vol.Required(CONF_WARM_WHITE): cv.use_variable_id(output.FloatOutput), vol.Required(CONF_COLD_WHITE_COLOR_TEMPERATURE): validate_color_temperature, vol.Required(CONF_WARM_WHITE_COLOR_TEMPERATURE): validate_color_temperature, vol.Optional(CONF_GAMMA_CORRECT): cv.positive_float, vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_EFFECTS): light.validate_effects(light.MONOCHROMATIC_EFFECTS), -}), validate_cold_white_colder) +}).extend(cv.COMPONENT_SCHEMA.schema), validate_cold_white_colder) def to_code(config): @@ -32,6 +32,7 @@ def to_code(config): cold_white, warm_white) light_struct = variable(config[CONF_MAKE_ID], rhs) light.setup_light(light_struct.Pstate, light_struct.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/light/fastled_clockless.py b/esphomeyaml/components/light/fastled_clockless.py index 4965820802..c779f4ceed 100644 --- a/esphomeyaml/components/light/fastled_clockless.py +++ b/esphomeyaml/components/light/fastled_clockless.py @@ -8,7 +8,7 @@ from esphomeyaml.const import CONF_CHIPSET, CONF_DEFAULT_TRANSITION_LENGTH, CONF CONF_MAKE_ID, CONF_MAX_REFRESH_RATE, CONF_NAME, CONF_NUM_LEDS, CONF_PIN, CONF_POWER_SUPPLY, \ CONF_RGB_ORDER, CONF_EFFECTS, CONF_COLOR_CORRECT from esphomeyaml.helpers import App, Application, RawExpression, TemplateArguments, add, \ - get_variable, variable + get_variable, variable, setup_component TYPES = [ 'NEOPIXEL', @@ -53,7 +53,7 @@ def validate(value): return value -MakeFastLEDLight = Application.MakeFastLEDLight +MakeFastLEDLight = Application.struct('MakeFastLEDLight') PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeFastLEDLight), @@ -70,7 +70,7 @@ PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_POWER_SUPPLY): cv.use_variable_id(PowerSupplyComponent), vol.Optional(CONF_EFFECTS): light.validate_effects(light.FASTLED_EFFECTS), -}), validate) +}).extend(cv.COMPONENT_SCHEMA.schema), validate) def to_code(config): @@ -89,7 +89,6 @@ def to_code(config): add(fast_led.set_max_refresh_rate(config[CONF_MAX_REFRESH_RATE])) if CONF_POWER_SUPPLY in config: - power_supply = None for power_supply in get_variable(config[CONF_POWER_SUPPLY]): yield add(fast_led.set_power_supply(power_supply)) @@ -99,6 +98,7 @@ def to_code(config): add(fast_led.set_correction(r, g, b)) light.setup_light(make.Pstate, make.Pmqtt, config) + setup_component(fast_led, config) BUILD_FLAGS = '-DUSE_FAST_LED_LIGHT' diff --git a/esphomeyaml/components/light/fastled_spi.py b/esphomeyaml/components/light/fastled_spi.py index 89fa933cab..bd4df644e3 100644 --- a/esphomeyaml/components/light/fastled_spi.py +++ b/esphomeyaml/components/light/fastled_spi.py @@ -8,7 +8,7 @@ from esphomeyaml.const import CONF_CHIPSET, CONF_CLOCK_PIN, CONF_DATA_PIN, \ CONF_DEFAULT_TRANSITION_LENGTH, CONF_GAMMA_CORRECT, CONF_MAKE_ID, CONF_MAX_REFRESH_RATE, \ CONF_NAME, CONF_NUM_LEDS, CONF_POWER_SUPPLY, CONF_RGB_ORDER, CONF_EFFECTS, CONF_COLOR_CORRECT from esphomeyaml.helpers import App, Application, RawExpression, TemplateArguments, add, \ - get_variable, variable + get_variable, variable, setup_component CHIPSETS = [ 'LPD8806', @@ -30,7 +30,7 @@ RGB_ORDERS = [ 'BGR', ] -MakeFastLEDLight = Application.MakeFastLEDLight +MakeFastLEDLight = Application.struct('MakeFastLEDLight') PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeFastLEDLight), @@ -48,7 +48,7 @@ PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_POWER_SUPPLY): cv.use_variable_id(PowerSupplyComponent), vol.Optional(CONF_EFFECTS): light.validate_effects(light.FASTLED_EFFECTS), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): @@ -69,7 +69,6 @@ def to_code(config): add(fast_led.set_max_refresh_rate(config[CONF_MAX_REFRESH_RATE])) if CONF_POWER_SUPPLY in config: - power_supply = None for power_supply in get_variable(config[CONF_POWER_SUPPLY]): yield add(fast_led.set_power_supply(power_supply)) @@ -79,6 +78,7 @@ def to_code(config): add(fast_led.set_correction(r, g, b)) light.setup_light(make.Pstate, make.Pmqtt, config) + setup_component(fast_led, config) BUILD_FLAGS = '-DUSE_FAST_LED_LIGHT' diff --git a/esphomeyaml/components/light/monochromatic.py b/esphomeyaml/components/light/monochromatic.py index aac07f144c..bed9f65d37 100644 --- a/esphomeyaml/components/light/monochromatic.py +++ b/esphomeyaml/components/light/monochromatic.py @@ -1,27 +1,27 @@ import voluptuous as vol +from esphomeyaml.components import light, output import esphomeyaml.config_validation as cv -from esphomeyaml.components import light -from esphomeyaml.const import CONF_DEFAULT_TRANSITION_LENGTH, CONF_GAMMA_CORRECT, CONF_MAKE_ID, \ - CONF_NAME, CONF_OUTPUT, CONF_EFFECTS -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.const import CONF_DEFAULT_TRANSITION_LENGTH, CONF_EFFECTS, CONF_GAMMA_CORRECT, \ + CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT +from esphomeyaml.helpers import App, get_variable, setup_component, variable PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_OUTPUT): cv.use_variable_id(None), + vol.Required(CONF_OUTPUT): cv.use_variable_id(output.FloatOutput), vol.Optional(CONF_GAMMA_CORRECT): cv.positive_float, vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_EFFECTS): light.validate_effects(light.MONOCHROMATIC_EFFECTS), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - output = None - for output in get_variable(config[CONF_OUTPUT]): + for output_ in get_variable(config[CONF_OUTPUT]): yield - rhs = App.make_monochromatic_light(config[CONF_NAME], output) + rhs = App.make_monochromatic_light(config[CONF_NAME], output_) light_struct = variable(config[CONF_MAKE_ID], rhs) light.setup_light(light_struct.Pstate, light_struct.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/light/rgb.py b/esphomeyaml/components/light/rgb.py index f73865216c..556ba9b241 100644 --- a/esphomeyaml/components/light/rgb.py +++ b/esphomeyaml/components/light/rgb.py @@ -1,35 +1,33 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import light +from esphomeyaml.components import light, output from esphomeyaml.const import CONF_BLUE, CONF_DEFAULT_TRANSITION_LENGTH, CONF_GAMMA_CORRECT, \ CONF_GREEN, CONF_MAKE_ID, CONF_NAME, CONF_RED, CONF_EFFECTS -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.helpers import App, get_variable, variable, setup_component PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_RED): cv.use_variable_id(None), - vol.Required(CONF_GREEN): cv.use_variable_id(None), - vol.Required(CONF_BLUE): cv.use_variable_id(None), + 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.Optional(CONF_GAMMA_CORRECT): cv.positive_float, vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_EFFECTS): light.validate_effects(light.RGB_EFFECTS), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - red = None for red in get_variable(config[CONF_RED]): yield - green = None for green in get_variable(config[CONF_GREEN]): yield - blue = None for blue in get_variable(config[CONF_BLUE]): yield rhs = App.make_rgb_light(config[CONF_NAME], red, green, blue) light_struct = variable(config[CONF_MAKE_ID], rhs) light.setup_light(light_struct.Pstate, light_struct.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/light/rgbw.py b/esphomeyaml/components/light/rgbw.py index 4e0577f7fc..07631ca71b 100644 --- a/esphomeyaml/components/light/rgbw.py +++ b/esphomeyaml/components/light/rgbw.py @@ -1,39 +1,36 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import light +from esphomeyaml.components import light, output from esphomeyaml.const import CONF_BLUE, CONF_DEFAULT_TRANSITION_LENGTH, CONF_GAMMA_CORRECT, \ CONF_GREEN, CONF_MAKE_ID, CONF_NAME, CONF_RED, CONF_WHITE, CONF_EFFECTS -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.helpers import App, get_variable, variable, setup_component PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_RED): cv.use_variable_id(None), - vol.Required(CONF_GREEN): cv.use_variable_id(None), - vol.Required(CONF_BLUE): cv.use_variable_id(None), - vol.Required(CONF_WHITE): cv.use_variable_id(None), + 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), vol.Optional(CONF_GAMMA_CORRECT): cv.positive_float, vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_EFFECTS): light.validate_effects(light.RGB_EFFECTS), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - red = None for red in get_variable(config[CONF_RED]): yield - green = None for green in get_variable(config[CONF_GREEN]): yield - blue = None for blue in get_variable(config[CONF_BLUE]): yield - white = None for white in get_variable(config[CONF_WHITE]): yield 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.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/light/rgbww.py b/esphomeyaml/components/light/rgbww.py index 267dfd9d34..fa7a92d6ff 100644 --- a/esphomeyaml/components/light/rgbww.py +++ b/esphomeyaml/components/light/rgbww.py @@ -1,11 +1,11 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import light +from esphomeyaml.components import light, output from esphomeyaml.const import CONF_BLUE, CONF_COLD_WHITE, CONF_COLD_WHITE_COLOR_TEMPERATURE, \ CONF_DEFAULT_TRANSITION_LENGTH, CONF_EFFECTS, CONF_GAMMA_CORRECT, CONF_GREEN, CONF_MAKE_ID, \ CONF_NAME, CONF_RED, CONF_WARM_WHITE, CONF_WARM_WHITE_COLOR_TEMPERATURE -from esphomeyaml.helpers import App, get_variable, variable +from esphomeyaml.helpers import App, get_variable, variable, setup_component def validate_color_temperature(value): @@ -30,18 +30,18 @@ def validate_cold_white_colder(value): PLATFORM_SCHEMA = cv.nameable(light.LIGHT_PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(light.MakeLight), - vol.Required(CONF_RED): cv.use_variable_id(None), - vol.Required(CONF_GREEN): cv.use_variable_id(None), - vol.Required(CONF_BLUE): cv.use_variable_id(None), - vol.Required(CONF_COLD_WHITE): cv.use_variable_id(None), - vol.Required(CONF_WARM_WHITE): cv.use_variable_id(None), + 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_COLD_WHITE): cv.use_variable_id(output.FloatOutput), + vol.Required(CONF_WARM_WHITE): cv.use_variable_id(output.FloatOutput), vol.Required(CONF_COLD_WHITE_COLOR_TEMPERATURE): validate_color_temperature, vol.Required(CONF_WARM_WHITE_COLOR_TEMPERATURE): validate_color_temperature, vol.Optional(CONF_GAMMA_CORRECT): cv.positive_float, vol.Optional(CONF_DEFAULT_TRANSITION_LENGTH): cv.positive_time_period_milliseconds, vol.Optional(CONF_EFFECTS): light.validate_effects(light.RGB_EFFECTS), -}), validate_cold_white_colder) +}).extend(cv.COMPONENT_SCHEMA.schema), validate_cold_white_colder) def to_code(config): @@ -60,6 +60,7 @@ def to_code(config): red, green, blue, cold_white, warm_white) light_struct = variable(config[CONF_MAKE_ID], rhs) light.setup_light(light_struct.Pstate, light_struct.Pmqtt, config) + setup_component(light_struct.Pstate, config) def to_hass_config(data, config): diff --git a/esphomeyaml/components/logger.py b/esphomeyaml/components/logger.py index 996732df4c..839d371d56 100644 --- a/esphomeyaml/components/logger.py +++ b/esphomeyaml/components/logger.py @@ -7,8 +7,8 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ARGS, CONF_BAUD_RATE, CONF_FORMAT, CONF_ID, CONF_LEVEL, \ CONF_LOGS, CONF_TAG, CONF_TX_BUFFER_SIZE from esphomeyaml.core import ESPHomeYAMLError, Lambda -from esphomeyaml.helpers import App, Pvariable, TemplateArguments, add, esphomelib_ns, global_ns, \ - process_lambda, RawExpression, statement +from esphomeyaml.helpers import App, Pvariable, RawExpression, TemplateArguments, add, \ + esphomelib_ns, global_ns, process_lambda, statement, Component LOG_LEVELS = { 'NONE': global_ns.ESPHOMELIB_LOG_LEVEL_NONE, @@ -44,7 +44,7 @@ def validate_local_no_higher_than_global(value): return value -LogComponent = esphomelib_ns.LogComponent +LogComponent = esphomelib_ns.class_('LogComponent', Component) CONFIG_SCHEMA = vol.All(vol.Schema({ cv.GenerateID(): cv.declare_variable_id(LogComponent), @@ -79,6 +79,7 @@ def maybe_simple_message(schema): if isinstance(value, dict): return vol.Schema(schema)(value) return vol.Schema(schema)({CONF_FORMAT: value}) + return validator diff --git a/esphomeyaml/components/mqtt.py b/esphomeyaml/components/mqtt.py index c8f85a870b..646ddc0e73 100644 --- a/esphomeyaml/components/mqtt.py +++ b/esphomeyaml/components/mqtt.py @@ -17,7 +17,7 @@ from esphomeyaml.const import CONF_BIRTH_MESSAGE, CONF_BROKER, CONF_CLIENT_ID, C from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, RawExpression, \ StructInitializer, TemplateArguments, add, esphomelib_ns, optional, std_string, templatable, \ - uint8, bool_, JsonObjectRef, process_lambda, JsonObjectConstRef + uint8, bool_, JsonObjectRef, process_lambda, JsonObjectConstRef, Component, Action, Trigger def validate_message_just_topic(value): @@ -38,12 +38,14 @@ MQTT_MESSAGE_SCHEMA = vol.Any(None, MQTT_MESSAGE_BASE.extend({ })) mqtt_ns = esphomelib_ns.namespace('mqtt') -MQTTMessage = mqtt_ns.MQTTMessage -MQTTClientComponent = mqtt_ns.MQTTClientComponent -MQTTPublishAction = mqtt_ns.MQTTPublishAction -MQTTPublishJsonAction = mqtt_ns.MQTTPublishJsonAction -MQTTMessageTrigger = mqtt_ns.MQTTMessageTrigger -MQTTJsonMessageTrigger = mqtt_ns.MQTTJsonMessageTrigger +MQTTMessage = mqtt_ns.struct('MQTTMessage') +MQTTClientComponent = mqtt_ns.class_('MQTTClientComponent', Component) +MQTTPublishAction = mqtt_ns.class_('MQTTPublishAction', Action) +MQTTPublishJsonAction = mqtt_ns.class_('MQTTPublishJsonAction', Action) +MQTTMessageTrigger = mqtt_ns.class_('MQTTMessageTrigger', Trigger.template(std_string)) +MQTTJsonMessageTrigger = mqtt_ns.class_('MQTTJsonMessageTrigger', + Trigger.template(JsonObjectConstRef)) +MQTTComponent = mqtt_ns.class_('MQTTComponent', Component) def validate_broker(value): diff --git a/esphomeyaml/components/ota.py b/esphomeyaml/components/ota.py index e7052d60e0..83b224cbed 100644 --- a/esphomeyaml/components/ota.py +++ b/esphomeyaml/components/ota.py @@ -7,16 +7,15 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ID, CONF_OTA, CONF_PASSWORD, CONF_PORT, CONF_SAFE_MODE, \ ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266 from esphomeyaml.core import ESPHomeYAMLError -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns +from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, Component _LOGGER = logging.getLogger(__name__) -OTAComponent = esphomelib_ns.OTAComponent +OTAComponent = esphomelib_ns.class_('OTAComponent', Component) CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(OTAComponent), vol.Optional(CONF_SAFE_MODE, default=True): cv.boolean, - # TODO Num attempts + wait time vol.Optional(CONF_PORT): cv.port, vol.Optional(CONF_PASSWORD): cv.string, }) diff --git a/esphomeyaml/components/output/__init__.py b/esphomeyaml/components/output/__init__.py index d4f6d27836..6456bd0991 100644 --- a/esphomeyaml/components/output/__init__.py +++ b/esphomeyaml/components/output/__init__.py @@ -5,7 +5,7 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components.power_supply import PowerSupplyComponent from esphomeyaml.const import CONF_INVERTED, CONF_MAX_POWER, CONF_POWER_SUPPLY, CONF_ID, CONF_LEVEL from esphomeyaml.helpers import add, esphomelib_ns, get_variable, TemplateArguments, Pvariable, \ - templatable, float_, add_job + templatable, float_, add_job, Action PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -25,9 +25,13 @@ FLOAT_OUTPUT_SCHEMA = BINARY_OUTPUT_SCHEMA.extend({ FLOAT_OUTPUT_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(FLOAT_OUTPUT_SCHEMA.schema) output_ns = esphomelib_ns.namespace('output') -TurnOffAction = output_ns.TurnOffAction -TurnOnAction = output_ns.TurnOnAction -SetLevelAction = output_ns.SetLevelAction +BinaryOutput = output_ns.class_('BinaryOutput') +FloatOutput = output_ns.class_('FloatOutput', BinaryOutput) + +# Actions +TurnOffAction = output_ns.class_('TurnOffAction', Action) +TurnOnAction = output_ns.class_('TurnOnAction', Action) +SetLevelAction = output_ns.class_('SetLevelAction', Action) def setup_output_platform_(obj, config, skip_power_supply=False): @@ -51,7 +55,7 @@ BUILD_FLAGS = '-DUSE_OUTPUT' CONF_OUTPUT_TURN_ON = 'output.turn_on' OUTPUT_TURN_ON_ACTION = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(BinaryOutput), }) @@ -67,7 +71,7 @@ def output_turn_on_to_code(config, action_id, arg_type): CONF_OUTPUT_TURN_OFF = 'output.turn_off' OUTPUT_TURN_OFF_ACTION = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None) + vol.Required(CONF_ID): cv.use_variable_id(BinaryOutput) }) @@ -83,7 +87,7 @@ def output_turn_off_to_code(config, action_id, arg_type): CONF_OUTPUT_SET_LEVEL = 'output.set_level' OUTPUT_SET_LEVEL_ACTION = vol.Schema({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(FloatOutput), vol.Required(CONF_LEVEL): cv.templatable(cv.percentage), }) diff --git a/esphomeyaml/components/output/esp8266_pwm.py b/esphomeyaml/components/output/esp8266_pwm.py index 84eae94241..a5ad50be33 100644 --- a/esphomeyaml/components/output/esp8266_pwm.py +++ b/esphomeyaml/components/output/esp8266_pwm.py @@ -5,7 +5,8 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components import output from esphomeyaml.const import CONF_ID, CONF_NUMBER, CONF_PIN, ESP_PLATFORM_ESP8266 from esphomeyaml.core import ESPHomeYAMLError -from esphomeyaml.helpers import App, Pvariable, gpio_output_pin_expression +from esphomeyaml.helpers import App, Pvariable, gpio_output_pin_expression, setup_component, \ + Component ESP_PLATFORMS = [ESP_PLATFORM_ESP8266] @@ -16,21 +17,21 @@ def valid_pwm_pin(value): return value -ESP8266PWMOutput = output.output_ns.ESP8266PWMOutput +ESP8266PWMOutput = output.output_ns.class_('ESP8266PWMOutput', output.FloatOutput, Component) PLATFORM_SCHEMA = output.FLOAT_OUTPUT_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ID): cv.declare_variable_id(ESP8266PWMOutput), vol.Required(CONF_PIN): vol.All(pins.internal_gpio_output_pin_schema, valid_pwm_pin), -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - pin = None for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_esp8266_pwm_output(pin) gpio = Pvariable(config[CONF_ID], rhs) output.setup_output_platform(gpio, config) + setup_component(gpio, config) BUILD_FLAGS = '-DUSE_ESP8266_PWM_OUTPUT' diff --git a/esphomeyaml/components/output/gpio.py b/esphomeyaml/components/output/gpio.py index 3f471e74b5..51e3ce27ef 100644 --- a/esphomeyaml/components/output/gpio.py +++ b/esphomeyaml/components/output/gpio.py @@ -4,23 +4,25 @@ from esphomeyaml import pins import esphomeyaml.config_validation as cv from esphomeyaml.components import output from esphomeyaml.const import CONF_ID, CONF_PIN -from esphomeyaml.helpers import App, Pvariable, gpio_output_pin_expression +from esphomeyaml.helpers import App, Pvariable, gpio_output_pin_expression, setup_component, \ + Component -GPIOBinaryOutputComponent = output.output_ns.GPIOBinaryOutputComponent +GPIOBinaryOutputComponent = output.output_ns.class_('GPIOBinaryOutputComponent', + output.BinaryOutput, Component) PLATFORM_SCHEMA = output.BINARY_OUTPUT_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ID): cv.declare_variable_id(GPIOBinaryOutputComponent), vol.Required(CONF_PIN): pins.gpio_output_pin_schema, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - pin = None for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_gpio_output(pin) gpio = Pvariable(config[CONF_ID], rhs) output.setup_output_platform(gpio, config) + setup_component(gpio, config) BUILD_FLAGS = '-DUSE_GPIO_OUTPUT' diff --git a/esphomeyaml/components/output/ledc.py b/esphomeyaml/components/output/ledc.py index 5db2a207c2..987d44a5be 100644 --- a/esphomeyaml/components/output/ledc.py +++ b/esphomeyaml/components/output/ledc.py @@ -5,7 +5,7 @@ from esphomeyaml import pins from esphomeyaml.components import output from esphomeyaml.const import APB_CLOCK_FREQ, CONF_BIT_DEPTH, CONF_CHANNEL, CONF_FREQUENCY, \ CONF_ID, CONF_PIN, ESP_PLATFORM_ESP32 -from esphomeyaml.helpers import App, Pvariable, add +from esphomeyaml.helpers import App, Pvariable, add, setup_component, Component ESP_PLATFORMS = [ESP_PLATFORM_ESP32] @@ -19,7 +19,7 @@ def validate_frequency_bit_depth(obj): return obj -LEDCOutputComponent = output.output_ns.LEDCOutputComponent +LEDCOutputComponent = output.output_ns.class_('LEDCOutputComponent', output.FloatOutput, Component) PLATFORM_SCHEMA = vol.All(output.FLOAT_OUTPUT_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ID): cv.declare_variable_id(LEDCOutputComponent), @@ -27,7 +27,7 @@ PLATFORM_SCHEMA = vol.All(output.FLOAT_OUTPUT_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_FREQUENCY): cv.frequency, vol.Optional(CONF_BIT_DEPTH): vol.All(vol.Coerce(int), vol.Range(min=1, max=15)), vol.Optional(CONF_CHANNEL): vol.All(vol.Coerce(int), vol.Range(min=0, max=15)) -}), validate_frequency_bit_depth) +}).extend(cv.COMPONENT_SCHEMA.schema), validate_frequency_bit_depth) def to_code(config): @@ -39,6 +39,7 @@ def to_code(config): if CONF_CHANNEL in config: add(ledc.set_channel(config[CONF_CHANNEL])) output.setup_output_platform(ledc, config) + setup_component(ledc, config) BUILD_FLAGS = '-DUSE_LEDC_OUTPUT' diff --git a/esphomeyaml/components/output/pca9685.py b/esphomeyaml/components/output/pca9685.py index 55c57377b9..ba2f57ce6d 100644 --- a/esphomeyaml/components/output/pca9685.py +++ b/esphomeyaml/components/output/pca9685.py @@ -8,7 +8,7 @@ from esphomeyaml.helpers import Pvariable, get_variable DEPENDENCIES = ['pca9685'] -Channel = PCA9685OutputComponent.Channel +Channel = PCA9685OutputComponent.class_('Channel', output.FloatOutput) PLATFORM_SCHEMA = output.FLOAT_OUTPUT_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ID): cv.declare_variable_id(Channel), @@ -23,7 +23,6 @@ def to_code(config): if CONF_POWER_SUPPLY in config: for power_supply in get_variable(config[CONF_POWER_SUPPLY]): yield - pca9685 = None for pca9685 in get_variable(config[CONF_PCA9685_ID]): yield rhs = pca9685.create_channel(config[CONF_CHANNEL], power_supply) diff --git a/esphomeyaml/components/pca9685.py b/esphomeyaml/components/pca9685.py index b2a3f2b096..ac3094abaf 100644 --- a/esphomeyaml/components/pca9685.py +++ b/esphomeyaml/components/pca9685.py @@ -1,13 +1,14 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import output +from esphomeyaml.components import output, i2c from esphomeyaml.const import CONF_ADDRESS, CONF_FREQUENCY, CONF_ID, CONF_PHASE_BALANCER -from esphomeyaml.helpers import App, HexIntLiteral, Pvariable, add +from esphomeyaml.helpers import App, HexIntLiteral, Pvariable, add, setup_component, Component DEPENDENCIES = ['i2c'] -PCA9685OutputComponent = output.output_ns.namespace('PCA9685OutputComponent') +PCA9685OutputComponent = output.output_ns.class_('PCA9685OutputComponent', + Component, i2c.I2CDevice) PHASE_BALANCER_MESSAGE = ("The phase_balancer option has been removed in version 1.5.0. " "esphomelib will now automatically choose a suitable phase balancer.") @@ -19,7 +20,7 @@ PCA9685_SCHEMA = vol.Schema({ vol.Optional(CONF_ADDRESS): cv.i2c_address, vol.Optional(CONF_PHASE_BALANCER): cv.invalid(PHASE_BALANCER_MESSAGE), -}) +}).extend(cv.COMPONENT_SCHEMA.schema) CONFIG_SCHEMA = vol.All(cv.ensure_list, [PCA9685_SCHEMA]) @@ -30,6 +31,7 @@ def to_code(config): pca9685 = Pvariable(conf[CONF_ID], rhs) if CONF_ADDRESS in conf: add(pca9685.set_address(HexIntLiteral(conf[CONF_ADDRESS]))) + setup_component(pca9685, conf) BUILD_FLAGS = '-DUSE_PCA9685_OUTPUT' diff --git a/esphomeyaml/components/pcf8574.py b/esphomeyaml/components/pcf8574.py index 9a1d40f843..e2f92d763f 100644 --- a/esphomeyaml/components/pcf8574.py +++ b/esphomeyaml/components/pcf8574.py @@ -1,19 +1,27 @@ import voluptuous as vol +from esphomeyaml import pins import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_PCF8575 -from esphomeyaml.helpers import App, Pvariable, esphomelib_ns +from esphomeyaml.helpers import App, GPIOInputPin, GPIOOutputPin, Pvariable, io_ns, setup_component DEPENDENCIES = ['i2c'] -io_ns = esphomelib_ns.namespace('io') -PCF8574Component = io_ns.PCF8574Component +PCF8574GPIOMode = io_ns.enum('PCF8574GPIOMode') +PCF8675_GPIO_MODES = { + 'INPUT': PCF8574GPIOMode.PCF8574_INPUT, + 'INPUT_PULLUP': PCF8574GPIOMode.PCF8574_INPUT_PULLUP, + 'OUTPUT': PCF8574GPIOMode.PCF8574_OUTPUT, +} + +PCF8574GPIOInputPin = io_ns.class_('PCF8574GPIOInputPin', GPIOInputPin) +PCF8574GPIOOutputPin = io_ns.class_('PCF8574GPIOOutputPin', GPIOOutputPin) PCF8574_SCHEMA = vol.Schema({ - vol.Required(CONF_ID): cv.declare_variable_id(PCF8574Component), + vol.Required(CONF_ID): cv.declare_variable_id(pins.PCF8574Component), vol.Optional(CONF_ADDRESS, default=0x21): cv.i2c_address, vol.Optional(CONF_PCF8575, default=False): cv.boolean, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) CONFIG_SCHEMA = vol.All(cv.ensure_list, [PCF8574_SCHEMA]) @@ -21,7 +29,8 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [PCF8574_SCHEMA]) def to_code(config): for conf in config: rhs = App.make_pcf8574_component(conf[CONF_ADDRESS], conf[CONF_PCF8575]) - Pvariable(conf[CONF_ID], rhs) + var = Pvariable(conf[CONF_ID], rhs) + setup_component(var, conf) BUILD_FLAGS = '-DUSE_PCF8574' diff --git a/esphomeyaml/components/pn532.py b/esphomeyaml/components/pn532.py index 6dda13b0c4..a33ac791bd 100644 --- a/esphomeyaml/components/pn532.py +++ b/esphomeyaml/components/pn532.py @@ -2,16 +2,18 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins, automation -from esphomeyaml.components import binary_sensor +from esphomeyaml.components import binary_sensor, spi from esphomeyaml.components.spi import SPIComponent from esphomeyaml.const import CONF_CS_PIN, CONF_ID, CONF_SPI_ID, CONF_UPDATE_INTERVAL, \ CONF_ON_TAG, CONF_TRIGGER_ID -from esphomeyaml.helpers import App, Pvariable, get_variable, gpio_output_pin_expression, std_string +from esphomeyaml.helpers import App, Pvariable, get_variable, gpio_output_pin_expression, \ + std_string, setup_component, PollingComponent, Trigger DEPENDENCIES = ['spi'] -PN532Component = binary_sensor.binary_sensor_ns.PN532Component -PN532Trigger = binary_sensor.binary_sensor_ns.PN532Trigger +PN532Component = binary_sensor.binary_sensor_ns.class_('PN532Component', PollingComponent, + spi.SPIDevice) +PN532Trigger = binary_sensor.binary_sensor_ns.class_('PN532Trigger', Trigger.template(std_string)) CONFIG_SCHEMA = vol.All(cv.ensure_list, [vol.Schema({ cv.GenerateID(): cv.declare_variable_id(PN532Component), @@ -21,23 +23,23 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [vol.Schema({ 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 conf in config: - spi = None - for spi in get_variable(conf[CONF_SPI_ID]): + for spi_ in get_variable(conf[CONF_SPI_ID]): yield - cs = None for cs in gpio_output_pin_expression(conf[CONF_CS_PIN]): yield - rhs = App.make_pn532_component(spi, cs, conf.get(CONF_UPDATE_INTERVAL)) + rhs = App.make_pn532_component(spi_, cs, conf.get(CONF_UPDATE_INTERVAL)) pn532 = Pvariable(conf[CONF_ID], rhs) for conf_ in conf.get(CONF_ON_TAG, []): trigger = Pvariable(conf_[CONF_TRIGGER_ID], pn532.make_trigger()) automation.build_automation(trigger, std_string, conf_) + setup_component(pn532, conf) + BUILD_FLAGS = '-DUSE_PN532' diff --git a/esphomeyaml/components/power_supply.py b/esphomeyaml/components/power_supply.py index 209b729be7..97efda4f1e 100644 --- a/esphomeyaml/components/power_supply.py +++ b/esphomeyaml/components/power_supply.py @@ -3,25 +3,26 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.const import CONF_ENABLE_TIME, CONF_ID, CONF_KEEP_ON_TIME, CONF_PIN -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, gpio_output_pin_expression +from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, gpio_output_pin_expression, \ + setup_component, Component -PowerSupplyComponent = esphomelib_ns.PowerSupplyComponent +PowerSupplyComponent = esphomelib_ns.class_('PowerSupplyComponent', Component) POWER_SUPPLY_SCHEMA = vol.Schema({ vol.Required(CONF_ID): cv.declare_variable_id(PowerSupplyComponent), vol.Required(CONF_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_ENABLE_TIME): cv.positive_time_period_milliseconds, vol.Optional(CONF_KEEP_ON_TIME): cv.positive_time_period_milliseconds, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) CONFIG_SCHEMA = vol.All(cv.ensure_list, [POWER_SUPPLY_SCHEMA]) def to_code(config): for conf in config: - pin = None for pin in gpio_output_pin_expression(conf[CONF_PIN]): yield + rhs = App.make_power_supply(pin) psu = Pvariable(conf[CONF_ID], rhs) if CONF_ENABLE_TIME in conf: @@ -29,5 +30,7 @@ def to_code(config): if CONF_KEEP_ON_TIME in conf: add(psu.set_keep_on_time(conf[CONF_KEEP_ON_TIME])) + setup_component(psu, conf) + BUILD_FLAGS = '-DUSE_OUTPUT' diff --git a/esphomeyaml/components/rdm6300.py b/esphomeyaml/components/rdm6300.py index 7cc05e4f2c..0ede8e290f 100644 --- a/esphomeyaml/components/rdm6300.py +++ b/esphomeyaml/components/rdm6300.py @@ -1,28 +1,28 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import binary_sensor -from esphomeyaml.components.uart import UARTComponent +from esphomeyaml.components import binary_sensor, uart from esphomeyaml.const import CONF_ID, CONF_UART_ID -from esphomeyaml.helpers import App, Pvariable, get_variable +from esphomeyaml.helpers import App, Pvariable, get_variable, setup_component, Component DEPENDENCIES = ['uart'] -RDM6300Component = binary_sensor.binary_sensor_ns.RDM6300Component +RDM6300Component = binary_sensor.binary_sensor_ns.class_('RDM6300Component', Component, + uart.UARTDevice) CONFIG_SCHEMA = vol.All(cv.ensure_list_not_empty, [vol.Schema({ cv.GenerateID(): cv.declare_variable_id(RDM6300Component), - cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent), -})]) + cv.GenerateID(CONF_UART_ID): cv.use_variable_id(uart.UARTComponent), +}).extend(cv.COMPONENT_SCHEMA.schema)]) def to_code(config): for conf in config: - uart = None - for uart in get_variable(conf[CONF_UART_ID]): + for uart_ in get_variable(conf[CONF_UART_ID]): yield - rhs = App.make_rdm6300_component(uart) - Pvariable(conf[CONF_ID], rhs) + rhs = App.make_rdm6300_component(uart_) + var = Pvariable(conf[CONF_ID], rhs) + setup_component(var, conf) BUILD_FLAGS = '-DUSE_RDM6300' diff --git a/esphomeyaml/components/remote_receiver.py b/esphomeyaml/components/remote_receiver.py index 8cb6be5a93..878e5a6fc5 100644 --- a/esphomeyaml/components/remote_receiver.py +++ b/esphomeyaml/components/remote_receiver.py @@ -4,20 +4,26 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.const import CONF_BUFFER_SIZE, CONF_DUMP, CONF_FILTER, CONF_ID, CONF_IDLE, \ CONF_PIN, CONF_TOLERANCE -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, gpio_input_pin_expression +from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, gpio_input_pin_expression, \ + setup_component, Component remote_ns = esphomelib_ns.namespace('remote') -RemoteReceiverComponent = remote_ns.RemoteReceiverComponent +RemoteControlComponentBase = remote_ns.class_('RemoteControlComponentBase') +RemoteReceiverComponent = remote_ns.class_('RemoteReceiverComponent', + RemoteControlComponentBase, + Component) + +RemoteReceiveDumper = remote_ns.class_('RemoteReceiveDumper') DUMPERS = { - 'lg': remote_ns.LGDumper, - 'nec': remote_ns.NECDumper, - 'panasonic': remote_ns.PanasonicDumper, - 'raw': remote_ns.RawDumper, - 'samsung': remote_ns.SamsungDumper, - 'sony': remote_ns.SonyDumper, - 'rc_switch': remote_ns.RCSwitchDumper, + 'lg': remote_ns.class_('LGDumper', RemoteReceiveDumper), + 'nec': remote_ns.class_('NECDumper', RemoteReceiveDumper), + 'panasonic': remote_ns.class_('PanasonicDumper', RemoteReceiveDumper), + 'raw': remote_ns.class_('RawDumper', RemoteReceiveDumper), + 'samsung': remote_ns.class_('SamsungDumper', RemoteReceiveDumper), + 'sony': remote_ns.class_('SonyDumper', RemoteReceiveDumper), + 'rc_switch': remote_ns.class_('RCSwitchDumper', RemoteReceiveDumper), } @@ -39,16 +45,16 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [vol.Schema({ vol.Optional(CONF_BUFFER_SIZE): cv.validate_bytes, vol.Optional(CONF_FILTER): cv.positive_time_period_microseconds, vol.Optional(CONF_IDLE): cv.positive_time_period_microseconds, -})]) +}).extend(cv.COMPONENT_SCHEMA.schema)]) def to_code(config): for conf in config: - pin = None for pin in gpio_input_pin_expression(conf[CONF_PIN]): yield rhs = App.make_remote_receiver_component(pin) receiver = Pvariable(conf[CONF_ID], rhs) + for dumper in conf[CONF_DUMP]: add(receiver.add_dumper(DUMPERS[dumper].new())) if CONF_TOLERANCE in conf: @@ -60,5 +66,7 @@ def to_code(config): if CONF_IDLE in conf: add(receiver.set_idle_us(conf[CONF_IDLE])) + setup_component(receiver, conf) + BUILD_FLAGS = '-DUSE_REMOTE_RECEIVER' diff --git a/esphomeyaml/components/remote_transmitter.py b/esphomeyaml/components/remote_transmitter.py index 7eb8ed3db1..144ac2bfdf 100644 --- a/esphomeyaml/components/remote_transmitter.py +++ b/esphomeyaml/components/remote_transmitter.py @@ -1,17 +1,18 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv from esphomeyaml import pins +from esphomeyaml.components.remote_receiver import RemoteControlComponentBase, remote_ns +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_DUTY_PERCENT, CONF_CHANNEL, CONF_CODE, \ CONF_DEVICE, CONF_FAMILY, CONF_GROUP, CONF_ID, CONF_INVERTED, CONF_ONE, CONF_PIN, \ CONF_PROTOCOL, CONF_PULSE_LENGTH, CONF_STATE, CONF_SYNC, CONF_ZERO from esphomeyaml.core import HexInt -from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, gpio_output_pin_expression +from esphomeyaml.helpers import App, Component, Pvariable, add, gpio_output_pin_expression, \ + setup_component -remote_ns = esphomelib_ns.namespace('remote') - -RemoteTransmitterComponent = remote_ns.RemoteTransmitterComponent -RCSwitchProtocol = remote_ns.RCSwitchProtocol +RemoteTransmitterComponent = remote_ns.class_('RemoteTransmitterComponent', + RemoteControlComponentBase, Component) +RCSwitchProtocol = remote_ns.class_('RCSwitchProtocol') rc_switch_protocols = remote_ns.rc_switch_protocols @@ -81,7 +82,7 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [vol.Schema({ vol.Required(CONF_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_CARRIER_DUTY_PERCENT): vol.All(cv.percentage_int, vol.Range(min=1, max=100)), -})]) +}).extend(cv.COMPONENT_SCHEMA.schema)]) def build_rc_switch_protocol(config): @@ -104,13 +105,15 @@ def binary_code(value): def to_code(config): for conf in config: - pin = None for pin in gpio_output_pin_expression(conf[CONF_PIN]): yield rhs = App.make_remote_transmitter_component(pin) transmitter = Pvariable(conf[CONF_ID], rhs) + if CONF_CARRIER_DUTY_PERCENT in conf: add(transmitter.set_carrier_duty_percent(conf[CONF_CARRIER_DUTY_PERCENT])) + setup_component(transmitter, conf) + BUILD_FLAGS = '-DUSE_REMOTE_TRANSMITTER' diff --git a/esphomeyaml/components/script.py b/esphomeyaml/components/script.py index 6c8c52469e..5aab19c3c8 100644 --- a/esphomeyaml/components/script.py +++ b/esphomeyaml/components/script.py @@ -4,10 +4,11 @@ from esphomeyaml import automation from esphomeyaml.automation import ACTION_REGISTRY, maybe_simple_id import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ID -from esphomeyaml.helpers import NoArg, Pvariable, TemplateArguments, esphomelib_ns, get_variable +from esphomeyaml.helpers import NoArg, Pvariable, TemplateArguments, esphomelib_ns, get_variable, \ + Trigger, Action -Script = esphomelib_ns.Script -ScriptExecuteAction = esphomelib_ns.ScriptExecuteAction +Script = esphomelib_ns.class_('Script', Trigger.template(NoArg)) +ScriptExecuteAction = esphomelib_ns.class_('ScriptExecuteAction', Action) CONFIG_SCHEMA = automation.validate_automation({ vol.Required(CONF_ID): cv.declare_variable_id(Script), diff --git a/esphomeyaml/components/sensor/__init__.py b/esphomeyaml/components/sensor/__init__.py index 6b648b418f..846c08ea81 100644 --- a/esphomeyaml/components/sensor/__init__.py +++ b/esphomeyaml/components/sensor/__init__.py @@ -1,17 +1,18 @@ import voluptuous as vol +from esphomeyaml import automation from esphomeyaml.components import mqtt import esphomeyaml.config_validation as cv -from esphomeyaml import automation from esphomeyaml.const import CONF_ABOVE, CONF_ACCURACY_DECIMALS, CONF_ALPHA, CONF_BELOW, \ CONF_DEBOUNCE, CONF_DELTA, CONF_EXPIRE_AFTER, CONF_EXPONENTIAL_MOVING_AVERAGE, CONF_FILTERS, \ CONF_FILTER_NAN, CONF_FILTER_OUT, CONF_HEARTBEAT, CONF_ICON, CONF_ID, CONF_INTERNAL, \ CONF_LAMBDA, CONF_MQTT_ID, CONF_MULTIPLY, CONF_OFFSET, CONF_ON_RAW_VALUE, CONF_ON_VALUE, \ - CONF_ON_VALUE_RANGE, CONF_OR, CONF_SEND_EVERY, CONF_SLIDING_WINDOW_MOVING_AVERAGE, \ - CONF_THROTTLE, CONF_TRIGGER_ID, CONF_UNIQUE, CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE, \ - CONF_SEND_FIRST_AT -from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, add, add_job, esphomelib_ns, \ - float_, process_lambda, setup_mqtt_component, templatable + CONF_ON_VALUE_RANGE, CONF_OR, CONF_SEND_EVERY, CONF_SEND_FIRST_AT, \ + CONF_SLIDING_WINDOW_MOVING_AVERAGE, CONF_THROTTLE, CONF_TRIGGER_ID, CONF_UNIQUE, \ + CONF_UNIT_OF_MEASUREMENT, CONF_WINDOW_SIZE +from esphomeyaml.helpers import App, ArrayInitializer, Component, Nameable, PollingComponent, \ + Pvariable, Trigger, add, add_job, esphomelib_ns, float_, process_lambda, setup_mqtt_component, \ + templatable PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -58,31 +59,38 @@ FILTERS_SCHEMA = vol.All(cv.ensure_list, [vol.All({ vol.Optional(CONF_OR): validate_recursive_filter, }, cv.has_exactly_one_key(*FILTER_KEYS))]) -# pylint: disable=invalid-name +# Base sensor_ns = esphomelib_ns.namespace('sensor') -Sensor = sensor_ns.Sensor -MQTTSensorComponent = sensor_ns.MQTTSensorComponent -OffsetFilter = sensor_ns.OffsetFilter -MultiplyFilter = sensor_ns.MultiplyFilter -FilterOutValueFilter = sensor_ns.FilterOutValueFilter -FilterOutNANFilter = sensor_ns.FilterOutNANFilter -SlidingWindowMovingAverageFilter = sensor_ns.SlidingWindowMovingAverageFilter -ExponentialMovingAverageFilter = sensor_ns.ExponentialMovingAverageFilter -LambdaFilter = sensor_ns.LambdaFilter -ThrottleFilter = sensor_ns.ThrottleFilter -DeltaFilter = sensor_ns.DeltaFilter -OrFilter = sensor_ns.OrFilter -HeartbeatFilter = sensor_ns.HeartbeatFilter -DebounceFilter = sensor_ns.DebounceFilter -UniqueFilter = sensor_ns.UniqueFilter +Sensor = sensor_ns.class_('Sensor', Nameable) +MQTTSensorComponent = sensor_ns.class_('MQTTSensorComponent', mqtt.MQTTComponent) -SensorStateTrigger = sensor_ns.SensorStateTrigger -SensorRawStateTrigger = sensor_ns.SensorRawStateTrigger -ValueRangeTrigger = sensor_ns.ValueRangeTrigger +PollingSensorComponent = sensor_ns.class_('PollingSensorComponent', PollingComponent, Sensor) +EmptySensor = sensor_ns.class_('EmptySensor', Sensor) +EmptyPollingParentSensor = sensor_ns.class_('EmptyPollingParentSensor', EmptySensor) + +# Triggers +SensorStateTrigger = sensor_ns.class_('SensorStateTrigger', Trigger.template(float_)) +SensorRawStateTrigger = sensor_ns.class_('SensorRawStateTrigger', Trigger.template(float_)) +ValueRangeTrigger = sensor_ns.class_('ValueRangeTrigger', Trigger.template(float_)) + +# Filters +Filter = sensor_ns.class_('Filter') +SlidingWindowMovingAverageFilter = sensor_ns.class_('SlidingWindowMovingAverageFilter', Filter) +ExponentialMovingAverageFilter = sensor_ns.class_('ExponentialMovingAverageFilter', Filter) +LambdaFilter = sensor_ns.class_('LambdaFilter', Filter) +OffsetFilter = sensor_ns.class_('OffsetFilter', Filter) +MultiplyFilter = sensor_ns.class_('MultiplyFilter', Filter) +FilterOutValueFilter = sensor_ns.class_('FilterOutValueFilter', Filter) +FilterOutNANFilter = sensor_ns.class_('FilterOutNANFilter', Filter) +ThrottleFilter = sensor_ns.class_('ThrottleFilter', Filter) +DebounceFilter = sensor_ns.class_('DebounceFilter', Filter, Component) +HeartbeatFilter = sensor_ns.class_('HeartbeatFilter', Filter, Component) +DeltaFilter = sensor_ns.class_('DeltaFilter', Filter) +OrFilter = sensor_ns.class_('OrFilter', Filter) +UniqueFilter = sensor_ns.class_('UniqueFilter', Filter) SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({ cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTSensorComponent), - cv.GenerateID(): cv.declare_variable_id(Sensor), vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string_strict, vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_ACCURACY_DECIMALS): vol.Coerce(int), diff --git a/esphomeyaml/components/sensor/adc.py b/esphomeyaml/components/sensor/adc.py index dc0c7bc561..f629ee3d4d 100644 --- a/esphomeyaml/components/sensor/adc.py +++ b/esphomeyaml/components/sensor/adc.py @@ -5,7 +5,7 @@ from esphomeyaml import pins from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ATTENUATION, CONF_MAKE_ID, CONF_NAME, CONF_PIN, \ CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, global_ns, variable +from esphomeyaml.helpers import App, Application, add, global_ns, variable, setup_component ATTENUATION_MODES = { '0db': global_ns.ADC_0db, @@ -22,8 +22,8 @@ def validate_adc_pin(value): return pins.analog_pin(value) -MakeADCSensor = Application.MakeADCSensor -ADCSensorComponent = sensor.sensor_ns.ADCSensorComponent +MakeADCSensor = Application.struct('MakeADCSensor') +ADCSensorComponent = sensor.sensor_ns.class_('ADCSensorComponent', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ADCSensorComponent), @@ -31,7 +31,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ vol.Required(CONF_PIN): validate_adc_pin, vol.Optional(CONF_ATTENUATION): vol.All(cv.only_on_esp32, cv.one_of(*ATTENUATION_MODES)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): @@ -44,7 +44,8 @@ def to_code(config): adc = make.Padc if CONF_ATTENUATION in config: add(adc.set_attenuation(ATTENUATION_MODES[config[CONF_ATTENUATION]])) - sensor.setup_sensor(make.Padc, make.Pmqtt, config) + sensor.setup_sensor(adc, make.Pmqtt, config) + setup_component(adc, config) BUILD_FLAGS = '-DUSE_ADC_SENSOR' diff --git a/esphomeyaml/components/sensor/ads1115.py b/esphomeyaml/components/sensor/ads1115.py index 153d55d640..5f6c296789 100644 --- a/esphomeyaml/components/sensor/ads1115.py +++ b/esphomeyaml/components/sensor/ads1115.py @@ -9,24 +9,26 @@ from esphomeyaml.helpers import get_variable DEPENDENCIES = ['ads1115'] +ADS1115Multiplexer = sensor.sensor_ns.enum('ADS1115Multiplexer') MUX = { - 'A0_A1': sensor.sensor_ns.ADS1115_MULTIPLEXER_P0_N1, - 'A0_A3': sensor.sensor_ns.ADS1115_MULTIPLEXER_P0_N3, - 'A1_A3': sensor.sensor_ns.ADS1115_MULTIPLEXER_P1_N3, - 'A2_A3': sensor.sensor_ns.ADS1115_MULTIPLEXER_P2_N3, - 'A0_GND': sensor.sensor_ns.ADS1115_MULTIPLEXER_P0_NG, - 'A1_GND': sensor.sensor_ns.ADS1115_MULTIPLEXER_P1_NG, - 'A2_GND': sensor.sensor_ns.ADS1115_MULTIPLEXER_P2_NG, - 'A3_GND': sensor.sensor_ns.ADS1115_MULTIPLEXER_P3_NG, + 'A0_A1': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P0_N1, + 'A0_A3': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P0_N3, + 'A1_A3': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P1_N3, + 'A2_A3': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P2_N3, + 'A0_GND': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P0_NG, + 'A1_GND': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P1_NG, + 'A2_GND': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P2_NG, + 'A3_GND': ADS1115Multiplexer.ADS1115_MULTIPLEXER_P3_NG, } +ADS1115Gain = sensor.sensor_ns.enum('ADS1115Gain') GAIN = { - '6.144': sensor.sensor_ns.ADS1115_GAIN_6P144, - '4.096': sensor.sensor_ns.ADS1115_GAIN_6P096, - '2.048': sensor.sensor_ns.ADS1115_GAIN_2P048, - '1.024': sensor.sensor_ns.ADS1115_GAIN_1P024, - '0.512': sensor.sensor_ns.ADS1115_GAIN_0P512, - '0.256': sensor.sensor_ns.ADS1115_GAIN_0P256, + '6.144': ADS1115Gain.ADS1115_GAIN_6P144, + '4.096': ADS1115Gain.ADS1115_GAIN_6P096, + '2.048': ADS1115Gain.ADS1115_GAIN_2P048, + '1.024': ADS1115Gain.ADS1115_GAIN_1P024, + '0.512': ADS1115Gain.ADS1115_GAIN_0P512, + '0.256': ADS1115Gain.ADS1115_GAIN_0P256, } @@ -45,7 +47,7 @@ def validate_mux(value): return cv.one_of(*MUX)(value) -ADS1115Sensor = sensor.sensor_ns.ADS1115Sensor +ADS1115Sensor = sensor.sensor_ns.class_('ADS1115Sensor', sensor.EmptySensor) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ADS1115Sensor), diff --git a/esphomeyaml/components/sensor/bh1750.py b/esphomeyaml/components/sensor/bh1750.py index d04041c2f8..09057e5f7f 100644 --- a/esphomeyaml/components/sensor/bh1750.py +++ b/esphomeyaml/components/sensor/bh1750.py @@ -1,21 +1,23 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, i2c from esphomeyaml.const import CONF_ADDRESS, CONF_MAKE_ID, CONF_NAME, CONF_RESOLUTION, \ CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component DEPENDENCIES = ['i2c'] +BH1750Resolution = sensor.sensor_ns.enum('BH1750Resolution') BH1750_RESOLUTIONS = { - 4.0: sensor.sensor_ns.BH1750_RESOLUTION_4P0_LX, - 1.0: sensor.sensor_ns.BH1750_RESOLUTION_1P0_LX, - 0.5: sensor.sensor_ns.BH1750_RESOLUTION_0P5_LX, + 4.0: BH1750Resolution.BH1750_RESOLUTION_4P0_LX, + 1.0: BH1750Resolution.BH1750_RESOLUTION_1P0_LX, + 0.5: BH1750Resolution.BH1750_RESOLUTION_0P5_LX, } -MakeBH1750Sensor = Application.MakeBH1750Sensor -BH1750Sensor = sensor.sensor_ns.BH1750Sensor +MakeBH1750Sensor = Application.struct('MakeBH1750Sensor') +BH1750Sensor = sensor.sensor_ns.class_('BH1750Sensor', sensor.PollingSensorComponent, + i2c.I2CDevice) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(BH1750Sensor), @@ -23,7 +25,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_ADDRESS, default=0x23): cv.i2c_address, vol.Optional(CONF_RESOLUTION): vol.All(cv.positive_float, cv.one_of(*BH1750_RESOLUTIONS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): @@ -34,6 +36,7 @@ def to_code(config): if CONF_RESOLUTION in config: add(bh1750.set_resolution(BH1750_RESOLUTIONS[config[CONF_RESOLUTION]])) sensor.setup_sensor(bh1750, make_bh1750.Pmqtt, config) + setup_component(bh1750, config) BUILD_FLAGS = '-DUSE_BH1750' diff --git a/esphomeyaml/components/sensor/ble_rssi.py b/esphomeyaml/components/sensor/ble_rssi.py index 4240b41bd4..e209b9af64 100644 --- a/esphomeyaml/components/sensor/ble_rssi.py +++ b/esphomeyaml/components/sensor/ble_rssi.py @@ -9,7 +9,7 @@ from esphomeyaml.helpers import get_variable, esphomelib_ns DEPENDENCIES = ['esp32_ble_tracker'] -ESP32BLERSSISensor = esphomelib_ns.ESP32BLERSSISensor +ESP32BLERSSISensor = esphomelib_ns.class_('ESP32BLERSSISensor', sensor.Sensor) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ESP32BLERSSISensor), @@ -19,7 +19,6 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ def to_code(config): - hub = None for hub in get_variable(config[CONF_ESP32_BLE_ID]): yield rhs = hub.make_rssi_sensor(config[CONF_NAME], make_address_array(config[CONF_MAC_ADDRESS])) diff --git a/esphomeyaml/components/sensor/bme280.py b/esphomeyaml/components/sensor/bme280.py index 580add1019..23e395f6ef 100644 --- a/esphomeyaml/components/sensor/bme280.py +++ b/esphomeyaml/components/sensor/bme280.py @@ -4,42 +4,56 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_HUMIDITY, CONF_IIR_FILTER, CONF_MAKE_ID, \ CONF_NAME, CONF_OVERSAMPLING, CONF_PRESSURE, CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component DEPENDENCIES = ['i2c'] +BME280Oversampling = sensor.sensor_ns.enum('BME280Oversampling') OVERSAMPLING_OPTIONS = { - 'NONE': sensor.sensor_ns.BME280_OVERSAMPLING_NONE, - '1X': sensor.sensor_ns.BME280_OVERSAMPLING_1X, - '2X': sensor.sensor_ns.BME280_OVERSAMPLING_2X, - '4X': sensor.sensor_ns.BME280_OVERSAMPLING_4X, - '8X': sensor.sensor_ns.BME280_OVERSAMPLING_8X, - '16X': sensor.sensor_ns.BME280_OVERSAMPLING_16X, + 'NONE': BME280Oversampling.BME280_OVERSAMPLING_NONE, + '1X': BME280Oversampling.BME280_OVERSAMPLING_1X, + '2X': BME280Oversampling.BME280_OVERSAMPLING_2X, + '4X': BME280Oversampling.BME280_OVERSAMPLING_4X, + '8X': BME280Oversampling.BME280_OVERSAMPLING_8X, + '16X': BME280Oversampling.BME280_OVERSAMPLING_16X, } +BME280IIRFilter = sensor.sensor_ns.enum('BME280IIRFilter') IIR_FILTER_OPTIONS = { - 'OFF': sensor.sensor_ns.BME280_IIR_FILTER_OFF, - '2X': sensor.sensor_ns.BME280_IIR_FILTER_2X, - '4X': sensor.sensor_ns.BME280_IIR_FILTER_4X, - '8X': sensor.sensor_ns.BME280_IIR_FILTER_8X, - '16X': sensor.sensor_ns.BME280_IIR_FILTER_16X, + 'OFF': BME280IIRFilter.BME280_IIR_FILTER_OFF, + '2X': BME280IIRFilter.BME280_IIR_FILTER_2X, + '4X': BME280IIRFilter.BME280_IIR_FILTER_4X, + '8X': BME280IIRFilter.BME280_IIR_FILTER_8X, + '16X': BME280IIRFilter.BME280_IIR_FILTER_16X, } BME280_OVERSAMPLING_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ vol.Optional(CONF_OVERSAMPLING): vol.All(vol.Upper, cv.one_of(*OVERSAMPLING_OPTIONS)), }) -MakeBME280Sensor = Application.MakeBME280Sensor +MakeBME280Sensor = Application.struct('MakeBME280Sensor') +BME280TemperatureSensor = sensor.sensor_ns.class_('BME280TemperatureSensor', + sensor.EmptyPollingParentSensor) +BME280PressureSensor = sensor.sensor_ns.class_('BME280PressureSensor', + sensor.EmptyPollingParentSensor) +BME280HumiditySensor = sensor.sensor_ns.class_('BME280HumiditySensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeBME280Sensor), vol.Optional(CONF_ADDRESS, default=0x77): cv.i2c_address, - vol.Required(CONF_TEMPERATURE): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_PRESSURE): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME280TemperatureSensor), + })), + vol.Required(CONF_PRESSURE): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME280PressureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(BME280_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME280HumiditySensor), + })), vol.Optional(CONF_IIR_FILTER): vol.All(vol.Upper, cv.one_of(*IIR_FILTER_OPTIONS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -69,6 +83,7 @@ def to_code(config): config[CONF_PRESSURE]) sensor.setup_sensor(bme280.Pget_humidity_sensor(), make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(bme280, config) BUILD_FLAGS = '-DUSE_BME280' diff --git a/esphomeyaml/components/sensor/bme680.py b/esphomeyaml/components/sensor/bme680.py index 352552bb59..71778574a1 100644 --- a/esphomeyaml/components/sensor/bme680.py +++ b/esphomeyaml/components/sensor/bme680.py @@ -6,43 +6,61 @@ from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_GAS_RESISTANCE, CONF_HUMIDITY, CONF_IIR_FILTER, \ CONF_MAKE_ID, CONF_NAME, CONF_OVERSAMPLING, CONF_PRESSURE, CONF_TEMPERATURE, \ CONF_UPDATE_INTERVAL, CONF_HEATER, CONF_DURATION -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component DEPENDENCIES = ['i2c'] +BME680Oversampling = sensor.sensor_ns.enum('BME680Oversampling') OVERSAMPLING_OPTIONS = { - 'NONE': sensor.sensor_ns.BME680_OVERSAMPLING_NONE, - '1X': sensor.sensor_ns.BME680_OVERSAMPLING_1X, - '2X': sensor.sensor_ns.BME680_OVERSAMPLING_2X, - '4X': sensor.sensor_ns.BME680_OVERSAMPLING_4X, - '8X': sensor.sensor_ns.BME680_OVERSAMPLING_8X, - '16X': sensor.sensor_ns.BME680_OVERSAMPLING_16X, + 'NONE': BME680Oversampling.BME680_OVERSAMPLING_NONE, + '1X': BME680Oversampling.BME680_OVERSAMPLING_1X, + '2X': BME680Oversampling.BME680_OVERSAMPLING_2X, + '4X': BME680Oversampling.BME680_OVERSAMPLING_4X, + '8X': BME680Oversampling.BME680_OVERSAMPLING_8X, + '16X': BME680Oversampling.BME680_OVERSAMPLING_16X, } +BME680IIRFilter = sensor.sensor_ns.enum('BME680IIRFilter') IIR_FILTER_OPTIONS = { - 'OFF': sensor.sensor_ns.BME680_IIR_FILTER_OFF, - '1X': sensor.sensor_ns.BME680_IIR_FILTER_1X, - '3X': sensor.sensor_ns.BME680_IIR_FILTER_3X, - '7X': sensor.sensor_ns.BME680_IIR_FILTER_7X, - '15X': sensor.sensor_ns.BME680_IIR_FILTER_15X, - '31X': sensor.sensor_ns.BME680_IIR_FILTER_31X, - '63X': sensor.sensor_ns.BME680_IIR_FILTER_63X, - '127X': sensor.sensor_ns.BME680_IIR_FILTER_127X, + 'OFF': BME680IIRFilter.BME680_IIR_FILTER_OFF, + '1X': BME680IIRFilter.BME680_IIR_FILTER_1X, + '3X': BME680IIRFilter.BME680_IIR_FILTER_3X, + '7X': BME680IIRFilter.BME680_IIR_FILTER_7X, + '15X': BME680IIRFilter.BME680_IIR_FILTER_15X, + '31X': BME680IIRFilter.BME680_IIR_FILTER_31X, + '63X': BME680IIRFilter.BME680_IIR_FILTER_63X, + '127X': BME680IIRFilter.BME680_IIR_FILTER_127X, } BME680_OVERSAMPLING_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ vol.Optional(CONF_OVERSAMPLING): vol.All(vol.Upper, cv.one_of(*OVERSAMPLING_OPTIONS)), }) -MakeBME680Sensor = Application.MakeBME680Sensor +MakeBME680Sensor = Application.struct('MakeBME680Sensor') +BME680TemperatureSensor = sensor.sensor_ns.class_('BME680TemperatureSensor', + sensor.EmptyPollingParentSensor) +BME680PressureSensor = sensor.sensor_ns.class_('BME680PressureSensor', + sensor.EmptyPollingParentSensor) +BME680HumiditySensor = sensor.sensor_ns.class_('BME680HumiditySensor', + sensor.EmptyPollingParentSensor) +BME680GasResistanceSensor = sensor.sensor_ns.class_('BME680GasResistanceSensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeBME680Sensor), vol.Optional(CONF_ADDRESS, default=0x76): cv.i2c_address, - vol.Required(CONF_TEMPERATURE): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_PRESSURE): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_GAS_RESISTANCE): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME680TemperatureSensor), + })), + vol.Required(CONF_PRESSURE): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME680PressureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(BME680_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME680HumiditySensor), + })), + vol.Required(CONF_GAS_RESISTANCE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BME680GasResistanceSensor), + })), vol.Optional(CONF_IIR_FILTER): vol.All(vol.Upper, cv.one_of(*IIR_FILTER_OPTIONS)), vol.Optional(CONF_HEATER): vol.Any(None, vol.All(vol.Schema({ vol.Optional(CONF_TEMPERATURE, default=320): vol.All(vol.Coerce(int), vol.Range(200, 400)), @@ -50,7 +68,7 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.positive_time_period_milliseconds, vol.Range(max=core.TimePeriod(milliseconds=4032))) }, cv.has_at_least_one_key(CONF_TEMPERATURE, CONF_DURATION)))), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -89,6 +107,7 @@ def to_code(config): config[CONF_HUMIDITY]) sensor.setup_sensor(bme680.Pget_gas_resistance_sensor(), make.Pmqtt_gas_resistance, config[CONF_GAS_RESISTANCE]) + setup_component(bme680, config) BUILD_FLAGS = '-DUSE_BME680' diff --git a/esphomeyaml/components/sensor/bmp085.py b/esphomeyaml/components/sensor/bmp085.py index ad1e22939d..24b7199a10 100644 --- a/esphomeyaml/components/sensor/bmp085.py +++ b/esphomeyaml/components/sensor/bmp085.py @@ -4,19 +4,27 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_MAKE_ID, CONF_NAME, CONF_PRESSURE, \ CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, HexIntLiteral, add, variable +from esphomeyaml.helpers import App, Application, HexIntLiteral, add, variable, setup_component DEPENDENCIES = ['i2c'] -MakeBMP085Sensor = Application.MakeBMP085Sensor +MakeBMP085Sensor = Application.struct('MakeBMP085Sensor') +BMP085TemperatureSensor = sensor.sensor_ns.class_('BMP085TemperatureSensor', + sensor.EmptyPollingParentSensor) +BMP085PressureSensor = sensor.sensor_ns.class_('BMP085PressureSensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeBMP085Sensor), - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_PRESSURE): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BMP085TemperatureSensor), + })), + vol.Required(CONF_PRESSURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BMP085PressureSensor), + })), vol.Optional(CONF_ADDRESS): cv.i2c_address, vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -31,6 +39,7 @@ def to_code(config): config[CONF_TEMPERATURE]) sensor.setup_sensor(bmp.Pbmp.Pget_pressure_sensor(), bmp.Pmqtt_pressure, config[CONF_PRESSURE]) + setup_component(bmp.Pbmp, config) BUILD_FLAGS = '-DUSE_BMP085_SENSOR' diff --git a/esphomeyaml/components/sensor/bmp280.py b/esphomeyaml/components/sensor/bmp280.py index 9aa67d6bc0..35d30403d8 100644 --- a/esphomeyaml/components/sensor/bmp280.py +++ b/esphomeyaml/components/sensor/bmp280.py @@ -4,41 +4,51 @@ import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_IIR_FILTER, CONF_MAKE_ID, \ CONF_NAME, CONF_OVERSAMPLING, CONF_PRESSURE, CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component DEPENDENCIES = ['i2c'] +BMP280Oversampling = sensor.sensor_ns.enum('BMP280Oversampling') OVERSAMPLING_OPTIONS = { - 'NONE': sensor.sensor_ns.BMP280_OVERSAMPLING_NONE, - '1X': sensor.sensor_ns.BMP280_OVERSAMPLING_1X, - '2X': sensor.sensor_ns.BMP280_OVERSAMPLING_2X, - '4X': sensor.sensor_ns.BMP280_OVERSAMPLING_4X, - '8X': sensor.sensor_ns.BMP280_OVERSAMPLING_8X, - '16X': sensor.sensor_ns.BMP280_OVERSAMPLING_16X, + 'NONE': BMP280Oversampling.BMP280_OVERSAMPLING_NONE, + '1X': BMP280Oversampling.BMP280_OVERSAMPLING_1X, + '2X': BMP280Oversampling.BMP280_OVERSAMPLING_2X, + '4X': BMP280Oversampling.BMP280_OVERSAMPLING_4X, + '8X': BMP280Oversampling.BMP280_OVERSAMPLING_8X, + '16X': BMP280Oversampling.BMP280_OVERSAMPLING_16X, } +BMP280IIRFilter = sensor.sensor_ns.enum('BMP280IIRFilter') IIR_FILTER_OPTIONS = { - 'OFF': sensor.sensor_ns.BMP280_IIR_FILTER_OFF, - '2X': sensor.sensor_ns.BMP280_IIR_FILTER_2X, - '4X': sensor.sensor_ns.BMP280_IIR_FILTER_4X, - '8X': sensor.sensor_ns.BMP280_IIR_FILTER_8X, - '16X': sensor.sensor_ns.BMP280_IIR_FILTER_16X, + 'OFF': BMP280IIRFilter.BMP280_IIR_FILTER_OFF, + '2X': BMP280IIRFilter.BMP280_IIR_FILTER_2X, + '4X': BMP280IIRFilter.BMP280_IIR_FILTER_4X, + '8X': BMP280IIRFilter.BMP280_IIR_FILTER_8X, + '16X': BMP280IIRFilter.BMP280_IIR_FILTER_16X, } BMP280_OVERSAMPLING_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ vol.Optional(CONF_OVERSAMPLING): vol.All(vol.Upper, cv.one_of(*OVERSAMPLING_OPTIONS)), }) -MakeBMP280Sensor = Application.MakeBMP280Sensor +MakeBMP280Sensor = Application.struct('MakeBMP280Sensor') +BMP280TemperatureSensor = sensor.sensor_ns.class_('BMP280TemperatureSensor', + sensor.EmptyPollingParentSensor) +BMP280PressureSensor = sensor.sensor_ns.class_('BMP280PressureSensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeBMP280Sensor), vol.Optional(CONF_ADDRESS, default=0x77): cv.i2c_address, - vol.Required(CONF_TEMPERATURE): cv.nameable(BMP280_OVERSAMPLING_SENSOR_SCHEMA), - vol.Required(CONF_PRESSURE): cv.nameable(BMP280_OVERSAMPLING_SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(BMP280_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BMP280TemperatureSensor), + })), + vol.Required(CONF_PRESSURE): cv.nameable(BMP280_OVERSAMPLING_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(BMP280PressureSensor), + })), vol.Optional(CONF_IIR_FILTER): vol.All(vol.Upper, cv.one_of(*IIR_FILTER_OPTIONS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -62,6 +72,7 @@ def to_code(config): config[CONF_TEMPERATURE]) sensor.setup_sensor(bmp280.Pget_pressure_sensor(), make.Pmqtt_pressure, config[CONF_PRESSURE]) + setup_component(bmp280, config) BUILD_FLAGS = '-DUSE_BMP280' diff --git a/esphomeyaml/components/sensor/cse7766.py b/esphomeyaml/components/sensor/cse7766.py index 7de127e4b6..091c146632 100644 --- a/esphomeyaml/components/sensor/cse7766.py +++ b/esphomeyaml/components/sensor/cse7766.py @@ -1,31 +1,44 @@ import voluptuous as vol -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, uart from esphomeyaml.components.uart import UARTComponent import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_CURRENT, CONF_ID, CONF_NAME, CONF_POWER, CONF_UART_ID, \ CONF_VOLTAGE -from esphomeyaml.helpers import App, Pvariable, get_variable +from esphomeyaml.helpers import App, Pvariable, get_variable, setup_component, Component DEPENDENCIES = ['uart'] -CSE7766Component = sensor.sensor_ns.CSE7766Component +CSE7766Component = sensor.sensor_ns.class_('CSE7766Component', Component, uart.UARTDevice) +CSE7766VoltageSensor = sensor.sensor_ns.class_('CSE7766VoltageSensor', + sensor.EmptySensor) +CSE7766CurrentSensor = sensor.sensor_ns.class_('CSE7766CurrentSensor', + sensor.EmptySensor) +CSE7766PowerSensor = sensor.sensor_ns.class_('CSE7766PowerSensor', + sensor.EmptySensor) PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(CSE7766Component), cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent), - vol.Optional(CONF_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA), -}), cv.has_at_least_one_key(CONF_VOLTAGE, CONF_CURRENT, CONF_POWER)) + vol.Optional(CONF_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(CSE7766VoltageSensor), + })), + vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(CSE7766CurrentSensor), + })), + vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(CSE7766PowerSensor), + })), +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_VOLTAGE, CONF_CURRENT, + CONF_POWER)) def to_code(config): - for uart in get_variable(config[CONF_UART_ID]): + for uart_ in get_variable(config[CONF_UART_ID]): yield - rhs = App.make_cse7766(uart) + rhs = App.make_cse7766(uart_) cse = Pvariable(config[CONF_ID], rhs) if CONF_VOLTAGE in config: @@ -37,6 +50,7 @@ def to_code(config): if CONF_POWER in config: conf = config[CONF_POWER] sensor.register_sensor(cse.make_power_sensor(conf[CONF_NAME]), conf) + setup_component(cse, config) BUILD_FLAGS = '-DUSE_CSE7766' diff --git a/esphomeyaml/components/sensor/dallas.py b/esphomeyaml/components/sensor/dallas.py index a5706cbf5e..b974c8e636 100644 --- a/esphomeyaml/components/sensor/dallas.py +++ b/esphomeyaml/components/sensor/dallas.py @@ -7,7 +7,8 @@ from esphomeyaml.const import CONF_ADDRESS, CONF_DALLAS_ID, CONF_INDEX, CONF_NAM CONF_RESOLUTION from esphomeyaml.helpers import HexIntLiteral, get_variable -DallasTemperatureSensor = sensor.sensor_ns.DallasTemperatureSensor +DallasTemperatureSensor = sensor.sensor_ns.class_('DallasTemperatureSensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(DallasTemperatureSensor), @@ -19,7 +20,6 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ def to_code(config): - hub = None for hub in get_variable(config[CONF_DALLAS_ID]): yield if CONF_ADDRESS in config: diff --git a/esphomeyaml/components/sensor/dht.py b/esphomeyaml/components/sensor/dht.py index c5a6dbff2b..a13d0f0fdd 100644 --- a/esphomeyaml/components/sensor/dht.py +++ b/esphomeyaml/components/sensor/dht.py @@ -3,46 +3,61 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_HUMIDITY, CONF_MAKE_ID, CONF_MODEL, CONF_NAME, CONF_PIN, \ - CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, gpio_output_pin_expression, variable + CONF_TEMPERATURE, CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, add, gpio_output_pin_expression, variable, \ + setup_component, PollingComponent, Pvariable from esphomeyaml.pins import gpio_output_pin_schema +DHTModel = sensor.sensor_ns.enum('DHTModel') DHT_MODELS = { - 'AUTO_DETECT': sensor.sensor_ns.DHT_MODEL_AUTO_DETECT, - 'DHT11': sensor.sensor_ns.DHT_MODEL_DHT11, - 'DHT22': sensor.sensor_ns.DHT_MODEL_DHT22, - 'AM2302': sensor.sensor_ns.DHT_MODEL_AM2302, - 'RHT03': sensor.sensor_ns.DHT_MODEL_RHT03, + 'AUTO_DETECT': DHTModel.DHT_MODEL_AUTO_DETECT, + 'DHT11': DHTModel.DHT_MODEL_DHT11, + 'DHT22': DHTModel.DHT_MODEL_DHT22, + 'AM2302': DHTModel.DHT_MODEL_AM2302, + 'RHT03': DHTModel.DHT_MODEL_RHT03, } -MakeDHTSensor = Application.MakeDHTSensor +MakeDHTSensor = Application.struct('MakeDHTSensor') +DHTComponent = sensor.sensor_ns.class_('DHTComponent', PollingComponent) +DHTTemperatureSensor = sensor.sensor_ns.class_('DHTTemperatureSensor', + sensor.EmptyPollingParentSensor) +DHTHumiditySensor = sensor.sensor_ns.class_('DHTHumiditySensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeDHTSensor), + cv.GenerateID(): cv.declare_variable_id(DHTComponent), vol.Required(CONF_PIN): gpio_output_pin_schema, - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(DHTTemperatureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(DHTHumiditySensor), + })), vol.Optional(CONF_MODEL): vol.All(vol.Upper, cv.one_of(*DHT_MODELS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - pin = None for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_dht_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], pin, config.get(CONF_UPDATE_INTERVAL)) - dht = variable(config[CONF_MAKE_ID], rhs) + make = variable(config[CONF_MAKE_ID], rhs) + dht = make.Pdht + Pvariable(config[CONF_ID], dht) + if CONF_MODEL in config: constant = DHT_MODELS[config[CONF_MODEL]] - add(dht.Pdht.set_dht_model(constant)) + add(dht.set_dht_model(constant)) - sensor.setup_sensor(dht.Pdht.Pget_temperature_sensor(), - dht.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(dht.Pdht.Pget_humidity_sensor(), - dht.Pmqtt_humidity, config[CONF_HUMIDITY]) + sensor.setup_sensor(dht.Pget_temperature_sensor(), + make.Pmqtt_temperature, config[CONF_TEMPERATURE]) + sensor.setup_sensor(dht.Pget_humidity_sensor(), + make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(dht, config) BUILD_FLAGS = '-DUSE_DHT_SENSOR' diff --git a/esphomeyaml/components/sensor/dht12.py b/esphomeyaml/components/sensor/dht12.py index e85e1fd3bc..7e80ab410e 100644 --- a/esphomeyaml/components/sensor/dht12.py +++ b/esphomeyaml/components/sensor/dht12.py @@ -1,33 +1,47 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, i2c from esphomeyaml.const import CONF_HUMIDITY, CONF_MAKE_ID, CONF_NAME, CONF_TEMPERATURE, \ - CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable + CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, variable, setup_component, PollingComponent, \ + Pvariable DEPENDENCIES = ['i2c'] -MakeDHT12Sensor = Application.MakeDHT12Sensor +MakeDHT12Sensor = Application.struct('MakeDHT12Sensor') +DHT12Component = sensor.sensor_ns.class_('DHT12Component', PollingComponent, i2c.I2CDevice) +DHT12TemperatureSensor = sensor.sensor_ns.class_('DHT12TemperatureSensor', + sensor.EmptyPollingParentSensor) +DHT12HumiditySensor = sensor.sensor_ns.class_('DHT12HumiditySensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeDHT12Sensor), - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), + cv.GenerateID(): cv.declare_variable_id(DHT12Component), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(DHT12TemperatureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(DHT12HumiditySensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): rhs = App.make_dht12_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) - dht = variable(config[CONF_MAKE_ID], rhs) + make = variable(config[CONF_MAKE_ID], rhs) + dht = make.Pdht12 + Pvariable(config[CONF_ID], dht) - sensor.setup_sensor(dht.Pdht12.Pget_temperature_sensor(), dht.Pmqtt_temperature, + sensor.setup_sensor(dht.Pget_temperature_sensor(), make.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(dht.Pdht12.Pget_humidity_sensor(), dht.Pmqtt_humidity, + sensor.setup_sensor(dht.Pget_humidity_sensor(), make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(dht, config) BUILD_FLAGS = '-DUSE_DHT12_SENSOR' diff --git a/esphomeyaml/components/sensor/duty_cycle.py b/esphomeyaml/components/sensor/duty_cycle.py index 58cf433070..85f8ec3bd9 100644 --- a/esphomeyaml/components/sensor/duty_cycle.py +++ b/esphomeyaml/components/sensor/duty_cycle.py @@ -4,27 +4,28 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_PIN, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, gpio_input_pin_expression, variable +from esphomeyaml.helpers import App, Application, gpio_input_pin_expression, variable, \ + setup_component -MakeDutyCycleSensor = Application.MakeDutyCycleSensor -DutyCycleSensor = sensor.sensor_ns.DutyCycleSensor +MakeDutyCycleSensor = Application.struct('MakeDutyCycleSensor') +DutyCycleSensor = sensor.sensor_ns.class_('DutyCycleSensor', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(DutyCycleSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeDutyCycleSensor), vol.Required(CONF_PIN): pins.internal_gpio_input_pin_schema, vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - pin = None for pin in gpio_input_pin_expression(config[CONF_PIN]): yield rhs = App.make_duty_cycle_sensor(config[CONF_NAME], pin, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) sensor.setup_sensor(make.Pduty, make.Pmqtt, config) + setup_component(make.Pduty, config) BUILD_FLAGS = '-DUSE_DUTY_CYCLE_SENSOR' diff --git a/esphomeyaml/components/sensor/esp32_hall.py b/esphomeyaml/components/sensor/esp32_hall.py index e86377dd39..ddf2af9b5e 100644 --- a/esphomeyaml/components/sensor/esp32_hall.py +++ b/esphomeyaml/components/sensor/esp32_hall.py @@ -3,24 +3,25 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL, ESP_PLATFORM_ESP32 -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.helpers import App, Application, variable, setup_component ESP_PLATFORMS = [ESP_PLATFORM_ESP32] -MakeESP32HallSensor = Application.MakeESP32HallSensor -ESP32HallSensor = sensor.sensor_ns.ESP32HallSensor +MakeESP32HallSensor = Application.struct('MakeESP32HallSensor') +ESP32HallSensor = sensor.sensor_ns.class_('ESP32HallSensor', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ESP32HallSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeESP32HallSensor), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_esp32_hall_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) sensor.setup_sensor(make.Phall, make.Pmqtt, config) + setup_component(make.Phall, config) BUILD_FLAGS = '-DUSE_ESP32_HALL_SENSOR' diff --git a/esphomeyaml/components/sensor/hdc1080.py b/esphomeyaml/components/sensor/hdc1080.py index d59663a71b..e8decc408a 100644 --- a/esphomeyaml/components/sensor/hdc1080.py +++ b/esphomeyaml/components/sensor/hdc1080.py @@ -1,34 +1,48 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, i2c from esphomeyaml.const import CONF_HUMIDITY, CONF_MAKE_ID, CONF_NAME, CONF_TEMPERATURE, \ - CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable + CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, variable, setup_component, PollingComponent, \ + Pvariable DEPENDENCIES = ['i2c'] -MakeHDC1080Sensor = Application.MakeHDC1080Sensor +MakeHDC1080Sensor = Application.struct('MakeHDC1080Sensor') +HDC1080Component = sensor.sensor_ns.class_('HDC1080Component', PollingComponent, i2c.I2CDevice) +HDC1080TemperatureSensor = sensor.sensor_ns.class_('HDC1080TemperatureSensor', + sensor.EmptyPollingParentSensor) +HDC1080HumiditySensor = sensor.sensor_ns.class_('HDC1080HumiditySensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeHDC1080Sensor), - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), + cv.GenerateID(): cv.declare_variable_id(HDC1080Component), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HDC1080TemperatureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HDC1080HumiditySensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): rhs = App.make_hdc1080_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) - hdc1080 = variable(config[CONF_MAKE_ID], rhs) + make = variable(config[CONF_MAKE_ID], rhs) + hdc1080 = make.Phdc1080 + Pvariable(config[CONF_ID], hdc1080) - sensor.setup_sensor(hdc1080.Phdc1080.Pget_temperature_sensor(), - hdc1080.Pmqtt_temperature, + sensor.setup_sensor(hdc1080.Pget_temperature_sensor(), + make.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(hdc1080.Phdc1080.Pget_humidity_sensor(), hdc1080.Pmqtt_humidity, + sensor.setup_sensor(hdc1080.Pget_humidity_sensor(), make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(hdc1080, config) BUILD_FLAGS = '-DUSE_HDC1080_SENSOR' diff --git a/esphomeyaml/components/sensor/hlw8012.py b/esphomeyaml/components/sensor/hlw8012.py index 818e2802c6..0f2f7d0aa5 100644 --- a/esphomeyaml/components/sensor/hlw8012.py +++ b/esphomeyaml/components/sensor/hlw8012.py @@ -1,17 +1,18 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import sensor +import esphomeyaml.config_validation as cv from esphomeyaml.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 esphomeyaml.helpers import App, Pvariable, add, gpio_output_pin_expression +from esphomeyaml.helpers import App, PollingComponent, Pvariable, add, gpio_output_pin_expression, \ + setup_component -HLW8012Component = sensor.sensor_ns.HLW8012Component -HLW8012VoltageSensor = sensor.sensor_ns.HLW8012VoltageSensor -HLW8012CurrentSensor = sensor.sensor_ns.HLW8012CurrentSensor -HLW8012PowerSensor = sensor.sensor_ns.HLW8012PowerSensor +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), @@ -19,19 +20,25 @@ PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ 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), - vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA), + 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, vol.Optional(CONF_CHANGE_MODE_EVERY): vol.All(cv.uint32_t, vol.Range(min=1)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}), cv.has_at_least_one_key(CONF_VOLTAGE, CONF_CURRENT, CONF_POWER)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_VOLTAGE, CONF_CURRENT, + CONF_POWER)) def to_code(config): - sel = None for sel in gpio_output_pin_expression(config[CONF_SEL_PIN]): yield @@ -54,6 +61,7 @@ def to_code(config): add(hlw.set_voltage_divider(config[CONF_VOLTAGE_DIVIDER])) if CONF_CHANGE_MODE_EVERY in config: add(hlw.set_change_mode_every(config[CONF_CHANGE_MODE_EVERY])) + setup_component(hlw, config) BUILD_FLAGS = '-DUSE_HLW8012' diff --git a/esphomeyaml/components/sensor/hmc5883l.py b/esphomeyaml/components/sensor/hmc5883l.py index a63f1737c2..a4ed04cf0a 100644 --- a/esphomeyaml/components/sensor/hmc5883l.py +++ b/esphomeyaml/components/sensor/hmc5883l.py @@ -2,9 +2,9 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, i2c from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_NAME, CONF_UPDATE_INTERVAL, CONF_RANGE -from esphomeyaml.helpers import App, Pvariable, add +from esphomeyaml.helpers import App, Pvariable, add, setup_component, PollingComponent DEPENDENCIES = ['i2c'] @@ -13,19 +13,23 @@ CONF_FIELD_STRENGTH_Y = 'field_strength_y' CONF_FIELD_STRENGTH_Z = 'field_strength_z' CONF_HEADING = 'heading' -HMC5883LComponent = sensor.sensor_ns.HMC5883LComponent -HMC5883LFieldStrengthSensor = sensor.sensor_ns.HMC5883LFieldStrengthSensor -HMC5883LHeadingSensor = sensor.sensor_ns.HMC5883LHeadingSensor +HMC5883LComponent = sensor.sensor_ns.class_('HMC5883LComponent', PollingComponent, i2c.I2CDevice) +HMC5883LFieldStrengthSensor = sensor.sensor_ns.class_('HMC5883LFieldStrengthSensor', + sensor.EmptyPollingParentSensor) +HMC5883LHeadingSensor = sensor.sensor_ns.class_('HMC5883LHeadingSensor', + sensor.EmptyPollingParentSensor) + +HMC5883LRange = sensor.sensor_ns.enum('HMC5883LRange') HMC5883L_RANGES = { - 88: sensor.sensor_ns.HMC5883L_RANGE_88_UT, - 130: sensor.sensor_ns.HMC5883L_RANGE_130_UT, - 190: sensor.sensor_ns.HMC5883L_RANGE_190_UT, - 250: sensor.sensor_ns.HMC5883L_RANGE_250_UT, - 400: sensor.sensor_ns.HMC5883L_RANGE_400_UT, - 470: sensor.sensor_ns.HMC5883L_RANGE_470_UT, - 560: sensor.sensor_ns.HMC5883L_RANGE_560_UT, - 810: sensor.sensor_ns.HMC5883L_RANGE_810_UT, + 88: HMC5883LRange.HMC5883L_RANGE_88_UT, + 130: HMC5883LRange.HMC5883L_RANGE_130_UT, + 190: HMC5883LRange.HMC5883L_RANGE_190_UT, + 250: HMC5883LRange.HMC5883L_RANGE_250_UT, + 400: HMC5883LRange.HMC5883L_RANGE_400_UT, + 470: HMC5883LRange.HMC5883L_RANGE_470_UT, + 560: HMC5883LRange.HMC5883L_RANGE_560_UT, + 810: HMC5883LRange.HMC5883L_RANGE_810_UT, } @@ -36,17 +40,27 @@ def validate_range(value): return cv.one_of(*HMC5883L_RANGES)(int(value)) +SENSOR_KEYS = [CONF_FIELD_STRENGTH_X, CONF_FIELD_STRENGTH_Y, CONF_FIELD_STRENGTH_Z, + CONF_HEADING] + PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(HMC5883LComponent), vol.Optional(CONF_ADDRESS): cv.i2c_address, - vol.Optional(CONF_FIELD_STRENGTH_X): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_FIELD_STRENGTH_Y): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_FIELD_STRENGTH_Z): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_HEADING): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_FIELD_STRENGTH_X): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HMC5883LFieldStrengthSensor), + })), + vol.Optional(CONF_FIELD_STRENGTH_Y): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HMC5883LFieldStrengthSensor), + })), + vol.Optional(CONF_FIELD_STRENGTH_Z): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HMC5883LFieldStrengthSensor), + })), + vol.Optional(CONF_HEADING): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HMC5883LHeadingSensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, vol.Optional(CONF_RANGE): validate_range, -}), cv.has_at_least_one_key(CONF_FIELD_STRENGTH_X, CONF_FIELD_STRENGTH_Y, CONF_FIELD_STRENGTH_Z, - CONF_HEADING)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS)) def to_code(config): @@ -68,6 +82,7 @@ def to_code(config): if CONF_HEADING in config: conf = config[CONF_HEADING] sensor.register_sensor(hmc.Pmake_heading_sensor(conf[CONF_NAME]), conf) + setup_component(hmc, config) BUILD_FLAGS = '-DUSE_HMC5883L' diff --git a/esphomeyaml/components/sensor/htu21d.py b/esphomeyaml/components/sensor/htu21d.py index 60c049fea9..6c4b077d80 100644 --- a/esphomeyaml/components/sensor/htu21d.py +++ b/esphomeyaml/components/sensor/htu21d.py @@ -1,32 +1,47 @@ import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor from esphomeyaml.const import CONF_HUMIDITY, CONF_MAKE_ID, CONF_NAME, CONF_TEMPERATURE, \ - CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable + CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, PollingComponent, setup_component, variable, \ + Pvariable DEPENDENCIES = ['i2c'] -MakeHTU21DSensor = Application.MakeHTU21DSensor +MakeHTU21DSensor = Application.struct('MakeHTU21DSensor') +HTU21DComponent = sensor.sensor_ns.class_('HTU21DComponent', PollingComponent, i2c.I2CDevice) +HTU21DTemperatureSensor = sensor.sensor_ns.class_('HTU21DTemperatureSensor', + sensor.EmptyPollingParentSensor) +HTU21DHumiditySensor = sensor.sensor_ns.class_('HTU21DHumiditySensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HTU21DComponent), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeHTU21DSensor), - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HTU21DTemperatureSensor), + })), + vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(HTU21DHumiditySensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): rhs = App.make_htu21d_sensor(config[CONF_TEMPERATURE][CONF_NAME], config[CONF_HUMIDITY][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) - htu21d = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(htu21d.Phtu21d.Pget_temperature_sensor(), htu21d.Pmqtt_temperature, + make = variable(config[CONF_MAKE_ID], rhs) + htu21d = make.Phtu21d + Pvariable(config[CONF_ID], htu21d) + + sensor.setup_sensor(htu21d.Pget_temperature_sensor(), make.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(htu21d.Phtu21d.Pget_humidity_sensor(), htu21d.Pmqtt_humidity, + sensor.setup_sensor(htu21d.Pget_humidity_sensor(), make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(htu21d, config) BUILD_FLAGS = '-DUSE_HTU21D_SENSOR' diff --git a/esphomeyaml/components/sensor/hx711.py b/esphomeyaml/components/sensor/hx711.py index 1df25c52ab..9f35e31cca 100644 --- a/esphomeyaml/components/sensor/hx711.py +++ b/esphomeyaml/components/sensor/hx711.py @@ -4,43 +4,47 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import sensor from esphomeyaml.const import CONF_GAIN, CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL, CONF_CLK_PIN -from esphomeyaml.helpers import App, Application, add, gpio_input_pin_expression, variable +from esphomeyaml.helpers import App, Application, add, gpio_input_pin_expression, variable, \ + setup_component -MakeHX711Sensor = Application.MakeHX711Sensor +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: sensor.sensor_ns.HX711_GAIN_128, - 32: sensor.sensor_ns.HX711_GAIN_32, - 64: sensor.sensor_ns.HX711_GAIN_64, + 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), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeHX711Sensor), vol.Required(CONF_DOUT_PIN): pins.gpio_input_pin_schema, vol.Required(CONF_CLK_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_GAIN): vol.All(cv.int_, cv.one_of(*GAINS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - dout_pin = None for dout_pin in gpio_input_pin_expression(config[CONF_DOUT_PIN]): yield - sck_pin = None for sck_pin in gpio_input_pin_expression(config[CONF_CLK_PIN]): yield rhs = App.make_hx711_sensor(config[CONF_NAME], dout_pin, sck_pin, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) + hx711 = make.Phx711 if CONF_GAIN in config: - add(make.Phx711.set_gain(GAINS[config[CONF_GAIN]])) + add(hx711.set_gain(GAINS[config[CONF_GAIN]])) - sensor.setup_sensor(make.Phx711, make.Pmqtt, config) + sensor.setup_sensor(hx711, make.Pmqtt, config) + setup_component(hx711, config) BUILD_FLAGS = '-DUSE_HX711' diff --git a/esphomeyaml/components/sensor/ina219.py b/esphomeyaml/components/sensor/ina219.py index b441b7e758..647c099978 100644 --- a/esphomeyaml/components/sensor/ina219.py +++ b/esphomeyaml/components/sensor/ina219.py @@ -1,34 +1,46 @@ # coding=utf-8 import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor -from esphomeyaml.const import CONF_ADDRESS, CONF_CURRENT, CONF_ID, CONF_MAX_CURRENT, \ - CONF_MAX_VOLTAGE, CONF_NAME, CONF_POWER, CONF_UPDATE_INTERVAL, CONF_BUS_VOLTAGE, \ - CONF_SHUNT_VOLTAGE, CONF_SHUNT_RESISTANCE -from esphomeyaml.helpers import App, Pvariable +from esphomeyaml.const import CONF_ADDRESS, CONF_BUS_VOLTAGE, CONF_CURRENT, CONF_ID, \ + CONF_MAX_CURRENT, CONF_MAX_VOLTAGE, CONF_NAME, CONF_POWER, CONF_SHUNT_RESISTANCE, \ + CONF_SHUNT_VOLTAGE, CONF_UPDATE_INTERVAL +from esphomeyaml.helpers import App, PollingComponent, Pvariable, setup_component DEPENDENCIES = ['i2c'] -INA219Component = sensor.sensor_ns.INA219Component -INA219VoltageSensor = sensor.sensor_ns.INA219VoltageSensor -INA219CurrentSensor = sensor.sensor_ns.INA219CurrentSensor -INA219PowerSensor = sensor.sensor_ns.INA219PowerSensor +INA219Component = sensor.sensor_ns.class_('INA219Component', PollingComponent, i2c.I2CDevice) +INA219VoltageSensor = sensor.sensor_ns.class_('INA219VoltageSensor', + sensor.EmptyPollingParentSensor) +INA219CurrentSensor = sensor.sensor_ns.class_('INA219CurrentSensor', + sensor.EmptyPollingParentSensor) +INA219PowerSensor = sensor.sensor_ns.class_('INA219PowerSensor', sensor.EmptyPollingParentSensor) + +SENSOR_KEYS = [CONF_BUS_VOLTAGE, CONF_SHUNT_VOLTAGE, CONF_CURRENT, + CONF_POWER] PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(INA219Component), vol.Optional(CONF_ADDRESS, default=0x40): cv.i2c_address, - vol.Optional(CONF_BUS_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_SHUNT_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_BUS_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA219VoltageSensor), + })), + vol.Optional(CONF_SHUNT_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA219VoltageSensor), + })), + vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA219CurrentSensor), + })), + vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA219PowerSensor), + })), vol.Optional(CONF_SHUNT_RESISTANCE, default=0.1): vol.All(cv.resistance, vol.Range(min=0.0, max=32.0)), vol.Optional(CONF_MAX_VOLTAGE, default=32.0): vol.All(cv.voltage, vol.Range(min=0.0, max=32.0)), vol.Optional(CONF_MAX_CURRENT, default=3.2): vol.All(cv.current, vol.Range(min=0.0)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}), cv.has_at_least_one_key(CONF_BUS_VOLTAGE, CONF_SHUNT_VOLTAGE, CONF_CURRENT, - CONF_POWER)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS)) def to_code(config): @@ -48,6 +60,7 @@ def to_code(config): if CONF_POWER in config: conf = config[CONF_POWER] sensor.register_sensor(ina.Pmake_power_sensor(conf[CONF_NAME]), conf) + setup_component(ina, config) BUILD_FLAGS = '-DUSE_INA219' diff --git a/esphomeyaml/components/sensor/ina3221.py b/esphomeyaml/components/sensor/ina3221.py index 360590960a..0ac8dff1d7 100644 --- a/esphomeyaml/components/sensor/ina3221.py +++ b/esphomeyaml/components/sensor/ina3221.py @@ -1,11 +1,11 @@ # coding=utf-8 import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_BUS_VOLTAGE, CONF_CURRENT, CONF_ID, CONF_NAME, \ CONF_POWER, CONF_SHUNT_RESISTANCE, CONF_SHUNT_VOLTAGE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Pvariable, add +from esphomeyaml.helpers import App, PollingComponent, Pvariable, add, setup_component DEPENDENCIES = ['i2c'] @@ -13,20 +13,31 @@ CONF_CHANNEL_1 = 'channel_1' CONF_CHANNEL_2 = 'channel_2' CONF_CHANNEL_3 = 'channel_3' -INA3221Component = sensor.sensor_ns.INA3221Component -INA3221VoltageSensor = sensor.sensor_ns.INA3221VoltageSensor -INA3221CurrentSensor = sensor.sensor_ns.INA3221CurrentSensor -INA3221PowerSensor = sensor.sensor_ns.INA3221PowerSensor +INA3221Component = sensor.sensor_ns.class_('INA3221Component', PollingComponent, i2c.I2CDevice) +INA3221VoltageSensor = sensor.sensor_ns.class_('INA3221VoltageSensor', + sensor.EmptyPollingParentSensor) +INA3221CurrentSensor = sensor.sensor_ns.class_('INA3221CurrentSensor', + sensor.EmptyPollingParentSensor) +INA3221PowerSensor = sensor.sensor_ns.class_('INA3221PowerSensor', sensor.EmptyPollingParentSensor) + +SENSOR_KEYS = [CONF_BUS_VOLTAGE, CONF_SHUNT_VOLTAGE, CONF_CURRENT, CONF_POWER] INA3221_CHANNEL_SCHEMA = vol.All(vol.Schema({ - vol.Optional(CONF_BUS_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_SHUNT_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_BUS_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA3221VoltageSensor), + })), + vol.Optional(CONF_SHUNT_VOLTAGE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA3221VoltageSensor), + })), + vol.Optional(CONF_CURRENT): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA3221CurrentSensor), + })), + vol.Optional(CONF_POWER): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(INA3221PowerSensor), + })), vol.Optional(CONF_SHUNT_RESISTANCE, default=0.1): vol.All(cv.resistance, vol.Range(min=0.0, max=32.0)), -}), cv.has_at_least_one_key(CONF_BUS_VOLTAGE, CONF_SHUNT_VOLTAGE, CONF_CURRENT, - CONF_POWER)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS)) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(INA3221Component), @@ -60,6 +71,8 @@ def to_code(config): c = conf[CONF_POWER] sensor.register_sensor(ina.Pmake_power_sensor(i, c[CONF_NAME]), c) + setup_component(ina, config) + BUILD_FLAGS = '-DUSE_INA3221' diff --git a/esphomeyaml/components/sensor/max6675.py b/esphomeyaml/components/sensor/max6675.py index 5d2432de62..5bf3907ebb 100644 --- a/esphomeyaml/components/sensor/max6675.py +++ b/esphomeyaml/components/sensor/max6675.py @@ -2,14 +2,16 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, spi from esphomeyaml.components.spi import SPIComponent from esphomeyaml.const import CONF_CS_PIN, CONF_MAKE_ID, CONF_NAME, CONF_SPI_ID, \ CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, get_variable, gpio_output_pin_expression, variable +from esphomeyaml.helpers import App, Application, get_variable, gpio_output_pin_expression, \ + variable, setup_component -MakeMAX6675Sensor = Application.MakeMAX6675Sensor -MAX6675Sensor = sensor.sensor_ns.MAX6675Sensor +MakeMAX6675Sensor = Application.struct('MakeMAX6675Sensor') +MAX6675Sensor = sensor.sensor_ns.class_('MAX6675Sensor', sensor.PollingSensorComponent, + spi.SPIDevice) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(MAX6675Sensor), @@ -17,20 +19,20 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ 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, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - spi = None - for spi in get_variable(config[CONF_SPI_ID]): + for spi_ in get_variable(config[CONF_SPI_ID]): yield - cs = None for cs in gpio_output_pin_expression(config[CONF_CS_PIN]): yield - rhs = App.make_max6675_sensor(config[CONF_NAME], spi, cs, + rhs = App.make_max6675_sensor(config[CONF_NAME], spi_, cs, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(make.Pmax6675, make.Pmqtt, config) + max6675 = make.Pmax6675 + sensor.setup_sensor(max6675, make.Pmqtt, config) + setup_component(max6675, config) BUILD_FLAGS = '-DUSE_MAX6675_SENSOR' diff --git a/esphomeyaml/components/sensor/mhz19.py b/esphomeyaml/components/sensor/mhz19.py index f2670356b3..06de766952 100644 --- a/esphomeyaml/components/sensor/mhz19.py +++ b/esphomeyaml/components/sensor/mhz19.py @@ -1,39 +1,51 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, uart from esphomeyaml.components.uart import UARTComponent +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_CO2, CONF_MAKE_ID, CONF_NAME, CONF_TEMPERATURE, CONF_UART_ID, \ - CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, get_variable, variable + CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, PollingComponent, get_variable, setup_component, \ + variable, Pvariable DEPENDENCIES = ['uart'] -MakeMHZ19Sensor = Application.MakeMHZ19Sensor +MakeMHZ19Sensor = Application.struct('MakeMHZ19Sensor') +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_MAKE_ID): cv.declare_variable_id(MakeMHZ19Sensor), cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent), - vol.Required(CONF_CO2): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), + 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 = None - for uart in get_variable(config[CONF_UART_ID]): + for uart_ in get_variable(config[CONF_UART_ID]): yield - rhs = App.make_mhz19_sensor(uart, config[CONF_CO2][CONF_NAME], + rhs = App.make_mhz19_sensor(uart_, config[CONF_CO2][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) mhz19 = make.Pmhz19 + Pvariable(config[CONF_ID], mhz19) sensor.setup_sensor(mhz19.Pget_co2_sensor(), make.Pmqtt, config[CONF_CO2]) if CONF_TEMPERATURE in config: sensor.register_sensor(mhz19.Pmake_temperature_sensor(config[CONF_TEMPERATURE][CONF_NAME]), config[CONF_TEMPERATURE]) + setup_component(mhz19, config) + BUILD_FLAGS = '-DUSE_MHZ19' diff --git a/esphomeyaml/components/sensor/mpu6050.py b/esphomeyaml/components/sensor/mpu6050.py index dc42c6a637..8fce2d8280 100644 --- a/esphomeyaml/components/sensor/mpu6050.py +++ b/esphomeyaml/components/sensor/mpu6050.py @@ -1,10 +1,10 @@ import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_NAME, CONF_TEMPERATURE, \ CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Pvariable +from esphomeyaml.helpers import App, PollingComponent, Pvariable, setup_component DEPENDENCIES = ['i2c'] @@ -15,24 +15,41 @@ CONF_GYRO_X = 'gyro_x' CONF_GYRO_Y = 'gyro_y' CONF_GYRO_Z = 'gyro_z' -MPU6050Component = sensor.sensor_ns.MPU6050Component -MPU6050AccelSensor = sensor.sensor_ns.MPU6050AccelSensor -MPU6050GyroSensor = sensor.sensor_ns.MPU6050GyroSensor -MPU6050TemperatureSensor = sensor.sensor_ns.MPU6050TemperatureSensor +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), - vol.Optional(CONF_ACCEL_Y): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_ACCEL_Z): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_GYRO_X): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_GYRO_Y): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_GYRO_Z): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), + 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), + })), + vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(MPU6050TemperatureSensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}), cv.has_at_least_one_key(CONF_ACCEL_X, CONF_ACCEL_Y, CONF_ACCEL_Z, - CONF_GYRO_X, CONF_GYRO_Y, CONF_GYRO_Z)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS)) def to_code(config): @@ -67,6 +84,8 @@ def to_code(config): rhs = mpu.Pmake_temperature_sensor(conf[CONF_NAME]) sensor.register_sensor(rhs, conf) + setup_component(mpu, config) + BUILD_FLAGS = '-DUSE_MPU6050' diff --git a/esphomeyaml/components/sensor/mqtt_subscribe.py b/esphomeyaml/components/sensor/mqtt_subscribe.py index 203c2595d6..e06cd456ae 100644 --- a/esphomeyaml/components/sensor/mqtt_subscribe.py +++ b/esphomeyaml/components/sensor/mqtt_subscribe.py @@ -3,29 +3,31 @@ import voluptuous as vol from esphomeyaml.components import sensor import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_QOS, CONF_TOPIC -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component, Component DEPENDENCIES = ['mqtt'] -MakeMQTTSubscribeSensor = Application.MakeMQTTSubscribeSensor -MQTTSubscribeSensor = sensor.sensor_ns.MQTTSubscribeSensor +MakeMQTTSubscribeSensor = Application.struct('MakeMQTTSubscribeSensor') +MQTTSubscribeSensor = sensor.sensor_ns.class_('MQTTSubscribeSensor', sensor.Sensor, Component) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(MQTTSubscribeSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeMQTTSubscribeSensor), vol.Required(CONF_TOPIC): cv.subscribe_topic, vol.Optional(CONF_QOS): cv.mqtt_qos, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_mqtt_subscribe_sensor(config[CONF_NAME], config[CONF_TOPIC]) make = variable(config[CONF_MAKE_ID], rhs) + subs = make.Psensor if CONF_QOS in config: - add(make.Psensor.set_qos(config[CONF_QOS])) + add(subs.set_qos(config[CONF_QOS])) - sensor.setup_sensor(make.Psensor, make.Pmqtt, config) + sensor.setup_sensor(subs, make.Pmqtt, config) + setup_component(subs, config) BUILD_FLAGS = '-DUSE_MQTT_SUBSCRIBE_SENSOR' diff --git a/esphomeyaml/components/sensor/ms5611.py b/esphomeyaml/components/sensor/ms5611.py index 9dc25ca057..de176b03db 100644 --- a/esphomeyaml/components/sensor/ms5611.py +++ b/esphomeyaml/components/sensor/ms5611.py @@ -1,22 +1,34 @@ import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor -from esphomeyaml.const import CONF_ADDRESS, CONF_MAKE_ID, CONF_NAME, CONF_PRESSURE, \ +from esphomeyaml.const import CONF_ADDRESS, CONF_ID, CONF_MAKE_ID, CONF_NAME, CONF_PRESSURE, \ CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, PollingComponent, Pvariable, add, \ + setup_component, \ + variable DEPENDENCIES = ['i2c'] -MakeMS5611Sensor = Application.MakeMS5611Sensor +MakeMS5611Sensor = Application.struct('MakeMS5611Sensor') +MS5611Component = sensor.sensor_ns.class_('MS5611Component', PollingComponent, i2c.I2CDevice) +MS5611TemperatureSensor = sensor.sensor_ns.class_('MS5611TemperatureSensor', + sensor.EmptyPollingParentSensor) +MS5611PressureSensor = sensor.sensor_ns.class_('MS5611PressureSensor', + sensor.EmptyPollingParentSensor) PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(MS5611Component), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeMS5611Sensor), vol.Optional(CONF_ADDRESS): cv.i2c_address, - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_PRESSURE): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(MS5611TemperatureSensor), + })), + vol.Required(CONF_PRESSURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(MS5611PressureSensor), + })), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -24,14 +36,17 @@ def to_code(config): config[CONF_PRESSURE][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) + ms5611 = make.Pms5611 + Pvariable(config[CONF_ID], ms5611) if CONF_ADDRESS in config: - add(make.Pms5611.set_address(config[CONF_ADDRESS])) + add(ms5611.set_address(config[CONF_ADDRESS])) - sensor.setup_sensor(make.Pms5611.Pget_temperature_sensor(), make.Pmqtt_temperature, + sensor.setup_sensor(ms5611.Pget_temperature_sensor(), make.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(make.Pms5611.Pget_pressure_sensor(), make.Pmqtt_pressure, + sensor.setup_sensor(ms5611.Pget_pressure_sensor(), make.Pmqtt_pressure, config[CONF_PRESSURE]) + setup_component(ms5611, config) BUILD_FLAGS = '-DUSE_MS5611' diff --git a/esphomeyaml/components/sensor/pmsx003.py b/esphomeyaml/components/sensor/pmsx003.py index 70e3586e87..fe4f2bc089 100644 --- a/esphomeyaml/components/sensor/pmsx003.py +++ b/esphomeyaml/components/sensor/pmsx003.py @@ -1,24 +1,26 @@ import voluptuous as vol -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, uart from esphomeyaml.components.uart import UARTComponent import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_FORMALDEHYDE, CONF_HUMIDITY, CONF_ID, CONF_NAME, CONF_PM_10_0, \ CONF_PM_1_0, CONF_PM_2_5, CONF_TEMPERATURE, CONF_TYPE, CONF_UART_ID -from esphomeyaml.helpers import App, Pvariable, get_variable +from esphomeyaml.helpers import App, Pvariable, get_variable, setup_component, Component DEPENDENCIES = ['uart'] -PMSX003Component = sensor.sensor_ns.PMSX003Component +PMSX003Component = sensor.sensor_ns.class_('PMSX003Component', uart.UARTDevice, Component) +PMSX003Sensor = sensor.sensor_ns.class_('PMSX003Sensor', sensor.Sensor) CONF_PMSX003 = 'PMSX003' CONF_PMS5003T = 'PMS5003T' CONF_PMS5003ST = 'PMS5003ST' +PMSX003Type = sensor.sensor_ns.enum('PMSX003Type') PMSX003_TYPES = { - CONF_PMSX003: sensor.sensor_ns.PMSX003_TYPE_X003, - CONF_PMS5003T: sensor.sensor_ns.PMSX003_TYPE_5003T, - CONF_PMS5003ST: sensor.sensor_ns.PMSX003_TYPE_5003ST, + CONF_PMSX003: PMSX003Type.PMSX003_TYPE_X003, + CONF_PMS5003T: PMSX003Type.PMSX003_TYPE_5003T, + CONF_PMS5003ST: PMSX003Type.PMSX003_TYPE_5003ST, } SENSORS_TO_TYPE = { @@ -38,25 +40,30 @@ def validate_pmsx003_sensors(value): return value +PMSX003_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(PMSX003Sensor), +}) + + PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(PMSX003Component), cv.GenerateID(CONF_UART_ID): cv.use_variable_id(UARTComponent), vol.Required(CONF_TYPE): vol.All(vol.Upper, cv.one_of(*PMSX003_TYPES)), - vol.Optional(CONF_PM_1_0): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_PM_2_5): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_PM_10_0): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_FORMALDEHYDE): cv.nameable(sensor.SENSOR_SCHEMA), -}), cv.has_at_least_one_key(*SENSORS_TO_TYPE)) + vol.Optional(CONF_PM_1_0): cv.nameable(PMSX003_SENSOR_SCHEMA), + vol.Optional(CONF_PM_2_5): cv.nameable(PMSX003_SENSOR_SCHEMA), + vol.Optional(CONF_PM_10_0): cv.nameable(PMSX003_SENSOR_SCHEMA), + vol.Optional(CONF_TEMPERATURE): cv.nameable(PMSX003_SENSOR_SCHEMA), + vol.Optional(CONF_HUMIDITY): cv.nameable(PMSX003_SENSOR_SCHEMA), + vol.Optional(CONF_FORMALDEHYDE): cv.nameable(PMSX003_SENSOR_SCHEMA), +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSORS_TO_TYPE)) def to_code(config): - for uart in get_variable(config[CONF_UART_ID]): + for uart_ in get_variable(config[CONF_UART_ID]): yield - rhs = App.make_pmsx003(uart, PMSX003_TYPES[config[CONF_TYPE]]) + rhs = App.make_pmsx003(uart_, PMSX003_TYPES[config[CONF_TYPE]]) pms = Pvariable(config[CONF_ID], rhs) if CONF_PM_1_0 in config: @@ -83,6 +90,8 @@ def to_code(config): conf = config[CONF_FORMALDEHYDE] sensor.register_sensor(pms.make_formaldehyde_sensor(conf[CONF_NAME]), conf) + setup_component(pms, config) + BUILD_FLAGS = '-DUSE_PMSX003' diff --git a/esphomeyaml/components/sensor/pulse_counter.py b/esphomeyaml/components/sensor/pulse_counter.py index 49bdfb68cb..b73d62123e 100644 --- a/esphomeyaml/components/sensor/pulse_counter.py +++ b/esphomeyaml/components/sensor/pulse_counter.py @@ -6,18 +6,23 @@ from esphomeyaml.components import sensor from esphomeyaml.const import CONF_COUNT_MODE, CONF_FALLING_EDGE, CONF_INTERNAL_FILTER, \ CONF_MAKE_ID, CONF_NAME, CONF_PIN, CONF_PULL_MODE, CONF_RISING_EDGE, CONF_UPDATE_INTERVAL, \ ESP_PLATFORM_ESP32 -from esphomeyaml.helpers import App, Application, add, variable, gpio_input_pin_expression +from esphomeyaml.helpers import App, Application, add, variable, gpio_input_pin_expression, \ + setup_component +PulseCounterCountMode = sensor.sensor_ns.enum('PulseCounterCountMode') COUNT_MODES = { - 'DISABLE': sensor.sensor_ns.PULSE_COUNTER_DISABLE, - 'INCREMENT': sensor.sensor_ns.PULSE_COUNTER_INCREMENT, - 'DECREMENT': sensor.sensor_ns.PULSE_COUNTER_DECREMENT, + 'DISABLE': PulseCounterCountMode.PULSE_COUNTER_DISABLE, + 'INCREMENT': PulseCounterCountMode.PULSE_COUNTER_INCREMENT, + 'DECREMENT': PulseCounterCountMode.PULSE_COUNTER_DECREMENT, } COUNT_MODE_SCHEMA = vol.All(vol.Upper, cv.one_of(*COUNT_MODES)) -MakePulseCounterSensor = Application.MakePulseCounterSensor -PulseCounterSensorComponent = sensor.sensor_ns.PulseCounterSensorComponent +PulseCounterBase = sensor.sensor_ns.class_('PulseCounterBase') +MakePulseCounterSensor = Application.struct('MakePulseCounterSensor') +PulseCounterSensorComponent = sensor.sensor_ns.class_('PulseCounterSensorComponent', + sensor.PollingSensorComponent, + PulseCounterBase) def validate_internal_filter(value): @@ -46,24 +51,26 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_PULL_MODE): cv.invalid("The pull_mode option has been removed in 1.7.0, " "please use the pin mode schema now.") -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - pin = None for pin in gpio_input_pin_expression(config[CONF_PIN]): yield rhs = App.make_pulse_counter_sensor(config[CONF_NAME], pin, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) pcnt = make.Ppcnt + if CONF_COUNT_MODE in config: rising_edge = COUNT_MODES[config[CONF_COUNT_MODE][CONF_RISING_EDGE]] falling_edge = COUNT_MODES[config[CONF_COUNT_MODE][CONF_FALLING_EDGE]] add(pcnt.set_edge_mode(rising_edge, falling_edge)) if CONF_INTERNAL_FILTER in config: add(pcnt.set_filter_us(config[CONF_INTERNAL_FILTER])) - sensor.setup_sensor(make.Ppcnt, make.Pmqtt, config) + + sensor.setup_sensor(pcnt, make.Pmqtt, config) + setup_component(pcnt, config) BUILD_FLAGS = '-DUSE_PULSE_COUNTER_SENSOR' diff --git a/esphomeyaml/components/sensor/rotary_encoder.py b/esphomeyaml/components/sensor/rotary_encoder.py index 595d398bb8..1be3d44803 100644 --- a/esphomeyaml/components/sensor/rotary_encoder.py +++ b/esphomeyaml/components/sensor/rotary_encoder.py @@ -4,20 +4,22 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.components import sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_RESOLUTION -from esphomeyaml.helpers import App, Application, add, gpio_input_pin_expression, variable +from esphomeyaml.helpers import App, Application, add, gpio_input_pin_expression, variable, \ + setup_component, Component +RotaryEncoderResolution = sensor.sensor_ns.enum('RotaryEncoderResolution') RESOLUTIONS = { - '1': sensor.sensor_ns.ROTARY_ENCODER_1_PULSE_PER_CYCLE, - '2': sensor.sensor_ns.ROTARY_ENCODER_2_PULSES_PER_CYCLE, - '4': sensor.sensor_ns.ROTARY_ENCODER_4_PULSES_PER_CYCLE, + '1': RotaryEncoderResolution.ROTARY_ENCODER_1_PULSE_PER_CYCLE, + '2': RotaryEncoderResolution.ROTARY_ENCODER_2_PULSES_PER_CYCLE, + '4': RotaryEncoderResolution.ROTARY_ENCODER_4_PULSES_PER_CYCLE, } CONF_PIN_A = 'pin_a' CONF_PIN_B = 'pin_b' CONF_PIN_RESET = 'pin_reset' -MakeRotaryEncoderSensor = Application.MakeRotaryEncoderSensor -RotaryEncoderSensor = sensor.sensor_ns.RotaryEncoderSensor +MakeRotaryEncoderSensor = Application.struct('MakeRotaryEncoderSensor') +RotaryEncoderSensor = sensor.sensor_ns.class_('RotaryEncoderSensor', sensor.Sensor, Component) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(RotaryEncoderSensor), @@ -26,19 +28,18 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ vol.Required(CONF_PIN_B): pins.internal_gpio_input_pin_schema, vol.Optional(CONF_PIN_RESET): pins.internal_gpio_input_pin_schema, vol.Optional(CONF_RESOLUTION): vol.All(cv.string, cv.one_of(*RESOLUTIONS)), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - pin_a = None for pin_a in gpio_input_pin_expression(config[CONF_PIN_A]): yield - pin_b = None for pin_b in gpio_input_pin_expression(config[CONF_PIN_B]): yield rhs = App.make_rotary_encoder_sensor(config[CONF_NAME], pin_a, pin_b) make = variable(config[CONF_MAKE_ID], rhs) encoder = make.Protary_encoder + if CONF_PIN_RESET in config: pin_i = None for pin_i in gpio_input_pin_expression(config[CONF_PIN_RESET]): @@ -47,7 +48,9 @@ def to_code(config): if CONF_RESOLUTION in config: resolution = RESOLUTIONS[config[CONF_RESOLUTION]] add(encoder.set_resolution(resolution)) + sensor.setup_sensor(encoder, make.Pmqtt, config) + setup_component(encoder, config) BUILD_FLAGS = '-DUSE_ROTARY_ENCODER_SENSOR' diff --git a/esphomeyaml/components/sensor/sht3xd.py b/esphomeyaml/components/sensor/sht3xd.py index 25688c31e1..ea6b81e75d 100644 --- a/esphomeyaml/components/sensor/sht3xd.py +++ b/esphomeyaml/components/sensor/sht3xd.py @@ -1,25 +1,33 @@ import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor -from esphomeyaml.const import CONF_ACCURACY, CONF_ADDRESS, CONF_HUMIDITY, CONF_MAKE_ID, CONF_NAME, \ - CONF_TEMPERATURE, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.const import CONF_ADDRESS, CONF_HUMIDITY, CONF_MAKE_ID, CONF_NAME, \ + CONF_TEMPERATURE, CONF_UPDATE_INTERVAL, CONF_ID +from esphomeyaml.helpers import App, Application, PollingComponent, setup_component, variable, \ + Pvariable DEPENDENCIES = ['i2c'] -MakeSHT3XDSensor = Application.MakeSHT3XDSensor +MakeSHT3XDSensor = Application.struct('MakeSHT3XDSensor') +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), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeSHT3XDSensor), - vol.Required(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Required(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), + 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, - - vol.Optional(CONF_ACCURACY): cv.invalid("The accuracy option has been removed and now " - "defaults to HIGH."), -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -27,12 +35,15 @@ def to_code(config): config[CONF_HUMIDITY][CONF_NAME], config[CONF_ADDRESS], config.get(CONF_UPDATE_INTERVAL)) - sht3xd = variable(config[CONF_MAKE_ID], rhs) + make = variable(config[CONF_MAKE_ID], rhs) + sht3xd = make.Psht3xd + Pvariable(config[CONF_ID], sht3xd) - sensor.setup_sensor(sht3xd.Psht3xd.Pget_temperature_sensor(), sht3xd.Pmqtt_temperature, + sensor.setup_sensor(sht3xd.Pget_temperature_sensor(), make.Pmqtt_temperature, config[CONF_TEMPERATURE]) - sensor.setup_sensor(sht3xd.Psht3xd.Pget_humidity_sensor(), sht3xd.Pmqtt_humidity, + sensor.setup_sensor(sht3xd.Pget_humidity_sensor(), make.Pmqtt_humidity, config[CONF_HUMIDITY]) + setup_component(sht3xd, config) BUILD_FLAGS = '-DUSE_SHT3XD' diff --git a/esphomeyaml/components/sensor/tcs34725.py b/esphomeyaml/components/sensor/tcs34725.py index 1e2ac88ac0..8322e1f054 100644 --- a/esphomeyaml/components/sensor/tcs34725.py +++ b/esphomeyaml/components/sensor/tcs34725.py @@ -1,11 +1,11 @@ # coding=utf-8 import voluptuous as vol +from esphomeyaml.components import i2c, sensor import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ADDRESS, CONF_COLOR_TEMPERATURE, CONF_GAIN, CONF_ID, \ CONF_ILLUMINANCE, CONF_INTEGRATION_TIME, CONF_NAME, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Pvariable, add +from esphomeyaml.helpers import App, PollingComponent, Pvariable, add, setup_component DEPENDENCIES = ['i2c'] @@ -14,38 +14,58 @@ CONF_GREEN_CHANNEL = 'green_channel' CONF_BLUE_CHANNEL = 'blue_channel' CONF_CLEAR_CHANNEL = 'clear_channel' -TCS34725Component = sensor.sensor_ns.TCS34725Component +TCS34725Component = sensor.sensor_ns.class_('TCS34725Component', PollingComponent, + i2c.I2CDevice) +TCS34725IntegrationTime = sensor.sensor_ns.enum('TCS34725IntegrationTime') TCS34725_INTEGRATION_TIMES = { - '2.4ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_2_4MS, - '24ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_24MS, - '50ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_50MS, - '101ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_101MS, - '154ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_154MS, - '700ms': sensor.sensor_ns.TCS34725_INTEGRATION_TIME_700MS, + '2.4ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_2_4MS, + '24ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_24MS, + '50ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_50MS, + '101ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_101MS, + '154ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_154MS, + '700ms': TCS34725IntegrationTime.TCS34725_INTEGRATION_TIME_700MS, } +TCS34725Gain = sensor.sensor_ns.enum('TCS34725Gain') TCS34725_GAINS = { - '1X': sensor.sensor_ns.TCS34725_GAIN_1X, - '4X': sensor.sensor_ns.TCS34725_GAIN_4X, - '16X': sensor.sensor_ns.TCS34725_GAIN_16X, - '60X': sensor.sensor_ns.TCS34725_GAIN_60X, + '1X': TCS34725Gain.TCS34725_GAIN_1X, + '4X': TCS34725Gain.TCS34725_GAIN_4X, + '16X': TCS34725Gain.TCS34725_GAIN_16X, + '60X': TCS34725Gain.TCS34725_GAIN_60X, } +TCS35725IlluminanceSensor = sensor.sensor_ns.class_('TCS35725IlluminanceSensor', + sensor.EmptyPollingParentSensor) +TCS35725ColorTemperatureSensor = sensor.sensor_ns.class_('TCS35725ColorTemperatureSensor', + sensor.EmptyPollingParentSensor) +TCS35725ColorChannelSensor = sensor.sensor_ns.class_('TCS35725ColorChannelSensor', + sensor.EmptyPollingParentSensor) + +COLOR_CHANNEL_SENSOR_SCHEMA = sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(TCS35725ColorChannelSensor), +}) + +SENSOR_KEYS = [CONF_RED_CHANNEL, CONF_GREEN_CHANNEL, CONF_BLUE_CHANNEL, + CONF_CLEAR_CHANNEL, CONF_ILLUMINANCE, CONF_COLOR_TEMPERATURE] + PLATFORM_SCHEMA = vol.All(sensor.PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TCS34725Component), vol.Optional(CONF_ADDRESS): cv.i2c_address, - vol.Optional(CONF_RED_CHANNEL): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_GREEN_CHANNEL): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_BLUE_CHANNEL): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_CLEAR_CHANNEL): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_ILLUMINANCE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_COLOR_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_RED_CHANNEL): cv.nameable(COLOR_CHANNEL_SENSOR_SCHEMA), + vol.Optional(CONF_GREEN_CHANNEL): cv.nameable(COLOR_CHANNEL_SENSOR_SCHEMA), + vol.Optional(CONF_BLUE_CHANNEL): cv.nameable(COLOR_CHANNEL_SENSOR_SCHEMA), + vol.Optional(CONF_CLEAR_CHANNEL): cv.nameable(COLOR_CHANNEL_SENSOR_SCHEMA), + vol.Optional(CONF_ILLUMINANCE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(TCS35725IlluminanceSensor), + })), + vol.Optional(CONF_COLOR_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_variable_id(TCS35725ColorTemperatureSensor), + })), vol.Optional(CONF_INTEGRATION_TIME): cv.one_of(*TCS34725_INTEGRATION_TIMES), vol.Optional(CONF_GAIN): vol.All(vol.Upper, cv.one_of(*TCS34725_GAINS)), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -}), cv.has_at_least_one_key(CONF_RED_CHANNEL, CONF_GREEN_CHANNEL, CONF_BLUE_CHANNEL, - CONF_CLEAR_CHANNEL, CONF_ILLUMINANCE, CONF_COLOR_TEMPERATURE)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(*SENSOR_KEYS)) def to_code(config): @@ -76,6 +96,8 @@ def to_code(config): conf = config[CONF_COLOR_TEMPERATURE] sensor.register_sensor(tcs.Pmake_color_temperature_sensor(conf[CONF_NAME]), conf) + setup_component(tcs, config) + BUILD_FLAGS = '-DUSE_TCS34725' diff --git a/esphomeyaml/components/sensor/template.py b/esphomeyaml/components/sensor/template.py index 5f090fb5b0..d34f96da5d 100644 --- a/esphomeyaml/components/sensor/template.py +++ b/esphomeyaml/components/sensor/template.py @@ -3,29 +3,32 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, process_lambda, variable, Application, float_, optional, add +from esphomeyaml.helpers import App, process_lambda, variable, Application, float_, optional, add, \ + setup_component -MakeTemplateSensor = Application.MakeTemplateSensor -TemplateSensor = sensor.sensor_ns.TemplateSensor +MakeTemplateSensor = Application.struct('MakeTemplateSensor') +TemplateSensor = sensor.sensor_ns.class_('TemplateSensor', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TemplateSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateSensor), vol.Required(CONF_LAMBDA): cv.lambda_, vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_template_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(make.Ptemplate_, make.Pmqtt, config) + template = make.Ptemplate_ + + sensor.setup_sensor(template, make.Pmqtt, config) + setup_component(template, config) - template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(float_)): yield - add(make.Ptemplate_.set_template(template_)) + add(template.set_template(template_)) BUILD_FLAGS = '-DUSE_TEMPLATE_SENSOR' diff --git a/esphomeyaml/components/sensor/total_daily_energy.py b/esphomeyaml/components/sensor/total_daily_energy.py index 644aa7d656..86f4678a2b 100644 --- a/esphomeyaml/components/sensor/total_daily_energy.py +++ b/esphomeyaml/components/sensor/total_daily_energy.py @@ -1,33 +1,35 @@ import voluptuous as vol -from esphomeyaml.components import sensor -from esphomeyaml.components.time import sntp +from esphomeyaml.components import sensor, time import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_TIME_ID -from esphomeyaml.helpers import App, Application, get_variable, variable +from esphomeyaml.helpers import App, Application, Component, get_variable, setup_component, variable DEPENDENCIES = ['time'] CONF_POWER_ID = 'power_id' -MakeTotalDailyEnergySensor = Application.MakeTotalDailyEnergySensor -TotalDailyEnergy = sensor.sensor_ns.TotalDailyEnergy +MakeTotalDailyEnergySensor = Application.struct('MakeTotalDailyEnergySensor') +TotalDailyEnergy = sensor.sensor_ns.class_('TotalDailyEnergy', sensor.Sensor, Component) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TotalDailyEnergy), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTotalDailyEnergySensor), - cv.GenerateID(CONF_TIME_ID): cv.use_variable_id(sntp.SNTPComponent), - vol.Required(CONF_POWER_ID): cv.use_variable_id(None), -})) + cv.GenerateID(CONF_TIME_ID): cv.use_variable_id(time.RealTimeClockComponent), + vol.Required(CONF_POWER_ID): cv.use_variable_id(sensor.Sensor), +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - for time in get_variable(config[CONF_TIME_ID]): + for time_ in get_variable(config[CONF_TIME_ID]): yield for sens in get_variable(config[CONF_POWER_ID]): yield - rhs = App.make_total_daily_energy_sensor(config[CONF_NAME], time, sens) + rhs = App.make_total_daily_energy_sensor(config[CONF_NAME], time_, sens) make = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(make.Ptotal_energy, make.Pmqtt, config) + total_energy = make.Ptotal_energy + + sensor.setup_sensor(total_energy, make.Pmqtt, config) + setup_component(total_energy, config) BUILD_FLAGS = '-DUSE_TOTAL_DAILY_ENERGY_SENSOR' diff --git a/esphomeyaml/components/sensor/tsl2561.py b/esphomeyaml/components/sensor/tsl2561.py index 3bf94eb0ce..53112efc46 100644 --- a/esphomeyaml/components/sensor/tsl2561.py +++ b/esphomeyaml/components/sensor/tsl2561.py @@ -1,21 +1,24 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import sensor, i2c from esphomeyaml.const import CONF_ADDRESS, CONF_GAIN, CONF_INTEGRATION_TIME, CONF_MAKE_ID, \ CONF_NAME, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component DEPENDENCIES = ['i2c'] +TSL2561IntegrationTime = sensor.sensor_ns.enum('TSL2561IntegrationTime') INTEGRATION_TIMES = { - 14: sensor.sensor_ns.TSL2561_INTEGRATION_14MS, - 101: sensor.sensor_ns.TSL2561_INTEGRATION_101MS, - 402: sensor.sensor_ns.TSL2561_INTEGRATION_402MS, + 14: TSL2561IntegrationTime.TSL2561_INTEGRATION_14MS, + 101: TSL2561IntegrationTime.TSL2561_INTEGRATION_101MS, + 402: TSL2561IntegrationTime.TSL2561_INTEGRATION_402MS, } + +TSL2561Gain = sensor.sensor_ns.enum('TSL2561Gain') GAINS = { - '1X': sensor.sensor_ns.TSL2561_GAIN_1X, - '16X': sensor.sensor_ns.TSL2561_GAIN_16X, + '1X': TSL2561Gain.TSL2561_GAIN_1X, + '16X': TSL2561Gain.TSL2561_GAIN_16X, } CONF_IS_CS_PACKAGE = 'is_cs_package' @@ -28,8 +31,9 @@ def validate_integration_time(value): return value -MakeTSL2561Sensor = Application.MakeTSL2561Sensor -TSL2561Sensor = Application.TSL2561Sensor +MakeTSL2561Sensor = Application.struct('MakeTSL2561Sensor') +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), @@ -39,7 +43,7 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_GAIN): vol.All(vol.Upper, cv.one_of(*GAINS)), 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): @@ -47,13 +51,16 @@ def to_code(config): config.get(CONF_UPDATE_INTERVAL)) make_tsl = variable(config[CONF_MAKE_ID], rhs) tsl2561 = make_tsl.Ptsl2561 + if CONF_INTEGRATION_TIME in config: add(tsl2561.set_integration_time(INTEGRATION_TIMES[config[CONF_INTEGRATION_TIME]])) if CONF_GAIN in config: add(tsl2561.set_gain(GAINS[config[CONF_GAIN]])) if CONF_IS_CS_PACKAGE in config: add(tsl2561.set_is_cs_package(config[CONF_IS_CS_PACKAGE])) + sensor.setup_sensor(tsl2561, make_tsl.Pmqtt, config) + setup_component(tsl2561, config) BUILD_FLAGS = '-DUSE_TSL2561' diff --git a/esphomeyaml/components/sensor/ultrasonic.py b/esphomeyaml/components/sensor/ultrasonic.py index 61794f431d..d12c4419fd 100644 --- a/esphomeyaml/components/sensor/ultrasonic.py +++ b/esphomeyaml/components/sensor/ultrasonic.py @@ -6,10 +6,11 @@ from esphomeyaml.components import sensor from esphomeyaml.const import CONF_ECHO_PIN, CONF_MAKE_ID, CONF_NAME, CONF_TIMEOUT_METER, \ CONF_TIMEOUT_TIME, CONF_TRIGGER_PIN, CONF_UPDATE_INTERVAL from esphomeyaml.helpers import App, Application, add, gpio_input_pin_expression, \ - gpio_output_pin_expression, variable + gpio_output_pin_expression, variable, setup_component -MakeUltrasonicSensor = Application.MakeUltrasonicSensor -UltrasonicSensorComponent = sensor.sensor_ns.UltrasonicSensorComponent +MakeUltrasonicSensor = Application.struct('MakeUltrasonicSensor') +UltrasonicSensorComponent = sensor.sensor_ns.class_('UltrasonicSensorComponent', + sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(UltrasonicSensorComponent), @@ -23,21 +24,22 @@ PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ def to_code(config): - trigger = None for trigger in gpio_output_pin_expression(config[CONF_TRIGGER_PIN]): yield - echo = None for echo in gpio_input_pin_expression(config[CONF_ECHO_PIN]): yield rhs = App.make_ultrasonic_sensor(config[CONF_NAME], trigger, echo, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) ultrasonic = make.Pultrasonic + if CONF_TIMEOUT_TIME in config: add(ultrasonic.set_timeout_us(config[CONF_TIMEOUT_TIME])) elif CONF_TIMEOUT_METER in config: add(ultrasonic.set_timeout_m(config[CONF_TIMEOUT_METER])) + sensor.setup_sensor(ultrasonic, make.Pmqtt, config) + setup_component(ultrasonic, config) BUILD_FLAGS = '-DUSE_ULTRASONIC_SENSOR' diff --git a/esphomeyaml/components/sensor/uptime.py b/esphomeyaml/components/sensor/uptime.py index 38548c1db3..60bf9a990b 100644 --- a/esphomeyaml/components/sensor/uptime.py +++ b/esphomeyaml/components/sensor/uptime.py @@ -3,22 +3,25 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.helpers import App, Application, variable, setup_component -MakeUptimeSensor = Application.MakeUptimeSensor -UptimeSensor = sensor.sensor_ns.UptimeSensor +MakeUptimeSensor = Application.struct('MakeUptimeSensor') +UptimeSensor = sensor.sensor_ns.class_('UptimeSensor', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(UptimeSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeUptimeSensor), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_uptime_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(make.Puptime, make.Pmqtt, config) + uptime = make.Puptime + + sensor.setup_sensor(uptime, make.Pmqtt, config) + setup_component(uptime, config) BUILD_FLAGS = '-DUSE_UPTIME_SENSOR' diff --git a/esphomeyaml/components/sensor/wifi_signal.py b/esphomeyaml/components/sensor/wifi_signal.py index 060377839e..564824b3aa 100644 --- a/esphomeyaml/components/sensor/wifi_signal.py +++ b/esphomeyaml/components/sensor/wifi_signal.py @@ -3,22 +3,25 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import sensor from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.helpers import App, Application, variable, setup_component -MakeWiFiSignalSensor = Application.MakeWiFiSignalSensor -WiFiSignalSensor = sensor.sensor_ns.WiFiSignalSensor +MakeWiFiSignalSensor = Application.struct('MakeWiFiSignalSensor') +WiFiSignalSensor = sensor.sensor_ns.class_('WiFiSignalSensor', sensor.PollingSensorComponent) PLATFORM_SCHEMA = cv.nameable(sensor.SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(WiFiSignalSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeWiFiSignalSensor), vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_wifi_signal_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - sensor.setup_sensor(make.Pwifi, make.Pmqtt, config) + wifi = make.Pwifi + + sensor.setup_sensor(wifi, make.Pmqtt, config) + setup_component(wifi, config) BUILD_FLAGS = '-DUSE_WIFI_SIGNAL_SENSOR' diff --git a/esphomeyaml/components/sensor/xiaomi_miflora.py b/esphomeyaml/components/sensor/xiaomi_miflora.py index 888a5c7cf3..6d02fe7e84 100644 --- a/esphomeyaml/components/sensor/xiaomi_miflora.py +++ b/esphomeyaml/components/sensor/xiaomi_miflora.py @@ -1,35 +1,32 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import esp32_ble_tracker, sensor from esphomeyaml.components.esp32_ble_tracker import CONF_ESP32_BLE_ID, ESP32BLETracker, \ make_address_array -from esphomeyaml.const import CONF_BATTERY_LEVEL, CONF_CONDUCTIVITY, CONF_ILLUMINANCE, \ - CONF_MAC_ADDRESS, CONF_MAKE_ID, CONF_MOISTURE, CONF_NAME, CONF_TEMPERATURE -from esphomeyaml.helpers import Pvariable, esphomelib_ns, get_variable +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_BATTERY_LEVEL, CONF_CONDUCTIVITY, CONF_ID, CONF_ILLUMINANCE, \ + CONF_MAC_ADDRESS, CONF_MOISTURE, CONF_NAME, CONF_TEMPERATURE +from esphomeyaml.helpers import Pvariable, get_variable DEPENDENCIES = ['esp32_ble_tracker'] -XiaomiDevice = esphomelib_ns.XiaomiDevice - PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ - cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(XiaomiDevice), + cv.GenerateID(): cv.declare_variable_id(esp32_ble_tracker.XiaomiDevice), cv.GenerateID(CONF_ESP32_BLE_ID): cv.use_variable_id(ESP32BLETracker), vol.Required(CONF_MAC_ADDRESS): cv.mac_address, - vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_MOISTURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_ILLUMINANCE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_CONDUCTIVITY): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_BATTERY_LEVEL): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_TEMPERATURE): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_MOISTURE): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_ILLUMINANCE): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_CONDUCTIVITY): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_BATTERY_LEVEL): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), }) def to_code(config): - hub = None for hub in get_variable(config[CONF_ESP32_BLE_ID]): yield rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS])) - dev = Pvariable(config[CONF_MAKE_ID], rhs) + dev = Pvariable(config[CONF_ID], rhs) if CONF_TEMPERATURE in config: conf = config[CONF_TEMPERATURE] sensor.register_sensor(dev.Pmake_temperature_sensor(conf[CONF_NAME]), conf) diff --git a/esphomeyaml/components/sensor/xiaomi_mijia.py b/esphomeyaml/components/sensor/xiaomi_mijia.py index 50ea4dc7a8..10a6573d6f 100644 --- a/esphomeyaml/components/sensor/xiaomi_mijia.py +++ b/esphomeyaml/components/sensor/xiaomi_mijia.py @@ -1,29 +1,26 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv -from esphomeyaml.components import sensor +from esphomeyaml.components import esp32_ble_tracker, sensor from esphomeyaml.components.esp32_ble_tracker import CONF_ESP32_BLE_ID, ESP32BLETracker, \ make_address_array +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_BATTERY_LEVEL, CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_MAKE_ID, \ CONF_NAME, CONF_TEMPERATURE -from esphomeyaml.helpers import Pvariable, esphomelib_ns, get_variable +from esphomeyaml.helpers import Pvariable, get_variable DEPENDENCIES = ['esp32_ble_tracker'] -XiaomiDevice = esphomelib_ns.XiaomiDevice - PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ - cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(XiaomiDevice), + cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(esp32_ble_tracker.XiaomiDevice), cv.GenerateID(CONF_ESP32_BLE_ID): cv.use_variable_id(ESP32BLETracker), vol.Required(CONF_MAC_ADDRESS): cv.mac_address, - vol.Optional(CONF_TEMPERATURE): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_HUMIDITY): cv.nameable(sensor.SENSOR_SCHEMA), - vol.Optional(CONF_BATTERY_LEVEL): cv.nameable(sensor.SENSOR_SCHEMA), + vol.Optional(CONF_TEMPERATURE): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_HUMIDITY): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), + vol.Optional(CONF_BATTERY_LEVEL): cv.nameable(esp32_ble_tracker.XIAOMI_SENSOR_SCHEMA), }) def to_code(config): - hub = None for hub in get_variable(config[CONF_ESP32_BLE_ID]): yield rhs = hub.make_xiaomi_device(make_address_array(config[CONF_MAC_ADDRESS])) diff --git a/esphomeyaml/components/spi.py b/esphomeyaml/components/spi.py index 116418e349..c9c98eae09 100644 --- a/esphomeyaml/components/spi.py +++ b/esphomeyaml/components/spi.py @@ -4,9 +4,10 @@ import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.const import CONF_CLK_PIN, CONF_ID, CONF_MISO_PIN, CONF_MOSI_PIN from esphomeyaml.helpers import App, Pvariable, esphomelib_ns, gpio_input_pin_expression, \ - gpio_output_pin_expression, add + gpio_output_pin_expression, add, setup_component, Component -SPIComponent = esphomelib_ns.SPIComponent +SPIComponent = esphomelib_ns.class_('SPIComponent', Component) +SPIDevice = esphomelib_ns.class_('SPIDevice') SPI_SCHEMA = vol.All(vol.Schema({ cv.GenerateID(): cv.declare_variable_id(SPIComponent), @@ -20,7 +21,6 @@ CONFIG_SCHEMA = vol.All(cv.ensure_list, [SPI_SCHEMA]) def to_code(config): for conf in config: - clk = None for clk in gpio_output_pin_expression(conf[CONF_CLK_PIN]): yield rhs = App.init_spi(clk) @@ -34,5 +34,7 @@ def to_code(config): yield add(spi.set_mosi(mosi)) + setup_component(spi, conf) + BUILD_FLAGS = '-DUSE_SPI' diff --git a/esphomeyaml/components/status_led.py b/esphomeyaml/components/status_led.py index bf24906bb7..e67621c0ee 100644 --- a/esphomeyaml/components/status_led.py +++ b/esphomeyaml/components/status_led.py @@ -2,22 +2,24 @@ import voluptuous as vol from esphomeyaml import config_validation as cv, pins from esphomeyaml.const import CONF_ID, CONF_PIN -from esphomeyaml.helpers import App, Pvariable, esphomelib_ns, gpio_output_pin_expression +from esphomeyaml.helpers import App, Pvariable, esphomelib_ns, gpio_output_pin_expression, \ + setup_component, Component -StatusLEDComponent = esphomelib_ns.StatusLEDComponent +StatusLEDComponent = esphomelib_ns.class_('StatusLEDComponent', Component) CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(StatusLEDComponent), vol.Optional(CONF_PIN): pins.gpio_output_pin_schema, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): - pin = None for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_status_led(pin) - Pvariable(config[CONF_ID], rhs) + var = Pvariable(config[CONF_ID], rhs) + + setup_component(var, config) BUILD_FLAGS = '-DUSE_STATUS_LED' diff --git a/esphomeyaml/components/stepper/__init__.py b/esphomeyaml/components/stepper/__init__.py index 3b5092d5cd..ac2ce911ac 100644 --- a/esphomeyaml/components/stepper/__init__.py +++ b/esphomeyaml/components/stepper/__init__.py @@ -5,7 +5,7 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ACCELERATION, CONF_DECELERATION, CONF_ID, CONF_MAX_SPEED, \ CONF_POSITION, CONF_TARGET from esphomeyaml.helpers import Pvariable, TemplateArguments, add, add_job, esphomelib_ns, \ - get_variable, int32, templatable + get_variable, int32, templatable, Action PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -13,10 +13,10 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ # pylint: disable=invalid-name stepper_ns = esphomelib_ns.namespace('stepper') -Stepper = stepper_ns.Stepper +Stepper = stepper_ns.class_('Stepper') -SetTargetAction = stepper_ns.SetTargetAction -ReportPositionAction = stepper_ns.ReportPositionAction +SetTargetAction = stepper_ns.class_('SetTargetAction', Action) +ReportPositionAction = stepper_ns.class_('ReportPositionAction', Action) def validate_acceleration(value): @@ -60,7 +60,6 @@ def validate_speed(value): STEPPER_SCHEMA = vol.Schema({ - cv.GenerateID(): cv.declare_variable_id(Stepper), vol.Required(CONF_MAX_SPEED): validate_speed, vol.Optional(CONF_ACCELERATION): validate_acceleration, vol.Optional(CONF_DECELERATION): validate_acceleration, @@ -86,7 +85,7 @@ BUILD_FLAGS = '-DUSE_STEPPER' CONF_STEPPER_SET_TARGET = 'stepper.set_target' STEPPER_SET_TARGET_ACTION_SCHEMA = vol.Schema({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Stepper), vol.Required(CONF_TARGET): cv.templatable(cv.int_), }) @@ -107,7 +106,7 @@ def stepper_set_target_to_code(config, action_id, arg_type): CONF_STEPPER_REPORT_POSITION = 'stepper.report_position' STEPPER_REPORT_POSITION_ACTION_SCHEMA = vol.Schema({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Stepper), vol.Required(CONF_POSITION): cv.templatable(cv.int_), }) diff --git a/esphomeyaml/components/stepper/a4988.py b/esphomeyaml/components/stepper/a4988.py index e14d4a4a5b..281ede1110 100644 --- a/esphomeyaml/components/stepper/a4988.py +++ b/esphomeyaml/components/stepper/a4988.py @@ -4,16 +4,17 @@ from esphomeyaml import pins from esphomeyaml.components import stepper import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_DIR_PIN, CONF_ID, CONF_SLEEP_PIN, CONF_STEP_PIN -from esphomeyaml.helpers import App, Pvariable, add, gpio_output_pin_expression +from esphomeyaml.helpers import App, Pvariable, add, gpio_output_pin_expression, setup_component, \ + Component -A4988 = stepper.stepper_ns.A4988 +A4988 = stepper.stepper_ns.class_('A4988', stepper.Stepper, Component) PLATFORM_SCHEMA = stepper.STEPPER_PLATFORM_SCHEMA.extend({ vol.Required(CONF_ID): cv.declare_variable_id(A4988), vol.Required(CONF_STEP_PIN): pins.gpio_output_pin_schema, vol.Required(CONF_DIR_PIN): pins.gpio_output_pin_schema, vol.Optional(CONF_SLEEP_PIN): pins.gpio_output_pin_schema, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -30,6 +31,7 @@ def to_code(config): add(a4988.set_sleep_pin(sleep_pin)) stepper.setup_stepper(a4988, config) + setup_component(a4988, config) BUILD_FLAGS = '-DUSE_A4988' diff --git a/esphomeyaml/components/switch/__init__.py b/esphomeyaml/components/switch/__init__.py index 65e650f44e..d1f36cdba8 100644 --- a/esphomeyaml/components/switch/__init__.py +++ b/esphomeyaml/components/switch/__init__.py @@ -6,21 +6,21 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ICON, CONF_ID, CONF_INVERTED, CONF_MQTT_ID, CONF_INTERNAL, \ CONF_OPTIMISTIC from esphomeyaml.helpers import App, Pvariable, add, esphomelib_ns, setup_mqtt_component, \ - TemplateArguments, get_variable + TemplateArguments, get_variable, Nameable, Action PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) switch_ns = esphomelib_ns.namespace('switch_') -Switch = switch_ns.Switch -MQTTSwitchComponent = switch_ns.MQTTSwitchComponent -ToggleAction = switch_ns.ToggleAction -TurnOffAction = switch_ns.TurnOffAction -TurnOnAction = switch_ns.TurnOnAction +Switch = switch_ns.class_('Switch', Nameable) +MQTTSwitchComponent = switch_ns.class_('MQTTSwitchComponent', mqtt.MQTTComponent) + +ToggleAction = switch_ns.class_('ToggleAction', Action) +TurnOffAction = switch_ns.class_('TurnOffAction', Action) +TurnOnAction = switch_ns.class_('TurnOnAction', Action) SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ - cv.GenerateID(): cv.declare_variable_id(Switch), cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTSwitchComponent), vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_INVERTED): cv.boolean, @@ -58,7 +58,7 @@ BUILD_FLAGS = '-DUSE_SWITCH' CONF_SWITCH_TOGGLE = 'switch.toggle' SWITCH_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Switch), }) @@ -74,7 +74,7 @@ def switch_toggle_to_code(config, action_id, arg_type): CONF_SWITCH_TURN_OFF = 'switch.turn_off' SWITCH_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Switch), }) @@ -90,7 +90,7 @@ def switch_turn_off_to_code(config, action_id, arg_type): CONF_SWITCH_TURN_ON = 'switch.turn_on' SWITCH_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ - vol.Required(CONF_ID): cv.use_variable_id(None), + vol.Required(CONF_ID): cv.use_variable_id(Switch), }) diff --git a/esphomeyaml/components/switch/gpio.py b/esphomeyaml/components/switch/gpio.py index 6ad76e9bd6..039bfcb4cb 100644 --- a/esphomeyaml/components/switch/gpio.py +++ b/esphomeyaml/components/switch/gpio.py @@ -4,26 +4,28 @@ from esphomeyaml import pins from esphomeyaml.components import switch import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_PIN -from esphomeyaml.helpers import App, Application, gpio_output_pin_expression, variable +from esphomeyaml.helpers import App, Application, gpio_output_pin_expression, variable, \ + setup_component, Component -MakeGPIOSwitch = Application.MakeGPIOSwitch -GPIOSwitch = switch.switch_ns.GPIOSwitch +MakeGPIOSwitch = Application.struct('MakeGPIOSwitch') +GPIOSwitch = switch.switch_ns.class_('GPIOSwitch', switch.Switch, Component) PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(GPIOSwitch), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeGPIOSwitch), vol.Required(CONF_PIN): pins.gpio_output_pin_schema, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - pin = None for pin in gpio_output_pin_expression(config[CONF_PIN]): yield rhs = App.make_gpio_switch(config[CONF_NAME], pin) - gpio = variable(config[CONF_MAKE_ID], rhs) + make = variable(config[CONF_MAKE_ID], rhs) + gpio = make.Pswitch_ - switch.setup_switch(gpio.Pswitch_, gpio.Pmqtt, config) + switch.setup_switch(gpio, make.Pmqtt, config) + setup_component(gpio, config) BUILD_FLAGS = '-DUSE_GPIO_SWITCH' diff --git a/esphomeyaml/components/switch/output.py b/esphomeyaml/components/switch/output.py index bc5d9b6d7e..4616c2127e 100644 --- a/esphomeyaml/components/switch/output.py +++ b/esphomeyaml/components/switch/output.py @@ -1,27 +1,29 @@ import voluptuous as vol +from esphomeyaml.components import output, switch import esphomeyaml.config_validation as cv -from esphomeyaml.components import switch from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_OUTPUT -from esphomeyaml.helpers import App, Application, get_variable, variable +from esphomeyaml.helpers import App, Application, Component, get_variable, setup_component, variable -MakeOutputSwitch = Application.MakeOutputSwitch -OutputSwitch = switch.switch_ns.OutputSwitch +MakeOutputSwitch = Application.struct('MakeOutputSwitch') +OutputSwitch = switch.switch_ns.class_('OutputSwitch', switch.Switch, Component) PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(OutputSwitch), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeOutputSwitch), - vol.Required(CONF_OUTPUT): cv.use_variable_id(None), -})) + vol.Required(CONF_OUTPUT): cv.use_variable_id(output.BinaryOutput), +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): - output = None - for output in get_variable(config[CONF_OUTPUT]): + for output_ in get_variable(config[CONF_OUTPUT]): yield - rhs = App.make_output_switch(config[CONF_NAME], output) - gpio = variable(config[CONF_MAKE_ID], rhs) - switch.setup_switch(gpio.Pswitch_, gpio.Pmqtt, config) + rhs = App.make_output_switch(config[CONF_NAME], output_) + make = variable(config[CONF_MAKE_ID], rhs) + switch_ = make.Pswitch_ + + switch.setup_switch(switch_, make.Pmqtt, config) + setup_component(switch, config) BUILD_FLAGS = '-DUSE_OUTPUT_SWITCH' diff --git a/esphomeyaml/components/switch/remote_transmitter.py b/esphomeyaml/components/switch/remote_transmitter.py index 95e56e1549..effa4121d5 100644 --- a/esphomeyaml/components/switch/remote_transmitter.py +++ b/esphomeyaml/components/switch/remote_transmitter.py @@ -1,18 +1,18 @@ import voluptuous as vol -import esphomeyaml.config_validation as cv from esphomeyaml.components import switch from esphomeyaml.components.remote_transmitter import RC_SWITCH_RAW_SCHEMA, \ RC_SWITCH_TYPE_A_SCHEMA, RC_SWITCH_TYPE_B_SCHEMA, RC_SWITCH_TYPE_C_SCHEMA, \ RC_SWITCH_TYPE_D_SCHEMA, RemoteTransmitterComponent, binary_code, build_rc_switch_protocol, \ remote_ns +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_CHANNEL, CONF_CODE, \ CONF_COMMAND, CONF_DATA, CONF_DEVICE, CONF_FAMILY, CONF_GROUP, CONF_INVERTED, CONF_LG, \ CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, CONF_PROTOCOL, CONF_RAW, CONF_RC_SWITCH_RAW, \ CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B, CONF_RC_SWITCH_TYPE_C, CONF_RC_SWITCH_TYPE_D, \ CONF_REPEAT, CONF_SAMSUNG, CONF_SONY, CONF_STATE, CONF_TIMES, \ CONF_WAIT_TIME -from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, add, get_variable +from esphomeyaml.helpers import ArrayInitializer, Pvariable, add, get_variable DEPENDENCIES = ['remote_transmitter'] @@ -23,18 +23,18 @@ REMOTE_KEYS = [CONF_NEC, CONF_LG, CONF_SAMSUNG, CONF_SONY, CONF_PANASONIC, CONF_ CONF_REMOTE_TRANSMITTER_ID = 'remote_transmitter_id' CONF_TRANSMITTER_ID = 'transmitter_id' -RemoteTransmitter = remote_ns.RemoteTransmitter -LGTransmitter = remote_ns.LGTransmitter -NECTransmitter = remote_ns.NECTransmitter -PanasonicTransmitter = remote_ns.PanasonicTransmitter -RawTransmitter = remote_ns.RawTransmitter -SamsungTransmitter = remote_ns.SamsungTransmitter -SonyTransmitter = remote_ns.SonyTransmitter -RCSwitchRawTransmitter = remote_ns.RCSwitchRawTransmitter -RCSwitchTypeATransmitter = remote_ns.RCSwitchTypeATransmitter -RCSwitchTypeBTransmitter = remote_ns.RCSwitchTypeBTransmitter -RCSwitchTypeCTransmitter = remote_ns.RCSwitchTypeCTransmitter -RCSwitchTypeDTransmitter = remote_ns.RCSwitchTypeDTransmitter +RemoteTransmitter = remote_ns.class_('RemoteTransmitter', switch.Switch) +LGTransmitter = remote_ns.class_('LGTransmitter', RemoteTransmitter) +NECTransmitter = remote_ns.class_('NECTransmitter', RemoteTransmitter) +PanasonicTransmitter = remote_ns.class_('PanasonicTransmitter', RemoteTransmitter) +RawTransmitter = remote_ns.class_('RawTransmitter', RemoteTransmitter) +SamsungTransmitter = remote_ns.class_('SamsungTransmitter', RemoteTransmitter) +SonyTransmitter = remote_ns.class_('SonyTransmitter', RemoteTransmitter) +RCSwitchRawTransmitter = remote_ns.class_('RCSwitchRawTransmitter', RemoteTransmitter) +RCSwitchTypeATransmitter = remote_ns.class_('RCSwitchTypeATransmitter', RCSwitchRawTransmitter) +RCSwitchTypeBTransmitter = remote_ns.class_('RCSwitchTypeBTransmitter', RCSwitchRawTransmitter) +RCSwitchTypeCTransmitter = remote_ns.class_('RCSwitchTypeCTransmitter', RCSwitchRawTransmitter) +RCSwitchTypeDTransmitter = remote_ns.class_('RCSwitchTypeDTransmitter', RCSwitchRawTransmitter) validate_raw_data = [vol.Any(vol.Coerce(int), cv.time_period_microseconds)] @@ -128,7 +128,6 @@ def transmitter_base(full_config): def to_code(config): - remote = None for remote in get_variable(config[CONF_REMOTE_TRANSMITTER_ID]): yield rhs = transmitter_base(config) diff --git a/esphomeyaml/components/switch/restart.py b/esphomeyaml/components/switch/restart.py index 90a8f1de3f..c81f486dd2 100644 --- a/esphomeyaml/components/switch/restart.py +++ b/esphomeyaml/components/switch/restart.py @@ -5,8 +5,8 @@ from esphomeyaml.components import switch from esphomeyaml.const import CONF_INVERTED, CONF_MAKE_ID, CONF_NAME from esphomeyaml.helpers import App, Application, variable -MakeRestartSwitch = Application.MakeRestartSwitch -RestartSwitch = switch.switch_ns.RestartSwitch +MakeRestartSwitch = Application.struct('MakeRestartSwitch') +RestartSwitch = switch.switch_ns.class_('RestartSwitch', switch.Switch) PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(RestartSwitch), diff --git a/esphomeyaml/components/switch/shutdown.py b/esphomeyaml/components/switch/shutdown.py index 330750eb0c..cca5f7184c 100644 --- a/esphomeyaml/components/switch/shutdown.py +++ b/esphomeyaml/components/switch/shutdown.py @@ -5,8 +5,8 @@ from esphomeyaml.components import switch from esphomeyaml.const import CONF_INVERTED, CONF_MAKE_ID, CONF_NAME from esphomeyaml.helpers import App, Application, variable -MakeShutdownSwitch = Application.MakeShutdownSwitch -ShutdownSwitch = switch.switch_ns.ShutdownSwitch +MakeShutdownSwitch = Application.struct('MakeShutdownSwitch') +ShutdownSwitch = switch.switch_ns.class_('ShutdownSwitch', switch.Switch) PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(ShutdownSwitch), diff --git a/esphomeyaml/components/switch/template.py b/esphomeyaml/components/switch/template.py index 121bbeff1c..464d90feb7 100644 --- a/esphomeyaml/components/switch/template.py +++ b/esphomeyaml/components/switch/template.py @@ -6,10 +6,10 @@ from esphomeyaml.components import switch from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, CONF_TURN_OFF_ACTION, \ CONF_TURN_ON_ACTION, CONF_OPTIMISTIC, CONF_RESTORE_STATE from esphomeyaml.helpers import App, Application, process_lambda, variable, NoArg, add, bool_, \ - optional + optional, setup_component, Component -MakeTemplateSwitch = Application.MakeTemplateSwitch -TemplateSwitch = switch.switch_ns.TemplateSwitch +MakeTemplateSwitch = Application.struct('MakeTemplateSwitch') +TemplateSwitch = switch.switch_ns.class_('TemplateSwitch', switch.Switch, Component) PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TemplateSwitch), @@ -19,32 +19,34 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ 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, -}), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC)) def to_code(config): rhs = App.make_template_switch(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) + template = make.Ptemplate_ - switch.setup_switch(make.Ptemplate_, make.Pmqtt, config) + switch.setup_switch(template, make.Pmqtt, config) if CONF_LAMBDA in config: - template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(bool_)): yield - add(make.Ptemplate_.set_state_lambda(template_)) + add(template.set_state_lambda(template_)) if CONF_TURN_OFF_ACTION in config: - automation.build_automation(make.Ptemplate_.get_turn_off_trigger(), NoArg, + automation.build_automation(template.get_turn_off_trigger(), NoArg, config[CONF_TURN_OFF_ACTION]) if CONF_TURN_ON_ACTION in config: - automation.build_automation(make.Ptemplate_.get_turn_on_trigger(), NoArg, + automation.build_automation(template.get_turn_on_trigger(), NoArg, config[CONF_TURN_ON_ACTION]) if CONF_OPTIMISTIC in config: - add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC])) + add(template.set_optimistic(config[CONF_OPTIMISTIC])) if CONF_RESTORE_STATE in config: - add(make.Ptemplate_.set_restore_state(config[CONF_RESTORE_STATE])) + add(template.set_restore_state(config[CONF_RESTORE_STATE])) + + setup_component(template, config) BUILD_FLAGS = '-DUSE_TEMPLATE_SWITCH' diff --git a/esphomeyaml/components/switch/uart.py b/esphomeyaml/components/switch/uart.py index dccfc75deb..3cbe26dc36 100644 --- a/esphomeyaml/components/switch/uart.py +++ b/esphomeyaml/components/switch/uart.py @@ -1,7 +1,7 @@ import voluptuous as vol import esphomeyaml.config_validation as cv -from esphomeyaml.components import switch +from esphomeyaml.components import switch, uart from esphomeyaml.components.uart import UARTComponent from esphomeyaml.const import CONF_DATA, CONF_INVERTED, CONF_MAKE_ID, CONF_NAME, CONF_UART_ID from esphomeyaml.core import HexInt @@ -9,8 +9,8 @@ from esphomeyaml.helpers import App, Application, ArrayInitializer, get_variable DEPENDENCIES = ['uart'] -MakeUARTSwitch = Application.MakeUARTSwitch -UARTSwitch = switch.switch_ns.UARTSwitch +MakeUARTSwitch = Application.struct('MakeUARTSwitch') +UARTSwitch = switch.switch_ns.class_('UARTSwitch', switch.Switch, uart.UARTDevice) def validate_data(value): @@ -33,13 +33,12 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({ def to_code(config): - uart = None - for uart in get_variable(config[CONF_UART_ID]): + for uart_ in get_variable(config[CONF_UART_ID]): yield data = config[CONF_DATA] if isinstance(data, str): data = [HexInt(ord(x)) for x in data] - rhs = App.make_uart_switch(uart, config[CONF_NAME], ArrayInitializer(*data, multiline=False)) + rhs = App.make_uart_switch(uart_, config[CONF_NAME], ArrayInitializer(*data, multiline=False)) restart = variable(config[CONF_MAKE_ID], rhs) switch.setup_switch(restart.Puart, restart.Pmqtt, config) diff --git a/esphomeyaml/components/text_sensor/__init__.py b/esphomeyaml/components/text_sensor/__init__.py index 89c3e2fead..0348b07fed 100644 --- a/esphomeyaml/components/text_sensor/__init__.py +++ b/esphomeyaml/components/text_sensor/__init__.py @@ -6,7 +6,7 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_MQTT_ID, CONF_ON_VALUE, \ CONF_TRIGGER_ID from esphomeyaml.helpers import App, Pvariable, add, add_job, esphomelib_ns, setup_mqtt_component, \ - std_string + std_string, Nameable, Trigger PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -14,14 +14,14 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ # pylint: disable=invalid-name text_sensor_ns = esphomelib_ns.namespace('text_sensor') -TextSensor = text_sensor_ns.TextSensor -MQTTTextSensor = text_sensor_ns.MQTTTextSensor +TextSensor = text_sensor_ns.class_('TextSensor', Nameable) +MQTTTextSensor = text_sensor_ns.class_('MQTTTextSensor', mqtt.MQTTComponent) -TextSensorStateTrigger = text_sensor_ns.TextSensorStateTrigger +TextSensorStateTrigger = text_sensor_ns.class_('TextSensorStateTrigger', + Trigger.template(std_string)) TEXT_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({ cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTTextSensor), - cv.GenerateID(): cv.declare_variable_id(TextSensor), vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_ON_VALUE): automation.validate_automation({ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(TextSensorStateTrigger), diff --git a/esphomeyaml/components/text_sensor/mqtt_subscribe.py b/esphomeyaml/components/text_sensor/mqtt_subscribe.py index 7b2ea32449..c4dafadee7 100644 --- a/esphomeyaml/components/text_sensor/mqtt_subscribe.py +++ b/esphomeyaml/components/text_sensor/mqtt_subscribe.py @@ -3,27 +3,32 @@ import voluptuous as vol from esphomeyaml.components import text_sensor import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME, CONF_QOS, CONF_TOPIC -from esphomeyaml.helpers import App, Application, add, variable +from esphomeyaml.helpers import App, Application, add, variable, setup_component, Component DEPENDENCIES = ['mqtt'] -MakeMQTTSubscribeTextSensor = Application.MakeMQTTSubscribeTextSensor -MQTTSubscribeTextSensor = text_sensor.text_sensor_ns.MQTTSubscribeTextSensor +MakeMQTTSubscribeTextSensor = Application.struct('MakeMQTTSubscribeTextSensor') +MQTTSubscribeTextSensor = text_sensor.text_sensor_ns.class_('MQTTSubscribeTextSensor', + text_sensor.TextSensor, Component) PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(MQTTSubscribeTextSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeMQTTSubscribeTextSensor), vol.Required(CONF_TOPIC): cv.subscribe_topic, vol.Optional(CONF_QOS): cv.mqtt_qos, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_mqtt_subscribe_text_sensor(config[CONF_NAME], config[CONF_TOPIC]) make = variable(config[CONF_MAKE_ID], rhs) + sensor_ = make.Psensor + if CONF_QOS in config: - add(make.Psensor.set_qos(config[CONF_QOS])) - text_sensor.setup_text_sensor(make.Psensor, make.Pmqtt, config) + add(sensor_.set_qos(config[CONF_QOS])) + + text_sensor.setup_text_sensor(sensor_, make.Pmqtt, config) + setup_component(sensor_, config) BUILD_FLAGS = '-DUSE_MQTT_SUBSCRIBE_TEXT_SENSOR' diff --git a/esphomeyaml/components/text_sensor/template.py b/esphomeyaml/components/text_sensor/template.py index 4a15ce967d..264841ea4b 100644 --- a/esphomeyaml/components/text_sensor/template.py +++ b/esphomeyaml/components/text_sensor/template.py @@ -4,29 +4,31 @@ from esphomeyaml.components import text_sensor import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_LAMBDA, CONF_MAKE_ID, CONF_NAME, CONF_UPDATE_INTERVAL from esphomeyaml.helpers import App, Application, add, optional, process_lambda, std_string, \ - variable + variable, setup_component, PollingComponent -MakeTemplateTextSensor = Application.MakeTemplateTextSensor -TemplateTextSensor = text_sensor.text_sensor_ns.TemplateTextSensor +MakeTemplateTextSensor = Application.struct('MakeTemplateTextSensor') +TemplateTextSensor = text_sensor.text_sensor_ns.class_('TemplateTextSensor', + text_sensor.TextSensor, PollingComponent) PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(TemplateTextSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateTextSensor), vol.Required(CONF_LAMBDA): cv.lambda_, vol.Optional(CONF_UPDATE_INTERVAL): cv.update_interval, -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_template_text_sensor(config[CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - text_sensor.setup_text_sensor(make.Ptemplate_, make.Pmqtt, config) + template = make.Ptemplate_ + text_sensor.setup_text_sensor(template, make.Pmqtt, config) + setup_component(template, config) - template_ = None for template_ in process_lambda(config[CONF_LAMBDA], [], return_type=optional.template(std_string)): yield - add(make.Ptemplate_.set_template(template_)) + add(template.set_template(template_)) BUILD_FLAGS = '-DUSE_TEMPLATE_TEXT_SENSOR' diff --git a/esphomeyaml/components/text_sensor/version.py b/esphomeyaml/components/text_sensor/version.py index 6eb8396000..1bfbac7fe6 100644 --- a/esphomeyaml/components/text_sensor/version.py +++ b/esphomeyaml/components/text_sensor/version.py @@ -1,21 +1,23 @@ from esphomeyaml.components import text_sensor import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_MAKE_ID, CONF_NAME -from esphomeyaml.helpers import App, Application, variable +from esphomeyaml.helpers import App, Application, variable, setup_component, Component -MakeVersionTextSensor = Application.MakeVersionTextSensor -VersionTextSensor = text_sensor.text_sensor_ns.VersionTextSensor +MakeVersionTextSensor = Application.struct('MakeVersionTextSensor') +VersionTextSensor = text_sensor.text_sensor_ns.class_('VersionTextSensor', + text_sensor.TextSensor, Component) PLATFORM_SCHEMA = cv.nameable(text_sensor.TEXT_SENSOR_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(VersionTextSensor), cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeVersionTextSensor), -})) +}).extend(cv.COMPONENT_SCHEMA.schema)) def to_code(config): rhs = App.make_version_text_sensor(config[CONF_NAME]) make = variable(config[CONF_MAKE_ID], rhs) text_sensor.setup_text_sensor(make.Psensor, make.Pmqtt, config) + setup_component(make.Psensor, config) BUILD_FLAGS = '-DUSE_VERSION_TEXT_SENSOR' diff --git a/esphomeyaml/components/time/__init__.py b/esphomeyaml/components/time/__init__.py index 3d4d7c44c7..30a4f5e7d7 100644 --- a/esphomeyaml/components/time/__init__.py +++ b/esphomeyaml/components/time/__init__.py @@ -8,7 +8,8 @@ import esphomeyaml.config_validation as cv from esphomeyaml import automation from esphomeyaml.const import CONF_CRON, CONF_DAYS_OF_MONTH, CONF_DAYS_OF_WEEK, CONF_HOURS, \ CONF_MINUTES, CONF_MONTHS, CONF_ON_TIME, CONF_SECONDS, CONF_TIMEZONE, CONF_TRIGGER_ID -from esphomeyaml.helpers import App, NoArg, Pvariable, add, add_job, esphomelib_ns, ArrayInitializer +from esphomeyaml.helpers import App, NoArg, Pvariable, add, add_job, esphomelib_ns, \ + ArrayInitializer, Component, Trigger _LOGGER = logging.getLogger(__name__) @@ -17,7 +18,9 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ }) time_ns = esphomelib_ns.namespace('time') -CronTrigger = time_ns.CronTrigger +RealTimeClockComponent = time_ns.class_('RealTimeClockComponent', Component) +CronTrigger = time_ns.class_('CronTrigger', Trigger.template(NoArg), Component) +EsphomelibTime = time_ns.struct('EsphomelibTime') def _tz_timedelta(td): diff --git a/esphomeyaml/components/time/sntp.py b/esphomeyaml/components/time/sntp.py index 9b09b6e220..0422c8b6fa 100644 --- a/esphomeyaml/components/time/sntp.py +++ b/esphomeyaml/components/time/sntp.py @@ -3,15 +3,15 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml.components import time as time_ from esphomeyaml.const import CONF_ID, CONF_LAMBDA, CONF_SERVERS -from esphomeyaml.helpers import App, Pvariable, add +from esphomeyaml.helpers import App, Pvariable, add, setup_component -SNTPComponent = time_.time_ns.SNTPComponent +SNTPComponent = time_.time_ns.class_('SNTPComponent', time_.RealTimeClockComponent) PLATFORM_SCHEMA = time_.TIME_PLATFORM_SCHEMA.extend({ cv.GenerateID(): cv.declare_variable_id(SNTPComponent), vol.Optional(CONF_SERVERS): vol.All(cv.ensure_list, [cv.string], vol.Length(max=3)), vol.Optional(CONF_LAMBDA): cv.lambda_, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -21,6 +21,7 @@ def to_code(config): add(sntp.set_servers(*config[CONF_SERVERS])) time_.setup_time(sntp, config) + setup_component(sntp, config) BUILD_FLAGS = '-DUSE_SNTP_COMPONENT' diff --git a/esphomeyaml/components/uart.py b/esphomeyaml/components/uart.py index 549fcb59d5..8f5b9463fb 100644 --- a/esphomeyaml/components/uart.py +++ b/esphomeyaml/components/uart.py @@ -3,18 +3,19 @@ import voluptuous as vol import esphomeyaml.config_validation as cv from esphomeyaml import pins from esphomeyaml.const import CONF_BAUD_RATE, CONF_ID, CONF_RX_PIN, CONF_TX_PIN -from esphomeyaml.helpers import App, Pvariable, esphomelib_ns +from esphomeyaml.helpers import App, Pvariable, esphomelib_ns, setup_component, Component -UARTComponent = esphomelib_ns.UARTComponent +UARTComponent = esphomelib_ns.class_('UARTComponent', Component) +UARTDevice = esphomelib_ns.class_('UARTDevice') -SPI_SCHEMA = vol.All(vol.Schema({ +UART_SCHEMA = vol.All(vol.Schema({ cv.GenerateID(): cv.declare_variable_id(UARTComponent), vol.Optional(CONF_TX_PIN): pins.output_pin, vol.Optional(CONF_RX_PIN): pins.input_pin, vol.Required(CONF_BAUD_RATE): cv.positive_int, -}), cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN)) +}).extend(cv.COMPONENT_SCHEMA.schema), cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN)) -CONFIG_SCHEMA = vol.All(cv.ensure_list, [SPI_SCHEMA]) +CONFIG_SCHEMA = vol.All(cv.ensure_list, [UART_SCHEMA]) def to_code(config): @@ -22,7 +23,9 @@ def to_code(config): tx = conf.get(CONF_TX_PIN, -1) rx = conf.get(CONF_RX_PIN, -1) rhs = App.init_uart(tx, rx, conf[CONF_BAUD_RATE]) - Pvariable(conf[CONF_ID], rhs) + var = Pvariable(conf[CONF_ID], rhs) + + setup_component(var, conf) BUILD_FLAGS = '-DUSE_UART' diff --git a/esphomeyaml/components/web_server.py b/esphomeyaml/components/web_server.py index ef4be334db..8a3a06436d 100644 --- a/esphomeyaml/components/web_server.py +++ b/esphomeyaml/components/web_server.py @@ -1,22 +1,19 @@ -import logging - import voluptuous as vol -import esphomeyaml.config_validation as cv from esphomeyaml import core -from esphomeyaml.const import CONF_PORT, CONF_JS_URL, CONF_CSS_URL, CONF_ID, ESP_PLATFORM_ESP32 -from esphomeyaml.helpers import App, add, Pvariable, esphomelib_ns +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_CSS_URL, CONF_ID, CONF_JS_URL, CONF_PORT, ESP_PLATFORM_ESP32 +from esphomeyaml.helpers import App, Component, Pvariable, StoringController, add, esphomelib_ns, \ + setup_component -_LOGGER = logging.getLogger(__name__) - -WebServer = esphomelib_ns.WebServer +WebServer = esphomelib_ns.class_('WebServer', Component, StoringController) CONFIG_SCHEMA = vol.Schema({ cv.GenerateID(): cv.declare_variable_id(WebServer), vol.Optional(CONF_PORT): cv.port, vol.Optional(CONF_CSS_URL): cv.string, vol.Optional(CONF_JS_URL): cv.string, -}) +}).extend(cv.COMPONENT_SCHEMA.schema) def to_code(config): @@ -27,6 +24,8 @@ def to_code(config): if CONF_JS_URL in config: add(web_server.set_js_url(config[CONF_JS_URL])) + setup_component(web_server, config) + BUILD_FLAGS = '-DUSE_WEB_SERVER' diff --git a/esphomeyaml/components/wifi.py b/esphomeyaml/components/wifi.py index 125ccb6711..a627f9bb73 100644 --- a/esphomeyaml/components/wifi.py +++ b/esphomeyaml/components/wifi.py @@ -5,7 +5,8 @@ import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_AP, CONF_CHANNEL, CONF_DNS1, CONF_DNS2, CONF_DOMAIN, \ CONF_GATEWAY, CONF_HOSTNAME, CONF_ID, CONF_MANUAL_IP, CONF_PASSWORD, CONF_POWER_SAVE_MODE,\ CONF_REBOOT_TIMEOUT, CONF_SSID, CONF_STATIC_IP, CONF_SUBNET, ESP_PLATFORM_ESP8266 -from esphomeyaml.helpers import App, Pvariable, StructInitializer, add, esphomelib_ns, global_ns +from esphomeyaml.helpers import App, Pvariable, StructInitializer, add, esphomelib_ns, global_ns, \ + Component def validate_password(value): @@ -64,16 +65,16 @@ def validate(config): return config -# pylint: disable=invalid-name -IPAddress = global_ns.IPAddress -ManualIP = esphomelib_ns.ManualIP -WiFiComponent = esphomelib_ns.WiFiComponent -WiFiAp = esphomelib_ns.WiFiAp +IPAddress = global_ns.class_('IPAddress') +ManualIP = esphomelib_ns.struct('ManualIP') +WiFiComponent = esphomelib_ns.class_('WiFiComponent', Component) +WiFiAp = esphomelib_ns.struct('WiFiAp') +WiFiPowerSaveMode = esphomelib_ns.enum('WiFiPowerSaveMode') WIFI_POWER_SAVE_MODES = { - 'NONE': esphomelib_ns.WIFI_POWER_SAVE_NONE, - 'LIGHT': esphomelib_ns.WIFI_POWER_SAVE_LIGHT, - 'HIGH': esphomelib_ns.WIFI_POWER_SAVE_HIGH, + 'NONE': WiFiPowerSaveMode.WIFI_POWER_SAVE_NONE, + 'LIGHT': WiFiPowerSaveMode.WIFI_POWER_SAVE_LIGHT, + 'HIGH': WiFiPowerSaveMode.WIFI_POWER_SAVE_HIGH, } CONFIG_SCHEMA = vol.All(vol.Schema({ diff --git a/esphomeyaml/config.py b/esphomeyaml/config.py index 814df4905b..9aaefbe860 100644 --- a/esphomeyaml/config.py +++ b/esphomeyaml/config.py @@ -10,7 +10,7 @@ from voluptuous.humanize import humanize_error from esphomeyaml import core, yaml_util, core_config from esphomeyaml.const import CONF_ESPHOMEYAML, CONF_PLATFORM, CONF_WIFI, ESP_PLATFORMS from esphomeyaml.core import ESPHomeYAMLError -from esphomeyaml.helpers import color +from esphomeyaml.helpers import color, MockObjClass from esphomeyaml.util import safe_print _LOGGER = logging.getLogger(__name__) @@ -106,11 +106,29 @@ def do_id_pass(result): # Check searched IDs for id, prefix, config in searching_ids: - if id.id is not None and not any(v[0].id == id.id for v in declare_ids): - result.add_error("Couldn't find ID {}".format(id.id), '.'.join(prefix), config) + if id.id is not None: + # manually declared + match = next((v[0] for v in declare_ids if v[0].id == id.id), None) + if match is None: + # No declared ID with this name + result.add_error("Couldn't find ID {}".format(id.id), '.'.join(prefix), config) + continue + if not isinstance(match.type, MockObjClass) or not isinstance(id.type, MockObjClass): + continue + if not match.type.inherits_from(id.type): + result.add_error("ID '{}' of type {} doesn't inherit from {}. Please double check " + "your ID is pointing to the correct value" + "".format(id.id, match.type, id.type)) + if id.id is None and id.type is not None: - id.id = next((v[0].id for v in declare_ids if v[0].type == id.type), None) - if id.id is None: + for v in declare_ids: + if v[0] is None or not isinstance(v[0].type, MockObjClass): + continue + inherits = v[0].type.inherits_from(id.type) + if inherits: + id.id = v[0].id + break + else: result.add_error("Couldn't resolve ID for type {}".format(id.type), '.'.join(prefix), config) diff --git a/esphomeyaml/config_validation.py b/esphomeyaml/config_validation.py index c521de4d09..e837936e18 100644 --- a/esphomeyaml/config_validation.py +++ b/esphomeyaml/config_validation.py @@ -13,7 +13,7 @@ from esphomeyaml import core, helpers from esphomeyaml.const import CONF_AVAILABILITY, CONF_COMMAND_TOPIC, CONF_DISCOVERY, CONF_ID, \ CONF_NAME, CONF_PAYLOAD_AVAILABLE, \ CONF_PAYLOAD_NOT_AVAILABLE, CONF_PLATFORM, CONF_RETAIN, CONF_STATE_TOPIC, CONF_TOPIC, \ - ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_INTERNAL + ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_INTERNAL, CONF_SETUP_PRIORITY from esphomeyaml.core import HexInt, IPAddress, Lambda, TimePeriod, TimePeriodMicroseconds, \ TimePeriodMilliseconds, TimePeriodSeconds @@ -641,9 +641,6 @@ def file_(value): return value -REGISTERED_IDS = set() - - class GenerateID(vol.Optional): def __init__(self, key=CONF_ID): super(GenerateID, self).__init__(key, default=lambda: None) @@ -688,3 +685,7 @@ MQTT_COMPONENT_SCHEMA = vol.Schema({ MQTT_COMMAND_COMPONENT_SCHEMA = MQTT_COMPONENT_SCHEMA.extend({ vol.Optional(CONF_COMMAND_TOPIC): subscribe_topic, }) + +COMPONENT_SCHEMA = vol.Schema({ + vol.Optional(CONF_SETUP_PRIORITY): vol.Coerce(float) +}) diff --git a/esphomeyaml/const.py b/esphomeyaml/const.py index 337c1f8c96..0071b6c500 100644 --- a/esphomeyaml/const.py +++ b/esphomeyaml/const.py @@ -202,6 +202,7 @@ CONF_RECEIVE_TIMEOUT = 'receive_timeout' CONF_SCAN_INTERVAL = 'scan_interval' CONF_MAC_ADDRESS = 'mac_address' CONF_SETUP_MODE = 'setup_mode' +CONF_SETUP_PRIORITY = 'setup_priority' CONF_IIR_FILTER = 'iir_filter' CONF_MEASUREMENT_DURATION = 'measurement_duration' CONF_LOW_VOLTAGE_REFERENCE = 'low_voltage_reference' @@ -364,6 +365,8 @@ CONF_TIME_ID = 'time_id' CONF_RESTORE_STATE = 'restore_state' CONF_TIMING = 'timing' CONF_INVALID_COOLDOWN = 'invalid_cooldown' +CONF_INITIAL_VALUE = 'initial_value' +CONF_RESTORE_VALUE = 'restore_value' ALLOWED_NAME_CHARS = u'abcdefghijklmnopqrstuvwxyz0123456789_' diff --git a/esphomeyaml/helpers.py b/esphomeyaml/helpers.py index aa7648172c..fcd368bbbf 100644 --- a/esphomeyaml/helpers.py +++ b/esphomeyaml/helpers.py @@ -9,7 +9,7 @@ from esphomeyaml import core from esphomeyaml.const import CONF_AVAILABILITY, CONF_COMMAND_TOPIC, CONF_DISCOVERY, \ CONF_INVERTED, \ CONF_MODE, CONF_NUMBER, CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_PCF8574, \ - CONF_RETAIN, CONF_STATE_TOPIC, CONF_TOPIC + CONF_RETAIN, CONF_STATE_TOPIC, CONF_TOPIC, CONF_SETUP_PRIORITY from esphomeyaml.core import ESPHomeYAMLError, HexInt, Lambda, TimePeriodMicroseconds, \ TimePeriodMilliseconds, TimePeriodSeconds @@ -407,15 +407,31 @@ def get_variable(id): yield None +def get_variable_with_full_id(id): + while True: + for k, v in _VARIABLES.iteritems(): + if k == id: + yield (k, v) + return + _LOGGER.debug("Waiting for variable %s", id) + yield None, None + + def process_lambda(value, parameters, capture='=', return_type=None): + from esphomeyaml.components.globals import GlobalVariableComponent + if value is None: yield return parts = value.parts[:] for i, id in enumerate(value.requires_ids): - var = None - for var in get_variable(id): + for full_id, var in get_variable_with_full_id(id): yield + if full_id is not None and isinstance(full_id.type, MockObjClass) and \ + full_id.type.inherits_from(GlobalVariableComponent): + parts[i * 3 + 1] = var.value() + continue + if parts[i * 3 + 2] == '.': parts[i * 3 + 1] = var._ else: @@ -530,6 +546,20 @@ class MockObj(Expression): obj.requires.append(self) return obj + def class_(self, name, *parents): + obj = MockObjClass(u'{}::{}'.format(self.base, name), u'.', parents=parents) + obj.requires.append(self) + return obj + + def struct(self, name): + return self.class_(name) + + def enum(self, name, is_class=False): + if is_class: + return self.namespace(name) + + return self + def operator(self, name): if name == 'ref': obj = MockObj(u'{} &'.format(self.base), u'') @@ -556,6 +586,37 @@ class MockObj(Expression): return obj +class MockObjClass(MockObj): + def __init__(self, *args, **kwargs): + parens = kwargs.pop('parents') + MockObj.__init__(self, *args, **kwargs) + self._parents = [] + for paren in parens: + if not isinstance(paren, MockObjClass): + raise ValueError + self._parents.append(paren) + # pylint: disable=protected-access + self._parents += paren._parents + + def inherits_from(self, other): + if self == other: + return True + for parent in self._parents: + if parent == other: + return True + return False + + def template(self, args): + if not isinstance(args, TemplateArguments): + args = TemplateArguments(args) + new_parents = self._parents[:] + new_parents.append(self) + obj = MockObjClass(u'{}{}'.format(self.base, args), parents=new_parents) + obj.requires.append(self) + obj.requires.append(args) + return obj + + global_ns = MockObj('', '') float_ = global_ns.namespace('float') bool_ = global_ns.namespace('bool') @@ -570,16 +631,24 @@ NAN = global_ns.namespace('NAN') esphomelib_ns = global_ns # using namespace esphomelib; NoArg = esphomelib_ns.NoArg App = esphomelib_ns.App -Application = esphomelib_ns.namespace('Application') -optional = esphomelib_ns.optional +io_ns = esphomelib_ns.namespace('io') +Nameable = esphomelib_ns.class_('Nameable') +Trigger = esphomelib_ns.class_('Trigger') +Action = esphomelib_ns.class_('Action') +Component = esphomelib_ns.class_('Component') +PollingComponent = esphomelib_ns.class_('PollingComponent', Component) +Application = esphomelib_ns.class_('Application') +optional = esphomelib_ns.class_('optional') arduino_json_ns = global_ns.namespace('ArduinoJson') -JsonObject = arduino_json_ns.JsonObject +JsonObject = arduino_json_ns.class_('JsonObject') JsonObjectRef = JsonObject.operator('ref') JsonObjectConstRef = JsonObjectRef.operator('const') +Controller = esphomelib_ns.class_('Controller') +StoringController = esphomelib_ns.class_('StoringController', Controller) -GPIOPin = esphomelib_ns.GPIOPin -GPIOOutputPin = esphomelib_ns.GPIOOutputPin -GPIOInputPin = esphomelib_ns.GPIOInputPin +GPIOPin = esphomelib_ns.class_('GPIOPin') +GPIOOutputPin = esphomelib_ns.class_('GPIOOutputPin', GPIOPin) +GPIOInputPin = esphomelib_ns.class_('GPIOInputPin', GPIOPin) def get_gpio_pin_number(conf): @@ -647,6 +716,11 @@ def setup_mqtt_component(obj, config): availability[CONF_PAYLOAD_NOT_AVAILABLE])) +def setup_component(obj, config): + if CONF_SETUP_PRIORITY in config: + add(obj.set_setup_priority(config[CONF_SETUP_PRIORITY])) + + def color(the_color, message='', reset=None): """Color helper.""" from colorlog.escape_codes import escape_codes, parse_colors diff --git a/esphomeyaml/pins.py b/esphomeyaml/pins.py index 1b67860ed4..8d736ee9b2 100644 --- a/esphomeyaml/pins.py +++ b/esphomeyaml/pins.py @@ -2,11 +2,11 @@ import logging import voluptuous as vol -import esphomeyaml.config_validation as cv from esphomeyaml import core -from esphomeyaml.components import pcf8574 +import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_INVERTED, CONF_MODE, CONF_NUMBER, CONF_PCF8574, \ ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266 +from esphomeyaml.helpers import Component, esphomelib_ns, io_ns _LOGGER = logging.getLogger(__name__) @@ -285,8 +285,11 @@ def shorthand_input_pin(value): return {CONF_NUMBER: value} +I2CDevice = esphomelib_ns.class_('I2CDevice') +PCF8574Component = io_ns.class_('PCF8574Component', Component, I2CDevice) + PCF8574_OUTPUT_PIN_SCHEMA = vol.Schema({ - vol.Required(CONF_PCF8574): cv.use_variable_id(pcf8574.PCF8574Component), + vol.Required(CONF_PCF8574): cv.use_variable_id(PCF8574Component), vol.Required(CONF_NUMBER): vol.Coerce(int), vol.Optional(CONF_MODE): vol.All(vol.Upper, cv.one_of("OUTPUT")), vol.Optional(CONF_INVERTED, default=False): cv.boolean, diff --git a/tests/test1.yaml b/tests/test1.yaml index 4ae2e3c665..d3acfa95c3 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -67,12 +67,10 @@ mqtt: ESP_LOGD("main", "Got message %s", x.c_str()); - topic: livingroom/ota_mode then: - - deep_sleep.prevent: - id: deep_sleep_1 + - deep_sleep.prevent: deep_sleep_1 - topic: livingroom/ota_mode then: - - deep_sleep.enter: - id: deep_sleep_1 + - deep_sleep.enter: deep_sleep_1 on_json_message: topic: the/topic then: @@ -97,6 +95,7 @@ i2c: scl: 22 scan: True frequency: 100kHz + setup_priority: -100 spi: clk_pin: GPIO21 @@ -157,6 +156,7 @@ sensor: icon: "mdi:water-percent" accuracy_decimals: 5 expire_after: 120s + setup_priority: -100 filters: - offset: 2.0 - multiply: 1.2 @@ -1001,3 +1001,22 @@ stepper: max_speed: 250 steps/s acceleration: 100 steps/s^2 deceleration: 200 steps/s^2 + + +globals: +- id: glob_int + type: int + restore_value: yes + initial_value: '0' +- id: glob_float + type: float + restore_value: yes + initial_value: '0.0f' +- id: glob_bool + type: bool + restore_value: no + initial_value: 'true' +- id: glob_string + type: std::string + restore_value: no + # initial_value: ""