Skip to content

Add Sensor device_class support#1115

Merged
balloob merged 2 commits intohome-assistant:masterfrom
OttoWinter:sensor-device-class
Apr 23, 2018
Merged

Add Sensor device_class support#1115
balloob merged 2 commits intohome-assistant:masterfrom
OttoWinter:sensor-device-class

Conversation

@OttoWinter
Copy link
Copy Markdown
Member

@OttoWinter OttoWinter commented Apr 21, 2018

This PR adds frontend device_class support for home-assistant/core#14010 based on the device_class implementations of binary_sensor and cover.

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_measurement to icon conversion is still in there and for the battery device class I pretty much just took this template and tried to convert it to JS. For undefined or NaN battery levels this will use the mdi:battery-unknown icon.

Once #1100 gets merged, this PR will also need to be updated. Especially the mdi:battery-number automatic mdi:* icon detection would not work.

Comment thread src/util/hass-util.html Outdated
}
};

window.hassUtil.sensorIcon = function (state) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= (state) => {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we have ES6. Well that makes the code a lot nicer :)

Comment thread src/util/hass-util.html Outdated
if (isNaN(state.state)) {
return 'mdi:battery-unknown';
}
var batteryRound = Math.round(state.state / 10) * 10;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const batteryRound

Comment thread src/util/hass-util.html Outdated
if (batteryRound <= 0) {
return 'mdi:battery-alert';
}
return 'mdi:battery-' + batteryRound;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return `mdi:battery-${batteryRound}`;

@balloob
Copy link
Copy Markdown
Member

balloob commented Apr 21, 2018

Thanks for wrapping up the work for me 😀 . looks good.

Comment thread src/util/hass-util.html

window.hassUtil.sensorIcon = (state) => {
switch (state.attributes.device_class) {
case 'battery': {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need brackets for case

Copy link
Copy Markdown
Member Author

@OttoWinter OttoWinter Apr 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@balloob balloob merged commit a22b62c into home-assistant:master Apr 23, 2018
@balloob
Copy link
Copy Markdown
Member

balloob commented Apr 23, 2018

We can trick the HASS iconset generator by just including all potential names in a comment.

@github-actions github-actions Bot locked and limited conversation to collaborators Jul 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants