ha-frontend/src/components/entity/state-badge.html

90 lines
2.3 KiB
HTML
Raw Normal View History

2015-08-23 10:26:31 +02:00
<link rel='import' href='../../../bower_components/polymer/polymer.html'>
2015-07-13 01:57:15 +02:00
<link rel='import' href='./ha-state-icon.html'>
2015-07-13 01:57:15 +02:00
<dom-module id='state-badge'>
<template>
2016-07-19 10:19:44 +02:00
<style>
:host {
position: relative;
display: inline-block;
width: 40px;
color: #44739E;
border-radius: 50%;
height: 40px;
text-align: center;
background-size: cover;
line-height: 40px;
}
ha-state-icon {
transition: color .3s ease-in-out;
}
/* Color the icon if light or sun is on */
ha-state-icon[data-domain=light][data-state=on],
ha-state-icon[data-domain=switch][data-state=on],
ha-state-icon[data-domain=binary_sensor][data-state=on],
ha-state-icon[data-domain=sun][data-state=above_horizon] {
color: #FDD835;
2016-07-19 10:19:44 +02:00
}
/* Color the icon if unavailable */
ha-state-icon[data-state=unavailable] {
color: var(--disabled-text-color);
}
</style>
<ha-state-icon
id='icon'
state-obj='[[stateObj]]'
data-domain$='[[computeDomain(stateObj)]]'
2016-07-19 10:19:44 +02:00
data-state$='[[stateObj.state]]'
></ha-state-icon>
2015-07-13 01:57:15 +02:00
</template>
</dom-module>
2016-07-18 08:18:48 +02:00
<script>
Polymer({
is: 'state-badge',
properties: {
stateObj: {
type: Object,
observer: 'updateIconColor',
},
},
computeDomain: function (stateObj) {
return window.hassUtil.computeDomain(stateObj);
},
2016-07-18 08:18:48 +02:00
/**
* Called when an attribute changes that influences the color of the icon.
*/
updateIconColor: function (newVal) {
// hide icon if we have entity picture
if (newVal.attributes.entity_picture) {
this.style.backgroundImage = 'url(' + newVal.attributes.entity_picture + ')';
this.$.icon.style.display = 'none';
return;
}
this.style.backgroundImage = '';
this.$.icon.style.display = 'inline';
// for domain light, set color of icon to light color if available and it is
// not very white (sum rgb colors < 730)
if (window.hassUtil.computeDomain(newVal) === 'light' &&
newVal.state === 'on' &&
newVal.attributes.rgb_color &&
newVal.attributes.rgb_color.reduce(function (cur, tot) { return cur + tot; }, 0) < 730) {
2016-07-18 08:18:48 +02:00
this.$.icon.style.color = 'rgb(' + newVal.attributes.rgb_color.join(',') + ')';
} else {
this.$.icon.style.color = null;
}
},
});
</script>