From 49f9ad66db298965927389df4b5dddb54637cb20 Mon Sep 17 00:00:00 2001 From: mtl010957 Date: Wed, 26 Jun 2019 14:47:34 -0500 Subject: [PATCH] Adding ignore bits to narrow compare of received codes (#650) * Adding bitmask to narrow compare of received codes Updated test to add mask configuration * Lint * Handle bitmask as ignore characters per review comment * Fixed test to cover ignore bits * Lint * Eliminate separate set_mask method per review comment --- esphome/components/remote_base/__init__.py | 18 +++++++++++++++++- .../remote_base/rc_switch_protocol.cpp | 11 ++++++++++- .../remote_base/rc_switch_protocol.h | 4 ++++ tests/test1.yaml | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/esphome/components/remote_base/__init__.py b/esphome/components/remote_base/__init__.py index d3da238b08..aa5ec62979 100644 --- a/esphome/components/remote_base/__init__.py +++ b/esphome/components/remote_base/__init__.py @@ -446,6 +446,22 @@ def validate_rc_switch_code(value): return value +def validate_rc_switch_raw_code(value): + if not isinstance(value, (str, text_type)): + raise cv.Invalid("All RCSwitch raw codes must be in quotes ('')") + for c in value: + if c not in ('0', '1', 'x'): + raise cv.Invalid( + "Invalid RCSwitch raw code character '{}'.Only '0', '1' and 'x' are allowed" + .format(c)) + if len(value) > 32: + raise cv.Invalid("Maximum length for RCSwitch raw codes is 32, code '{}' has length {}" + "".format(value, len(value))) + if not value: + raise cv.Invalid("RCSwitch raw code must not be empty") + return value + + def build_rc_switch_protocol(config): if isinstance(config, int): return rc_switch_protocols[config] @@ -457,7 +473,7 @@ def build_rc_switch_protocol(config): RC_SWITCH_RAW_SCHEMA = cv.Schema({ - cv.Required(CONF_CODE): validate_rc_switch_code, + cv.Required(CONF_CODE): validate_rc_switch_raw_code, cv.Optional(CONF_PROTOCOL, default=1): RC_SWITCH_PROTOCOL_SCHEMA, }) RC_SWITCH_TYPE_A_SCHEMA = cv.Schema({ diff --git a/esphome/components/remote_base/rc_switch_protocol.cpp b/esphome/components/remote_base/rc_switch_protocol.cpp index a05842b3aa..a04b13ecfd 100644 --- a/esphome/components/remote_base/rc_switch_protocol.cpp +++ b/esphome/components/remote_base/rc_switch_protocol.cpp @@ -216,13 +216,22 @@ uint32_t decode_binary_string(const std::string &data) { return ret; } +uint32_t decode_binary_string_mask(const std::string &data) { + uint32_t ret = 0; + for (char c : data) { + ret <<= 1UL; + ret |= (c != 'x'); + } + return ret; +} + bool RCSwitchRawReceiver::matches(RemoteReceiveData src) { uint32_t decoded_code; uint8_t decoded_nbits; if (!this->protocol_.decode(src, &decoded_code, &decoded_nbits)) return false; - return decoded_nbits == this->nbits_ && decoded_code == this->code_; + return decoded_nbits == this->nbits_ && (decoded_code & this->mask_) == (this->code_ & this->mask_); } bool RCSwitchDumper::dump(RemoteReceiveData src) { for (uint8_t i = 1; i <= 7; i++) { diff --git a/esphome/components/remote_base/rc_switch_protocol.h b/esphome/components/remote_base/rc_switch_protocol.h index 728561c140..d937efbd25 100644 --- a/esphome/components/remote_base/rc_switch_protocol.h +++ b/esphome/components/remote_base/rc_switch_protocol.h @@ -55,6 +55,8 @@ extern RCSwitchBase rc_switch_protocols[8]; uint32_t decode_binary_string(const std::string &data); +uint32_t decode_binary_string_mask(const std::string &data); + template class RCSwitchRawAction : public RemoteTransmitterActionBase { public: TEMPLATABLE_VALUE(RCSwitchBase, protocol); @@ -167,6 +169,7 @@ class RCSwitchRawReceiver : public RemoteReceiverBinarySensorBase { void set_code(uint32_t code) { this->code_ = code; } void set_code(const std::string &code) { this->code_ = decode_binary_string(code); + this->mask_ = decode_binary_string_mask(code); this->nbits_ = code.size(); } void set_nbits(uint8_t nbits) { this->nbits_ = nbits; } @@ -192,6 +195,7 @@ class RCSwitchRawReceiver : public RemoteReceiverBinarySensorBase { RCSwitchBase protocol_; uint32_t code_; + uint32_t mask_{0xFFFFFFFF}; uint8_t nbits_; }; diff --git a/tests/test1.yaml b/tests/test1.yaml index 5ffd8180fc..bfdc34fac1 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1018,7 +1018,7 @@ switch: name: RC Switch Raw turn_on_action: remote_transmitter.transmit_rc_switch_raw: - code: '001010011001111101011011' + code: '00101001100111110101xxxx' protocol: 1 - platform: template name: RC Switch Type A