iobroker.lovelace
Version:
With this adapter you can build visualization for ioBroker with Home Assistant Lovelace UI
1 lines • 10 kB
JavaScript
"use strict";(self.rspackChunkhome_assistant_frontend=self.rspackChunkhome_assistant_frontend||[]).push([[67518],{59277:function(e,t,a){a.a(e,async function(e,r){try{a.r(t);a(74423),a(44114),a(72712),a(26910),a(13609),a(18111),a(22489),a(7588),a(18237),a(45367),a(92731),a(3362),a(17642),a(58004),a(33853),a(45876),a(32475),a(15024),a(31698),a(42762),a(62953);var s=a(40445),o=a(5124),i=a(67576),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),y=e([h,c]);[h,c]=y.then?(await y)():y;let p,f,v,b,m,x=e=>e;const k={group_by_floor:!0,group_by_area:!0},E=.001;class M 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 Object.assign({type:"power-sankey",layout:"auto"},k)}setConfig(e){e.collection_key&&(0,c.s8)(e.collection_key),this._config=Object.assign(Object.assign({},k),e)}hassSubscribe(){var e;return[(0,c.tb)(this.hass,{key:null===(e=this._config)||void 0===e?void 0:e.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(0,o.qy)(p||(p=x`${0}`),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*E,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 y=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=null!=o?o:"home";return y.has(a)||y.set(a,[]),void y.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)}),y.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 m=u.filter(e=>!h[e.id]),{group_by_area:k,group_by_floor:M}=this._config;if(k||M){const{areas:e,floors:t}=this._groupByFloorAndArea(m);Object.keys(t).sort((e,t)=>{var a,r,s,o;return(null!==(a=null===(r=this.hass.floors[t])||void 0===r?void 0:r.level)&&void 0!==a?a:-1/0)-(null!==(s=null===(o=this.hass.floors[e])||void 0===o?void 0:o.level)&&void 0!==s?s:-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&&k){var n;const l=`area_${t}`;s.push({id:l,label:(null===(n=this.hass.areas[t])||void 0===n?void 0:n.name)||t,value:e[t].value,index:3,color:a.getPropertyValue("--primary-color").trim()}),i.push({source:o,target:l,value:e[t].value}),r=l}else r=o;e[t].devices.forEach(e=>{i.push({source:r,target:e.id,value:e.value})})})})}else m.forEach(e=>{i.push({source:"home",target:e.id,value:e.value})});const P=this._getDeviceSections(h,u);P.forEach((e,t)=>{e.forEach(e=>{s.push(Object.assign(Object.assign({},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+P.length}),i.push({source:"home",target:"untracked",value:c}));const w=s.some(e=>e.value>0),C="vertical"===this._config.layout||"horizontal"!==this._config.layout&&this._isMobileSize;return(0,o.qy)(f||(f=x` <ha-card .header="${0}" class="${0}"> <div class="card-content"> ${0} </div> </ha-card> `),this._config.title,(0,n.H)({"is-grid":"grid"===this.layout,"is-panel":"panel"===this.layout,"is-vertical":C}),w?(0,o.qy)(v||(v=x`<ha-sankey-chart .hass="${0}" .data="${0}" .vertical="${0}" .valueFormatter="${0}" -click="${0}"></ha-sankey-chart>`),this.hass,{nodes:s,links:i},C,this._valueFormatter,this._handleNodeClick):(0,o.qy)(b||(b=x`${0}`),this.hass.localize("ui.panel.lovelace.cards.energy.no_data")))}_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 y=Math.max(0,Math.min(h,c-_));g+=y,h-=y,c-=y;const p=Math.min(l,h);h-=p,l-=p;const f=Math.min(l,d);d-=f,l-=f;const v=Math.min(u,d);u-=v;const b=Math.min(c,h);g+=b,c-=b;const m=Math.min(_,l);_-=m;const x=Math.min(u,_);_-=x;const k=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:p,solar_to_grid:f,used_solar:m,used_grid:k,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){var t;return this._entities.add(e),null!==(t=(0,c.h4)(this.hass.states[e]))&&void 0!==t?t: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)}}M.styles=(0,o.AH)(m||(m=x`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})],M.prototype,"hass",void 0),(0,s.Cg)([(0,i.MZ)({attribute:!1})],M.prototype,"layout",void 0),(0,s.Cg)([(0,i.wk)()],M.prototype,"_config",void 0),(0,s.Cg)([(0,i.wk)()],M.prototype,"_data",void 0),M=(0,s.Cg)([(0,i.EM)("hui-power-sankey-card")],M),r()}catch(p){r(p)}})}}]);