deconz-rest-plugin icon indicating copy to clipboard operation
deconz-rest-plugin copied to clipboard

DDF: 3RSB22BZ Third Reality, Inc smart button

Open louix opened this issue 1 year ago • 1 comments

Issue: #7863

louix avatar Aug 22 '24 17:08 louix

Hey @louix, thanks for your pull request!

[!TIP] Modified bundles can be downloaded here. Relative expire date

DDB changes

Modified

  • third_reality/3RSB22BZ_smart_button.json : Smart Button 3RSB22BZ :heavy_check_mark:

Validation

[!TIP] Everything is fine !

:duck: Updated for commit 4a2cf71d1e9775b04d50c4618342d204aa8f4cae

github-actions[bot] avatar Aug 23 '24 16:08 github-actions[bot]

Item.val={1: 1002, 2: 1004, 0: 1001, 255: 1003}[Number(ZclFrame.at(3))]

I don't think that will work in Javascript since object keys must be strings.

Here's an alternative approach (1):

const v = [1, 1002, 2, 1004, 0, 1001, 255, 1003];
const n = v.indexOf(ZclFrame.at(3));
if (n >= 0) Item.val = v[n + 1];

Or another one as one liner (2):

const n = ZclFrame.at(3); if (n < 3) { Item.val = [1001, 1002, 1004][n]; } else if (n == 255) { Item.val = 1003; }

Not pretty but should do the trick without string conversions :)

manup avatar Sep 05 '24 10:09 manup

Another one that have workied for him

n=Number(ZclFrame.at(3));t={'1': 1002, '2': 1004, '0': 1001, '255': 1003};if(n in t){Item.val=t[n]}

https://github.com/dresden-elektronik/deconz-rest-plugin/issues/7863#issuecomment-2302732124

Or just adding quote ?

Smanar avatar Sep 05 '24 13:09 Smanar

Hey, apologies for the confusion.

It should be fine, JS engines internally coerces keys that are not string | Symbol via ToPropertyKey as part of the ECMAScript spec:

$ node --eval "console.log({1: true})"
{ '1': true }
$ node --eval "console.log({'1': 'hello'}[1])"
'hello'

A Map could also be used:

$ node --eval "console.log(new Map([[1, true]]))"
Map(1) { 1 => true }
$ node --eval "console.log(new Map([[1, 'hello'], [2, 'goodbye']]).get(1))"
hello

But it's subjectively a bit harder to read and probably a bit slower:

Item.val=new Map([[1, 1002], [2, 1004], [0, 1001], [255, 1003]]).get(Number(ZclFrame.at(3)))

Or you could be explicit about the conversions:

Item.val={'1': 1002, '2': 1004, '0': 1001, '255': 1003}[String(Number(ZclFrame.at(3)))]

The Number(ZclFrame.at(3) conversion is for 0 padding:

$ node --eval "console.log(Number('003'))"
3

Let me know what you prefer!

louix avatar Sep 05 '24 15:09 louix

lol, sometime we are searching a special function, sometime we found too much of them ^^. On my side for a so simple convertion I prefer a "one liner", but you can use the one you want.

Smanar avatar Sep 05 '24 15:09 Smanar

It should be fine, JS engines internally coerces keys that are not string | Symbol via ToPropertyKey as part of the ECMAScript spec:

Thanks I didn't know that. Could you test if this is also supported in DucktapeJS engine? This is the engine used in deCONZ and supports only ECMAScript E5 and just a subset of newer versions https://duktape.org

manup avatar Sep 09 '24 11:09 manup

Thanks Manuel.

Could you test if this is also supported in DucktapeJS engine?

Sure. They have a nice web REPL, and I am happy to report it works (version 20500).

I have tested the device integration with deCONZ, too.

All said and done, this is your codebase and the conventions are up to you, I'm happy to change to to something if it makes it more difficult for maintainers to reason about.

louix avatar Sep 09 '24 12:09 louix

Cool nice that it works, then I see no problem for merging.

I like Web REPL, perhaps some time this should also be integrated into the UI for easier testing.

manup avatar Sep 09 '24 13:09 manup

This pull request is now merged. The new DDB files have been uploaded to the store.

DDB Files

Modified

  • third_reality/3RSB22BZ_smart_button.json : Smart Button 3RSB22BZ : with hash (d901e7ac7f)

:clock1230: Updated for commit 7e34c7ec228a94481a6144ceb342e5ef130b0ee1

github-actions[bot] avatar Sep 09 '24 13:09 github-actions[bot]