esphome/esphomeyaml/components/switch/remote_transmitter.py

163 lines
7.9 KiB
Python
Raw Normal View History

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
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_JVC, \
CONF_LG, CONF_NAME, CONF_NBITS, CONF_NEC, CONF_PANASONIC, CONF_PROTOCOL, 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, CONF_REPEAT, CONF_SAMSUNG, CONF_SONY, CONF_STATE, CONF_TIMES, \
CONF_WAIT_TIME, CONF_ID
from esphomeyaml.cpp_generator import Pvariable, add, get_variable, progmem_array
from esphomeyaml.cpp_types import int32
DEPENDENCIES = ['remote_transmitter']
REMOTE_KEYS = [CONF_JVC, 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]
CONF_REMOTE_TRANSMITTER_ID = 'remote_transmitter_id'
CONF_TRANSMITTER_ID = 'transmitter_id'
RemoteTransmitter = remote_ns.class_('RemoteTransmitter', switch.Switch)
JVCTransmitter = remote_ns.class_('JVCTransmitter', RemoteTransmitter)
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)
def validate_raw(value):
if isinstance(value, dict):
return vol.Schema({
cv.GenerateID(): cv.declare_variable_id(int32),
vol.Required(CONF_DATA): [vol.Any(vol.Coerce(int), cv.time_period_microseconds)],
vol.Optional(CONF_CARRIER_FREQUENCY): vol.All(cv.frequency, vol.Coerce(int)),
})(value)
return validate_raw({
CONF_DATA: value
})
2018-06-13 21:27:58 +02:00
PLATFORM_SCHEMA = cv.nameable(switch.SWITCH_PLATFORM_SCHEMA.extend({
cv.GenerateID(): cv.declare_variable_id(RemoteTransmitter),
vol.Optional(CONF_JVC): vol.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
}),
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),
}),
vol.Optional(CONF_NEC): vol.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint16_t,
}),
vol.Optional(CONF_SAMSUNG): vol.Schema({
vol.Required(CONF_DATA): cv.hex_uint32_t,
}),
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),
}),
vol.Optional(CONF_PANASONIC): vol.Schema({
vol.Required(CONF_ADDRESS): cv.hex_uint16_t,
vol.Required(CONF_COMMAND): cv.hex_uint32_t,
}),
vol.Optional(CONF_RAW): validate_raw,
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,
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_JVC:
return JVCTransmitter.new(name, config[CONF_DATA])
2018-08-13 19:11:33 +02:00
if key == CONF_LG:
return LGTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
if key == CONF_NEC:
2018-08-13 19:11:33 +02:00
return NECTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
if key == CONF_PANASONIC:
2018-08-13 19:11:33 +02:00
return PanasonicTransmitter.new(name, config[CONF_ADDRESS], config[CONF_COMMAND])
if key == CONF_SAMSUNG:
return SamsungTransmitter.new(name, config[CONF_DATA])
if key == CONF_SONY:
2018-08-13 19:11:33 +02:00
return SonyTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
if key == CONF_RAW:
arr = progmem_array(config[CONF_ID], config[CONF_DATA])
return RawTransmitter.new(name, arr, len(config[CONF_DATA]),
config.get(CONF_CARRIER_FREQUENCY))
if key == CONF_RC_SWITCH_RAW:
2018-08-13 19:11:33 +02:00
return RCSwitchRawTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
binary_code(config[CONF_CODE]), len(config[CONF_CODE]))
if key == CONF_RC_SWITCH_TYPE_A:
2018-08-13 19:11:33 +02:00
return RCSwitchTypeATransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
binary_code(config[CONF_GROUP]),
binary_code(config[CONF_DEVICE]),
config[CONF_STATE])
if key == CONF_RC_SWITCH_TYPE_B:
2018-08-13 19:11:33 +02:00
return RCSwitchTypeBTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
config[CONF_ADDRESS], config[CONF_CHANNEL],
config[CONF_STATE])
if key == CONF_RC_SWITCH_TYPE_C:
2018-08-13 19:11:33 +02:00
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])
if key == CONF_RC_SWITCH_TYPE_D:
2018-08-13 19:11:33 +02:00
return RCSwitchTypeDTransmitter.new(name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
ord(config[CONF_GROUP][0]) - ord('a'),
config[CONF_DEVICE], config[CONF_STATE])
raise NotImplementedError("Unknown transmitter type {}".format(config))
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)
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'
def to_hass_config(data, config):
return switch.core_to_hass_config(data, config)