Skip to content

Commit

Permalink
Merge pull request #14 from nudded/master
Browse files Browse the repository at this point in the history
Allow entity_id als value in severity list.
  • Loading branch information
Gluwc authored Jul 23, 2019
2 parents f30f113 + 70b7690 commit 5e08e05
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions bar-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class BarCard extends HTMLElement {
justify-content: space-around;
flex-wrap: wrap;
${cardStyle}
}
}
`
// For each entity in entities list create cardElements
this._configArray = []
Expand All @@ -136,7 +136,7 @@ class BarCard extends HTMLElement {
// Add card container to root
this.shadowRoot.appendChild(cardContainer)
this.shadowRoot.appendChild(cardContainerStyle)

// For each entity in entities list update entity.
if (this._hass) {
for(let i=0; i <= config.entities.length-1; i++){
Expand Down Expand Up @@ -174,7 +174,7 @@ class BarCard extends HTMLElement {
const contentBar = document.createElement('div')
contentBar.id = 'contentBar_'+id
var icon = document.createElement('ha-icon')
icon.id = 'icon_'+id
icon.id = 'icon_'+id
var title = document.createElement('div')
title.id = 'title_'+id
var titleBar = document.createElement('div')
Expand All @@ -199,7 +199,7 @@ class BarCard extends HTMLElement {
indicatorBar.id = 'indicatorBar_'+id
var indicator = document.createElement('div')
indicator.id = 'indicator_'+id

// Start building card
background.appendChild(backgroundBar)
background.appendChild(bar)
Expand Down Expand Up @@ -247,7 +247,7 @@ class BarCard extends HTMLElement {
container.appendChild(background)
break
case 'off':
container.appendChild(background)
container.appendChild(background)
}
contentBar.appendChild(valueContainer)
if (config.show_minmax == true) {
Expand Down Expand Up @@ -275,7 +275,7 @@ class BarCard extends HTMLElement {

return card
}

// Create style elements
_styleElements(config, id) {
const style = document.createElement('style');
Expand Down Expand Up @@ -335,7 +335,7 @@ class BarCard extends HTMLElement {
titleflexDirection = 'flex-direction: column-reverse;'
break
}

// Set value flex direction based on card direction
let valueflexDirection
switch (config.direction) {
Expand Down Expand Up @@ -423,7 +423,7 @@ class BarCard extends HTMLElement {
padding-bottom: 4px;
text-overflow: ellipsis;
overflow: hidden;
`
`
}
let iconMarginStyle = ''
if (config.icon_position !== 'inside' && config.title_position !== 'off' && config.title_position !== 'inside') {
Expand Down Expand Up @@ -686,11 +686,12 @@ class BarCard extends HTMLElement {
}

// Returns color based on severity array
_computeSeverity (stateValue, sections) {
_computeSeverity (stateValue, sections, hass) {
let numberValue = Number(stateValue)
let color
sections.forEach(section => {
if (numberValue <= section.value && !color) {
let actualValue = this._valueEntityCheck(section.value, hass)
if (numberValue <= actualValue && !color) {
color = section.color
}
})
Expand Down Expand Up @@ -730,7 +731,7 @@ class BarCard extends HTMLElement {
_maxCheck (entity, hass, index) {
const config = this._configAttributeCheck(entity, index)
if (hass.states[entity].attributes.max && config.entity_config == true) return hass.states[entity].attributes.max
else return config.max
else return config.max
}

_serviceCall (domain, service, data) {
Expand Down Expand Up @@ -891,7 +892,7 @@ class BarCard extends HTMLElement {
indicatorBarElement.style.setProperty('--flex-direction','column')
case 'top':
indicatorBarElement.style.setProperty('--justify-content','flex-start')
indicatorBarElement.style.setProperty('--flex-direction','column')
indicatorBarElement.style.setProperty('--flex-direction','column')
}
break
case 'off':
Expand All @@ -914,7 +915,7 @@ class BarCard extends HTMLElement {
let currentPercent = this._translatePercent(entityState, minValue, maxValue, index, entity)
let targetPercent = this._translatePercent(target, minValue, maxValue, index, entity)
let initialPercent
let diffPercent
let diffPercent
if (currentPercent > targetPercent) {
initialPercent = targetPercent
diffPercent = currentPercent
Expand All @@ -933,12 +934,12 @@ class BarCard extends HTMLElement {
}
}

_calculateBarColor (config, entityState) {
_calculateBarColor (config, entityState, hass) {
let barColor
if (config.severity == false) {
barColor = config.color
barColor = config.color
} else {
barColor = this._computeSeverity(entityState, config.severity)
barColor = this._computeSeverity(entityState, config.severity, hass)
}
return barColor
}
Expand All @@ -958,7 +959,7 @@ class BarCard extends HTMLElement {
}
})
}
return config
return config
}

// On entity update
Expand Down Expand Up @@ -1028,7 +1029,7 @@ class BarCard extends HTMLElement {

// On entity update
if (entityState !== this._entityState[id]) {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)

if (config.visibility !== false) {
if (entityState == 'N/A' || config.visibility == true) {
Expand Down Expand Up @@ -1061,7 +1062,7 @@ class BarCard extends HTMLElement {

// Animation is auto
if (config.animation == 'auto') {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)
if (entityState > this._entityState[id]) {
this._animationDirection[id] = 'normal'
if (this._currentAnimation[id]) {
Expand Down Expand Up @@ -1092,13 +1093,13 @@ class BarCard extends HTMLElement {
}
}
}
}
}

// Animation is charge
if (config.charge_entity !== false) {
if (!this._currentChargeState) this._currentChargeState = {}
if (this._currentChargeState[id] !== chargeEntityState || entityState !== this._entityState[id]) {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)
switch (chargeEntityState) {
case "charging":
case "on":
Expand All @@ -1113,7 +1114,7 @@ class BarCard extends HTMLElement {
case "discharging":
case "off":
case "false":
if (config.indicator !== 'off') this._updateIndicator(config, config.indicator, 'down', id, barColor)
if (config.indicator !== 'off') this._updateIndicator(config, config.indicator, 'down', id, barColor)
if (!this._currentAnimation[id] || chargeEntityState != this._currentChargeState || entityState < this._entityState[id]) {
this._currentChargeState[id] = chargeEntityState
this._animationDirection[id] = 'reverse'
Expand All @@ -1123,23 +1124,23 @@ class BarCard extends HTMLElement {
}
}
}

// On target update
if (config.target != false) {
if (configTarget != this._entityTarget[id]) {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)
this._updateTargetBar(entityState, configTarget, barColor, id, entity, index)
this._entityTarget[id] = configTarget
if (this._currentAnimation[id] && config.animation !== 'off') this._currentAnimation[id] = this._updateAnimation(entityState, config.delay, false, id, entity, index)
}
}

// On min update
if (configMin !== this._currentMin[id]) {
this._updateBar(entityState, hass, id, entity, index)
this._currentMin[id] = configMin
if (config.target != false) {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)
this._updateTargetBar(entityState, configTarget, barColor, id, entity, index)
this._currentMin[id] = configMin
}
Expand All @@ -1151,7 +1152,7 @@ class BarCard extends HTMLElement {
this._updateBar(entityState, hass, id, entity, index)
this._currentMax[id] = configMax
if (config.target != false) {
const barColor = this._calculateBarColor(config, entityState)
const barColor = this._calculateBarColor(config, entityState, hass)
this._updateTargetBar(entityState, configTarget, barColor, id, entity, index)
this._currentMax[id] = configMax
}
Expand Down

0 comments on commit 5e08e05

Please sign in to comment.