on_boot and on_shutdown triggers

This commit is contained in:
Otto Winter 2018-06-07 17:06:50 +02:00
parent 31d7656c07
commit 361a9da868
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
3 changed files with 27 additions and 5 deletions

View file

@ -8,19 +8,21 @@ import voluptuous as vol
from voluptuous.humanize import humanize_error from voluptuous.humanize import humanize_error
import esphomeyaml.config_validation as cv import esphomeyaml.config_validation as cv
from esphomeyaml import core, yaml_util from esphomeyaml import core, yaml_util, automation
from esphomeyaml.const import CONF_BOARD, CONF_BOARD_FLASH_MODE, CONF_ESPHOMEYAML, \ from esphomeyaml.const import CONF_BOARD, CONF_BOARD_FLASH_MODE, CONF_ESPHOMEYAML, \
CONF_LIBRARY_URI, \ CONF_LIBRARY_URI, CONF_NAME, CONF_PLATFORM, CONF_SIMPLIFY, CONF_USE_BUILD_FLAGS, CONF_WIFI, \
CONF_NAME, CONF_PLATFORM, CONF_SIMPLIFY, CONF_USE_BUILD_FLAGS, CONF_WIFI, ESP_PLATFORMS, \ ESP_PLATFORMS, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_ON_BOOT, CONF_TRIGGER_ID, \
ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266 CONF_PRIORITY, CONF_ON_SHUTDOWN
from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.core import ESPHomeYAMLError
from esphomeyaml.helpers import App, add, color from esphomeyaml.helpers import App, add, color, esphomelib_ns, Pvariable, NoArg, const_char_p
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_LIBRARY_URI = u'https://github.com/OttoWinter/esphomelib.git#v1.6.2' DEFAULT_LIBRARY_URI = u'https://github.com/OttoWinter/esphomelib.git#v1.6.2'
BUILD_FLASH_MODES = ['qio', 'qout', 'dio', 'dout'] BUILD_FLASH_MODES = ['qio', 'qout', 'dio', 'dout']
StartupTrigger = esphomelib_ns.StartupTrigger
ShutdownTrigger = esphomelib_ns.ShutdownTrigger
CORE_SCHEMA = vol.Schema({ CORE_SCHEMA = vol.Schema({
vol.Required(CONF_NAME): cv.valid_name, vol.Required(CONF_NAME): cv.valid_name,
@ -30,6 +32,13 @@ CORE_SCHEMA = vol.Schema({
vol.Optional(CONF_SIMPLIFY, default=True): cv.boolean, vol.Optional(CONF_SIMPLIFY, default=True): cv.boolean,
vol.Optional(CONF_USE_BUILD_FLAGS, default=True): cv.boolean, vol.Optional(CONF_USE_BUILD_FLAGS, default=True): cv.boolean,
vol.Optional(CONF_BOARD_FLASH_MODE): vol.All(vol.Lower, cv.one_of(*BUILD_FLASH_MODES)), vol.Optional(CONF_BOARD_FLASH_MODE): vol.All(vol.Lower, cv.one_of(*BUILD_FLASH_MODES)),
vol.Optional(CONF_ON_BOOT): vol.All(cv.ensure_list, [automation.AUTOMATION_SCHEMA.extend({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(StartupTrigger),
vol.Optional(CONF_PRIORITY): vol.Coerce(float),
})]),
vol.Optional(CONF_ON_SHUTDOWN): vol.All(cv.ensure_list, [automation.AUTOMATION_SCHEMA.extend({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_variable_id(ShutdownTrigger),
})]),
}) })
REQUIRED_COMPONENTS = [ REQUIRED_COMPONENTS = [
@ -43,6 +52,15 @@ _ALL_COMPONENTS = []
def core_to_code(config): def core_to_code(config):
add(App.set_name(config[CONF_NAME])) add(App.set_name(config[CONF_NAME]))
for conf in config.get(CONF_ON_BOOT, []):
rhs = App.register_component(StartupTrigger.new(conf.get(CONF_PRIORITY)))
trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs)
automation.build_automation(trigger, NoArg, conf)
for conf in config.get(CONF_ON_SHUTDOWN, []):
trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new())
automation.build_automation(trigger, const_char_p, conf)
def get_component(domain): def get_component(domain):
if domain in _COMPONENT_CACHE: if domain in _COMPONENT_CACHE:

View file

@ -231,6 +231,9 @@ CONF_CLOSE_ACTION = 'close_action'
CONF_STOP_ACTION = 'stop_action' CONF_STOP_ACTION = 'stop_action'
CONF_DOMAIN = 'domain' CONF_DOMAIN = 'domain'
CONF_OPTIMISTIC = 'optimistic' CONF_OPTIMISTIC = 'optimistic'
CONF_ON_BOOT = 'on_boot'
CONF_ON_SHUTDOWN = 'on_shutdown'
CONF_PRIORITY = 'priority'
ESP32_BOARDS = [ ESP32_BOARDS = [
'featheresp32', 'node32s', 'espea32', 'firebeetle32', 'esp32doit-devkit-v1', 'featheresp32', 'node32s', 'espea32', 'firebeetle32', 'esp32doit-devkit-v1',

View file

@ -534,6 +534,7 @@ std_string = std_ns.string
uint8 = global_ns.namespace('uint8_t') uint8 = global_ns.namespace('uint8_t')
uint16 = global_ns.namespace('uint16_t') uint16 = global_ns.namespace('uint16_t')
uint32 = global_ns.namespace('uint32_t') uint32 = global_ns.namespace('uint32_t')
const_char_p = global_ns.namespace('const char *')
NAN = global_ns.namespace('NAN') NAN = global_ns.namespace('NAN')
esphomelib_ns = global_ns # using namespace esphomelib; esphomelib_ns = global_ns # using namespace esphomelib;
NoArg = esphomelib_ns.NoArg NoArg = esphomelib_ns.NoArg