From 5c3268b8d40ea42ba7f3b1fd83eab49d99e32e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Panella?= Date: Mon, 31 May 2021 14:24:22 -0500 Subject: [PATCH] Rf Bridge: add bucket sniffing and beep functionality (#1819) Co-authored-by: Otto winter Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/rf_bridge/__init__.py | 37 ++++++++++++++++++++++ esphome/components/rf_bridge/rf_bridge.cpp | 19 +++++++++++ esphome/components/rf_bridge/rf_bridge.h | 24 ++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/esphome/components/rf_bridge/__init__.py b/esphome/components/rf_bridge/__init__.py index c7f1b819b8..228e7d882b 100644 --- a/esphome/components/rf_bridge/__init__.py +++ b/esphome/components/rf_bridge/__init__.py @@ -12,6 +12,7 @@ from esphome.const import ( CONF_RAW, CONF_SYNC, CONF_TRIGGER_ID, + CONF_DURATION, ) DEPENDENCIES = ["uart"] @@ -49,6 +50,12 @@ RFBridgeStopAdvancedSniffingAction = rf_bridge_ns.class_( "RFBridgeStopAdvancedSniffingAction", automation.Action ) +RFBridgeStartBucketSniffingAction = rf_bridge_ns.class_( + "RFBridgeStartBucketSniffingAction", automation.Action +) + +RFBridgeBeepAction = rf_bridge_ns.class_("RFBridgeBeepAction", automation.Action) + RFBridgeSendRawAction = rf_bridge_ns.class_("RFBridgeSendRawAction", automation.Action) CONF_ON_CODE_RECEIVED = "on_code_received" @@ -159,6 +166,19 @@ async def rf_bridge_stop_advanced_sniffing_to_code( return var +@automation.register_action( + "rf_bridge.start_bucket_sniffing", + RFBridgeStartBucketSniffingAction, + RFBRIDGE_ID_SCHEMA, +) +async def rf_bridge_start_bucket_sniffing_to_code( + config, action_id, template_args, args +): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_args, paren) + return var + + RFBRIDGE_SEND_ADVANCED_CODE_SCHEMA = cv.Schema( { cv.GenerateID(): cv.use_id(RFBridgeComponent), @@ -203,3 +223,20 @@ async def rf_bridge_send_raw_to_code(config, action_id, template_args, args): template_ = await cg.templatable(config[CONF_RAW], args, cg.std_string) cg.add(var.set_raw(template_)) return var + + +RFBRIDGE_BEEP_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.use_id(RFBridgeComponent), + cv.Required(CONF_DURATION): cv.templatable(cv.uint16_t), + } +) + + +@automation.register_action("rf_bridge.beep", RFBridgeBeepAction, RFBRIDGE_BEEP_SCHEMA) +async def rf_bridge_beep_to_code(config, action_id, template_args, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_args, paren) + template_ = await cg.templatable(config[CONF_DURATION], args, cg.uint16) + cg.add(var.set_duration(template_)) + return var diff --git a/esphome/components/rf_bridge/rf_bridge.cpp b/esphome/components/rf_bridge/rf_bridge.cpp index 32e72453e0..b284a969b1 100644 --- a/esphome/components/rf_bridge/rf_bridge.cpp +++ b/esphome/components/rf_bridge/rf_bridge.cpp @@ -201,6 +201,14 @@ void RFBridgeComponent::stop_advanced_sniffing() { this->flush(); } +void RFBridgeComponent::start_bucket_sniffing() { + ESP_LOGD(TAG, "Raw Bucket Sniffing on"); + this->write(RF_CODE_START); + this->write(RF_CODE_RFIN_BUCKET); + this->write(RF_CODE_STOP); + this->flush(); +} + void RFBridgeComponent::send_raw(std::string raw_code) { ESP_LOGD(TAG, "Sending Raw Code: %s", raw_code.c_str()); @@ -208,5 +216,16 @@ void RFBridgeComponent::send_raw(std::string raw_code) { this->flush(); } +void RFBridgeComponent::beep(uint16_t ms) { + ESP_LOGD(TAG, "Beeping for %hu ms", ms); + + this->write(RF_CODE_START); + this->write(RF_CODE_BEEP); + this->write((ms >> 8) & 0xFF); + this->write(ms & 0xFF); + this->write(RF_CODE_STOP); + this->flush(); +} + } // namespace rf_bridge } // namespace esphome diff --git a/esphome/components/rf_bridge/rf_bridge.h b/esphome/components/rf_bridge/rf_bridge.h index b850140b75..573bb2df3f 100644 --- a/esphome/components/rf_bridge/rf_bridge.h +++ b/esphome/components/rf_bridge/rf_bridge.h @@ -24,6 +24,7 @@ static const uint8_t RF_CODE_LEARN_KO_NEW = 0xAA; static const uint8_t RF_CODE_LEARN_OK_NEW = 0xAB; static const uint8_t RF_CODE_RFOUT_BUCKET = 0xB0; static const uint8_t RF_CODE_RFIN_BUCKET = 0xB1; +static const uint8_t RF_CODE_BEEP = 0xC0; static const uint8_t RF_CODE_STOP = 0x55; static const uint8_t RF_DEBOUNCE = 200; @@ -55,7 +56,9 @@ class RFBridgeComponent : public uart::UARTDevice, public Component { void learn(); void start_advanced_sniffing(); void stop_advanced_sniffing(); + void start_bucket_sniffing(); void send_raw(std::string code); + void beep(uint16_t ms); protected: void ack_(); @@ -154,6 +157,16 @@ template class RFBridgeStopAdvancedSniffingAction : public Actio RFBridgeComponent *parent_; }; +template class RFBridgeStartBucketSniffingAction : public Action { + public: + RFBridgeStartBucketSniffingAction(RFBridgeComponent *parent) : parent_(parent) {} + + void play(Ts... x) { this->parent_->start_bucket_sniffing(); } + + protected: + RFBridgeComponent *parent_; +}; + template class RFBridgeSendRawAction : public Action { public: RFBridgeSendRawAction(RFBridgeComponent *parent) : parent_(parent) {} @@ -165,5 +178,16 @@ template class RFBridgeSendRawAction : public Action { RFBridgeComponent *parent_; }; +template class RFBridgeBeepAction : public Action { + public: + RFBridgeBeepAction(RFBridgeComponent *parent) : parent_(parent) {} + TEMPLATABLE_VALUE(uint16_t, duration) + + void play(Ts... x) { this->parent_->beep(this->duration_.value(x...)); } + + protected: + RFBridgeComponent *parent_; +}; + } // namespace rf_bridge } // namespace esphome