diff --git a/src/util/hass-attributes-util.html b/src/util/hass-attributes-util.html
index 3d50b73f97..bdd91f91df 100644
--- a/src/util/hass-attributes-util.html
+++ b/src/util/hass-attributes-util.html
@@ -28,6 +28,11 @@ window.hassAttributeUtil.DOMAIN_DEVICE_CLASS = {
'window'
],
cover: ['garage'],
+ sensor: [
+ 'battery',
+ 'humidity',
+ 'temperature'
+ ],
};
window.hassAttributeUtil.UNKNOWN_TYPE = 'json';
@@ -70,7 +75,7 @@ window.hassAttributeUtil.LOGIC_STATE_ATTRIBUTES =
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: {
diff --git a/src/util/hass-util.html b/src/util/hass-util.html
index 2c511aab89..d00ee7f642 100644
--- a/src/util/hass-util.html
+++ b/src/util/hass-util.html
@@ -281,6 +281,30 @@ window.hassUtil.coverIcon = function (state) {
}
};
+window.hassUtil.sensorIcon = (state) => {
+ switch (state.attributes.device_class) {
+ case 'battery': {
+ 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;
@@ -288,10 +312,12 @@ window.hassUtil.stateIcon = function (state) {
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') {