dresden-elektronik / deconz-rest-plugin

deCONZ REST-API plugin to control ZigBee devices
BSD 3-Clause "New" or "Revised" License
1.9k stars 503 forks source link

DDF for Third Reality, Inc smart button 3RSB22BZ #7897

Closed louix closed 2 months ago

louix commented 2 months ago

Issue: #7863

github-actions[bot] commented 2 months ago

Hey @louix, thanks for your pull request!

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

DDB changes

Modified

Validation

[!TIP] Everything is fine !

:duck: Updated for commit 4a2cf71d1e9775b04d50c4618342d204aa8f4cae

manup commented 2 months ago
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 :)

Smanar commented 2 months ago

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 ?

louix commented 2 months ago

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!

Smanar commented 2 months ago

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.

manup commented 2 months ago

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

louix commented 2 months ago

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.

[0]: https://duktape.org/dukweb#print(JSON.stringify(%7B1%3A%20true%7D))%0Aprint(%7B'1'%3A%20'hello'%7D%5B1%5D)%0A%0Aprint(%22%3A)%22)

manup commented 2 months ago

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.

github-actions[bot] commented 2 months ago

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

DDB Files

Modified

:clock1230: Updated for commit 7e34c7ec228a94481a6144ceb342e5ef130b0ee1