2018-06-11 10:01:54 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from esphomeyaml.components import switch
|
2018-08-13 19:11:33 +02:00
|
|
|
from esphomeyaml.components.remote_transmitter import RC_SWITCH_RAW_SCHEMA, \
|
|
|
|
RC_SWITCH_TYPE_A_SCHEMA, RC_SWITCH_TYPE_B_SCHEMA, RC_SWITCH_TYPE_C_SCHEMA, \
|
|
|
|
RC_SWITCH_TYPE_D_SCHEMA, RemoteTransmitterComponent, binary_code, build_rc_switch_protocol, \
|
|
|
|
remote_ns
|
2018-11-12 23:30:31 +01:00
|
|
|
import esphomeyaml.config_validation as cv
|
2018-08-13 19:11:33 +02:00
|
|
|
from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_CHANNEL, CONF_CODE, \
|
|
|
|
CONF_COMMAND, CONF_DATA, CONF_DEVICE, CONF_FAMILY, CONF_GROUP, CONF_INVERTED, CONF_LG, \
|
|
|
|
CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, CONF_PROTOCOL, CONF_RAW, CONF_RC_SWITCH_RAW, \
|
2018-08-25 22:18:22 +02:00
|
|
|
CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B, CONF_RC_SWITCH_TYPE_C, CONF_RC_SWITCH_TYPE_D, \
|
2018-10-13 19:21:06 +02:00
|
|
|
CONF_REPEAT, CONF_SAMSUNG, CONF_SONY, CONF_STATE, CONF_TIMES, \
|
2018-08-13 19:11:33 +02:00
|
|
|
CONF_WAIT_TIME
|
2018-12-05 21:22:06 +01:00
|
|
|
from esphomeyaml.cpp_generator import ArrayInitializer, Pvariable, add, get_variable
|
2018-06-11 10:01:54 +02:00
|
|
|
|
|
|
|
DEPENDENCIES = ['remote_transmitter']
|
|
|
|
|
2018-10-13 19:21:06 +02:00
|
|
|
REMOTE_KEYS = [CONF_NEC, CONF_LG, CONF_SAMSUNG, CONF_SONY, CONF_PANASONIC, CONF_RAW,
|
|
|
|
CONF_RC_SWITCH_RAW, CONF_RC_SWITCH_TYPE_A, CONF_RC_SWITCH_TYPE_B,
|
|
|
|
CONF_RC_SWITCH_TYPE_C, CONF_RC_SWITCH_TYPE_D]
|
2018-06-11 10:01:54 +02:00
|
|
|
|
|
|
|
CONF_REMOTE_TRANSMITTER_ID = 'remote_transmitter_id'
|
|
|
|
CONF_TRANSMITTER_ID = 'transmitter_id'
|
|
|
|
|
2018-11-12 23:30:31 +01:00
|
|
|
RemoteTransmitter = remote_ns.class_('RemoteTransmitter', switch.Switch)
|
|
|
|
LGTransmitter = remote_ns.class_('LGTransmitter', RemoteTransmitter)
|
|
|
|
NECTransmitter = remote_ns.class_('NECTransmitter', RemoteTransmitter)
|
|
|
|
PanasonicTransmitter = remote_ns.class_('PanasonicTransmitter', RemoteTransmitter)
|
|
|
|
RawTransmitter = remote_ns.class_('RawTransmitter', RemoteTransmitter)
|
|
|
|
SamsungTransmitter = remote_ns.class_('SamsungTransmitter', RemoteTransmitter)
|
|
|
|
SonyTransmitter = remote_ns.class_('SonyTransmitter', RemoteTransmitter)
|
|
|
|
RCSwitchRawTransmitter = remote_ns.class_('RCSwitchRawTransmitter', RemoteTransmitter)
|
|
|
|
RCSwitchTypeATransmitter = remote_ns.class_('RCSwitchTypeATransmitter', RCSwitchRawTransmitter)
|
|
|
|
RCSwitchTypeBTransmitter = remote_ns.class_('RCSwitchTypeBTransmitter', RCSwitchRawTransmitter)
|
|
|
|
RCSwitchTypeCTransmitter = remote_ns.class_('RCSwitchTypeCTransmitter', RCSwitchRawTransmitter)
|
|
|
|
RCSwitchTypeDTransmitter = remote_ns.class_('RCSwitchTypeDTransmitter', RCSwitchRawTransmitter)
|
2018-06-11 10:01:54 +02:00
|
|
|
|
2018-06-13 21:27:58 +02:00
|
|
|
validate_raw_data = [vol.Any(vol.Coerce(int), cv.time_period_microseconds)]
|
|
|
|
|
2018-06-11 10:01:54 +02:00
|
|
|
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
|
2018-11-08 16:01:07 +01:00
|
|
|
cv.GenerateID(): cv.declare_variable_id(RemoteTransmitter),
|
2018-06-11 10:01:54 +02:00
|
|
|
vol.Optional(CONF_LG): vol.Schema({
|
|
|
|
vol.Required(CONF_DATA): cv.hex_uint32_t,
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Optional(CONF_NBITS, default=28): cv.one_of(28, 32, int=True),
|
2018-06-11 10:01:54 +02:00
|
|
|
}),
|
|
|
|
vol.Optional(CONF_NEC): vol.Schema({
|
|
|
|
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
|
|
|
|
vol.Required(CONF_COMMAND): cv.hex_uint16_t,
|
|
|
|
}),
|
2018-10-13 19:21:06 +02:00
|
|
|
vol.Optional(CONF_SAMSUNG): vol.Schema({
|
|
|
|
vol.Required(CONF_DATA): cv.hex_uint32_t,
|
|
|
|
}),
|
2018-06-11 10:01:54 +02:00
|
|
|
vol.Optional(CONF_SONY): vol.Schema({
|
|
|
|
vol.Required(CONF_DATA): cv.hex_uint32_t,
|
2018-11-23 13:51:22 +01:00
|
|
|
vol.Optional(CONF_NBITS, default=12): cv.one_of(12, 15, 20, int=True),
|
2018-06-11 10:01:54 +02:00
|
|
|
}),
|
|
|
|
vol.Optional(CONF_PANASONIC): vol.Schema({
|
|
|
|
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
|
|
|
|
vol.Required(CONF_COMMAND): cv.hex_uint32_t,
|
|
|
|
}),
|
2018-06-13 21:27:58 +02:00
|
|
|
vol.Optional(CONF_RAW): vol.Any(validate_raw_data, vol.Schema({
|
|
|
|
vol.Required(CONF_DATA): validate_raw_data,
|
2018-06-11 10:01:54 +02:00
|
|
|
vol.Optional(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
|
2018-06-13 21:27:58 +02:00
|
|
|
})),
|
2018-08-13 19:11:33 +02:00
|
|
|
vol.Optional(CONF_RC_SWITCH_RAW): RC_SWITCH_RAW_SCHEMA,
|
|
|
|
vol.Optional(CONF_RC_SWITCH_TYPE_A): RC_SWITCH_TYPE_A_SCHEMA,
|
|
|
|
vol.Optional(CONF_RC_SWITCH_TYPE_B): RC_SWITCH_TYPE_B_SCHEMA,
|
|
|
|
vol.Optional(CONF_RC_SWITCH_TYPE_C): RC_SWITCH_TYPE_C_SCHEMA,
|
|
|
|
vol.Optional(CONF_RC_SWITCH_TYPE_D): RC_SWITCH_TYPE_D_SCHEMA,
|
|
|
|
|
2018-06-11 10:01:54 +02:00
|
|
|
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,
|
|
|
|
})),
|
|
|
|
cv.GenerateID(CONF_REMOTE_TRANSMITTER_ID): cv.use_variable_id(RemoteTransmitterComponent),
|
|
|
|
cv.GenerateID(CONF_TRANSMITTER_ID): cv.declare_variable_id(RemoteTransmitter),
|
|
|
|
vol.Optional(CONF_INVERTED): cv.invalid("Remote Transmitters do not support inverted mode!"),
|
2018-08-13 19:11:33 +02:00
|
|
|
}), cv.has_exactly_one_key(*REMOTE_KEYS))
|
|
|
|
|
|
|
|
|
|
|
|
def transmitter_base(full_config):
|
|
|
|
name = full_config[CONF_NAME]
|
|
|
|
key, config = next((k, v) for k, v in full_config.items() if k in REMOTE_KEYS)
|
|
|
|
|
|
|
|
if key == CONF_LG:
|
|
|
|
return LGTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
|
|
|
|
elif key == CONF_NEC:
|
|
|
|
return NECTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
|
|
|
|
elif key == CONF_PANASONIC:
|
|
|
|
return PanasonicTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
|
2018-10-13 19:21:06 +02:00
|
|
|
elif key == CONF_SAMSUNG:
|
|
|
|
return SamsungTransmitter.new(name, config[CONF_DATA])
|
2018-08-13 19:11:33 +02:00
|
|
|
elif key == CONF_SONY:
|
|
|
|
return SonyTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
|
|
|
|
elif key == CONF_RAW:
|
|
|
|
if isinstance(config, dict):
|
|
|
|
data = config[CONF_DATA]
|
|
|
|
carrier_frequency = config.get(CONF_CARRIER_FREQUENCY)
|
2018-06-13 21:27:58 +02:00
|
|
|
else:
|
2018-08-13 19:11:33 +02:00
|
|
|
data = config
|
2018-06-13 21:27:58 +02:00
|
|
|
carrier_frequency = None
|
2018-08-13 19:11:33 +02:00
|
|
|
return RawTransmitter.new(name, ArrayInitializer(*data, multiline=False),
|
2018-06-13 21:27:58 +02:00
|
|
|
carrier_frequency)
|
2018-08-13 19:11:33 +02:00
|
|
|
elif key == CONF_RC_SWITCH_RAW:
|
|
|
|
return RCSwitchRawTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
|
|
|
|
binary_code(config[CONF_CODE]), len(config[CONF_CODE]))
|
|
|
|
elif key == CONF_RC_SWITCH_TYPE_A:
|
|
|
|
return RCSwitchTypeATransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
|
|
|
|
binary_code(config[CONF_GROUP]),
|
|
|
|
binary_code(config[CONF_DEVICE]),
|
|
|
|
config[CONF_STATE])
|
|
|
|
elif key == CONF_RC_SWITCH_TYPE_B:
|
|
|
|
return RCSwitchTypeBTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
|
|
|
|
config[CONF_ADDRESS], config[CONF_CHANNEL],
|
|
|
|
config[CONF_STATE])
|
|
|
|
elif key == CONF_RC_SWITCH_TYPE_C:
|
|
|
|
return RCSwitchTypeCTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
|
|
|
|
ord(config[CONF_FAMILY][0]) - ord('a'),
|
|
|
|
config[CONF_GROUP], config[CONF_DEVICE],
|
|
|
|
config[CONF_STATE])
|
|
|
|
elif key == CONF_RC_SWITCH_TYPE_D:
|
|
|
|
return RCSwitchTypeDTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
|
|
|
|
ord(config[CONF_GROUP][0]) - ord('a'),
|
|
|
|
config[CONF_DEVICE], config[CONF_STATE])
|
2018-06-11 10:01:54 +02:00
|
|
|
else:
|
2018-08-13 19:11:33 +02:00
|
|
|
raise NotImplementedError("Unknown transmitter type {}".format(config))
|
2018-06-11 10:01:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def to_code(config):
|
|
|
|
for remote in get_variable(config[CONF_REMOTE_TRANSMITTER_ID]):
|
|
|
|
yield
|
2018-11-10 14:07:11 +01:00
|
|
|
rhs = transmitter_base(config)
|
2018-06-11 10:01:54 +02:00
|
|
|
transmitter = Pvariable(config[CONF_TRANSMITTER_ID], rhs)
|
|
|
|
|
|
|
|
if CONF_REPEAT in config:
|
|
|
|
if isinstance(config[CONF_REPEAT], int):
|
|
|
|
times = config[CONF_REPEAT]
|
|
|
|
wait_us = 1000
|
|
|
|
else:
|
|
|
|
times = config[CONF_REPEAT][CONF_TIMES]
|
|
|
|
wait_us = config[CONF_REPEAT][CONF_WAIT_TIME]
|
|
|
|
add(transmitter.set_repeat(times, wait_us))
|
|
|
|
|
|
|
|
switch.register_switch(remote.add_transmitter(transmitter), config)
|
|
|
|
|
|
|
|
|
|
|
|
BUILD_FLAGS = '-DUSE_REMOTE_TRANSMITTER'
|
2018-11-03 14:08:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def to_hass_config(data, config):
|
|
|
|
return switch.core_to_hass_config(data, config)
|