Add light restore mode

I don't want users to rely on setup priority.

Ref https://github.com/esphome/esphome-docs/pull/248
This commit is contained in:
Otto Winter
2019-05-27 19:22:16 +02:00
parent 72218171b3
commit 4bc3067725
3 changed files with 41 additions and 6 deletions
+13 -1
View File
@@ -3,7 +3,7 @@ import esphome.config_validation as cv
from esphome.components import mqtt, power_supply
from esphome.const import CONF_COLOR_CORRECT, \
CONF_DEFAULT_TRANSITION_LENGTH, CONF_EFFECTS, CONF_GAMMA_CORRECT, CONF_ID, \
CONF_INTERNAL, CONF_NAME, CONF_MQTT_ID, CONF_POWER_SUPPLY
CONF_INTERNAL, CONF_NAME, CONF_MQTT_ID, CONF_POWER_SUPPLY, CONF_RESTORE_MODE
from esphome.core import coroutine, coroutine_with_priority
from .automation import light_control_to_code # noqa
from .effects import validate_effects, BINARY_EFFECTS, \
@@ -12,9 +12,20 @@ from .types import ( # noqa
LightState, AddressableLightState, light_ns, LightOutput, AddressableLight)
IS_PLATFORM_COMPONENT = True
LightRestoreMode = light_ns.enum('LightRestoreMode')
RESTORE_MODES = {
'RESTORE_DEFAULT_OFF': LightRestoreMode.LIGHT_RESTORE_DEFAULT_OFF,
'RESTORE_DEFAULT_ON': LightRestoreMode.LIGHT_RESTORE_DEFAULT_ON,
'ALWAYS_OFF': LightRestoreMode.LIGHT_ALWAYS_OFF,
'ALWAYS_ON': LightRestoreMode.LIGHT_ALWAYS_ON,
}
LIGHT_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(LightState),
cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_id(mqtt.MQTTJSONLightComponent),
cv.Optional(CONF_RESTORE_MODE, default='restore_default_off'):
cv.enum(RESTORE_MODES, upper=True, space='_'),
})
BINARY_LIGHT_SCHEMA = LIGHT_SCHEMA.extend({
@@ -41,6 +52,7 @@ ADDRESSABLE_LIGHT_SCHEMA = RGB_LIGHT_SCHEMA.extend({
@coroutine
def setup_light_core_(light_var, output_var, config):
cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE]))
if CONF_INTERNAL in config:
cg.add(light_var.set_internal(config[CONF_INTERNAL]))
if CONF_DEFAULT_TRANSITION_LENGTH in config: