Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of Release specific Key #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions components/ble_keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion components/ble_keyboard/automation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ template<typename... Ts> class Esp32BleKeyboardPressAction : public Action<Ts...
template<typename... Ts> class Esp32BleKeyboardReleaseAction : public Action<Ts...> {
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<uint8_t> &keys) { keys_ = keys; }

protected:
std::vector<uint8_t> keys_;
Esp32BleKeyboard *ble_keyboard_;
};

Expand Down
15 changes: 15 additions & 0 deletions components/ble_keyboard/ble_keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions components/ble_keyboard/ble_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion components/ble_keyboard/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down