mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-08 18:06:36 +00:00
Add Sensor device_class support (#1115)
* Add Sensor Device Class Support * ES6
This commit is contained in:
parent
db2f588e86
commit
a22b62cf2a
@ -28,6 +28,11 @@ window.hassAttributeUtil.DOMAIN_DEVICE_CLASS = {
|
|||||||
'window'
|
'window'
|
||||||
],
|
],
|
||||||
cover: ['garage'],
|
cover: ['garage'],
|
||||||
|
sensor: [
|
||||||
|
'battery',
|
||||||
|
'humidity',
|
||||||
|
'temperature'
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
window.hassAttributeUtil.UNKNOWN_TYPE = 'json';
|
window.hassAttributeUtil.UNKNOWN_TYPE = 'json';
|
||||||
@ -70,7 +75,7 @@ window.hassAttributeUtil.LOGIC_STATE_ATTRIBUTES =
|
|||||||
type: 'array',
|
type: 'array',
|
||||||
options: window.hassAttributeUtil.DOMAIN_DEVICE_CLASS,
|
options: window.hassAttributeUtil.DOMAIN_DEVICE_CLASS,
|
||||||
description: 'Device class',
|
description: 'Device class',
|
||||||
domains: ['binary_sensor', 'cover']
|
domains: ['binary_sensor', 'cover', 'sensor']
|
||||||
},
|
},
|
||||||
hidden: { type: 'boolean', description: 'Hide from UI' },
|
hidden: { type: 'boolean', description: 'Hide from UI' },
|
||||||
assumed_state: {
|
assumed_state: {
|
||||||
|
@ -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) {
|
window.hassUtil.stateIcon = function (state) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
return window.hassUtil.DEFAULT_ICON;
|
return window.hassUtil.DEFAULT_ICON;
|
||||||
@ -288,10 +312,12 @@ window.hassUtil.stateIcon = function (state) {
|
|||||||
return state.attributes.icon;
|
return state.attributes.icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
var unit = state.attributes.unit_of_measurement;
|
const unit = state.attributes.unit_of_measurement;
|
||||||
var domain = window.hassUtil.computeDomain(state);
|
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') {
|
if (unit === '°C' || unit === '°F') {
|
||||||
return 'mdi:thermometer';
|
return 'mdi:thermometer';
|
||||||
} else if (unit === 'Mice') {
|
} else if (unit === 'Mice') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user