Closed louix closed 2 months ago
Hey @louix, thanks for your pull request!
[!TIP] Modified bundles can be downloaded here.
third_reality/3RSB22BZ_smart_button.json
: Smart Button 3RSB22BZ :heavy_check_mark:[!TIP] Everything is fine !
:duck: Updated for commit 4a2cf71d1e9775b04d50c4618342d204aa8f4cae
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 :)
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 ?
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!
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.
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
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][0] (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.
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.
This pull request is now merged. The new DDB files have been uploaded to the store.
third_reality/3RSB22BZ_smart_button.json
: Smart Button 3RSB22BZ : with hash (d901e7ac7f):clock1230: Updated for commit 7e34c7ec228a94481a6144ceb342e5ef130b0ee1
Issue: #7863