iobroker.lovelace
Version:
With this adapter you can build visualization for ioBroker with Home Assistant Lovelace UI
1 lines • 13.1 kB
JavaScript
"use strict";(self.rspackChunkhome_assistant_frontend=self.rspackChunkhome_assistant_frontend||[]).push([[51486],{36525:function(t,i,s){s.a(t,async function(t,e){try{s.d(i,{n:function(){return I}});s(74423),s(44114),s(26910),s(18111),s(81148),s(7588),s(61701),s(3362),s(17642),s(58004),s(33853),s(45876),s(32475),s(15024),s(31698),s(62953);var a=s(40445),o=s(5124),h=s(67576),n=s(89784),r=s(22786),l=s(90433),d=s(36312),c=s(1087),p=s(72487),m=s(95747),u=s(30162),g=s(20058),y=s(8549),f=s(15638),v=s(23528),_=s(29961),b=s(44439),x=s(1253),D=(s(33062),s(47245)),S=s(89938),C=t([b,_,p,u,m,v,g]);[b,_,p,u,m,v,g]=C.then?(await C)():C;let T,M,$,k,A,w,L,Y,Z,O=t=>t;const I={mean:"mean",min:"min",max:"max",sum:"sum",state:"sum",change:"sum"},F=Object.keys(I).map(t=>`-${t}`);class E extends o.WF{shouldUpdate(t){return t.size>1||!t.has("hass")}willUpdate(t){(t.has("statisticsData")||t.has("statTypes")||t.has("chartType")||t.has("hideLegend")||t.has("_hiddenStats")||t.has("names"))&&this._generateData(),(!this.hasUpdated||t.has("unit")||t.has("period")||t.has("chartType")||t.has("minYAxis")||t.has("maxYAxis")||t.has("fitYData")||t.has("logarithmicScale")||t.has("hideLegend")||t.has("startTime")||t.has("endTime")||t.has("_legendData")||t.has("_chartData"))&&this._createOptions()}firstUpdated(){this._computedStyle=getComputedStyle(this)}render(){return(0,d.x)(this.hass.config,"history")?this.isLoadingData&&!this.statisticsData?(0,o.qy)(M||(M=O`<div class="info"> ${0} </div>`),this.hass.localize("ui.components.statistics_charts.loading_statistics")):this.statisticsData&&Object.keys(this.statisticsData).length?(0,o.qy)(k||(k=O` <ha-chart-base .hass="${0}" .data="${0}" .options="${0}" .height="${0}" style="${0}" -hidden="${0}" -unhidden="${0}" .expandLegend="${0}" .clickLabelForMoreInfo="${0}" -label-click="${0}"></ha-chart-base> `),this.hass,this._chartData,this._chartOptions,this.height,(0,n.W)({height:this.height}),this._datasetHidden,this._datasetUnhidden,this.expandLegend,this.clickForMoreInfo&&!this._statisticIds.every(v.OQ),this._handleLegendLabelClick):(0,o.qy)($||($=O`<div class="info"> ${0} </div>`),this.hass.localize("ui.components.statistics_charts.no_statistics_found")):(0,o.qy)(T||(T=O`<div class="info"> ${0} </div>`),this.hass.localize("ui.components.history_charts.history_disabled"))}_datasetHidden(t){this._hiddenStats.add(t.detail.id),this.requestUpdate("_hiddenStats")}_datasetUnhidden(t){this._hiddenStats.delete(t.detail.id),this.requestUpdate("_hiddenStats")}_handleLegendLabelClick(t){const i=t.detail.id;if((0,v.OQ)(i))return;let s=i;if(!this.hass.states[s])for(const e of F)if(i.endsWith(e)){s=i.slice(0,-e.length);break}this.hass.states[s]&&(0,c.r)(this,"hass-more-info",{entityId:s})}_createOptions(){var t,i;const s=null!==(t=this.daysToShow)&&void 0!==t?t:1;let e=this.minYAxis,a=this.maxYAxis;"number"==typeof e?this.fitYData&&(e=({min:t})=>Math.min(this._roundYAxis(t,Math.floor),this.minYAxis)):this.logarithmicScale&&(e=({min:t})=>{const i=t>0?.95*t:1.05*t;return this._roundYAxis(i,Math.floor)}),"number"==typeof a?this.fitYData&&(a=({max:t})=>Math.max(this._roundYAxis(t,Math.ceil),this.maxYAxis)):this.logarithmicScale&&(a=({max:t})=>{const i=t>0?1.05*t:.95*t;return this._roundYAxis(i,Math.ceil)});const o=null!==(i=this.endTime)&&void 0!==i?i:new Date;let h=this.startTime;h||(this._chartData.forEach(t=>{var i;if(!Array.isArray(t.data)||!t.data[0])return;const s=t.data[0],e=Array.isArray(s)?s[0]:null===(i=s.value)||void 0===i?void 0:i[0];e&&(!h||new Date(e)<h)&&(h=new Date(e))}),h||(h=new Date(o.getTime()-24*s*3600*1e3))),this._chartOptions={xAxis:[Object.assign(Object.assign({id:"xAxis",type:"time",min:h,max:this.endTime},"month"===this.period&&{minInterval:24192e5,axisLabel:(0,_.d)("month",this.hass.locale,this.hass.config)}),"year"===this.period&&{minInterval:31536e6,axisLabel:(0,_.d)("year",this.hass.locale,this.hass.config)}),{id:"hiddenAxis",type:"time",show:!1}],yAxis:{type:this.logarithmicScale?"log":"value",name:this.unit,nameGap:2,nameTextStyle:{align:"left"},position:(0,f.qC)(this.hass.language,this.hass.translationMetadata.translations)?"right":"left",scale:this.chartType.startsWith("line")||this.logarithmicScale||void 0!==e||void 0!==a,min:this._clampYAxis(e),max:this._clampYAxis(a),splitLine:{show:!0},axisLabel:{formatter:this._formatYAxisLabel}},legend:{type:"custom",show:!this.hideLegend,data:this._legendData},grid:{top:15,left:1,right:1,bottom:0,containLabel:!0},tooltip:{trigger:"axis",renderMode:"html",position:x.mJ,confine:!0,formatter:this._renderTooltip}}}async _generateData(){var t;if(!this.statisticsData)return;const i=this.metadata||await this._getStatisticsMetaData(Object.keys(this.statisticsData));let s=0;const e=this.chartType.startsWith("line")?"line":"bar",a=this.chartType.endsWith("stack"),o=Object.entries(this.statisticsData),h=[];let n=1/0,r=-1/0;const d=t=>{"number"==typeof t&&Number.isFinite(t)&&(t<n&&(n=t),t>r&&(r=t))},c=[],p=[];let m;if(0===o.length)return;m=this.endTime||new Date(Math.max(...o.map(([t,i])=>new Date(i[i.length-1].start).getTime()))),m>new Date&&(m=new Date);const u=new Date,g=u.getTime()-m.getTime()<=6e5;if(!this.unit){let t;o.forEach(([s,e])=>{const a=null==i?void 0:i[s],o=(0,v.JE)(this.hass,s,a);void 0===t?t=o:null!==t&&t!==o&&(t=null)}),t&&(this.unit=t)}const y=this.names||{},f=this.colors||{};o.forEach(([t,o])=>{const n=null==i?void 0:i[t];let r=y[t];void 0===r&&(r=(0,v.$O)(this.hass,t,n));let _,b=null;const x=[],D=[],S="bar"===e&&("5minute"===this.period||"hour"===this.period);let C=f[t];void 0===C&&(C=(0,l.fI)(s,this._computedStyle||getComputedStyle(this)),s++);const T=[],M=this.statTypes.includes("mean")&&(0,v.iY)(o,"mean"),$=this.statTypes.includes("max")&&(0,v.iY)(o,"max"),k=this.statTypes.includes("min")&&(0,v.iY)(o,"min"),A=!a&&[M,$,k].filter(Boolean).length>1,w=this.statTypes.includes("state"),L=$?"max":"mean",Y=k?"min":"mean",Z=A?[...this.statTypes].sort((t,i)=>"min"===t||"max"===i?-1:"max"===t||"min"===i?1:0):this.statTypes;let O=!1;Z.forEach(i=>{if((0,v.iY)(o,i)){const o=A&&(i===L||i===Y);T.push(i);const h=o&&k&&$&&M||w&&["change","sum"].includes(i)?C+(this.hideLegend?"00":"7F"):C,n=o?C+"3F":C+"7F",l={id:`${t}-${i}`,type:e,smooth:"line"===e&&.4,cursor:"default",data:[],name:r?`${r} (${this.hass.localize(`ui.components.statistics_charts.statistic_types.${i}`)})`:this.hass.localize(`ui.components.statistics_charts.statistic_types.${i}`),symbol:"none",sampling:o&&A?"lttb":"minmax",animationDurationUpdate:0,lineStyle:{width:1.5},itemStyle:"bar"===e?{borderColor:h,borderWidth:1.5}:void 0,color:"bar"===e?n:h};if(a?(l.stack="band-stacked",l.stackStrategy="samesign","line"===e&&(l.areaStyle={color:C+"3F"})):o&&"line"===e&&(l.stack=`band-${t}`,l.stackStrategy="all",this._hiddenStats.has(`${t}-${Y}`)?(l.stackOrder="seriesDesc",l.areaStyle=void 0):(l.stackOrder="seriesAsc",i===L&&(l.areaStyle={color:C+"3F"}))),!this.hideLegend){const e=M?"mean"===i:!1===O;var s;if(e)D.push({id:t,name:r,color:l.color,borderColor:null===(s=l.itemStyle)||void 0===s?void 0:s.borderColor,noLabelClick:(0,v.OQ)(t)});O=O||e}x.push(l),p.push(t)}});let I=null,F=null;o.forEach(i=>{const s=new Date(i.start),a=new Date(i.end);if(I===s)return;I=s;const o=[];T.forEach(s=>{const a=[];if("sum"===s)null==F?(a.push(0),F=i.sum):a.push((i.sum||0)-F);else if(s===L&&"line"===e&&A&&!this._hiddenStats.has(`${t}-${Y}`)){const t=i[L]||0;a.push(Math.abs(t-(i[Y]||0))),a.push(t)}else{var h;a.push(null!==(h=i[s])&&void 0!==h?h:null)}o.push(a)}),this._hiddenStats.has(t)||((t,i,s,a)=>{a.length&&(s=i.getTime()<s.getTime()?i:s,t.getTime()>s.getTime()||(x.forEach((s,o)=>{if("line"===e)_&&b&&_.getTime()!==t.getTime()&&(s.data.push([_,...b[o]]),s.data.push([_,null])),s.data.push([t,...a[o]]),d(a[o][a[o].length-1]);else{let e=t;S&&(e=new Date((t.getTime()+i.getTime())/2)),s.data.push([e,a[o][0],t,i]),d(a[o][0])}}),b=a,_=s))})(s,a,m,o)});const E=_,q=b;"line"===e&&E&&q&&x.forEach((t,i)=>{t.data.push([E,...q[i]])});const N=(0,v.JE)(this.hass,t,n);if(g&&!a&&(!this.unit||!N||this.unit===N)&&!(0,v.OQ)(t)){const i=this.hass.states[t];if(i){const s=parseFloat(i.state);isFinite(s)&&!this._hiddenStats.has(t)&&T.forEach((i,a)=>{if("sum"===i||"change"===i)return;const o=[];i===L&&"line"===e&&A&&!this._hiddenStats.has(`${t}-${Y}`)?(o.push(0),o.push(s)):o.push(s),x[a].data.push([u,...o]),d(o[o.length-1])})}}Array.prototype.push.apply(h,x),Array.prototype.push.apply(c,D)}),"bar"===e&&(0,D.R)(h,a),c.forEach(({id:t,name:i,color:s,borderColor:a})=>{h.push({id:t,name:i,color:s,itemStyle:{borderColor:a},type:e,data:[],xAxisIndex:1})}),this._yAxisFractionDigits=(0,S.G)(n,r),this._chartData=h,c.length!==(null===(t=this._legendData)||void 0===t?void 0:t.length)&&(this._legendData=c.length>1?c.map(({id:t,name:i,noLabelClick:s})=>({id:t,name:i,noLabelClick:s})):void 0),this._statisticIds=p}_clampYAxis(t){if(this.logarithmicScale){if("number"==typeof t)return Math.max(t,Number.EPSILON);if("function"==typeof t)return i=>Math.max(t(i),Number.EPSILON)}return t}_roundYAxis(t,i){return Math.abs(t)<1?t:i(t)}constructor(...t){super(...t),this.statTypes=["sum","min","mean","max"],this.chartType="line",this.fitYData=!1,this.hideLegend=!1,this.logarithmicScale=!1,this.isLoadingData=!1,this.clickForMoreInfo=!0,this._chartData=[],this._statisticIds=[],this._hiddenStats=new Set,this._yAxisFractionDigits=1,this._renderTooltip=t=>{const i={},s=this.chartType.startsWith("bar"),e=this.period,a=this.unit?`${(0,y.A)(this.unit,this.hass.locale)}${this.unit}`:"",h=[];for(const o of t){var n,r;if(i[o.seriesIndex])continue;i[o.seriesIndex]=!0;const t=this._statisticIds[o.seriesIndex],d=this.hass.states[t],c=this.hass.entities[t];let y,f;if(s){let t,i;y=String(o.value[1]),o.value[2]?(t=new Date(o.value[2]),o.value[3]&&(i=new Date(o.value[3]))):t=new Date(o.value[0]),f="year"===e||"month"===e||"week"===e||"day"===e?(0,p.Yq)(t,this.hass.locale,this.hass.config)+(i&&"day"!==e?` – ${(0,p.Yq)(i,this.hass.locale,this.hass.config)}`:""):(0,m.yg)(t,this.hass.locale,this.hass.config)+(i?` – ${(0,u.ie)(i,this.hass.locale,this.hass.config)}`:"")}else{var l;y=String(null!==(l=o.value[2])&&void 0!==l?l:o.value[1]),f=(0,m.yg)(new Date(o.value[0]),this.hass.locale,this.hass.config)}const v=null!==(n=(0,g.ZQ)(d,c))&&void 0!==n?n:{maximumFractionDigits:2},_=`${(0,g.ZV)(y,this.hass.locale,v)}${a}`;h.push({time:0===h.length?f:void 0,color:String(null!==(r=o.color)&&void 0!==r?r:""),seriesName:o.seriesName,value:_})}return 0===h.length?o.s6:(0,o.qy)(A||(A=O`${0}`),h.map((t,i)=>(0,o.qy)(w||(w=O`${0}<ha-chart-tooltip-marker .color="${0}"></ha-chart-tooltip-marker> ${0}: ${0}${0}`),t.time?(0,o.qy)(L||(L=O`${0}<br>`),t.time):o.s6,t.color,t.seriesName,t.value,i<h.length-1?(0,o.qy)(Y||(Y=O`<br>`)):o.s6)))},this._getStatisticsMetaData=(0,r.A)(async t=>{const i=await(0,v.Wr)(this.hass,t),s={};return i.forEach(t=>{s[t.statistic_id]=t}),s}),this._formatYAxisLabel=t=>(0,g.ZV)(t,this.hass.locale,{maximumFractionDigits:this._yAxisFractionDigits})}}E.styles=(0,o.AH)(Z||(Z=O`:host{display:block;min-height:60px}.info{text-align:center;line-height:60px;color:var(--secondary-text-color)}`)),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"hass",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"statisticsData",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"metadata",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"names",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"colors",void 0),(0,a.Cg)([(0,h.MZ)()],E.prototype,"unit",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"startTime",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"endTime",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"statTypes",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"chartType",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"minYAxis",void 0),(0,a.Cg)([(0,h.MZ)({attribute:!1})],E.prototype,"maxYAxis",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"fit-y-data",type:Boolean})],E.prototype,"fitYData",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"hide-legend",type:Boolean})],E.prototype,"hideLegend",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"logarithmic-scale",type:Boolean})],E.prototype,"logarithmicScale",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"is-loading-data",type:Boolean})],E.prototype,"isLoadingData",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"click-for-more-info",type:Boolean})],E.prototype,"clickForMoreInfo",void 0),(0,a.Cg)([(0,h.MZ)()],E.prototype,"period",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"days-to-show",type:Number})],E.prototype,"daysToShow",void 0),(0,a.Cg)([(0,h.MZ)({type:String})],E.prototype,"height",void 0),(0,a.Cg)([(0,h.MZ)({attribute:"expand-legend",type:Boolean})],E.prototype,"expandLegend",void 0),(0,a.Cg)([(0,h.wk)()],E.prototype,"_chartData",void 0),(0,a.Cg)([(0,h.wk)()],E.prototype,"_legendData",void 0),(0,a.Cg)([(0,h.wk)()],E.prototype,"_statisticIds",void 0),(0,a.Cg)([(0,h.wk)()],E.prototype,"_chartOptions",void 0),(0,a.Cg)([(0,h.wk)()],E.prototype,"_hiddenStats",void 0),E=(0,a.Cg)([(0,h.EM)("statistics-chart")],E),e()}catch(T){e(T)}})}}]);