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
This commit is contained in:
mtl010957 2019-06-26 14:47:34 -05:00 committed by Otto Winter
parent 0dfab4d93c
commit 49f9ad66db
4 changed files with 32 additions and 3 deletions

View File

@ -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({

View File

@ -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++) {

View File

@ -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<typename... Ts> class RCSwitchRawAction : public RemoteTransmitterActionBase<Ts...> {
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_;
};

View File

@ -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