esphome/esphomeyaml/components/switch/ir_transmitter.py

105 lines
4.1 KiB
Python
Raw Normal View History

2018-04-07 01:23:03 +02:00
import voluptuous as vol
import esphomeyaml.config_validation as cv
from esphomeyaml.components import switch
2018-05-20 12:41:52 +02:00
from esphomeyaml.components.ir_transmitter import IRTransmitterComponent
2018-05-14 11:50:56 +02:00
from esphomeyaml.const import CONF_ADDRESS, CONF_CARRIER_FREQUENCY, CONF_COMMAND, CONF_DATA, \
CONF_INVERTED, CONF_IR_TRANSMITTER_ID, CONF_LG, CONF_NAME, CONF_NBITS, CONF_NEC, \
2018-05-20 12:41:52 +02:00
CONF_PANASONIC, CONF_RAW, CONF_REPEAT, CONF_SONY, CONF_TIMES, CONF_WAIT_TIME
2018-04-07 01:23:03 +02:00
from esphomeyaml.core import ESPHomeYAMLError
from esphomeyaml.helpers import App, ArrayInitializer, HexIntLiteral, get_variable
2018-04-07 01:23:03 +02:00
2018-05-14 11:50:56 +02:00
DEPENDENCIES = ['ir_transmitter']
IR_KEYS = [CONF_NEC, CONF_LG, CONF_SONY, CONF_PANASONIC, CONF_RAW]
WAIT_TIME_MESSAGE = "The wait_time_us option has been renamed to wait_time in order to decrease " \
"ambiguity. "
PLATFORM_SCHEMA = vol.All(switch.PLATFORM_SCHEMA.extend({
2018-04-07 01:23:03 +02:00
vol.Exclusive(CONF_NEC, 'code'): vol.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint16_t,
}),
vol.Exclusive(CONF_LG, 'code'): vol.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
vol.Optional(CONF_NBITS, default=28): vol.All(vol.Coerce(int), vol.Range(min=0, max=32)),
}),
vol.Exclusive(CONF_SONY, 'code'): vol.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
vol.Optional(CONF_NBITS, default=12): vol.All(vol.Coerce(int), vol.Range(min=0, max=32)),
}),
vol.Exclusive(CONF_PANASONIC, 'code'): vol.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint32_t,
}),
vol.Exclusive(CONF_RAW, 'code'): vol.Schema({
vol.Required(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
vol.Required(CONF_DATA): [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,
2018-05-14 11:50:56 +02:00
vol.Required(CONF_WAIT_TIME): cv.positive_time_period_microseconds,
vol.Optional('wait_time_us'): cv.invalid(WAIT_TIME_MESSAGE),
2018-04-07 01:23:03 +02:00
})),
2018-06-02 22:22:20 +02:00
cv.GenerateID(CONF_IR_TRANSMITTER_ID): cv.use_variable_id(IRTransmitterComponent),
vol.Optional(CONF_INVERTED): cv.invalid("IR Transmitters do not support inverted mode!"),
2018-05-20 12:41:52 +02:00
}).extend(switch.SWITCH_SCHEMA.schema), cv.has_at_least_one_key(*IR_KEYS))
# pylint: disable=invalid-name
2018-05-20 12:41:52 +02:00
ir_ns = switch.switch_ns.namespace('ir')
SendData = ir_ns.namespace('SendData')
DataTransmitter = IRTransmitterComponent.DataTransmitter
2018-04-07 01:23:03 +02:00
def safe_hex(value):
if value is None:
return None
return HexIntLiteral(value)
def exp_send_data(config):
if CONF_NEC in config:
conf = config[CONF_NEC]
base = SendData.from_nec(safe_hex(conf[CONF_ADDRESS]),
safe_hex(conf[CONF_COMMAND]))
elif CONF_LG in config:
conf = config[CONF_LG]
base = SendData.from_lg(safe_hex(conf[CONF_DATA]), conf.get(CONF_NBITS))
elif CONF_SONY in config:
conf = config[CONF_SONY]
base = SendData.from_sony(safe_hex(conf[CONF_DATA]), conf.get(CONF_NBITS))
elif CONF_PANASONIC in config:
conf = config[CONF_PANASONIC]
base = SendData.from_panasonic(safe_hex(conf[CONF_ADDRESS]),
safe_hex(conf[CONF_COMMAND]))
elif CONF_RAW in config:
conf = config[CONF_RAW]
data = ArrayInitializer(*conf[CONF_DATA])
base = SendData.from_raw(data, conf[CONF_CARRIER_FREQUENCY])
else:
raise ESPHomeYAMLError(u"Unsupported IR mode {}".format(config))
if CONF_REPEAT in config:
if isinstance(config[CONF_REPEAT], int):
times = config[CONF_REPEAT]
wait_us = None
else:
times = config[CONF_REPEAT][CONF_TIMES]
2018-05-14 11:50:56 +02:00
wait_us = config[CONF_REPEAT][CONF_WAIT_TIME]
2018-04-07 01:23:03 +02:00
base = base.repeat(times, wait_us)
return base
def to_code(config):
2018-06-02 22:22:20 +02:00
ir = None
for ir in get_variable(config[CONF_IR_TRANSMITTER_ID]):
yield
2018-04-07 01:23:03 +02:00
send_data = exp_send_data(config)
2018-05-20 12:41:52 +02:00
rhs = App.register_component(ir.create_transmitter(config[CONF_NAME], send_data))
switch.register_switch(rhs, config)
2018-05-06 15:56:12 +02:00
BUILD_FLAGS = '-DUSE_IR_TRANSMITTER'