diff --git a/esphomeyaml/__main__.py b/esphomeyaml/__main__.py index b044622ebe..1462cdf29e 100644 --- a/esphomeyaml/__main__.py +++ b/esphomeyaml/__main__.py @@ -13,7 +13,7 @@ from esphomeyaml.const import CONF_BAUD_RATE, CONF_DOMAIN, CONF_ESPHOMEYAML, CON CONF_LOGGER, CONF_MANUAL_IP, CONF_NAME, CONF_STATIC_IP, CONF_WIFI, ESP_PLATFORM_ESP8266 from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import AssignmentExpression, Expression, RawStatement, _EXPRESSIONS, add, \ - add_task, color, flush_tasks, indent, quote, statement + add_job, color, flush_tasks, indent, quote, statement _LOGGER = logging.getLogger(__name__) @@ -113,24 +113,20 @@ def run_miniterm(config, port, escape=False): def write_cpp(config): _LOGGER.info("Generating C++ source...") - add_task(core_to_code, config[CONF_ESPHOMEYAML], 'esphomeyaml') + add_job(core_to_code, config[CONF_ESPHOMEYAML], domain='esphomeyaml') for domain in PRE_INITIALIZE: - if domain == CONF_ESPHOMEYAML: + if domain == CONF_ESPHOMEYAML or domain not in config: continue - if domain in config: - add_task(get_component(domain).to_code, config[domain], domain) + add_job(get_component(domain).to_code, config[domain], domain=domain) for domain, component, conf in iter_components(config): - if domain in PRE_INITIALIZE: + if domain in PRE_INITIALIZE or not hasattr(component, 'to_code'): continue - if not hasattr(component, 'to_code'): - continue - add_task(component.to_code, conf, domain) + add_job(component.to_code, conf, domain=domain) flush_tasks() add(RawStatement('')) add(RawStatement('')) - all_code = [] for exp in _EXPRESSIONS: if core.SIMPLIFY: diff --git a/esphomeyaml/automation.py b/esphomeyaml/automation.py index f42c0a1220..f7ffb51ff6 100644 --- a/esphomeyaml/automation.py +++ b/esphomeyaml/automation.py @@ -11,7 +11,7 @@ from esphomeyaml.const import CONF_ABOVE, CONF_ACTION_ID, CONF_AND, CONF_AUTOMAT from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, TemplateArguments, add, \ bool_, esphomelib_ns, float_, get_variable, process_lambda, std_string, templatable, uint32, \ - uint8 + uint8, add_job CONF_MQTT_PUBLISH = 'mqtt.publish' CONF_LIGHT_TOGGLE = 'light.toggle' @@ -390,7 +390,7 @@ def build_actions(config, arg_type): yield ArrayInitializer(*actions) -def build_automation(trigger, arg_type, config): +def build_automation_(trigger, arg_type, config): rhs = App.make_automation(trigger) type = Automation.template(arg_type) obj = Pvariable(config[CONF_AUTOMATION_ID], rhs, type=type) @@ -403,3 +403,7 @@ def build_automation(trigger, arg_type, config): for actions in build_actions(config[CONF_THEN], arg_type): yield add(obj.add_actions(actions)) + + +def build_automation(trigger, arg_type, config): + add_job(build_automation_, trigger, arg_type, config) diff --git a/esphomeyaml/components/binary_sensor/__init__.py b/esphomeyaml/components/binary_sensor/__init__.py index 406ad11e8a..04be11d173 100644 --- a/esphomeyaml/components/binary_sensor/__init__.py +++ b/esphomeyaml/components/binary_sensor/__init__.py @@ -5,7 +5,8 @@ from esphomeyaml import automation from esphomeyaml.const import CONF_DEVICE_CLASS, CONF_ID, CONF_INVERTED, CONF_MAX_LENGTH, \ CONF_MIN_LENGTH, CONF_MQTT_ID, CONF_ON_CLICK, CONF_ON_DOUBLE_CLICK, CONF_ON_PRESS, \ CONF_ON_RELEASE, CONF_TRIGGER_ID -from esphomeyaml.helpers import App, NoArg, Pvariable, add, esphomelib_ns, setup_mqtt_component +from esphomeyaml.helpers import App, NoArg, Pvariable, add, esphomelib_ns, setup_mqtt_component, \ + add_job DEVICE_CLASSES = [ '', 'battery', 'cold', 'connectivity', 'door', 'garage_door', 'gas', @@ -60,27 +61,23 @@ def setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config): for conf in config.get(CONF_ON_PRESS, []): rhs = binary_sensor_var.make_press_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, NoArg, conf): - yield + automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_RELEASE, []): rhs = binary_sensor_var.make_release_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, NoArg, conf): - yield + automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_CLICK, []): rhs = binary_sensor_var.make_click_trigger(conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, NoArg, conf): - yield + automation.build_automation(trigger, NoArg, conf) for conf in config.get(CONF_ON_DOUBLE_CLICK, []): rhs = binary_sensor_var.make_double_click_trigger(conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, NoArg, conf): - yield + automation.build_automation(trigger, NoArg, conf) setup_mqtt_component(mqtt_var, config) @@ -90,16 +87,14 @@ def setup_binary_sensor(binary_sensor_obj, mqtt_obj, config): has_side_effects=False) mqtt_var = Pvariable(config[CONF_MQTT_ID], mqtt_obj, has_side_effects=False) - for _ in setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config): - yield + add_job(setup_binary_sensor_core_, binary_sensor_var, mqtt_var, config) def register_binary_sensor(var, config): binary_sensor_var = Pvariable(config[CONF_ID], var, has_side_effects=True) rhs = App.register_binary_sensor(binary_sensor_var) mqtt_var = Pvariable(config[CONF_MQTT_ID], rhs, has_side_effects=True) - for _ in setup_binary_sensor_core_(binary_sensor_var, mqtt_var, config): - yield + add_job(setup_binary_sensor_core_, binary_sensor_var, mqtt_var, config) BUILD_FLAGS = '-DUSE_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/esp32_ble.py b/esphomeyaml/components/binary_sensor/esp32_ble.py index dc31f7d910..06c3e105dc 100644 --- a/esphomeyaml/components/binary_sensor/esp32_ble.py +++ b/esphomeyaml/components/binary_sensor/esp32_ble.py @@ -42,8 +42,7 @@ def to_code(config): yield addr = [HexInt(i) for i in config[CONF_MAC_ADDRESS].parts] rhs = hub.make_device(config[CONF_NAME], ArrayInitializer(*addr, multiline=False)) - for _ in binary_sensor.register_binary_sensor(rhs, config): - yield + binary_sensor.register_binary_sensor(rhs, config) BUILD_FLAGS = '-DUSE_ESP32_BLE_TRACKER' diff --git a/esphomeyaml/components/binary_sensor/esp32_touch.py b/esphomeyaml/components/binary_sensor/esp32_touch.py index b88d75f006..922467cc1c 100644 --- a/esphomeyaml/components/binary_sensor/esp32_touch.py +++ b/esphomeyaml/components/binary_sensor/esp32_touch.py @@ -47,8 +47,7 @@ def to_code(config): yield touch_pad = TOUCH_PADS[config[CONF_PIN]] rhs = hub.make_touch_pad(config[CONF_NAME], touch_pad, config[CONF_THRESHOLD]) - for _ in binary_sensor.register_binary_sensor(rhs, config): - yield + binary_sensor.register_binary_sensor(rhs, config) BUILD_FLAGS = '-DUSE_ESP32_TOUCH_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/gpio.py b/esphomeyaml/components/binary_sensor/gpio.py index 5a38409e29..9890970313 100644 --- a/esphomeyaml/components/binary_sensor/gpio.py +++ b/esphomeyaml/components/binary_sensor/gpio.py @@ -20,8 +20,7 @@ def to_code(config): yield rhs = App.make_gpio_binary_sensor(config[CONF_NAME], pin) gpio = variable(config[CONF_MAKE_ID], rhs) - for _ in binary_sensor.setup_binary_sensor(gpio.Pgpio, gpio.Pmqtt, config): - yield + binary_sensor.setup_binary_sensor(gpio.Pgpio, gpio.Pmqtt, config) BUILD_FLAGS = '-DUSE_GPIO_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/status.py b/esphomeyaml/components/binary_sensor/status.py index 5c305aef3d..82548aa46f 100644 --- a/esphomeyaml/components/binary_sensor/status.py +++ b/esphomeyaml/components/binary_sensor/status.py @@ -15,8 +15,7 @@ PLATFORM_SCHEMA = binary_sensor.PLATFORM_SCHEMA.extend({ def to_code(config): rhs = App.make_status_binary_sensor(config[CONF_NAME]) status = variable(config[CONF_MAKE_ID], rhs) - for _ in binary_sensor.setup_binary_sensor(status.Pstatus, status.Pmqtt, config): - yield + binary_sensor.setup_binary_sensor(status.Pstatus, status.Pmqtt, config) BUILD_FLAGS = '-DUSE_STATUS_BINARY_SENSOR' diff --git a/esphomeyaml/components/binary_sensor/template.py b/esphomeyaml/components/binary_sensor/template.py index 0a6f05667e..663e82430f 100644 --- a/esphomeyaml/components/binary_sensor/template.py +++ b/esphomeyaml/components/binary_sensor/template.py @@ -19,8 +19,7 @@ def to_code(config): yield rhs = App.make_template_binary_sensor(config[CONF_NAME], template_) make = variable(config[CONF_MAKE_ID], rhs) - for _ in binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config): - yield + binary_sensor.setup_binary_sensor(make.Ptemplate_, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_TEMPLATE_BINARY_SENSOR' diff --git a/esphomeyaml/components/mqtt.py b/esphomeyaml/components/mqtt.py index 484d74b862..bcb3e660a0 100644 --- a/esphomeyaml/components/mqtt.py +++ b/esphomeyaml/components/mqtt.py @@ -134,8 +134,7 @@ def to_code(config): for conf in config.get(CONF_ON_MESSAGE, []): rhs = mqtt.make_message_trigger(conf[CONF_TOPIC], conf[CONF_QOS]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, std_string, conf): - yield + automation.build_automation(trigger, std_string, conf) def required_build_flags(config): diff --git a/esphomeyaml/components/sensor/__init__.py b/esphomeyaml/components/sensor/__init__.py index 905c1b07a6..b37bf774d3 100644 --- a/esphomeyaml/components/sensor/__init__.py +++ b/esphomeyaml/components/sensor/__init__.py @@ -9,7 +9,7 @@ from esphomeyaml.const import CONF_ABOVE, CONF_ACCURACY_DECIMALS, CONF_ALPHA, CO 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 from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, add, esphomelib_ns, float_, \ - process_lambda, setup_mqtt_component, templatable + process_lambda, setup_mqtt_component, templatable, add_job PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -152,13 +152,11 @@ def setup_sensor_core_(sensor_var, mqtt_var, config): for conf in config.get(CONF_ON_VALUE, []): rhs = sensor_var.make_value_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, float_, conf): - yield + automation.build_automation(trigger, float_, conf) for conf in config.get(CONF_ON_RAW_VALUE, []): rhs = sensor_var.make_raw_value_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - for _ in automation.build_automation(trigger, float_, conf): - yield + automation.build_automation(trigger, float_, conf) for conf in config.get(CONF_ON_VALUE_RANGE, []): rhs = sensor_var.make_value_range_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) @@ -172,8 +170,7 @@ def setup_sensor_core_(sensor_var, mqtt_var, config): for template_ in templatable(conf[CONF_BELOW], float_, float_): yield trigger.set_max(template_) - for _ in automation.build_automation(trigger, float_, conf): - yield + automation.build_automation(trigger, float_, conf) if CONF_EXPIRE_AFTER in config: if config[CONF_EXPIRE_AFTER] is None: @@ -186,16 +183,14 @@ def setup_sensor_core_(sensor_var, mqtt_var, config): def setup_sensor(sensor_obj, mqtt_obj, config): sensor_var = Pvariable(config[CONF_ID], sensor_obj, has_side_effects=False) mqtt_var = Pvariable(config[CONF_MQTT_ID], mqtt_obj, has_side_effects=False) - for _ in setup_sensor_core_(sensor_var, mqtt_var, config): - yield + add_job(setup_sensor_core_, sensor_var, mqtt_var, config) def register_sensor(var, config): sensor_var = Pvariable(config[CONF_ID], var, has_side_effects=True) rhs = App.register_sensor(sensor_var) mqtt_var = Pvariable(config[CONF_MQTT_ID], rhs, has_side_effects=True) - for _ in setup_sensor_core_(sensor_var, mqtt_var, config): - yield + add_job(setup_sensor_core_, sensor_var, mqtt_var, config) BUILD_FLAGS = '-DUSE_SENSOR' diff --git a/esphomeyaml/components/sensor/adc.py b/esphomeyaml/components/sensor/adc.py index 70a9f83432..ea8531d918 100644 --- a/esphomeyaml/components/sensor/adc.py +++ b/esphomeyaml/components/sensor/adc.py @@ -42,8 +42,7 @@ def to_code(config): adc = make.Padc if CONF_ATTENUATION in config: add(adc.set_attenuation(ATTENUATION_MODES[config[CONF_ATTENUATION]])) - for _ in sensor.setup_sensor(make.Padc, make.Pmqtt, config): - yield + sensor.setup_sensor(make.Padc, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_ADC_SENSOR' diff --git a/esphomeyaml/components/sensor/ads1115.py b/esphomeyaml/components/sensor/ads1115.py index eee600248e..8dbdf6238a 100644 --- a/esphomeyaml/components/sensor/ads1115.py +++ b/esphomeyaml/components/sensor/ads1115.py @@ -61,8 +61,7 @@ def to_code(config): mux = MUX[config[CONF_MULTIPLEXER]] gain = GAIN[config[CONF_GAIN]] rhs = hub.get_sensor(config[CONF_NAME], mux, gain, config.get(CONF_UPDATE_INTERVAL)) - for _ in sensor.register_sensor(rhs, config): - yield + sensor.register_sensor(rhs, config) BUILD_FLAGS = '-DUSE_ADS1115_SENSOR' diff --git a/esphomeyaml/components/sensor/bh1750.py b/esphomeyaml/components/sensor/bh1750.py index a9cf8f61ac..30a33192b4 100644 --- a/esphomeyaml/components/sensor/bh1750.py +++ b/esphomeyaml/components/sensor/bh1750.py @@ -31,8 +31,7 @@ def to_code(config): bh1750 = make_bh1750.Pbh1750 if CONF_RESOLUTION in config: add(bh1750.set_resolution(BH1750_RESOLUTIONS[config[CONF_RESOLUTION]])) - for _ in sensor.setup_sensor(bh1750, make_bh1750.Pmqtt, config): - yield + sensor.setup_sensor(bh1750, make_bh1750.Pmqtt, config) BUILD_FLAGS = '-DUSE_BH1750' diff --git a/esphomeyaml/components/sensor/bme280.py b/esphomeyaml/components/sensor/bme280.py index 7634ad3421..0920e95807 100644 --- a/esphomeyaml/components/sensor/bme280.py +++ b/esphomeyaml/components/sensor/bme280.py @@ -63,15 +63,12 @@ def to_code(config): constant = IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]] add(bme280.set_iir_filter(constant)) - for _ in sensor.setup_sensor(bme280.Pget_temperature_sensor(), make.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(bme280.Pget_pressure_sensor(), make.Pmqtt_pressure, - config[CONF_PRESSURE]): - yield - for _ in sensor.setup_sensor(bme280.Pget_humidity_sensor(), make.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield + sensor.setup_sensor(bme280.Pget_temperature_sensor(), make.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(bme280.Pget_pressure_sensor(), make.Pmqtt_pressure, + config[CONF_PRESSURE]) + sensor.setup_sensor(bme280.Pget_humidity_sensor(), make.Pmqtt_humidity, + config[CONF_HUMIDITY]) BUILD_FLAGS = '-DUSE_BME280' diff --git a/esphomeyaml/components/sensor/bme680.py b/esphomeyaml/components/sensor/bme680.py index 773bcfda8d..82ed3ee7ad 100644 --- a/esphomeyaml/components/sensor/bme680.py +++ b/esphomeyaml/components/sensor/bme680.py @@ -70,18 +70,14 @@ def to_code(config): constant = IIR_FILTER_OPTIONS[config[CONF_IIR_FILTER]] add(bme680.set_iir_filter(constant)) - for _ in sensor.setup_sensor(bme680.Pget_temperature_sensor(), make.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(bme680.Pget_pressure_sensor(), make.Pmqtt_pressure, - config[CONF_PRESSURE]): - yield - for _ in sensor.setup_sensor(bme680.Pget_humidity_sensor(), make.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield - for _ in sensor.setup_sensor(bme680.Pget_gas_resistance_sensor(), make.Pmqtt_gas_resistance, - config[CONF_GAS_RESISTANCE]): - yield + sensor.setup_sensor(bme680.Pget_temperature_sensor(), make.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(bme680.Pget_pressure_sensor(), make.Pmqtt_pressure, + config[CONF_PRESSURE]) + sensor.setup_sensor(bme680.Pget_humidity_sensor(), make.Pmqtt_humidity, + config[CONF_HUMIDITY]) + sensor.setup_sensor(bme680.Pget_gas_resistance_sensor(), make.Pmqtt_gas_resistance, + config[CONF_GAS_RESISTANCE]) BUILD_FLAGS = '-DUSE_BME680' diff --git a/esphomeyaml/components/sensor/bmp085.py b/esphomeyaml/components/sensor/bmp085.py index 0b89c8145b..bca0d51a33 100644 --- a/esphomeyaml/components/sensor/bmp085.py +++ b/esphomeyaml/components/sensor/bmp085.py @@ -4,7 +4,7 @@ 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, HexIntLiteral, add, variable, Application +from esphomeyaml.helpers import App, Application, HexIntLiteral, add, variable DEPENDENCIES = ['i2c'] @@ -27,12 +27,10 @@ def to_code(config): if CONF_ADDRESS in config: add(bmp.Pbmp.set_address(HexIntLiteral(config[CONF_ADDRESS]))) - for _ in sensor.setup_sensor(bmp.Pbmp.Pget_temperature_sensor(), bmp.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(bmp.Pbmp.Pget_pressure_sensor(), bmp.Pmqtt_pressure, - config[CONF_PRESSURE]): - yield + sensor.setup_sensor(bmp.Pbmp.Pget_temperature_sensor(), bmp.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(bmp.Pbmp.Pget_pressure_sensor(), bmp.Pmqtt_pressure, + config[CONF_PRESSURE]) BUILD_FLAGS = '-DUSE_BMP085_SENSOR' diff --git a/esphomeyaml/components/sensor/dallas.py b/esphomeyaml/components/sensor/dallas.py index 6a5097b574..f6a5e6fb59 100644 --- a/esphomeyaml/components/sensor/dallas.py +++ b/esphomeyaml/components/sensor/dallas.py @@ -30,8 +30,7 @@ def to_code(config): else: rhs = hub.Pget_sensor_by_index(config[CONF_NAME], config[CONF_INDEX], update_interval, config.get(CONF_RESOLUTION)) - for _ in sensor.register_sensor(rhs, config): - yield + sensor.register_sensor(rhs, config) BUILD_FLAGS = '-DUSE_DALLAS_SENSOR' diff --git a/esphomeyaml/components/sensor/dht.py b/esphomeyaml/components/sensor/dht.py index ba7d29b956..6e7eaca881 100644 --- a/esphomeyaml/components/sensor/dht.py +++ b/esphomeyaml/components/sensor/dht.py @@ -39,12 +39,10 @@ def to_code(config): constant = DHT_MODELS[config[CONF_MODEL]] add(dht.Pdht.set_dht_model(constant)) - for _ in sensor.setup_sensor(dht.Pdht.Pget_temperature_sensor(), - dht.Pmqtt_temperature, config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(dht.Pdht.Pget_humidity_sensor(), - dht.Pmqtt_humidity, config[CONF_HUMIDITY]): - yield + 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]) BUILD_FLAGS = '-DUSE_DHT_SENSOR' diff --git a/esphomeyaml/components/sensor/dht12.py b/esphomeyaml/components/sensor/dht12.py index 88105a0762..35f668b2cb 100644 --- a/esphomeyaml/components/sensor/dht12.py +++ b/esphomeyaml/components/sensor/dht12.py @@ -24,12 +24,10 @@ def to_code(config): config.get(CONF_UPDATE_INTERVAL)) dht = variable(config[CONF_MAKE_ID], rhs) - for _ in sensor.setup_sensor(dht.Pdht.Pget_temperature_sensor(), dht.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(dht.Pdht.Pget_humidity_sensor(), dht.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield + 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]) BUILD_FLAGS = '-DUSE_DHT12_SENSOR' diff --git a/esphomeyaml/components/sensor/esp32_hall.py b/esphomeyaml/components/sensor/esp32_hall.py index c3fbc137bb..dfa0a7d570 100644 --- a/esphomeyaml/components/sensor/esp32_hall.py +++ b/esphomeyaml/components/sensor/esp32_hall.py @@ -18,8 +18,7 @@ PLATFORM_SCHEMA = sensor.PLATFORM_SCHEMA.extend({ 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) - for _ in sensor.setup_sensor(make.Phall, make.Pmqtt, config): - yield + sensor.setup_sensor(make.Phall, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_ESP32_HALL_SENSOR' diff --git a/esphomeyaml/components/sensor/hdc1080.py b/esphomeyaml/components/sensor/hdc1080.py index d295345ef3..ccad75970c 100644 --- a/esphomeyaml/components/sensor/hdc1080.py +++ b/esphomeyaml/components/sensor/hdc1080.py @@ -4,7 +4,7 @@ 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, variable, Application +from esphomeyaml.helpers import App, Application, variable DEPENDENCIES = ['i2c'] @@ -24,13 +24,11 @@ def to_code(config): config.get(CONF_UPDATE_INTERVAL)) hdc1080 = variable(config[CONF_MAKE_ID], rhs) - for _ in sensor.setup_sensor(hdc1080.Phdc1080.Pget_temperature_sensor(), - hdc1080.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(hdc1080.Phdc1080.Pget_humidity_sensor(), hdc1080.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield + sensor.setup_sensor(hdc1080.Phdc1080.Pget_temperature_sensor(), + hdc1080.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(hdc1080.Phdc1080.Pget_humidity_sensor(), hdc1080.Pmqtt_humidity, + config[CONF_HUMIDITY]) BUILD_FLAGS = '-DUSE_HDC1080_SENSOR' diff --git a/esphomeyaml/components/sensor/htu21d.py b/esphomeyaml/components/sensor/htu21d.py index ef85bc7819..631f389314 100644 --- a/esphomeyaml/components/sensor/htu21d.py +++ b/esphomeyaml/components/sensor/htu21d.py @@ -4,7 +4,7 @@ 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, variable, Application +from esphomeyaml.helpers import App, Application, variable DEPENDENCIES = ['i2c'] @@ -23,12 +23,10 @@ def to_code(config): config[CONF_HUMIDITY][CONF_NAME], config.get(CONF_UPDATE_INTERVAL)) htu21d = variable(config[CONF_MAKE_ID], rhs) - for _ in sensor.setup_sensor(htu21d.Phtu21d.Pget_temperature_sensor(), htu21d.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(htu21d.Phtu21d.Pget_humidity_sensor(), htu21d.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield + sensor.setup_sensor(htu21d.Phtu21d.Pget_temperature_sensor(), htu21d.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(htu21d.Phtu21d.Pget_humidity_sensor(), htu21d.Pmqtt_humidity, + config[CONF_HUMIDITY]) BUILD_FLAGS = '-DUSE_HTU21D_SENSOR' diff --git a/esphomeyaml/components/sensor/max6675.py b/esphomeyaml/components/sensor/max6675.py index 12af4f90c8..95ee0e311a 100644 --- a/esphomeyaml/components/sensor/max6675.py +++ b/esphomeyaml/components/sensor/max6675.py @@ -32,8 +32,7 @@ def to_code(config): rhs = App.make_max6675_sensor(config[CONF_NAME], pin_cs, pin_clock, pin_miso, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - for _ in sensor.setup_sensor(make.Pmax6675, make.Pmqtt, config): - yield + sensor.setup_sensor(make.Pmax6675, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_MAX6675_SENSOR' diff --git a/esphomeyaml/components/sensor/mpu6050.py b/esphomeyaml/components/sensor/mpu6050.py index f4cae29b14..dbb61f828d 100644 --- a/esphomeyaml/components/sensor/mpu6050.py +++ b/esphomeyaml/components/sensor/mpu6050.py @@ -41,38 +41,31 @@ def to_code(config): if CONF_ACCEL_X in config: conf = config[CONF_ACCEL_X] rhs = mpu.Pmake_accel_x_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_ACCEL_Y in config: conf = config[CONF_ACCEL_Y] rhs = mpu.Pmake_accel_y_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_ACCEL_Z in config: conf = config[CONF_ACCEL_Z] rhs = mpu.Pmake_accel_z_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_GYRO_X in config: conf = config[CONF_GYRO_X] rhs = mpu.Pmake_gyro_x_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_GYRO_Y in config: conf = config[CONF_GYRO_Y] rhs = mpu.Pmake_gyro_y_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_GYRO_Z in config: conf = config[CONF_GYRO_Z] rhs = mpu.Pmake_gyro_z_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) if CONF_TEMPERATURE in config: conf = config[CONF_TEMPERATURE] rhs = mpu.Pmake_temperature_sensor(conf[CONF_NAME]) - for _ in sensor.register_sensor(rhs, conf): - yield + sensor.register_sensor(rhs, conf) BUILD_FLAGS = '-DUSE_MPU6050' diff --git a/esphomeyaml/components/sensor/pulse_counter.py b/esphomeyaml/components/sensor/pulse_counter.py index 2ee0109bc5..8733795589 100644 --- a/esphomeyaml/components/sensor/pulse_counter.py +++ b/esphomeyaml/components/sensor/pulse_counter.py @@ -57,8 +57,7 @@ def to_code(config): add(pcnt.set_edge_mode(rising_edge, falling_edge)) if CONF_INTERNAL_FILTER in config: add(pcnt.set_filter(config[CONF_INTERNAL_FILTER])) - for _ in sensor.setup_sensor(make.Ppcnt, make.Pmqtt, config): - yield + sensor.setup_sensor(make.Ppcnt, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_PULSE_COUNTER_SENSOR' diff --git a/esphomeyaml/components/sensor/rotary_encoder.py b/esphomeyaml/components/sensor/rotary_encoder.py index f4f02d8333..c5dfee9bbf 100644 --- a/esphomeyaml/components/sensor/rotary_encoder.py +++ b/esphomeyaml/components/sensor/rotary_encoder.py @@ -45,8 +45,7 @@ def to_code(config): if CONF_RESOLUTION in config: resolution = RESOLUTIONS[config[CONF_RESOLUTION]] add(encoder.set_resolution(resolution)) - for _ in sensor.setup_sensor(encoder, make.Pmqtt, config): - yield + sensor.setup_sensor(encoder, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_ROTARY_ENCODER_SENSOR' diff --git a/esphomeyaml/components/sensor/sht3xd.py b/esphomeyaml/components/sensor/sht3xd.py index bda732daf2..2c37f62b99 100644 --- a/esphomeyaml/components/sensor/sht3xd.py +++ b/esphomeyaml/components/sensor/sht3xd.py @@ -35,12 +35,10 @@ def to_code(config): if CONF_ACCURACY in config: add(sht3xd.Psht3xd.set_accuracy(SHT_ACCURACIES[config[CONF_ACCURACY]])) - for _ in sensor.setup_sensor(sht3xd.Psht3xd.Pget_temperature_sensor(), sht3xd.Pmqtt_temperature, - config[CONF_TEMPERATURE]): - yield - for _ in sensor.setup_sensor(sht3xd.Psht3xd.Pget_humidity_sensor(), sht3xd.Pmqtt_humidity, - config[CONF_HUMIDITY]): - yield + sensor.setup_sensor(sht3xd.Psht3xd.Pget_temperature_sensor(), sht3xd.Pmqtt_temperature, + config[CONF_TEMPERATURE]) + sensor.setup_sensor(sht3xd.Psht3xd.Pget_humidity_sensor(), sht3xd.Pmqtt_humidity, + config[CONF_HUMIDITY]) BUILD_FLAGS = '-DUSE_SHT3XD' diff --git a/esphomeyaml/components/sensor/template.py b/esphomeyaml/components/sensor/template.py index 0df4536032..78ae6b7021 100644 --- a/esphomeyaml/components/sensor/template.py +++ b/esphomeyaml/components/sensor/template.py @@ -21,8 +21,7 @@ def to_code(config): rhs = App.make_template_sensor(config[CONF_NAME], template_, config.get(CONF_UPDATE_INTERVAL)) make = variable(config[CONF_MAKE_ID], rhs) - for _ in sensor.setup_sensor(make.Ptemplate_, make.Pmqtt, config): - yield + sensor.setup_sensor(make.Ptemplate_, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_TEMPLATE_SENSOR' diff --git a/esphomeyaml/components/sensor/tsl2561.py b/esphomeyaml/components/sensor/tsl2561.py index 2b8c91a295..bca7c0420a 100644 --- a/esphomeyaml/components/sensor/tsl2561.py +++ b/esphomeyaml/components/sensor/tsl2561.py @@ -51,8 +51,7 @@ def to_code(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])) - for _ in sensor.setup_sensor(tsl2561, make_tsl.Pmqtt, config): - yield + sensor.setup_sensor(tsl2561, make_tsl.Pmqtt, config) BUILD_FLAGS = '-DUSE_TSL2561' diff --git a/esphomeyaml/components/sensor/ultrasonic.py b/esphomeyaml/components/sensor/ultrasonic.py index f413b7566a..a43f0d27ab 100644 --- a/esphomeyaml/components/sensor/ultrasonic.py +++ b/esphomeyaml/components/sensor/ultrasonic.py @@ -35,8 +35,7 @@ def to_code(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])) - for _ in sensor.setup_sensor(ultrasonic, make.Pmqtt, config): - yield + sensor.setup_sensor(ultrasonic, make.Pmqtt, config) BUILD_FLAGS = '-DUSE_ULTRASONIC_SENSOR' diff --git a/esphomeyaml/helpers.py b/esphomeyaml/helpers.py index 444b04995e..42dc2478ea 100644 --- a/esphomeyaml/helpers.py +++ b/esphomeyaml/helpers.py @@ -3,7 +3,7 @@ from __future__ import print_function import inspect import logging import re -from collections import OrderedDict +from collections import OrderedDict, deque from esphomeyaml import core from esphomeyaml.const import CONF_AVAILABILITY, CONF_COMMAND_TOPIC, CONF_DISCOVERY, \ @@ -388,7 +388,7 @@ def Pvariable(id, rhs, has_side_effects=True, type=None): return obj -_TASKS = [] +_TASKS = deque() _VARIABLES = {} _EXPRESSIONS = [] @@ -426,39 +426,35 @@ def templatable(value, input_type, output_type): yield value -def add_task(func, config, domain): +def add_job(func, *args, **kwargs): + domain = kwargs.get('domain') if inspect.isgeneratorfunction(func): def func_(): yield - for _ in func(config): + for _ in func(*args): yield else: def func_(): yield - func(config) - _TASKS.append((func_(), domain)) - - -def run_tasks(): - global _TASKS - - new_tasks = [] - for task, domain in _TASKS: - try: - task.next() - new_tasks.append((task, domain)) - except StopIteration: - pass - _TASKS = new_tasks + func(*args) + gen = func_() + _TASKS.append((gen, domain)) + return gen def flush_tasks(): - for _ in range(1000000): - run_tasks() - if not _TASKS: - break - else: - raise ESPHomeYAMLError("Circular dependency detected!") + i = 0 + while _TASKS: + i += 1 + if i > 1000000: + raise ESPHomeYAMLError("Circular dependency detected!") + + task, domain = _TASKS.popleft() + try: + task.next() + _TASKS.append((task, domain)) + except StopIteration: + pass def add(expression, require=True):