Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 26 additions & 1 deletion src/util/hass-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@
}
};

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 :)

switch (state.attributes.device_class) {
case 'battery':
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

if (batteryRound >= 100) {
return 'mdi:battery';
}
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}`;

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;
Expand All @@ -291,7 +314,9 @@
var unit = state.attributes.unit_of_measurement;
var 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