diff --git a/esphome/components/remote_base/__init__.py b/esphome/components/remote_base/__init__.py index 2c8b6be51c..05a3e7e1aa 100644 --- a/esphome/components/remote_base/__init__.py +++ b/esphome/components/remote_base/__init__.py @@ -546,7 +546,9 @@ RC_SWITCH_TRANSMITTER = cv.Schema({ }) rc_switch_protocols = ns.rc_switch_protocols +RCSwitchData = ns.struct('RCSwitchData') RCSwitchBase = ns.class_('RCSwitchBase') +RCSwitchTrigger = ns.class_('RCSwitchTrigger', RemoteReceiverTrigger) RCSwitchDumper = ns.class_('RCSwitchDumper', RemoteTransmitterDumper) RCSwitchRawAction = ns.class_('RCSwitchRawAction', RemoteTransmitterActionBase) RCSwitchTypeAAction = ns.class_('RCSwitchTypeAAction', RemoteTransmitterActionBase) @@ -642,6 +644,11 @@ def rc_switch_type_d_action(var, config, args): cg.add(var.set_state((yield cg.templatable(config[CONF_STATE], args, bool)))) +@register_trigger('rc_switch', RCSwitchTrigger, RCSwitchData) +def rc_switch_trigger(var, config): + pass + + @register_dumper('rc_switch', RCSwitchDumper) def rc_switch_dumper(var, config): pass diff --git a/esphome/components/remote_base/rc_switch_protocol.cpp b/esphome/components/remote_base/rc_switch_protocol.cpp index b2ff22eb2a..91b22500e6 100644 --- a/esphome/components/remote_base/rc_switch_protocol.cpp +++ b/esphome/components/remote_base/rc_switch_protocol.cpp @@ -127,6 +127,19 @@ bool RCSwitchBase::decode(RemoteReceiveData &src, uint64_t *out_data, uint8_t *o } return true; } +optional RCSwitchBase::decode(RemoteReceiveData &src) const { + RCSwitchData out; + uint8_t out_nbits; + for (uint8_t i = 1; i <= 8; i++) { + src.reset(); + RCSwitchBase *protocol = &rc_switch_protocols[i]; + if (protocol->decode(src, &out.code, &out_nbits) && out_nbits >= 3) { + out.protocol = i; + return out; + } + } + return {}; +} void RCSwitchBase::simple_code_to_tristate(uint16_t code, uint8_t nbits, uint64_t *out_code) { *out_code = 0; diff --git a/esphome/components/remote_base/rc_switch_protocol.h b/esphome/components/remote_base/rc_switch_protocol.h index 0983da27ea..8362899cec 100644 --- a/esphome/components/remote_base/rc_switch_protocol.h +++ b/esphome/components/remote_base/rc_switch_protocol.h @@ -6,6 +6,13 @@ namespace esphome { namespace remote_base { +struct RCSwitchData { + uint64_t code; + uint8_t protocol; + + bool operator==(const RCSwitchData &rhs) const { return code == rhs.code && protocol == rhs.protocol; } +}; + class RCSwitchBase { public: RCSwitchBase() = default; @@ -28,6 +35,8 @@ class RCSwitchBase { bool decode(RemoteReceiveData &src, uint64_t *out_data, uint8_t *out_nbits) const; + optional decode(RemoteReceiveData &src) const; + static void simple_code_to_tristate(uint16_t code, uint8_t nbits, uint64_t *out_code); static void type_a_code(uint8_t switch_group, uint8_t switch_device, bool state, uint64_t *out_code, @@ -204,5 +213,7 @@ class RCSwitchDumper : public RemoteReceiverDumperBase { bool dump(RemoteReceiveData src) override; }; +using RCSwitchTrigger = RemoteReceiverTrigger; + } // namespace remote_base } // namespace esphome