mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 12:55:46 +01:00
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:
parent
0dfab4d93c
commit
49f9ad66db
@ -446,6 +446,22 @@ def validate_rc_switch_code(value):
|
|||||||
return 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):
|
def build_rc_switch_protocol(config):
|
||||||
if isinstance(config, int):
|
if isinstance(config, int):
|
||||||
return rc_switch_protocols[config]
|
return rc_switch_protocols[config]
|
||||||
@ -457,7 +473,7 @@ def build_rc_switch_protocol(config):
|
|||||||
|
|
||||||
|
|
||||||
RC_SWITCH_RAW_SCHEMA = cv.Schema({
|
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,
|
cv.Optional(CONF_PROTOCOL, default=1): RC_SWITCH_PROTOCOL_SCHEMA,
|
||||||
})
|
})
|
||||||
RC_SWITCH_TYPE_A_SCHEMA = cv.Schema({
|
RC_SWITCH_TYPE_A_SCHEMA = cv.Schema({
|
||||||
|
@ -216,13 +216,22 @@ uint32_t decode_binary_string(const std::string &data) {
|
|||||||
return ret;
|
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) {
|
bool RCSwitchRawReceiver::matches(RemoteReceiveData src) {
|
||||||
uint32_t decoded_code;
|
uint32_t decoded_code;
|
||||||
uint8_t decoded_nbits;
|
uint8_t decoded_nbits;
|
||||||
if (!this->protocol_.decode(src, &decoded_code, &decoded_nbits))
|
if (!this->protocol_.decode(src, &decoded_code, &decoded_nbits))
|
||||||
return false;
|
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) {
|
bool RCSwitchDumper::dump(RemoteReceiveData src) {
|
||||||
for (uint8_t i = 1; i <= 7; i++) {
|
for (uint8_t i = 1; i <= 7; i++) {
|
||||||
|
@ -55,6 +55,8 @@ extern RCSwitchBase rc_switch_protocols[8];
|
|||||||
|
|
||||||
uint32_t decode_binary_string(const std::string &data);
|
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...> {
|
template<typename... Ts> class RCSwitchRawAction : public RemoteTransmitterActionBase<Ts...> {
|
||||||
public:
|
public:
|
||||||
TEMPLATABLE_VALUE(RCSwitchBase, protocol);
|
TEMPLATABLE_VALUE(RCSwitchBase, protocol);
|
||||||
@ -167,6 +169,7 @@ class RCSwitchRawReceiver : public RemoteReceiverBinarySensorBase {
|
|||||||
void set_code(uint32_t code) { this->code_ = code; }
|
void set_code(uint32_t code) { this->code_ = code; }
|
||||||
void set_code(const std::string &code) {
|
void set_code(const std::string &code) {
|
||||||
this->code_ = decode_binary_string(code);
|
this->code_ = decode_binary_string(code);
|
||||||
|
this->mask_ = decode_binary_string_mask(code);
|
||||||
this->nbits_ = code.size();
|
this->nbits_ = code.size();
|
||||||
}
|
}
|
||||||
void set_nbits(uint8_t nbits) { this->nbits_ = nbits; }
|
void set_nbits(uint8_t nbits) { this->nbits_ = nbits; }
|
||||||
@ -192,6 +195,7 @@ class RCSwitchRawReceiver : public RemoteReceiverBinarySensorBase {
|
|||||||
|
|
||||||
RCSwitchBase protocol_;
|
RCSwitchBase protocol_;
|
||||||
uint32_t code_;
|
uint32_t code_;
|
||||||
|
uint32_t mask_{0xFFFFFFFF};
|
||||||
uint8_t nbits_;
|
uint8_t nbits_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1018,7 +1018,7 @@ switch:
|
|||||||
name: RC Switch Raw
|
name: RC Switch Raw
|
||||||
turn_on_action:
|
turn_on_action:
|
||||||
remote_transmitter.transmit_rc_switch_raw:
|
remote_transmitter.transmit_rc_switch_raw:
|
||||||
code: '001010011001111101011011'
|
code: '00101001100111110101xxxx'
|
||||||
protocol: 1
|
protocol: 1
|
||||||
- platform: template
|
- platform: template
|
||||||
name: RC Switch Type A
|
name: RC Switch Type A
|
||||||
|
Loading…
Reference in New Issue
Block a user