Skip to content

Commit 8079b11

Browse files
committed
bug fix + messages
Fixed a bug where current bar percentage would keep animating when value is increasing after it decreasing. Added error message for not having a charge entity defined when using charge mode. Added N/A state when entity is undefined or entity state is unknown.
1 parent c754090 commit 8079b11

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

bar-card.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ class BarCard extends HTMLElement {
1313
const cardConfig = Object.assign({}, config);
1414
if (!cardConfig.height) cardConfig.height = "40px";
1515
if (!cardConfig.from) cardConfig.from = "left";
16-
if (cardConfig.from == "left")
17-
{
18-
cardConfig.from = "right";
19-
}
20-
else
21-
{
22-
cardConfig.from = "left";
23-
}
16+
if (cardConfig.from == "left") cardConfig.from = "right";
17+
else cardConfig.from = "left";
2418
if (!config.rounding) config.rounding = "3px";
2519
if (!config.width) config.width = "70%";
2620
if (!config.indicator) config.indicator = true;
@@ -159,7 +153,7 @@ class BarCard extends HTMLElement {
159153
for(; i <= totalFrames; ){
160154
let brightness = this._scale(i/10, currentPercent, currentPercent+25, 50, 15);
161155
if(brightness <= 15) brightness = 15;
162-
let keyframe = {"--bar-charge-percent": i/10+"%", "--bar-charge-color": "hsla("+hue+","+saturation+","+brightness+"%,0.5)"};
156+
let keyframe = {"--bar-charge-percent": i/10+"%", "--bar-percent": currentPercent+"%", "--bar-charge-color": "hsla("+hue+","+saturation+","+brightness+"%,0.5)"};
163157
keyframes.push(keyframe);
164158
i++;
165159
}
@@ -212,10 +206,16 @@ class BarCard extends HTMLElement {
212206
if(!config.animation)config.animation = 'auto';
213207
if(!config.indicator)config.indicator = true;
214208
const root = this.shadowRoot;
215-
let entityState = hass.states[config.entity].state;
216-
entityState = Math.min(entityState, config.max);
217-
entityState = Math.max(entityState, config.min);
218-
const measurement = hass.states[config.entity].attributes.unit_of_measurement || "";
209+
let entityState;
210+
if(hass.states[config.entity] == undefined || hass.states[config.entity].state == "unknown") entityState = "N/A";
211+
else {
212+
entityState = hass.states[config.entity].state;
213+
entityState = Math.min(entityState, config.max);
214+
entityState = Math.max(entityState, config.min);
215+
}
216+
let measurement
217+
if(hass.states[config.entity] == undefined || hass.states[config.entity].state == "unknown") measurement = "";
218+
else measurement = hass.states[config.entity].attributes.unit_of_measurement || "";
219219
let hue;
220220
if(!config.severity) {
221221
hue = 220;
@@ -226,7 +226,6 @@ class BarCard extends HTMLElement {
226226
else{
227227
hue = this._computeSeverity(entityState, config.severity);
228228
}
229-
//const gradientHue = scale((100 - this._translatePercent(entityState, config.min, config.max)), 0, 100, 0, 120);
230229
const color = 'hsl('+hue+','+config.saturation+',50%)';
231230
const backgroundColor = 'hsla('+hue+','+config.saturation+',15%,0.5)';
232231
const chargeColor = 'hsla('+hue+','+config.saturation+',30%,0.5)';
@@ -263,10 +262,14 @@ class BarCard extends HTMLElement {
263262
}
264263

265264
if(config.animation == 'charge'){
265+
let chargeEntityState;
266266
if(!config.charge_entity){
267-
throw new Error('Please define a charge entity');
267+
entityState = "define 'charge_entity'";
268+
measurement = "";
269+
root.getElementById("value").style.setProperty('color', '#FF0000');
270+
}else{
271+
chargeEntityState = hass.states[config.charge_entity].state;
268272
}
269-
const chargeEntityState = hass.states[config.charge_entity].state;
270273
if (chargeEntityState == "charging" || chargeEntityState =="on" || chargeEntityState == "true") {
271274
root.getElementById("indicator").style.setProperty('right', 0);
272275
root.getElementById("indicator").textContent = '▲';

0 commit comments

Comments
 (0)