1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 06:33:51 +00:00

Allow simpler automation syntax

This commit is contained in:
Otto Winter
2018-06-13 21:27:58 +02:00
parent 9bb06782b2
commit 6c6d21a7ab
10 changed files with 70 additions and 51 deletions

View File

@@ -22,6 +22,8 @@ PanasonicTransmitter = remote_ns.PanasonicTransmitter
RawTransmitter = remote_ns.RawTransmitter
SonyTransmitter = remote_ns.SonyTransmitter
validate_raw_data = [vol.Any(vol.Coerce(int), cv.time_period_microseconds)]
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_LG): vol.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
@@ -39,10 +41,10 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint32_t,
}),
vol.Optional(CONF_RAW): vol.Schema({
vol.Required(CONF_DATA): [vol.Any(vol.Coerce(int), cv.time_period_microseconds)],
vol.Optional(CONF_RAW): vol.Any(validate_raw_data, vol.Schema({
vol.Required(CONF_DATA): validate_raw_data,
vol.Optional(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
}),
})),
vol.Optional(CONF_REPEAT): vol.Any(cv.positive_not_null_int, vol.Schema({
vol.Required(CONF_TIMES): cv.positive_not_null_int,
vol.Required(CONF_WAIT_TIME): cv.positive_time_period_microseconds,
@@ -69,8 +71,14 @@ def transmitter_base(config):
return SonyTransmitter.new(config[CONF_NAME], conf[CONF_DATA], conf[CONF_NBITS])
elif CONF_RAW in config:
conf = config[CONF_RAW]
data = ArrayInitializer(*conf[CONF_DATA])
return RawTransmitter.new(data, conf[CONF_CARRIER_FREQUENCY])
if isinstance(conf, dict):
data = conf[CONF_DATA]
carrier_frequency = conf.get(CONF_CARRIER_FREQUENCY)
else:
data = conf
carrier_frequency = None
return RawTransmitter.new(config[CONF_NAME], ArrayInitializer(*data, multiline=False),
carrier_frequency)
else:
raise ValueError("Unknown transmitter type {}".format(config))

View File

@@ -14,8 +14,8 @@ PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(CONF_MAKE_ID): cv.declare_variable_id(MakeTemplateSwitch),
vol.Optional(CONF_LAMBDA): cv.lambda_,
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_TURN_OFF_ACTION): automation.ACTIONS_SCHEMA,
vol.Optional(CONF_TURN_ON_ACTION): automation.ACTIONS_SCHEMA,
vol.Optional(CONF_TURN_OFF_ACTION): automation.validate_automation(),
vol.Optional(CONF_TURN_ON_ACTION): automation.validate_automation(),
}), cv.has_at_least_one_key(CONF_LAMBDA, CONF_OPTIMISTIC))
@@ -30,15 +30,11 @@ def to_code(config):
yield
add(make.Ptemplate_.set_state_lambda(template_))
if CONF_TURN_OFF_ACTION in config:
actions = None
for actions in automation.build_actions(config[CONF_TURN_OFF_ACTION], NoArg):
yield
add(make.Ptemplate_.add_turn_off_actions(actions))
automation.build_automation(make.Ptemplate_.get_turn_off_trigger(), NoArg,
config[CONF_TURN_OFF_ACTION])
if CONF_TURN_ON_ACTION in config:
actions = None
for actions in automation.build_actions(config[CONF_TURN_ON_ACTION], NoArg):
yield
add(make.Ptemplate_.add_turn_on_actions(actions))
automation.build_automation(make.Ptemplate_.get_turn_on_trigger(), NoArg,
config[CONF_TURN_ON_ACTION])
if CONF_OPTIMISTIC in config:
add(make.Ptemplate_.set_optimistic(config[CONF_OPTIMISTIC]))