Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/util/hass-attributes-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
'window'
],
cover: ['garage'],
sensor: [
'battery',
'humidity',
'temperature'
],
};

window.hassAttributeUtil.UNKNOWN_TYPE = 'json';
Expand Down Expand Up @@ -70,7 +75,7 @@
type: 'array',
options: window.hassAttributeUtil.DOMAIN_DEVICE_CLASS,
description: 'Device class',
domains: ['binary_sensor', 'cover']
domains: ['binary_sensor', 'cover', 'sensor']
},
hidden: { type: 'boolean', description: 'Hide from UI' },
assumed_state: {
Expand Down
32 changes: 29 additions & 3 deletions src/util/hass-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,43 @@
}
};

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.

if (isNaN(state.state)) {
return 'mdi:battery-unknown';
}
const batteryRound = Math.round(state.state / 10) * 10;
if (batteryRound >= 100) {
return 'mdi:battery';
}
if (batteryRound <= 0) {
return 'mdi:battery-alert';
}
return `mdi:battery-${batteryRound}`;
}
case 'humidity':
return 'mdi:water-percent';
case 'temperature':
return 'mdi:thermometer';
default:
return 'mdi:eye';
}
};

window.hassUtil.stateIcon = function (state) {
if (!state) {
return window.hassUtil.DEFAULT_ICON;
} else if (state.attributes.icon) {
return state.attributes.icon;
}

var unit = state.attributes.unit_of_measurement;
var domain = window.hassUtil.computeDomain(state);
const unit = state.attributes.unit_of_measurement;
const domain = window.hassUtil.computeDomain(state);

if (unit && domain === 'sensor') {
if (domain === 'sensor' && state.attributes.device_class) {
return window.hassUtil.sensorIcon(state);
} else if (domain === 'sensor' && unit) {
if (unit === '°C' || unit === '°F') {
return 'mdi:thermometer';
} else if (unit === 'Mice') {
Expand Down