diff --git a/README.md b/README.md index e7d7953..9b6c65c 100644 --- a/README.md +++ b/README.md @@ -115,10 +115,27 @@ ble_keyboard.press: #### ble_keyboard.release -Release keys. +Release all keys. ```yaml -ble_keyboard.release: my_ble_keyboard +ble_keyboard.release: + id: my_ble_keyboard +``` + +Release a specific Key +```yaml +ble_keyboard.release + id: my_ble_keyboard + code: 0x80 +``` + +Release a specific media Key +```yaml +ble_keyboard.release + id: my_ble_keyboard + code: + - 0 + - 1 ``` #### ble_keyboard.combination diff --git a/components/ble_keyboard/__init__.py b/components/ble_keyboard/__init__.py index 2e79757..bf65381 100644 --- a/components/ble_keyboard/__init__.py +++ b/components/ble_keyboard/__init__.py @@ -188,7 +188,17 @@ def adding_dependencies(use_default_libs: bool = True) -> None: @automation.register_action( f"{DOMAIN}.release", BLEKeyboardReleaseAction, - maybe_simple_id(OPERATION_BASE_SCHEMA), + OPERATION_BASE_SCHEMA.extend( + { + cv.Optional(CONF_CODE): cv.Any( + cv.templatable(cv.positive_int), + cv.All( + [cv.uint8_t], + cv.Length(min=2, max=2), + ), + ), + } + ), ) async def ble_keyboard_release_to_code( config: dict, action_id: ID, template_arg: TemplateArguments, args: list @@ -203,8 +213,15 @@ async def ble_keyboard_release_to_code( """ paren: MockObj = await cg.get_variable(config[CONF_ID]) + var: MockObj = cg.new_Pvariable(action_id, template_arg, paren) + if CONF_CODE in config: + if isinstance(config[CONF_CODE], list): + cg.add(var.set_keys(config[CONF_CODE])) + else: + template_: LambdaExpression = await cg.templatable(config[CONF_CODE], args, int) + cg.add(var.set_code(template_)) - return cg.new_Pvariable(action_id, template_arg, paren) + return var BLEKeyboardPrintAction = ble_keyboard_ns.class_(ACTION_PRINT_CLASS, automation.Action) diff --git a/components/ble_keyboard/automation.h b/components/ble_keyboard/automation.h index 96076b3..fccbc16 100644 --- a/components/ble_keyboard/automation.h +++ b/components/ble_keyboard/automation.h @@ -45,10 +45,22 @@ template class Esp32BleKeyboardPressAction : public Action class Esp32BleKeyboardReleaseAction : public Action { public: explicit Esp32BleKeyboardReleaseAction(Esp32BleKeyboard *ble_keyboard) : ble_keyboard_(ble_keyboard) {} + TEMPLATABLE_VALUE(uint8_t, code) + + void play(Ts... x) override { + if(keys_.size() > 1) { + MediaKeyReport mediaKey = {keys_[0], keys_[1]}; + + this->ble_keyboard_->release(mediaKey); + } else { + this->ble_keyboard_->release(this->code_.value(x...)); + } + } - void play(Ts... x) override { this->ble_keyboard_->release(); } + void set_keys(const std::vector &keys) { keys_ = keys; } protected: + std::vector keys_; Esp32BleKeyboard *ble_keyboard_; }; diff --git a/components/ble_keyboard/ble_keyboard.cpp b/components/ble_keyboard/ble_keyboard.cpp index 178d316..1d6e128 100644 --- a/components/ble_keyboard/ble_keyboard.cpp +++ b/components/ble_keyboard/ble_keyboard.cpp @@ -108,6 +108,21 @@ void Esp32BleKeyboard::release() { bleKeyboard.releaseAll(); } } + +void Esp32BleKeyboard::release(uint8_t key) { + if(this->is_connected()) { + this->cancel_timeout((const std::string) TAG); + bleKeyboard.release(key); + } +} + +void Esp32BleKeyboard::release(MediaKeyReport key) { + if(this->is_connected()) { + this->cancel_timeout((const std::string) TAG); + bleKeyboard.release(key); + } +} + } // namespace ble_keyboard } // namespace esphome diff --git a/components/ble_keyboard/ble_keyboard.h b/components/ble_keyboard/ble_keyboard.h index c575f1f..c1d7cff 100644 --- a/components/ble_keyboard/ble_keyboard.h +++ b/components/ble_keyboard/ble_keyboard.h @@ -31,6 +31,8 @@ class Esp32BleKeyboard : public PollingComponent { void press(std::string message); void press(uint8_t key, bool with_timer = true); void press(MediaKeyReport key, bool with_timer = true); + void release(uint8_t key); + void release(MediaKeyReport key); void release(); void start(); diff --git a/components/ble_keyboard/const.py b/components/ble_keyboard/const.py index 1f20b16..9e80b10 100644 --- a/components/ble_keyboard/const.py +++ b/components/ble_keyboard/const.py @@ -53,7 +53,7 @@ """Libraries""" LIBS_DEFAULT: Final = [ - ("ESP32 BLE Arduino", "1.0.1", None), + ("ESP32 BLE Arduino", "1.0.2", None), ] LIBS_ADDITIONAL: Final = [