2018-04-18 18:43:13 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome import config_validation as cv, pins
|
|
|
|
from esphome.automation import ACTION_REGISTRY, maybe_simple_id
|
|
|
|
from esphome.const import CONF_ID, CONF_MODE, CONF_NUMBER, CONF_PINS, CONF_RUN_CYCLES, \
|
2018-12-05 21:22:06 +01:00
|
|
|
CONF_RUN_DURATION, CONF_SLEEP_DURATION, CONF_WAKEUP_PIN
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.cpp_generator import Pvariable, StructInitializer, add, get_variable
|
|
|
|
from esphome.cpp_helpers import gpio_input_pin_expression, setup_component
|
|
|
|
from esphome.cpp_types import Action, App, Component, esphome_ns, global_ns
|
2018-04-18 18:43:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def validate_pin_number(value):
|
|
|
|
valid_pins = [0, 2, 4, 12, 13, 14, 15, 25, 26, 27, 32, 39]
|
2018-05-17 19:57:55 +02:00
|
|
|
if value[CONF_NUMBER] not in valid_pins:
|
2018-04-18 18:43:13 +02:00
|
|
|
raise vol.Invalid(u"Only pins {} support wakeup"
|
|
|
|
u"".format(', '.join(str(x) for x in valid_pins)))
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
DeepSleepComponent = esphome_ns.class_('DeepSleepComponent', Component)
|
|
|
|
EnterDeepSleepAction = esphome_ns.class_('EnterDeepSleepAction', Action)
|
|
|
|
PreventDeepSleepAction = esphome_ns.class_('PreventDeepSleepAction', Action)
|
2018-06-02 22:22:20 +02:00
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
WakeupPinMode = esphome_ns.enum('WakeupPinMode')
|
2018-06-05 22:31:39 +02:00
|
|
|
WAKEUP_PIN_MODES = {
|
2018-11-12 23:30:31 +01:00
|
|
|
'IGNORE': WakeupPinMode.WAKEUP_PIN_MODE_IGNORE,
|
|
|
|
'KEEP_AWAKE': WakeupPinMode.WAKEUP_PIN_MODE_KEEP_AWAKE,
|
|
|
|
'INVERT_WAKEUP': WakeupPinMode.WAKEUP_PIN_MODE_INVERT_WAKEUP,
|
2018-06-05 22:31:39 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 15:36:49 +01:00
|
|
|
esp_sleep_ext1_wakeup_mode_t = global_ns.enum('esp_sleep_ext1_wakeup_mode_t')
|
2019-02-13 16:54:02 +01:00
|
|
|
Ext1Wakeup = esphome_ns.struct('Ext1Wakeup')
|
2018-11-13 15:36:49 +01:00
|
|
|
EXT1_WAKEUP_MODES = {
|
|
|
|
'ALL_LOW': esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ALL_LOW,
|
|
|
|
'ANY_HIGH': esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ANY_HIGH,
|
|
|
|
}
|
|
|
|
|
2018-06-05 22:31:39 +02:00
|
|
|
CONF_WAKEUP_PIN_MODE = 'wakeup_pin_mode'
|
2018-11-13 15:36:49 +01:00
|
|
|
CONF_ESP32_EXT1_WAKEUP = 'esp32_ext1_wakeup'
|
2018-06-05 22:31:39 +02:00
|
|
|
|
2019-02-26 19:22:33 +01:00
|
|
|
CONFIG_SCHEMA = cv.Schema({
|
2018-06-02 22:22:20 +02:00
|
|
|
cv.GenerateID(): cv.declare_variable_id(DeepSleepComponent),
|
2018-05-14 11:50:56 +02:00
|
|
|
vol.Optional(CONF_SLEEP_DURATION): cv.positive_time_period_milliseconds,
|
2018-06-06 08:55:53 +02:00
|
|
|
vol.Optional(CONF_WAKEUP_PIN): vol.All(cv.only_on_esp32, pins.internal_gpio_input_pin_schema,
|
2018-05-17 19:57:55 +02:00
|
|
|
validate_pin_number),
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Optional(CONF_WAKEUP_PIN_MODE): vol.All(cv.only_on_esp32,
|
|
|
|
cv.one_of(*WAKEUP_PIN_MODES), upper=True),
|
2019-02-26 19:22:33 +01:00
|
|
|
vol.Optional(CONF_ESP32_EXT1_WAKEUP): vol.All(cv.only_on_esp32, cv.Schema({
|
2018-12-18 19:31:43 +01:00
|
|
|
vol.Required(CONF_PINS): cv.ensure_list(pins.shorthand_input_pin, validate_pin_number),
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Required(CONF_MODE): cv.one_of(*EXT1_WAKEUP_MODES, upper=True),
|
2018-11-13 15:36:49 +01:00
|
|
|
})),
|
2018-05-14 11:50:56 +02:00
|
|
|
vol.Optional(CONF_RUN_DURATION): cv.positive_time_period_milliseconds,
|
2019-01-19 16:51:20 +01:00
|
|
|
|
|
|
|
vol.Optional(CONF_RUN_CYCLES): cv.invalid("The run_cycles option has been removed in 1.11.0 as "
|
|
|
|
"it was essentially the same as a run_duration of 0s."
|
|
|
|
"Please use run_duration now.")
|
2018-11-12 23:30:31 +01:00
|
|
|
}).extend(cv.COMPONENT_SCHEMA.schema)
|
2018-04-18 18:43:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def to_code(config):
|
|
|
|
rhs = App.make_deep_sleep_component()
|
2018-06-02 22:22:20 +02:00
|
|
|
deep_sleep = Pvariable(config[CONF_ID], rhs)
|
2018-04-18 18:43:13 +02:00
|
|
|
if CONF_SLEEP_DURATION in config:
|
|
|
|
add(deep_sleep.set_sleep_duration(config[CONF_SLEEP_DURATION]))
|
|
|
|
if CONF_WAKEUP_PIN in config:
|
2018-06-02 22:22:20 +02:00
|
|
|
for pin in gpio_input_pin_expression(config[CONF_WAKEUP_PIN]):
|
|
|
|
yield
|
2018-04-18 18:43:13 +02:00
|
|
|
add(deep_sleep.set_wakeup_pin(pin))
|
2018-06-05 22:31:39 +02:00
|
|
|
if CONF_WAKEUP_PIN_MODE in config:
|
|
|
|
add(deep_sleep.set_wakeup_pin_mode(WAKEUP_PIN_MODES[config[CONF_WAKEUP_PIN_MODE]]))
|
2018-04-18 18:43:13 +02:00
|
|
|
if CONF_RUN_DURATION in config:
|
|
|
|
add(deep_sleep.set_run_duration(config[CONF_RUN_DURATION]))
|
|
|
|
|
2018-11-13 15:36:49 +01:00
|
|
|
if CONF_ESP32_EXT1_WAKEUP in config:
|
|
|
|
conf = config[CONF_ESP32_EXT1_WAKEUP]
|
|
|
|
mask = 0
|
|
|
|
for pin in conf[CONF_PINS]:
|
|
|
|
mask |= 1 << pin[CONF_NUMBER]
|
|
|
|
struct = StructInitializer(
|
|
|
|
Ext1Wakeup,
|
|
|
|
('mask', mask),
|
|
|
|
('wakeup_mode', EXT1_WAKEUP_MODES[conf[CONF_MODE]])
|
|
|
|
)
|
|
|
|
add(deep_sleep.set_ext1_wakeup(struct))
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
setup_component(deep_sleep, config)
|
2018-04-18 18:43:13 +02:00
|
|
|
|
2018-10-16 23:16:06 +02:00
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
BUILD_FLAGS = '-DUSE_DEEP_SLEEP'
|
2018-10-16 23:16:06 +02:00
|
|
|
|
|
|
|
CONF_DEEP_SLEEP_ENTER = 'deep_sleep.enter'
|
|
|
|
DEEP_SLEEP_ENTER_ACTION_SCHEMA = maybe_simple_id({
|
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(DeepSleepComponent),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_DEEP_SLEEP_ENTER, DEEP_SLEEP_ENTER_ACTION_SCHEMA)
|
2019-02-26 19:28:11 +01:00
|
|
|
def deep_sleep_enter_to_code(config, action_id, template_arg, args):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_enter_deep_sleep_action(template_arg)
|
2019-02-26 19:28:11 +01:00
|
|
|
type = EnterDeepSleepAction.template(template_arg)
|
2018-10-16 23:16:06 +02:00
|
|
|
yield Pvariable(action_id, rhs, type=type)
|
|
|
|
|
|
|
|
|
|
|
|
CONF_DEEP_SLEEP_PREVENT = 'deep_sleep.prevent'
|
|
|
|
DEEP_SLEEP_PREVENT_ACTION_SCHEMA = maybe_simple_id({
|
|
|
|
vol.Required(CONF_ID): cv.use_variable_id(DeepSleepComponent),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@ACTION_REGISTRY.register(CONF_DEEP_SLEEP_PREVENT, DEEP_SLEEP_PREVENT_ACTION_SCHEMA)
|
2019-02-26 19:28:11 +01:00
|
|
|
def deep_sleep_prevent_to_code(config, action_id, template_arg, args):
|
2018-10-16 23:16:06 +02:00
|
|
|
for var in get_variable(config[CONF_ID]):
|
|
|
|
yield None
|
|
|
|
rhs = var.make_prevent_deep_sleep_action(template_arg)
|
2019-02-26 19:28:11 +01:00
|
|
|
type = PreventDeepSleepAction.template(template_arg)
|
2018-10-16 23:16:06 +02:00
|
|
|
yield Pvariable(action_id, rhs, type=type)
|