Add Sensor device_class support#1115
Conversation
| } | ||
| }; | ||
|
|
||
| window.hassUtil.sensorIcon = function (state) { |
There was a problem hiding this comment.
Oh, we have ES6. Well that makes the code a lot nicer :)
| if (isNaN(state.state)) { | ||
| return 'mdi:battery-unknown'; | ||
| } | ||
| var batteryRound = Math.round(state.state / 10) * 10; |
| if (batteryRound <= 0) { | ||
| return 'mdi:battery-alert'; | ||
| } | ||
| return 'mdi:battery-' + batteryRound; |
There was a problem hiding this comment.
return `mdi:battery-${batteryRound}`;
|
Thanks for wrapping up the work for me 😀 . looks good. |
|
|
||
| window.hassUtil.sensorIcon = (state) => { | ||
| switch (state.attributes.device_class) { | ||
| case 'battery': { |
There was a problem hiding this comment.
You don't need brackets for case
There was a problem hiding this comment.
Yes you're right, they're not strictly required. I added it because ESLint was complaining about it (no-case-declarations). And apparently having the const in there could have some hard-to-debug issues if more logic would be added to other case statements:
This rule disallows lexical declarations (let, const, function and class) in case/default clauses. The reason is that the lexical declaration is visible in the entire switch block but it only gets initialized when it is assigned, which will only happen if the case where it is defined is reached.
|
We can trick the HASS iconset generator by just including all potential names in a comment. |
This PR adds frontend
device_classsupport for home-assistant/core#14010 based on the device_class implementations ofbinary_sensorandcover.Please note that JavaScript is not my "native" language so feel free to close this in favor of a new PR if there are many problems with this code change. I'm also currently battling with the build-system and I haven't yet been able to fully test the change.
For full compatibility, the
unit_of_measurementto icon conversion is still in there and for thebatterydevice class I pretty much just took this template and tried to convert it to JS. ForundefinedorNaNbattery levels this will use themdi:battery-unknownicon.Once #1100 gets merged, this PR will also need to be updated. Especially the
mdi:battery-number automaticmdi:*icon detection would not work.