diff --git a/esphome/components/remote_base/__init__.py b/esphome/components/remote_base/__init__.py index 3d57041e56..2c8b6be51c 100644 --- a/esphome/components/remote_base/__init__.py +++ b/esphome/components/remote_base/__init__.py @@ -5,7 +5,7 @@ from esphome.components import binary_sensor from esphome.const import CONF_DATA, CONF_TRIGGER_ID, CONF_NBITS, CONF_ADDRESS, \ CONF_COMMAND, CONF_CODE, CONF_PULSE_LENGTH, CONF_SYNC, CONF_ZERO, CONF_ONE, CONF_INVERTED, \ CONF_PROTOCOL, CONF_GROUP, CONF_DEVICE, CONF_STATE, CONF_CHANNEL, CONF_FAMILY, CONF_REPEAT, \ - CONF_WAIT_TIME, CONF_TIMES, CONF_TYPE_ID, CONF_CARRIER_FREQUENCY + CONF_WAIT_TIME, CONF_TIMES, CONF_TYPE_ID, CONF_CARRIER_FREQUENCY, CONF_RC_CODE_1, CONF_RC_CODE_2 from esphome.core import coroutine from esphome.util import Registry, SimpleRegistry @@ -86,7 +86,7 @@ def validate_repeat(value): if isinstance(value, dict): return cv.Schema({ cv.Required(CONF_TIMES): cv.templatable(cv.positive_int), - cv.Optional(CONF_WAIT_TIME, default='10ms'): + cv.Optional(CONF_WAIT_TIME, default='25ms'): cv.templatable(cv.positive_time_period_microseconds), })(value) return validate_repeat({CONF_TIMES: value}) @@ -288,6 +288,42 @@ def nec_action(var, config, args): cg.add(var.set_command(template_)) +# Pioneer +(PioneerData, PioneerBinarySensor, PioneerTrigger, PioneerAction, + PioneerDumper) = declare_protocol('Pioneer') +PIONEER_SCHEMA = cv.Schema({ + cv.Required(CONF_RC_CODE_1): cv.hex_uint16_t, + cv.Optional(CONF_RC_CODE_2, default=0): cv.hex_uint16_t, +}) + + +@register_binary_sensor('pioneer', PioneerBinarySensor, PIONEER_SCHEMA) +def pioneer_binary_sensor(var, config): + cg.add(var.set_data(cg.StructInitializer( + PioneerData, + ('rc_code_1', config[CONF_RC_CODE_1]), + ('rc_code_2', config[CONF_RC_CODE_2]), + ))) + + +@register_trigger('pioneer', PioneerTrigger, PioneerData) +def pioneer_trigger(var, config): + pass + + +@register_dumper('pioneer', PioneerDumper) +def pioneer_dumper(var, config): + pass + + +@register_action('pioneer', PioneerAction, PIONEER_SCHEMA) +def pioneer_action(var, config, args): + template_ = yield cg.templatable(config[CONF_RC_CODE_1], args, cg.uint16) + cg.add(var.set_rc_code_1(template_)) + template_ = yield cg.templatable(config[CONF_RC_CODE_2], args, cg.uint16) + cg.add(var.set_rc_code_2(template_)) + + # Sony SonyData, SonyBinarySensor, SonyTrigger, SonyAction, SonyDumper = declare_protocol('Sony') SONY_SCHEMA = cv.Schema({ diff --git a/esphome/components/remote_base/pioneer_protocol.cpp b/esphome/components/remote_base/pioneer_protocol.cpp new file mode 100644 index 0000000000..49a27e08e7 --- /dev/null +++ b/esphome/components/remote_base/pioneer_protocol.cpp @@ -0,0 +1,150 @@ +#include "pioneer_protocol.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace remote_base { + +static const char *TAG = "remote.pioneer"; + +static const uint32_t HEADER_HIGH_US = 9000; +static const uint32_t HEADER_LOW_US = 4500; +static const uint32_t BIT_HIGH_US = 560; +static const uint32_t BIT_ONE_LOW_US = 1690; +static const uint32_t BIT_ZERO_LOW_US = 560; +static const uint32_t TRAILER_SPACE_US = 25500; + +void PioneerProtocol::encode(RemoteTransmitData *dst, const PioneerData &data) { + uint32_t address1 = ((data.rc_code_1 & 0xff00) | (~(data.rc_code_1 >> 8) & 0xff)); + uint32_t address2 = ((data.rc_code_2 & 0xff00) | (~(data.rc_code_2 >> 8) & 0xff)); + uint32_t command1 = 0; + uint32_t command2 = 0; + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((data.rc_code_1 >> bit) & 1) + command1 |= (1UL << (7 - bit)); + } + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((data.rc_code_1 >> (bit + 4)) & 1) + command1 |= (1UL << (3 - bit)); + } + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((data.rc_code_2 >> bit) & 1) + command2 |= (1UL << (7 - bit)); + } + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((data.rc_code_2 >> (bit + 4)) & 1) + command2 |= (1UL << (3 - bit)); + } + + command1 = (command1 << 8) | ((~command1) & 0xff); + command2 = (command2 << 8) | ((~command2) & 0xff); + + if (data.rc_code_2 == 0) + dst->reserve(68); + else + dst->reserve((68 * 2) + 1); + + dst->set_carrier_frequency(40000); + + dst->item(HEADER_HIGH_US, HEADER_LOW_US); + for (uint32_t mask = 1UL << 15; mask; mask >>= 1) { + if (address1 & mask) + dst->item(BIT_HIGH_US, BIT_ONE_LOW_US); + else + dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US); + } + + for (uint32_t mask = 1UL << 15; mask; mask >>= 1) { + if (command1 & mask) + dst->item(BIT_HIGH_US, BIT_ONE_LOW_US); + else + dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US); + } + + dst->mark(BIT_HIGH_US); + + if (data.rc_code_2 != 0) { + dst->space(TRAILER_SPACE_US); + dst->item(HEADER_HIGH_US, HEADER_LOW_US); + for (uint32_t mask = 1UL << 15; mask; mask >>= 1) { + if (address2 & mask) + dst->item(BIT_HIGH_US, BIT_ONE_LOW_US); + else + dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US); + } + + for (uint32_t mask = 1UL << 15; mask; mask >>= 1) { + if (command2 & mask) + dst->item(BIT_HIGH_US, BIT_ONE_LOW_US); + else + dst->item(BIT_HIGH_US, BIT_ZERO_LOW_US); + } + + dst->mark(BIT_HIGH_US); + } +} +optional PioneerProtocol::decode(RemoteReceiveData src) { + uint16_t address1 = 0; + uint16_t command1 = 0; + + PioneerData data{ + .rc_code_1 = 0, + .rc_code_2 = 0, + }; + if (!src.expect_item(HEADER_HIGH_US, HEADER_LOW_US)) + return {}; + + for (uint32_t mask = 1UL << 15; mask != 0; mask >>= 1) { + if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) { + address1 |= mask; + } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) { + address1 &= ~mask; + } else { + return {}; + } + } + + for (uint32_t mask = 1UL << 15; mask != 0; mask >>= 1) { + if (src.expect_item(BIT_HIGH_US, BIT_ONE_LOW_US)) { + command1 |= mask; + } else if (src.expect_item(BIT_HIGH_US, BIT_ZERO_LOW_US)) { + command1 &= ~mask; + } else { + return {}; + } + } + + if (!src.expect_mark(BIT_HIGH_US)) + return {}; + + if ((address1 >> 8) != ((~address1) & 0xff)) + return {}; + + if ((command1 >> 8) != ((~command1) & 0xff)) + return {}; + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((~command1 >> bit) & 1) + data.rc_code_1 |= (1UL << (7 - bit)); + } + + for (uint32_t bit = 0; bit < 4; bit++) { + if ((~command1 >> (bit + 4)) & 1) + data.rc_code_1 |= (1UL << (3 - bit)); + } + data.rc_code_1 |= address1 & 0xff00; + + return data; +} +void PioneerProtocol::dump(const PioneerData &data) { + if (data.rc_code_2 == 0) + ESP_LOGD(TAG, "Received Pioneer: rc_code_X=0x%04X", data.rc_code_1); + else + ESP_LOGD(TAG, "Received Pioneer: rc_code_1=0x%04X, rc_code_2=0x%04X", data.rc_code_1, data.rc_code_2); +} + +} // namespace remote_base +} // namespace esphome diff --git a/esphome/components/remote_base/pioneer_protocol.h b/esphome/components/remote_base/pioneer_protocol.h new file mode 100644 index 0000000000..f93e51a033 --- /dev/null +++ b/esphome/components/remote_base/pioneer_protocol.h @@ -0,0 +1,37 @@ +#pragma once + +#include "remote_base.h" + +namespace esphome { +namespace remote_base { + +struct PioneerData { + uint16_t rc_code_1; + uint16_t rc_code_2; + + bool operator==(const PioneerData &rhs) const { return rc_code_1 == rhs.rc_code_1 && rc_code_2 == rhs.rc_code_2; } +}; + +class PioneerProtocol : public RemoteProtocol { + public: + void encode(RemoteTransmitData *dst, const PioneerData &data) override; + optional decode(RemoteReceiveData src) override; + void dump(const PioneerData &data) override; +}; + +DECLARE_REMOTE_PROTOCOL(Pioneer) + +template class PioneerAction : public RemoteTransmitterActionBase { + public: + TEMPLATABLE_VALUE(uint16_t, rc_code_1) + TEMPLATABLE_VALUE(uint16_t, rc_code_2) + void encode(RemoteTransmitData *dst, Ts... x) override { + PioneerData data{}; + data.rc_code_1 = this->rc_code_1_.value(x...); + data.rc_code_2 = this->rc_code_2_.value(x...); + PioneerProtocol().encode(dst, data); + } +}; + +} // namespace remote_base +} // namespace esphome diff --git a/esphome/const.py b/esphome/const.py index 8a45f27880..f287a15734 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -356,6 +356,8 @@ CONF_RANGE_FROM = 'range_from' CONF_RANGE_TO = 'range_to' CONF_RATE = 'rate' CONF_RAW = 'raw' +CONF_RC_CODE_1 = 'rc_code_1' +CONF_RC_CODE_2 = 'rc_code_2' CONF_REBOOT_TIMEOUT = 'reboot_timeout' CONF_RECEIVE_TIMEOUT = 'receive_timeout' CONF_RED = 'red' diff --git a/tests/test1.yaml b/tests/test1.yaml index 043fafc9c3..f0de50d9b6 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1239,6 +1239,14 @@ switch: remote_transmitter.transmit_panasonic: address: 0x4004 command: 0x1000BCD + - platform: template + name: Pioneer + turn_on_action: + - remote_transmitter.transmit_pioneer: + rc_code_1: 0xA556 + rc_code_2: 0xA506 + repeat: + times: 2 - platform: template name: RC Switch Raw turn_on_action: