Skip to content

Commit

Permalink
Merge pull request #40 from htmltiger/htmltiger-patch-1
Browse files Browse the repository at this point in the history
Flicker fix
  • Loading branch information
htmltiger authored Feb 15, 2022
2 parents 3e067de + 7b27828 commit 0dbd0fb
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions numberbox-card.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((LitElement) => {

console.info('NUMBERBOX_CARD 3.5');
console.info('NUMBERBOX_CARD 3.6');
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
class NumberBox extends LitElement {
Expand All @@ -11,6 +11,7 @@ constructor() {
this.pending = false;
this.rolling = false;
this.state = 0;
this.laststate = undefined;
}

render() {
Expand All @@ -27,8 +28,6 @@ render() {
if(!this.config.icon_plus){this.config.icon_plus='mdi:plus';}
if(!this.config.icon_minus){this.config.icon_minus='mdi:minus';}
if( this.config.delay === undefined ){ this.config.delay=1000;}
this.state=this.stateObj.state;
if(this.config.state){this.state=this.stateObj.attributes[this.config.state];}
if(this.config.min === undefined){ this.config.min=this.stateObj.attributes.min;}
if(this.config.max === undefined){ this.config.max=this.stateObj.attributes.max;}
if(this.config.step === undefined){ this.config.step=this.stateObj.attributes.step;}
Expand Down Expand Up @@ -127,6 +126,7 @@ publishNum(dhis){
const s=dhis.config.service.split('.');
const v={entity_id: dhis.config.entity, [dhis.config.param]: dhis.pending};
dhis.pending=false;
dhis.laststate=dhis.state;
dhis._hass.callService(s[0], s[1], v);
}

Expand All @@ -141,7 +141,9 @@ niceNum(){
}
}
let stp=Number(this.config.step) || 1;
if( Math.round(stp) != stp ){ fix=stp.toString().split(".")[1].length || 1; stp=fix;}
if( Math.round(stp) != stp ){
fix=stp.toString().split(".")[1].length || 1; stp=fix;
}else{ stp=fix; }
fix = v.toFixed(fix);
const u=this.config.unit;
if( u=="time" ){
Expand All @@ -155,7 +157,7 @@ niceNum(){
}
fix = new Intl.NumberFormat(
this._hass.language,
{maximumFractionDigits: stp}
{maximumFractionDigits: stp, minimumFractionDigits: stp}
).format(Number(fix));
return u===false ? fix: html`${fix}<span class="cur-unit" >${u}</span>`;
}
Expand All @@ -182,6 +184,7 @@ static get properties() {
rolling: {},
pending: {},
state: {},
laststate: {},
}
}

Expand Down Expand Up @@ -283,7 +286,11 @@ set hass(hass) {
}

shouldUpdate(changedProps) {
if( changedProps.has('config') || changedProps.has('stateObj') || changedProps.has('pending') ){return true;}
if( changedProps.has('config') || changedProps.has('stateObj') || changedProps.has('pending') ){
this.state=this.stateObj.state;
if(this.config.state){this.state=this.stateObj.attributes[this.config.state];}
if(this.laststate != this.state){ return true; }
}
}

static getConfigElement() {
Expand Down

0 comments on commit 0dbd0fb

Please sign in to comment.