Rf Bridge: add bucket sniffing and beep functionality (#1819)

Co-authored-by: Otto winter <otto@otto-winter.com>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Adrián Panella 2021-05-31 14:24:22 -05:00 committed by GitHub
parent 25af5ab7c6
commit 5c3268b8d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 0 deletions

View file

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

View file

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

View file

@ -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<typename... Ts> class RFBridgeStopAdvancedSniffingAction : public Actio
RFBridgeComponent *parent_;
};
template<typename... Ts> class RFBridgeStartBucketSniffingAction : public Action<Ts...> {
public:
RFBridgeStartBucketSniffingAction(RFBridgeComponent *parent) : parent_(parent) {}
void play(Ts... x) { this->parent_->start_bucket_sniffing(); }
protected:
RFBridgeComponent *parent_;
};
template<typename... Ts> class RFBridgeSendRawAction : public Action<Ts...> {
public:
RFBridgeSendRawAction(RFBridgeComponent *parent) : parent_(parent) {}
@ -165,5 +178,16 @@ template<typename... Ts> class RFBridgeSendRawAction : public Action<Ts...> {
RFBridgeComponent *parent_;
};
template<typename... Ts> class RFBridgeBeepAction : public Action<Ts...> {
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