iobroker.lovelace
Version:
With this adapter you can build visualization for ioBroker with Home Assistant Lovelace UI
1 lines • 9.51 kB
JavaScript
export const __rspack_esm_id=67518;export const __rspack_esm_ids=[67518];export const __webpack_modules__={59277(e,t,a){a.a(e,async function(e,r){try{a.r(t);a(74423),a(44114),a(18111),a(22489),a(7588),a(18237),a(45367),a(92731),a(17642),a(58004),a(33853),a(45876),a(32475),a(15024),a(31698);var s=a(62826),o=a(5124),i=a(22441),n=a(6412),l=(a(76776),a(67094),a(1087)),c=a(38114),u=a(54706),h=a(3609),d=a(90433),_=a(28455),g=a(48949),p=e([h,c]);[h,c]=p.then?(await p)():p;const y={group_by_floor:!0,group_by_area:!0},f=.001;class v extends((0,u.E)((0,g.Q)(o.WF))){static async getConfigElement(){return await a.e(92923).then(a.bind(a,46358)),document.createElement("hui-energy-sankey-card-editor")}static getStubConfig(e,t,a){return{type:"power-sankey",layout:"auto",...y}}setConfig(e){e.collection_key&&(0,c.s8)(e.collection_key),this._config={...y,...e}}hassSubscribe(){return[(0,c.tb)(this.hass,{key:this._config?.collection_key}).subscribe(e=>{this._data=e})]}getCardSize(){return 5}getGridOptions(){return{columns:12,min_columns:6,rows:6,min_rows:2}}shouldUpdate(e){if(e.has("_config")||e.has("_data")||e.has("_isMobileSize"))return!0;if(e.has("hass")){const t=e.get("hass");if(!t||!this._entities.size)return!0;for(const e of this._entities)if(t.states[e]!==this.hass.states[e])return!0}return!1}render(){if(!this._config)return o.s6;if(!this._data)return o.qy`${this.hass.localize("ui.panel.lovelace.cards.energy.loading")}`;const e=this._data.prefs,t=this._computePowerData(e),a=getComputedStyle(this),r=t.used_total*f,s=[],i=[],l={id:"home",label:this.hass.config.location_name,value:Math.max(0,t.used_total),color:a.getPropertyValue("--primary-color").trim(),index:1};s.push(l),t.from_battery>0&&(s.push({id:"battery",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_distribution.battery"),value:t.from_battery,color:a.getPropertyValue("--energy-battery-out-color").trim(),index:0}),i.push({source:"battery",target:"home"})),t.to_battery>0&&(s.push({id:"battery_in",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_distribution.battery"),value:t.to_battery,color:a.getPropertyValue("--energy-battery-in-color").trim(),index:1}),t.grid_to_battery>0&&i.push({source:"grid",target:"battery_in"}),t.solar_to_battery>0&&i.push({source:"solar",target:"battery_in"})),t.from_grid>0&&(s.push({id:"grid",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_distribution.grid"),value:t.from_grid,color:a.getPropertyValue("--energy-grid-consumption-color").trim(),index:0}),i.push({source:"grid",target:"home"})),t.solar>0&&(s.push({id:"solar",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_distribution.solar"),value:t.solar,color:a.getPropertyValue("--energy-solar-color").trim(),index:0}),i.push({source:"solar",target:"home"})),t.to_grid>0&&(s.push({id:"grid_return",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_distribution.grid"),value:t.to_grid,color:a.getPropertyValue("--energy-grid-return-color").trim(),index:1}),t.battery_to_grid>0&&i.push({source:"battery",target:"grid_return"}),t.solar_to_grid>0&&i.push({source:"solar",target:"grid_return"}));let c=l.value;const u=[],h={},_=new Map;e.device_consumption.forEach(e=>{_.set(e.stat_consumption,{stat_rate:e.stat_rate,included_in_stat:e.included_in_stat})});const g=new Set;e.device_consumption.forEach(e=>{if(e.stat_rate){this._getCurrentPower(e.stat_rate)>=r&&g.add(e.stat_rate)}});const p=new Map;e.device_consumption.forEach((e,t)=>{if(!e.stat_rate)return;const s=this._getCurrentPower(e.stat_rate),o=(e=>{let t=e;for(;t;){const e=_.get(t);if(!e)return;if(e.stat_rate&&g.has(e.stat_rate))return e.stat_rate;t=e.included_in_stat}})(e.included_in_stat);if(s<r){const a=o??"home";return p.has(a)||p.set(a,[]),void p.get(a).push({statRate:e.stat_rate,name:e.name,value:s,effectiveParent:o,idx:t})}const n={id:e.stat_rate,label:e.name||this._getEntityLabel(e.stat_rate),value:s,color:(0,d.fI)(t,a),index:4,parent:o,entityId:e.stat_rate};n.parent?(h[n.id]=n.parent,i.push({source:n.parent,target:n.id})):c-=s,u.push(n)}),p.forEach((e,t)=>{const r=e.reduce((e,t)=>e+t.value,0);if(!(r<=0))if(1===e.length){const t=e[0],r={id:t.statRate,label:t.name||this._getEntityLabel(t.statRate),value:t.value,color:(0,d.fI)(t.idx,a),index:4,parent:t.effectiveParent,entityId:t.statRate};r.parent?(h[r.id]=r.parent,i.push({source:r.parent,target:r.id})):c-=t.value,u.push(r)}else{const e=`other_${t}`,s={id:e,label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_devices_detail_graph.other"),value:Math.ceil(r),color:a.getPropertyValue("--state-unavailable-color").trim(),index:4};"home"!==t?(h[e]=t,i.push({source:t,target:e})):c-=r,u.push(s)}});const y=u.filter(e=>!h[e.id]),{group_by_area:v,group_by_floor:m}=this._config;if(v||m){const{areas:e,floors:t}=this._groupByFloorAndArea(y);Object.keys(t).sort((e,t)=>(this.hass.floors[t]?.level??-1/0)-(this.hass.floors[e]?.level??-1/0)).forEach(r=>{let o=`floor_${r}`;"no_floor"!==r&&m?(s.push({id:o,label:this.hass.floors[r].name,value:t[r].value,index:2,color:a.getPropertyValue("--primary-color").trim()}),i.push({source:"home",target:o})):o="home",t[r].areas.forEach(t=>{let r;if("no_area"!==t&&v){const n=`area_${t}`;s.push({id:n,label:this.hass.areas[t]?.name||t,value:e[t].value,index:3,color:a.getPropertyValue("--primary-color").trim()}),i.push({source:o,target:n,value:e[t].value}),r=n}else r=o;e[t].devices.forEach(e=>{i.push({source:r,target:e.id,value:e.value})})})})}else y.forEach(e=>{i.push({source:"home",target:e.id,value:e.value})});const b=this._getDeviceSections(h,u);b.forEach((e,t)=>{e.forEach(e=>{s.push({...e,index:4+t})})}),c>1&&(s.push({id:"untracked",label:this.hass.localize("ui.panel.lovelace.cards.energy.energy_devices_detail_graph.untracked_consumption"),value:c,color:a.getPropertyValue("--state-unavailable-color").trim(),index:3+b.length}),i.push({source:"home",target:"untracked",value:c}));const x=s.some(e=>e.value>0),E="vertical"===this._config.layout||"horizontal"!==this._config.layout&&this._isMobileSize;return o.qy` <ha-card .header="${this._config.title}" class="${(0,n.H)({"is-grid":"grid"===this.layout,"is-panel":"panel"===this.layout,"is-vertical":E})}"> <div class="card-content"> ${x?o.qy`<ha-sankey-chart .hass="${this.hass}" .data="${{nodes:s,links:i}}" .vertical="${E}" .valueFormatter="${this._valueFormatter}" -click="${this._handleNodeClick}"></ha-sankey-chart>`:o.qy`${this.hass.localize("ui.panel.lovelace.cards.energy.no_data")}`} </div> </ha-card> `}_handleNodeClick(e){const{node:t}=e.detail;t.entityId&&(0,l.r)(this,"hass-more-info",{entityId:t.entityId})}_computePowerData(e){this._entities.clear();let t=0,a=0,r=0,s=0,o=0;e.energy_sources.filter(e=>"solar"===e.type).forEach(e=>{if("solar"===e.type&&e.stat_rate){const a=this._getCurrentPower(e.stat_rate);a>0&&(t+=a)}}),e.energy_sources.filter(e=>"grid"===e.type).forEach(e=>{if("grid"===e.type&&e.stat_rate){const t=this._getCurrentPower(e.stat_rate);t>0?a+=t:t<0&&(r+=Math.abs(t))}});let i=0;e.energy_sources.filter(e=>"battery"===e.type).forEach(e=>{"battery"===e.type&&e.stat_rate&&(i+=this._getCurrentPower(e.stat_rate))}),i>0?s=i:i<0&&(o=Math.abs(i));const n=a+t+s-r-o;let l=t,c=a,u=s,h=o,d=r,_=Math.max(n,0),g=0;const p=Math.max(0,Math.min(h,c-_));g+=p,h-=p,c-=p;const y=Math.min(l,h);h-=y,l-=y;const f=Math.min(l,d);d-=f,l-=f;const v=Math.min(u,d);u-=v;const m=Math.min(c,h);g+=m,c-=m;const b=Math.min(_,l);_-=b;const x=Math.min(u,_);_-=x;const E=Math.min(_,c);return{solar:t,from_grid:a,to_grid:r,from_battery:s,to_battery:o,grid_to_battery:g,battery_to_grid:v,solar_to_battery:y,solar_to_grid:f,used_solar:b,used_grid:E,used_battery:x,used_total:Math.max(0,n)}}_groupByFloorAndArea(e){const t={no_area:{value:0,devices:[]}},a={no_floor:{value:0,areas:["no_area"]}};return e.forEach(e=>{const r=this.hass.states[e.id],{area:s,floor:o}=r?(0,_.ln)(r,this.hass.entities,this.hass.devices,this.hass.areas,this.hass.floors):{area:null,floor:null};s?(s.area_id in t?(t[s.area_id].value+=e.value,t[s.area_id].devices.push(e)):t[s.area_id]={value:e.value,devices:[e]},o?o.floor_id in a?(a[o.floor_id].value+=e.value,a[o.floor_id].areas.includes(s.area_id)||a[o.floor_id].areas.push(s.area_id)):a[o.floor_id]={value:e.value,areas:[s.area_id]}:(a.no_floor.value+=e.value,a.no_floor.areas.includes(s.area_id)||a.no_floor.areas.unshift(s.area_id))):(t.no_area.value+=e.value,t.no_area.devices.push(e))}),{areas:t,floors:a}}_getDeviceSections(e,t){const a=[],r=[],s=Object.values(e),o={};return t.forEach(t=>{const o=t.id in e;s.includes(t.id)&&!o?a.push(t):r.push(t)}),Object.entries(e).forEach(([e,t])=>{a.some(e=>e.id===t)||(o[e]=t)}),a.length>0?[a,...this._getDeviceSections(o,r)]:[t]}_getCurrentPower(e){return this._entities.add(e),(0,c.h4)(this.hass.states[e])??0}_getEntityLabel(e){const t=this.hass.states[e];return t&&t.attributes.friendly_name||e}constructor(...e){super(...e),this._entities=new Set,this.hassSubscribeRequiredHostProps=["_config"],this._valueFormatter=e=>(0,c.t5)(this.hass,e)}}v.styles=o.AH`ha-card{height:400px;display:flex;flex-direction:column;--chart-max-height:none}ha-card.is-vertical{height:500px}ha-card.is-grid,ha-card.is-panel{height:100%}.card-content{flex:1;display:flex}`,(0,s.Cg)([(0,i.MZ)({attribute:!1})],v.prototype,"hass",void 0),(0,s.Cg)([(0,i.MZ)({attribute:!1})],v.prototype,"layout",void 0),(0,s.Cg)([(0,i.wk)()],v.prototype,"_config",void 0),(0,s.Cg)([(0,i.wk)()],v.prototype,"_data",void 0),v=(0,s.Cg)([(0,i.EM)("hui-power-sankey-card")],v),r()}catch(e){r(e)}})}};