From 6b98a320b1ae5424af7579874d65669e0b92861f Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Fri, 2 Aug 2024 15:43:31 +0200 Subject: [PATCH] Berry `zigbee.find()` (#21889) --- CHANGELOG.md | 1 + lib/libesp32/berry_tasmota/src/be_zigbee.c | 2 ++ .../xdrv_52_3_berry_zigbee.ino | 24 ++++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 151288337..91f9e9e7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Berry new type "addr" to ctypes mapping (#21883) - Berry `file.savecode()` (#21884) - Berry `solidify.nocompact()` and reduce size of Matter UI (#21885) +- Berry `zigbee.find()` ### Breaking Changed - Berry `energy` module support for 8 phases and move to pseudo-arrays (#21887) diff --git a/lib/libesp32/berry_tasmota/src/be_zigbee.c b/lib/libesp32/berry_tasmota/src/be_zigbee.c index 22c67301a..0f4510aaf 100644 --- a/lib/libesp32/berry_tasmota/src/be_zigbee.c +++ b/lib/libesp32/berry_tasmota/src/be_zigbee.c @@ -51,6 +51,7 @@ static int zd_member(bvm *vm) { extern int zc_info(struct bvm *vm); extern int zc_item(struct bvm *vm); +extern int zc_find(struct bvm *vm); extern int32_t zc_size(void* d); BE_FUNC_CTYPE_DECLARE(zc_size, "i", "."); extern int zc_iter(bvm *vm); extern void zc_abort(void); BE_FUNC_CTYPE_DECLARE(zc_abort, "", "."); @@ -113,6 +114,7 @@ class be_class_zb_coord_ntv (scope: global, name: zb_coord_ntv, strings: weak) { info, func(zc_info) item, func(zc_item) + find, func(zc_find) size, ctype_func(zc_size) iter, func(zc_iter) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_zigbee.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_zigbee.ino index f684a9a6b..390859afe 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_zigbee.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_zigbee.ino @@ -99,8 +99,8 @@ extern "C" { be_raise(vm, kTypeError, nullptr); } - int zc_item(struct bvm *vm); - int zc_item(struct bvm *vm) { + // implement item() and find() + int zc_item_or_find(struct bvm *vm, bbool raise_if_unknown) { int32_t top = be_top(vm); // Get the number of arguments if (zigbee.init_phase) { be_raise(vm, "internal_error", "zigbee not started"); @@ -109,7 +109,11 @@ extern "C" { const Z_Device & device = be_isint(vm, 2) ? zigbee_devices.findShortAddr(be_toint(vm, 2)) : zigbee_devices.parseDeviceFromName(be_tostring(vm, 2)); if (!device.valid()) { - be_raise(vm, "value_error", "unknown device"); + if (raise_if_unknown) { + be_raise(vm, "index_error", "unknown device"); + } else { + be_return_nil(vm); + } } be_pushntvclass(vm, &be_class_zb_device); @@ -121,6 +125,20 @@ extern "C" { be_raise(vm, kTypeError, nullptr); } + // `zigbee.item(shortaddr:int | friendlyname:str) -> instance of zb_device` + // raise en exception if not found + int zc_item(struct bvm *vm); + int zc_item(struct bvm *vm) { + return zc_item_or_find(vm, true); + } + + // `zigbee.find(shortaddr:int | friendlyname:str) -> instance of zb_device` + // return `nil` if not found + int zc_find(struct bvm *vm); + int zc_find(struct bvm *vm) { + return zc_item_or_find(vm, false); + } + int32_t zc_size(void*) { return zigbee_devices.devicesSize(); }