diff --git a/esphome/components/cc1101/__init__.py b/esphome/components/cc1101/__init__.py index 58a1e752c2..1f40d4bb1d 100644 --- a/esphome/components/cc1101/__init__.py +++ b/esphome/components/cc1101/__init__.py @@ -4,9 +4,12 @@ from esphome import automation, pins from esphome.components import sensor from esphome.components import spi from esphome.automation import maybe_simple_id +from esphome.components import remote_base from esphome.const import ( CONF_ID, CONF_FREQUENCY, + CONF_PROTOCOL, + CONF_CODE, UNIT_EMPTY, UNIT_DECIBEL_MILLIWATT, DEVICE_CLASS_SIGNAL_STRENGTH, @@ -14,7 +17,7 @@ from esphome.const import ( ) DEPENDENCIES = ["spi"] -AUTO_LOAD = ["sensor"] +AUTO_LOAD = ["sensor", "remote_base"] CODEOWNERS = ["@gabest11"] @@ -23,12 +26,11 @@ CONF_BANDWIDTH = "bandwidth" # CONF_FREQUENCY = "frequency" CONF_RSSI = "rssi" CONF_LQI = "lqi" +CONF_CC1101_ID = "cc1101_id" -cc1101_ns = cg.esphome_ns.namespace("cc1101") -CC1101 = cc1101_ns.class_("CC1101", cg.PollingComponent, spi.SPIDevice) +ns = cg.esphome_ns.namespace("cc1101") -BeginTxAction = cc1101_ns.class_("BeginTxAction", automation.Action) -EndTxAction = cc1101_ns.class_("EndTxAction", automation.Action) +CC1101 = ns.class_("CC1101", cg.PollingComponent, spi.SPIDevice) CONFIG_SCHEMA = ( cv.Schema( @@ -54,21 +56,6 @@ CONFIG_SCHEMA = ( .extend(spi.spi_device_schema(cs_pin_required=True)) ) -CC1101_ACTION_SCHEMA = maybe_simple_id( - { - cv.Required(CONF_ID): cv.use_id(CC1101), - } -) - - -@automation.register_action("cc1101.begin_tx", BeginTxAction, CC1101_ACTION_SCHEMA) -@automation.register_action("cc1101.end_tx", EndTxAction, CC1101_ACTION_SCHEMA) -async def cc1101_action_to_code(config, action_id, template_arg, args): - var = cg.new_Pvariable(action_id, template_arg) - await cg.register_parented(var, config[CONF_ID]) - return var - - async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) @@ -85,3 +72,25 @@ async def to_code(config): if CONF_LQI in config: lqi = await sensor.new_sensor(config[CONF_LQI]) cg.add(var.set_config_lqi_sensor(lqi)) + +CC1101RawAction = ns.class_("CC1101RawAction", remote_base.RemoteTransmitterActionBase) + +CC1101_TRANSMIT_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(CONF_CC1101_ID): cv.use_id(CC1101), + } + ) + .extend(remote_base.REMOTE_TRANSMITTABLE_SCHEMA) + .extend(remote_base.RC_SWITCH_RAW_SCHEMA) + .extend(remote_base.RC_SWITCH_TRANSMITTER) +) + +@remote_base.register_action("cc1101", CC1101RawAction, CC1101_TRANSMIT_SCHEMA) +async def cc1101_action(var, config, args): + proto = await cg.templatable( + config[CONF_PROTOCOL], args, remote_base.RCSwitchBase, to_exp=remote_base.build_rc_switch_protocol + ) + cg.add(var.set_protocol(proto)) + cg.add(var.set_code(await cg.templatable(config[CONF_CODE], args, cg.std_string))) + await cg.register_parented(var, config[CONF_CC1101_ID]) diff --git a/esphome/components/cc1101/cc1101.cpp b/esphome/components/cc1101/cc1101.cpp index c308a85726..c06068f6f7 100644 --- a/esphome/components/cc1101/cc1101.cpp +++ b/esphome/components/cc1101/cc1101.cpp @@ -12,8 +12,6 @@ On ESP32, this will not work, you must connect two separate pins. TX to GDO0, RX to GDO2. If only TX works, they are probably switched. - Transferst must be surrounded with cc1101.begin_tx and cc1101.end_tx. - The source code is a mashup of the following github projects with some special esphome sauce: https://github.com/dbuezas/esphome-cc1101 (the original esphome component) diff --git a/esphome/components/cc1101/cc1101.h b/esphome/components/cc1101/cc1101.h index db0179b361..16328fee9f 100644 --- a/esphome/components/cc1101/cc1101.h +++ b/esphome/components/cc1101/cc1101.h @@ -3,6 +3,7 @@ #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" #include "esphome/components/spi/spi.h" +#include "esphome/components/remote_base/rc_switch_protocol.h" namespace esphome { namespace cc1101 { @@ -89,14 +90,15 @@ class CC1101 : public PollingComponent, void end_tx(); }; -template class BeginTxAction : public Action, public Parented { - public: - void play(Ts... x) override { this->parent_->begin_tx(); } -}; +template class CC1101RawAction : public remote_base::RCSwitchRawAction, public Parented { + protected: + void play(Ts... x) override { + this->parent_->begin_tx(); + remote_base::RCSwitchRawAction::play(x...); + this->parent_->end_tx(); + } -template class EndTxAction : public Action, public Parented { public: - void play(Ts... x) override { this->parent_->end_tx(); } }; } // namespace cc1101 diff --git a/tests/components/cc1101/test.esp32-c3-idf.yaml b/tests/components/cc1101/test.esp32-c3-idf.yaml index 204c37f470..4b7a29a96b 100644 --- a/tests/components/cc1101/test.esp32-c3-idf.yaml +++ b/tests/components/cc1101/test.esp32-c3-idf.yaml @@ -51,8 +51,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: transmitter_id: realtx code: '0111000110010011110110010100011111110001001011110111' protocol: @@ -62,5 +61,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp32-c3.yaml b/tests/components/cc1101/test.esp32-c3.yaml index 204c37f470..4b7a29a96b 100644 --- a/tests/components/cc1101/test.esp32-c3.yaml +++ b/tests/components/cc1101/test.esp32-c3.yaml @@ -51,8 +51,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: transmitter_id: realtx code: '0111000110010011110110010100011111110001001011110111' protocol: @@ -62,5 +61,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp32-idf.yaml b/tests/components/cc1101/test.esp32-idf.yaml index 24055d30b7..bd7084a4c7 100644 --- a/tests/components/cc1101/test.esp32-idf.yaml +++ b/tests/components/cc1101/test.esp32-idf.yaml @@ -43,8 +43,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: code: '0111000110010011110110010100011111110001001011110111' protocol: pulse_length: 434 @@ -53,5 +52,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp32-s2-idf.yaml b/tests/components/cc1101/test.esp32-s2-idf.yaml index 15f468e458..24e27c3c6d 100644 --- a/tests/components/cc1101/test.esp32-s2-idf.yaml +++ b/tests/components/cc1101/test.esp32-s2-idf.yaml @@ -43,8 +43,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: code: '0111000110010011110110010100011111110001001011110111' protocol: pulse_length: 434 @@ -53,5 +52,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp32-s2.yaml b/tests/components/cc1101/test.esp32-s2.yaml index 15f468e458..24e27c3c6d 100644 --- a/tests/components/cc1101/test.esp32-s2.yaml +++ b/tests/components/cc1101/test.esp32-s2.yaml @@ -43,8 +43,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: code: '0111000110010011110110010100011111110001001011110111' protocol: pulse_length: 434 @@ -53,5 +52,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp32.yaml b/tests/components/cc1101/test.esp32.yaml index 24055d30b7..bd7084a4c7 100644 --- a/tests/components/cc1101/test.esp32.yaml +++ b/tests/components/cc1101/test.esp32.yaml @@ -43,8 +43,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: code: '0111000110010011110110010100011111110001001011110111' protocol: pulse_length: 434 @@ -53,5 +52,4 @@ button: one: [2, 1] inverted: true repeat: - times: 10 - - cc1101.end_tx: transceiver + times: 10 \ No newline at end of file diff --git a/tests/components/cc1101/test.esp8266.yaml b/tests/components/cc1101/test.esp8266.yaml index 708067b5f2..632dc26c8d 100644 --- a/tests/components/cc1101/test.esp8266.yaml +++ b/tests/components/cc1101/test.esp8266.yaml @@ -50,8 +50,7 @@ button: - platform: template name: "Gate" on_press: - - cc1101.begin_tx: transceiver - - remote_transmitter.transmit_rc_switch_raw: + - remote_transmitter.transmit_cc1101: code: '0111000110010011110110010100011111110001001011110111' protocol: pulse_length: 434 @@ -61,4 +60,3 @@ button: inverted: true repeat: times: 10 - - cc1101.end_tx: transceiver