${t.name}`}),w=l.map(s=>{const n=function(t,e,i,s,n){const o=e[t.id];return o&&o.s===t.space?{x:o.x*Pa,y:o.y*Pa}:s[t.id]?s[t.id]:Ba(il(n))}(s,t.layout,t.cfg,c,e),o=(n.x-g[0])/g[2]*100,a=(n.y-g[1])/g[3]*100,l=i.showLqi??!1!==t.showSignal,h=t.presentations?.get(Sc(s.id,l))||ao(r,s,{liveStates:!1!==t.liveStates,showTemperature:!1!==t.showTemperature,showSignal:l,activityRuntime:t.activityRuntime?.get(s.id),sourceDetails:!1}),d=[`left:${o}%`,`top:${a}%`,...Lt(h)];return B`
- ${It(h,{})}
-
`}),k=i.showNames?e.rooms.filter(t=>t.name).map(s=>{const n=function(t,e,i){const s=i["rl_"+(t.id||"")];if(s&&s.s===e)return{x:s.x*wa,y:s.y*wa};const n=Xa(t);return Ia({x:n[0],y:n[1]})}(s,e.id,t.layout,t.cfg),o=(n.x-g[0])/g[2]*100,r=(n.y-g[1])/g[3]*100,a=Math.min(1,i.opacity+.25);return W`
`}),k=i.showNames?e.rooms.filter(t=>t.name).map(s=>{const n=function(t,e,i){const s=i["rl_"+(t.id||"")];if(s&&s.s===e)return{x:s.x*Pa,y:s.y*Pa};const n=al(t);return Ba({x:n[0],y:n[1]})}(s,e.id,t.layout,t.cfg),o=(n.x-g[0])/g[2]*100,r=(n.y-g[1])/g[3]*100,a=Math.min(1,i.opacity+.25);return B`
${s.name}
`}):[],$=e.bg?t.displayUrl?t.displayUrl(e.bg.href):e.bg.href:"",x=t.cfg.spaces.find(e=>e.id===t.spaceId)?.settings||{};let S="";if("daynight"===Vo(t.cfg?.settings,x)&&null!==Go(t.cfg?.settings,x)){const e=Yo(t.hass);e&&(S=Oo(e.elevation).bg)}const M=S||Zi(t.cfg?.settings,i),C=p.length?Jr(e.rooms,p,[],Aa,u,Pa,wa):Ze(e.rooms),D=(p.length||_.length)&&i.showBorders?Kr(e.rooms,p,[],[],Aa,u,Pa,wa,_):null,T=t.stageWidth&&g[2]?t.stageWidth/g[2]:1,R=!!D&&cr(D.depthUnits,T),P=i.color||"#607d8b";return W`
+ style="left:${o}%;top:${r}%;color:${i.color};opacity:${a}">${s.name}
`}):[],$=e.bg?t.displayUrl?t.displayUrl(e.bg.href):e.bg.href:"",x=t.cfg.spaces.find(e=>e.id===t.spaceId)?.settings||{};let S="";if("daynight"===ir(t.cfg?.settings,x)&&null!==er(t.cfg?.settings,x)){const e=nr(t.hass);e&&(S=Vo(e.elevation).bg)}const M=S||ss(t.cfg?.settings,i),C=p.length?oa(e.rooms,p,[],qa,u,Ha,Pa):si(e.rooms),D=(p.length||_.length)&&i.showBorders?sa(e.rooms,p,[],[],qa,u,Ha,Pa,_):null,T=t.stageWidth&&g[2]?t.stageWidth/g[2]:1,P=!!D&&vr(D.depthUnits,T),R=i.color||"#607d8b";return B`
${""}
-
${w}${k}
+
${w}${k}
- `}let yc=null,bc=null,wc=0,kc=-1,$c=!1;const xc=new Set;function Sc(){if(yc)return yc;try{const t=JSON.parse(localStorage.getItem("houseplan_card_cfg_v1")||"null");if(t&&t.config&&Array.isArray(t.config.spaces)){const e=t.layout||{};return{config:t.config,rev:t.rev||0,configFingerprint:t.config_fingerprint||Fl(t.config),layout:e,layoutRev:t.layout_rev||0,layoutFingerprint:t.layout_fingerprint||Fl(e)}}}catch{}return null}function Mc(t,e=!1){if(e&&(yc=null,wc++,bc))return bc.catch(()=>null).then(()=>Mc(t,!1));if(yc)return Promise.resolve(yc);if(bc)return kc!==wc?bc.catch(()=>null).then(()=>Mc(t,!1)):bc;return kc=wc,bc=async function(t,e){const[i,s]=await Promise.all([t.callWS({type:"houseplan/config/get"}),t.callWS({type:"houseplan/layout/get"})]),n={config:i?.config??null,rev:i?.rev??0,configFingerprint:Fl(i?.config??null),layout:s?.layout??{},layoutRev:s?.rev??0,layoutFingerprint:Fl(s?.layout??{})};if(e===wc&&(yc=n),!$c&&t.connection?.subscribeEvents){$c=!0;const e=()=>{yc=null,wc++,xc.forEach(t=>t())};try{await t.connection.subscribeEvents(e,"houseplan_config_updated"),await t.connection.subscribeEvents(e,"houseplan_layout_updated")}catch{$c=!1}}return n}(t,wc).finally(()=>{bc=null,kc=-1}),bc}class Cc extends ct{constructor(){super(...arguments),this._spaces=null,this._spacesLoading=!1}setConfig(t){this._config=t}async _loadSpaces(){if(!this._spaces&&!this._spacesLoading&&this.hass){this._spacesLoading=!0;try{const t=await this.hass.callWS({type:"houseplan/config/get"});this._spaces=(t?.config?.spaces||[]).map(t=>({value:t.id,label:t.title||t.id}))}catch{this._spaces=[]}finally{this._spacesLoading=!1}}}get _lang(){return ho(this.hass,this._config?.language)}get _schema(){const t=this._spaces||[];return[t.length?{name:"space",selector:{select:{mode:"dropdown",options:t}}}:{name:"space",selector:{text:{}}},{name:"title",selector:{text:{}}},{name:"show_button",selector:{boolean:{}}},{name:"button_label",selector:{text:{}}},{name:"button_target",selector:{text:{}}},{name:"icon_size",selector:{number:{min:1,max:6,step:.1,mode:"box"}}},{name:"show_temperature",selector:{boolean:{}}},{name:"live_states",selector:{boolean:{}}},{name:"show_signal",selector:{boolean:{}}}]}render(){if(!this.hass||!this._config)return G;this._loadSpaces();const t=this._lang,e={space:po(t,"editor.space"),title:po(t,"editor.title"),show_button:po(t,"editor.show_button"),button_label:po(t,"editor.button_label"),button_target:po(t,"editor.button_target"),icon_size:po(t,"editor.icon_size"),show_temperature:po(t,"editor.show_temperature"),live_states:po(t,"editor.live_states"),show_signal:po(t,"editor.show_signal")};return W`
null).then(()=>zc(t,!1));if(Dc)return Promise.resolve(Dc);if(Tc)return Rc!==Pc?Tc.catch(()=>null).then(()=>zc(t,!1)):Tc;return Rc=Pc,Tc=async function(t,e){const[i,s]=await Promise.all([t.callWS({type:"houseplan/config/get"}),t.callWS({type:"houseplan/layout/get"})]),n={config:i?.config??null,rev:i?.rev??0,configFingerprint:Wl(i?.config??null),layout:s?.layout??{},layoutRev:s?.rev??0,layoutFingerprint:Wl(s?.layout??{})};if(e===Pc&&(Dc=n),!Ac&&t.connection?.subscribeEvents){Ac=!0;const e=()=>{Dc=null,Pc++,Ec.forEach(t=>t())};try{await t.connection.subscribeEvents(e,"houseplan_config_updated"),await t.connection.subscribeEvents(e,"houseplan_layout_updated")}catch{Ac=!1}}return n}(t,Pc).finally(()=>{Tc=null,Rc=-1}),Tc}class Fc extends ht{constructor(){super(...arguments),this._spaces=null,this._spacesLoading=!1}setConfig(t){this._config=t}async _loadSpaces(){if(!this._spaces&&!this._spacesLoading&&this.hass){this._spacesLoading=!0;try{const t=await this.hass.callWS({type:"houseplan/config/get"});this._spaces=(t?.config?.spaces||[]).map(t=>({value:t.id,label:t.title||t.id}))}catch{this._spaces=[]}finally{this._spacesLoading=!1}}}get _lang(){return bo(this.hass,this._config?.language)}get _schema(){const t=this._spaces||[];return[t.length?{name:"space",selector:{select:{mode:"dropdown",options:t}}}:{name:"space",selector:{text:{}}},{name:"title",selector:{text:{}}},{name:"show_button",selector:{boolean:{}}},{name:"button_label",selector:{text:{}}},{name:"button_target",selector:{text:{}}},{name:"icon_size",selector:{number:{min:1,max:6,step:.1,mode:"box"}}},{name:"show_temperature",selector:{boolean:{}}},{name:"live_states",selector:{boolean:{}}},{name:"show_signal",selector:{boolean:{}}}]}render(){if(!this.hass||!this._config)return G;this._loadSpaces();const t=this._lang,e={space:wo(t,"editor.space"),title:wo(t,"editor.title"),show_button:wo(t,"editor.show_button"),button_label:wo(t,"editor.button_label"),button_target:wo(t,"editor.button_target"),icon_size:wo(t,"editor.icon_size"),show_temperature:wo(t,"editor.show_temperature"),live_states:wo(t,"editor.live_states"),show_signal:wo(t,"editor.show_signal")};return B`e[t.name]||t.name}
@value-changed=${this._valueChanged}
- >`}_valueChanged(t){const e={...this._config||{},...t.detail.value};delete e.aspect_ratio;const i=new Event("config-changed",{bubbles:!0,composed:!0});i.detail={config:e},this.dispatchEvent(i)}}Cc.properties={hass:{attribute:!1},_config:{state:!0},_spaces:{state:!0}},customElements.get("houseplan-space-card-editor")||customElements.define("houseplan-space-card-editor",Cc);const Dc=t=>{history.pushState(null,"",t),((t,e,i)=>{const s=new Event(e,{bubbles:!0,composed:!0});s.detail=i??{},t.dispatchEvent(s)})(window,"location-changed",{replace:!1})};class Tc extends ct{constructor(){super(...arguments),this._snap=null,this._loading=!1,this._reloadQueued=!1,this._forceReloadQueued=!1,this._reloadRetryTimer=0,this._stageWidth=0,this._pendingStageWidth=0,this._stageWidthRaf=0,this._haRegistryConnection=null,this._haRegistryRevision=-1,this._devices=[],this._continuity=this._newContinuityController(),this._continuityHistory=[],this._continuityEpoch=0,this._continuityDataReady=!0,this._continuityPaintToken=-1,this._continuityDisposed=!1,this._renderSnapshotAt=Date.now(),this._hassSequence=0,this._connHooked=null,this._connectionWasLost=!1,this._visibleDeviceSnapshot=null,this._candidateDeviceSnapshot=null,this._stagedDeviceSnapshotToken=-1,this._capturedSnapshotSequence=-1,this._capturedSnapshotDevices=null,this._capturedSnapshotActivity="",this._activityRuntime=new Map,this._onHaRegistryUpdate=()=>{const t=sn(this.hass).revision;t!==this._haRegistryRevision&&(this._haRegistryRevision=t,this._refreshDevices(),this._capturedSnapshotSequence=-1,this.requestUpdate())},this._pageVisibility=t=>{this._continuity.visibility(t),"hidden"!==t.kind&&t.long&&(Date.now()-this._renderSnapshotAt>1e3&&this._continuity.note("device-snapshot-stale"),this._continuityDataReady=!1,this._continuityPaintToken=-1,this._load(!0))},this._onConnLost=()=>{this._connectionWasLost=!0,this._continuityDataReady=!1,this._continuityPaintToken=-1,this._continuity.connectionLost()},this._onConnReady=()=>{!this._connectionWasLost&&this._continuity.hasCompleteFrame?this._beginContinuityCandidate("connection-ready",!1,"plan"):(this._continuityDataReady=!1,this._continuityPaintToken=-1),this._load(!0)},this._onAssetLoaded=(t,e)=>{this._signer.markLoaded(this.hass,t,e),this._continuity.note("asset-ready"),this._continuityPaintToken=-1,"steady"!==this._continuity.state&&this.requestUpdate()},this._loadedOnce=!1,this._signer=new Ll(()=>this.requestUpdate()),this._retryContinuity=()=>{this._continuityDataReady=!1,this._continuityPaintToken=-1,this._continuity.retry(this._continuity.recoveryReason||"plan"),this._load(!0)},this._goToSpace=()=>{const t=(this._config?.button_target||"/plan-doma").replace(/#.*$/,"");Dc(`${t}#space=${encodeURIComponent(this._config.space)}`)}}_ensureHaRegistryAuthority(){const t=this.hass?.connection||null;t&&t!==this._haRegistryConnection&&(this._haRegistryRelease?.(),this._haRegistryConnection=t,this._haRegistryRevision=-1,this._haRegistryRelease=en(this.hass,this._onHaRegistryUpdate),this._onHaRegistryUpdate())}_newContinuityController(){return new El(()=>{this._continuityEpoch++,this.isConnected&&this.requestUpdate()})}_hookConnection(){const t=this.hass?.connection;t&&t!==this._connHooked&&(this._connHooked?.removeEventListener?.("ready",this._onConnReady),this._connHooked?.removeEventListener?.("disconnected",this._onConnLost),this._connHooked?.removeEventListener?.("reconnect-error",this._onConnLost),t.addEventListener?.("ready",this._onConnReady),t.addEventListener?.("disconnected",this._onConnLost),t.addEventListener?.("reconnect-error",this._onConnLost),this._connHooked=t)}static getConfigElement(){return document.createElement("houseplan-space-card-editor")}static getStubConfig(t){const e=Sc();return{type:"custom:houseplan-space-card",space:Ca(e?.config||null)[0]?.id||"",show_button:!0,live_states:!0,show_temperature:!0,show_signal:!0}}setConfig(t){if(!t||!t.space)throw new Error('houseplan-space-card: "space" is required');this._config={show_button:!0,button_target:"/plan-doma",live_states:!0,show_temperature:!0,show_signal:!0,...t},this._snap=this._snap||Sc()}connectedCallback(){var t;this._continuityDisposed&&(this._continuity=this._newContinuityController(),this._continuityDisposed=!1,this._continuityPaintToken=-1),super.connectedCallback(),this.hass&&this._ensureHaRegistryAuthority(),this._continuityUnsub?.(),this._continuityUnsub=zl(this.ownerDocument,this._pageVisibility),this._unsub=(t=()=>{this._beginContinuityCandidate("config-event",!1),this._reloadQueued=!0,this._load()},xc.add(t),()=>xc.delete(t)),this._signer.start(()=>this.hass,()=>this._referenced())}disconnectedCallback(){this._continuityUnsub?.(),this._continuityUnsub=void 0,this._unsub?.(),this._unsub=void 0,window.clearTimeout(this._reloadRetryTimer),this._reloadRetryTimer=0,this._stageObserver?.disconnect(),this._stageObserver=void 0,this._observedStage=void 0,this._stageWidthRaf&&cancelAnimationFrame(this._stageWidthRaf),this._stageWidthRaf=0,this._pendingStageWidth=0,this._signer.dispose(),this._haRegistryRelease?.(),this._haRegistryRelease=void 0,this._haRegistryConnection=null,this._connHooked?.removeEventListener?.("ready",this._onConnReady),this._connHooked?.removeEventListener?.("disconnected",this._onConnLost),this._connHooked?.removeEventListener?.("reconnect-error",this._onConnLost),this._connHooked=null;for(const t of this._activityRuntime.values())window.clearTimeout(t.timer);this._activityRuntime.clear(),this._continuityHistory=[...this._continuityHistory,...this._continuity.trace].slice(-80),this._continuity.dispose(),this._continuityDisposed=!0,super.disconnectedCallback()}willUpdate(t){t.has("hass")&&this.hass&&(this._hassSequence++,this._renderSnapshotAt=Date.now(),this._continuity.note("hass-snapshot"),this._ensureHaRegistryAuthority(),this._hookConnection()),!this.hass||this._loading||this._snap&&!t.has("hass")||this._snap&&this._loadedOnce||this._load(),(t.has("hass")||t.has("_snap")||t.has("_config")||!this._devices.length)&&this._refreshDevices(),this._captureRenderDeviceSnapshot(),this._continuity.hasCompleteFrame&&"steady"===this._continuity.state&&this._continuity.refreshCompleteFrame(this._frameFingerprint())}_stampActivity(t,e){t.flashTs&&Date.now()-t.flashTs<3300&&"event"===t.flashKind&&"transition"===e||(t.flashTs=Date.now(),t.flashKind=e,t.gen++,window.clearTimeout(t.timer),t.timer=window.setTimeout(()=>this.requestUpdate(),3360))}_syncActivity(t,e){if(!1===this._config?.live_states){for(const t of this._activityRuntime.values())window.clearTimeout(t.timer);return void this._activityRuntime.clear()}const i=new Set;for(const s of t){if(s.hidden)continue;if("static_icon"===Si(s.marker?.display))continue;i.add(s.id);const t=Vn(e,s),n=t.samples,o=Jn(e,s,!1!==this._config?.show_temperature,t);let r=this._activityRuntime.get(s.id);if(!r||r.sources!==o){r&&window.clearTimeout(r.timer),r={sources:o,last:Object.fromEntries(n.map(t=>[t.eid,t.state])),flashTs:0,flashKind:null,timer:0,gen:0},this._activityRuntime.set(s.id,r);continue}"transition"===r.flashKind&&n.some(t=>"transition"===t.activity)&&(window.clearTimeout(r.timer),r.flashTs=0,r.flashKind=null);let a=null;for(const t of n){const e=js(r.last[t.eid],t);("event"===e||!a&&e)&&(a=e),r.last[t.eid]=t.state}a&&this._stampActivity(r,a)}for(const[t,e]of this._activityRuntime)i.has(t)||(window.clearTimeout(e.timer),this._activityRuntime.delete(t))}_refreshDevices(){if(!this.hass||!this._snap?.config||!this._config)return;const t=sn(this.hass),e=fc({hass:this.hass,registry:t,cfg:this._snap.config,lang:this._lang});this._syncActivity(e,an(this.hass,t)),this._devices=e}_captureRenderDeviceSnapshot(){if(!this.hass)return;const t=[...this._activityRuntime.entries()].map(([t,e])=>`${t}:${e.gen}:${e.flashTs}`).join("|");if(this._capturedSnapshotSequence===this._hassSequence&&this._capturedSnapshotDevices===this._devices&&this._capturedSnapshotActivity===t)return;const e=an(this.hass,sn(this.hass)),i=new Map,s=new Set(["sun.sun"]),n=new Set,o=new Set,r=t=>{if(!t)return;const e=t.indexOf(":");if(e<0)return void s.add(t);const i=t.slice(0,e),o=t.slice(e+1);"device"===i?n.add(o):"entity"===i&&s.add(o)},a=Ca(this._snap?.config||null).find(t=>t.id===this._config?.space);for(const t of a?.rooms||[])t.area&&o.add(t.area),r(t.settings?.temp_source),r(t.settings?.hum_source);const l=this._snap?.config?.spaces?.find(t=>t.id===this._config?.space);for(const t of l?.openings||[])t.contact&&s.add(t.contact),t.lock&&s.add(t.lock);for(const t of this._devices)for(const s of[!1,!0])i.set(gc(t.id,s),Xn(e,t,{liveStates:!1!==this._config?.live_states,showTemperature:!1!==this._config?.show_temperature,showSignal:s,activityRuntime:this._activityRuntime.get(t.id),sourceDetails:!1}));const c=mc({sourceSequence:this._hassSequence,hass:e,devices:this._devices,presentations:i,entityIds:s,deviceIds:n,areaIds:o});this._capturedSnapshotSequence=this._hassSequence,this._capturedSnapshotDevices=this._devices,this._capturedSnapshotActivity=t,this._visibleDeviceSnapshot&&"steady"!==this._continuity.state?this._candidateDeviceSnapshot=c:(this._visibleDeviceSnapshot=c,this._candidateDeviceSnapshot=null)}get _renderDeviceSnapshot(){return this._stagedDeviceSnapshotToken===this._continuity.token?this._candidateDeviceSnapshot||this._visibleDeviceSnapshot:this._visibleDeviceSnapshot||this._candidateDeviceSnapshot}_beginContinuityCandidate(t,e,i="plan"){return this._continuityDataReady=e,this._continuityPaintToken=-1,this._stagedDeviceSnapshotToken=-1,this._continuity.beginCandidate(t,i)}_backdropRaw(){return this._snap?.config&&this._config&&Ca(this._snap.config).find(t=>t.id===this._config.space)?.bg?.href||""}_candidateBackdrop(t){return t&&this._config&&Ca(t).find(t=>t.id===this._config.space)?.bg?.href||""}_assetsReady(){const t=this._backdropRaw();return!t||this._signer.isReady(this.hass,t)}_frameFingerprint(){const t=this._snap;return Nl([t?.rev||0,t?.configFingerprint||Fl(t?.config),t?.layoutRev||0,t?.layoutFingerprint||Fl(t?.layout),this._config?.space||"",this._stageWidth,this.hass?.themes?.darkMode??this.hass?.themes?.default_theme??""])}_stageValid(){const t=this._observedStage;return!!t&&t.clientWidth>0&&t.clientHeight>0}_settleContinuityFrame(){if(!this._stageValid())return;if(!this._continuity.hasCompleteFrame&&"steady"===this._continuity.state){if(this._assetsReady())return this._renderSnapshotAt=Date.now(),void this._continuity.markCompleteFrame(this._frameFingerprint());this._beginContinuityCandidate("asset-wait",!0,"asset")}if(!this._continuityDataReady)return;if(!["holding","offline-stale","overlay-pending","overlay-visible","candidate-ready"].includes(this._continuity.state))return;const t=this._continuity.token;if(this._candidateDeviceSnapshot&&this._candidateDeviceSnapshot!==this._visibleDeviceSnapshot&&this._stagedDeviceSnapshotToken!==t)return this._stagedDeviceSnapshotToken=t,void this.requestUpdate();this._continuityPaintToken!==t&&(this._continuityPaintToken=t,this._continuity.candidateReady(t)&&this._continuity.commitAfterPaint(t,{updateComplete:()=>this.updateComplete,stageValid:()=>this.isConnected&&this._stageValid(),assetsReady:()=>this._assetsReady(),frameFingerprint:()=>this._frameFingerprint()}).then(e=>{e&&t===this._continuity.token?(this._renderSnapshotAt=Date.now(),this._candidateDeviceSnapshot&&(this._visibleDeviceSnapshot=this._candidateDeviceSnapshot),this._candidateDeviceSnapshot=null,this._stagedDeviceSnapshotToken=-1):t===this._continuity.token&&(this._continuityPaintToken=-1,this._stagedDeviceSnapshotToken=-1,this._candidateDeviceSnapshot=null,this.requestUpdate())}))}updated(){const t=this.renderRoot.querySelector(".hp-static-stage")||void 0;if(t!==this._observedStage)if(this._stageObserver?.disconnect(),this._observedStage=t,t){const e=()=>{const i=t.clientWidth;if(!(i<=0||Math.abs(i-this._stageWidth)<=.5)){if(this._stageWidth<=0)return this._stageWidth=i,void this.requestUpdate();this._pendingStageWidth=i,this._stageWidthRaf||(this._stageWidthRaf=requestAnimationFrame(()=>{this._stageWidthRaf=0;const t=this._pendingStageWidth;this._pendingStageWidth=0,!t||!this._observedStage||this._observedStage.clientWidth<=0||(Math.abs(this._observedStage.clientWidth-t)>.5?e():Math.abs(t-this._stageWidth)<=.5||(this._continuity.hasCompleteFrame&&this._beginContinuityCandidate("stage-resize",!0,"stage-size"),this._stageWidth=t,this.requestUpdate()))}))}};this._stageObserver=new ResizeObserver(e),this._stageObserver.observe(t),e()}else this._stageObserver=void 0;this._settleContinuityFrame()}async _load(t=!1){if(this.hass){if(this._loading)return this._reloadQueued=!0,void(this._forceReloadQueued||=t);this._loading=!0,this._reloadQueued=!1;try{const e=await Mc(this.hass,t),i=!this._snap||this._snap.configFingerprint!==e.configFingerprint,s=!this._snap||this._snap.layoutFingerprint!==e.layoutFingerprint;if(i&&!await this._signer.prepareImage(this.hass,this._candidateBackdrop(e.config)))return this._continuity.note("asset-failed"),window.clearTimeout(this._reloadRetryTimer),void(this._reloadRetryTimer=window.setTimeout(()=>{this._load(!0)},1e3));window.clearTimeout(this._reloadRetryTimer),this._reloadRetryTimer=0,(i||s)&&this._continuity.hasCompleteFrame&&"steady"===this._continuity.state&&this._beginContinuityCandidate("structural-response",!0),i||s?this._snap=e:this._snap&&(this._snap.rev=e.rev,this._snap.layoutRev=e.layoutRev),i&&this._continuity.note("config-candidate",{configRev:e.rev}),s&&this._continuity.note("layout-candidate",{layoutRev:e.layoutRev}),this._loadedOnce=!0,this._connectionWasLost=!1,this._continuityDataReady=!0,this._refreshDevices()}catch{}finally{if(this._loading=!1,this._continuityDataReady=!0,this.requestUpdate(),this._reloadQueued){const t=this._forceReloadQueued;this._reloadQueued=!1,this._forceReloadQueued=!1,this._load(t)}}}}get _lang(){return ho(this.hass,this._config?.language)}getCardSize(){const t=Ca(this._snap?.config||null).find(t=>t.id===this._config?.space);if(t){const e=t.vb[3]/t.vb[2];return Math.max(3,Math.round(8*e))+(!1===this._config?.show_button?0:1)}return 6}_errorCard(t){return W``}_valueChanged(t){const e={...this._config||{},...t.detail.value};delete e.aspect_ratio;const i=new Event("config-changed",{bubbles:!0,composed:!0});i.detail={config:e},this.dispatchEvent(i)}}Fc.properties={hass:{attribute:!1},_config:{state:!0},_spaces:{state:!0}},customElements.get("houseplan-space-card-editor")||customElements.define("houseplan-space-card-editor",Fc);const Nc=t=>{history.pushState(null,"",t),((t,e,i)=>{const s=new Event(e,{bubbles:!0,composed:!0});s.detail=i??{},t.dispatchEvent(s)})(window,"location-changed",{replace:!1})};class Oc extends ht{constructor(){super(...arguments),this._snap=null,this._loading=!1,this._reloadQueued=!1,this._forceReloadQueued=!1,this._reloadRetryTimer=0,this._stageWidth=0,this._pendingStageWidth=0,this._stageWidthRaf=0,this._haRegistryConnection=null,this._haRegistryRevision=-1,this._devices=[],this._continuity=this._newContinuityController(),this._continuityHistory=[],this._continuityEpoch=0,this._continuityDataReady=!0,this._continuityPaintToken=-1,this._continuityDisposed=!1,this._renderSnapshotAt=Date.now(),this._hassSequence=0,this._connHooked=null,this._connectionWasLost=!1,this._visibleDeviceSnapshot=null,this._candidateDeviceSnapshot=null,this._stagedDeviceSnapshotToken=-1,this._capturedSnapshotSequence=-1,this._capturedSnapshotDevices=null,this._capturedSnapshotActivity="",this._activityRuntime=new Map,this._onHaRegistryUpdate=()=>{const t=cn(this.hass).revision;t!==this._haRegistryRevision&&(this._haRegistryRevision=t,this._refreshDevices(),this._capturedSnapshotSequence=-1,this.requestUpdate())},this._pageVisibility=t=>{this._continuity.visibility(t),"hidden"!==t.kind&&t.long&&(Date.now()-this._renderSnapshotAt>1e3&&this._continuity.note("device-snapshot-stale"),this._continuityDataReady=!1,this._continuityPaintToken=-1,this._load(!0))},this._onConnLost=()=>{this._connectionWasLost=!0,this._continuityDataReady=!1,this._continuityPaintToken=-1,this._continuity.connectionLost()},this._onConnReady=()=>{!this._connectionWasLost&&this._continuity.hasCompleteFrame?this._beginContinuityCandidate("connection-ready",!1,"plan"):(this._continuityDataReady=!1,this._continuityPaintToken=-1),this._load(!0)},this._onAssetLoaded=(t,e)=>{this._signer.markLoaded(this.hass,t,e),this._continuity.note("asset-ready"),this._continuityPaintToken=-1,"steady"!==this._continuity.state&&this.requestUpdate()},this._loadedOnce=!1,this._signer=new Kl(()=>this.requestUpdate()),this._retryContinuity=()=>{this._continuityDataReady=!1,this._continuityPaintToken=-1,this._continuity.retry(this._continuity.recoveryReason||"plan"),this._load(!0)},this._goToSpace=()=>{const t=(this._config?.button_target||"/plan-doma").replace(/#.*$/,"");Nc(`${t}#space=${encodeURIComponent(this._config.space)}`)}}_ensureHaRegistryAuthority(){const t=this.hass?.connection||null;t&&t!==this._haRegistryConnection&&(this._haRegistryRelease?.(),this._haRegistryConnection=t,this._haRegistryRevision=-1,this._haRegistryRelease=ln(this.hass,this._onHaRegistryUpdate),this._onHaRegistryUpdate())}_newContinuityController(){return new jl(()=>{this._continuityEpoch++,this.isConnected&&this.requestUpdate()})}_hookConnection(){const t=this.hass?.connection;t&&t!==this._connHooked&&(this._connHooked?.removeEventListener?.("ready",this._onConnReady),this._connHooked?.removeEventListener?.("disconnected",this._onConnLost),this._connHooked?.removeEventListener?.("reconnect-error",this._onConnLost),t.addEventListener?.("ready",this._onConnReady),t.addEventListener?.("disconnected",this._onConnLost),t.addEventListener?.("reconnect-error",this._onConnLost),this._connHooked=t)}static getConfigElement(){return document.createElement("houseplan-space-card-editor")}static getStubConfig(t){const e=Ic();return{type:"custom:houseplan-space-card",space:Fa(e?.config||null)[0]?.id||"",show_button:!0,live_states:!0,show_temperature:!0,show_signal:!0}}setConfig(t){if(!t||!t.space)throw new Error('houseplan-space-card: "space" is required');this._config={show_button:!0,button_target:"/plan-doma",live_states:!0,show_temperature:!0,show_signal:!0,...t},this._snap=this._snap||Ic()}connectedCallback(){var t;this._continuityDisposed&&(this._continuity=this._newContinuityController(),this._continuityDisposed=!1,this._continuityPaintToken=-1),super.connectedCallback(),this.hass&&this._ensureHaRegistryAuthority(),this._continuityUnsub?.(),this._continuityUnsub=Ul(this.ownerDocument,this._pageVisibility),this._unsub=(t=()=>{this._beginContinuityCandidate("config-event",!1),this._reloadQueued=!0,this._load()},Ec.add(t),()=>Ec.delete(t)),this._signer.start(()=>this.hass,()=>this._referenced())}disconnectedCallback(){this._continuityUnsub?.(),this._continuityUnsub=void 0,this._unsub?.(),this._unsub=void 0,window.clearTimeout(this._reloadRetryTimer),this._reloadRetryTimer=0,this._stageObserver?.disconnect(),this._stageObserver=void 0,this._observedStage=void 0,this._stageWidthRaf&&cancelAnimationFrame(this._stageWidthRaf),this._stageWidthRaf=0,this._pendingStageWidth=0,this._signer.dispose(),this._haRegistryRelease?.(),this._haRegistryRelease=void 0,this._haRegistryConnection=null,this._connHooked?.removeEventListener?.("ready",this._onConnReady),this._connHooked?.removeEventListener?.("disconnected",this._onConnLost),this._connHooked?.removeEventListener?.("reconnect-error",this._onConnLost),this._connHooked=null;for(const t of this._activityRuntime.values())window.clearTimeout(t.timer);this._activityRuntime.clear(),this._continuityHistory=[...this._continuityHistory,...this._continuity.trace].slice(-80),this._continuity.dispose(),this._continuityDisposed=!0,super.disconnectedCallback()}willUpdate(t){t.has("hass")&&this.hass&&(this._hassSequence++,this._renderSnapshotAt=Date.now(),this._continuity.note("hass-snapshot"),this._ensureHaRegistryAuthority(),this._hookConnection()),!this.hass||this._loading||this._snap&&!t.has("hass")||this._snap&&this._loadedOnce||this._load(),(t.has("hass")||t.has("_snap")||t.has("_config")||!this._devices.length)&&this._refreshDevices(),this._captureRenderDeviceSnapshot(),this._continuity.hasCompleteFrame&&"steady"===this._continuity.state&&this._continuity.refreshCompleteFrame(this._frameFingerprint())}_stampActivity(t,e){t.flashTs&&Date.now()-t.flashTs<3300&&"event"===t.flashKind&&"transition"===e||(t.flashTs=Date.now(),t.flashKind=e,t.gen++,window.clearTimeout(t.timer),t.timer=window.setTimeout(()=>this.requestUpdate(),3360))}_syncActivity(t,e){if(!1===this._config?.live_states){for(const t of this._activityRuntime.values())window.clearTimeout(t.timer);return void this._activityRuntime.clear()}const i=new Set;for(const s of t){if(s.hidden)continue;if("static_icon"===Ri(s.marker?.display))continue;i.add(s.id);const t=io(e,s),n=t.samples,o=oo(e,s,!1!==this._config?.show_temperature,t);let r=this._activityRuntime.get(s.id);if(!r||r.sources!==o){r&&window.clearTimeout(r.timer),r={sources:o,last:Object.fromEntries(n.map(t=>[t.eid,t.state])),flashTs:0,flashKind:null,timer:0,gen:0},this._activityRuntime.set(s.id,r);continue}"transition"===r.flashKind&&n.some(t=>"transition"===t.activity)&&(window.clearTimeout(r.timer),r.flashTs=0,r.flashKind=null);let a=null;for(const t of n){const e=Js(r.last[t.eid],t);("event"===e||!a&&e)&&(a=e),r.last[t.eid]=t.state}a&&this._stampActivity(r,a)}for(const[t,e]of this._activityRuntime)i.has(t)||(window.clearTimeout(e.timer),this._activityRuntime.delete(t))}_refreshDevices(){if(!this.hass||!this._snap?.config||!this._config)return;const t=cn(this.hass),e=Mc({hass:this.hass,registry:t,cfg:this._snap.config,lang:this._lang});this._syncActivity(e,un(this.hass,t)),this._devices=e}_captureRenderDeviceSnapshot(){if(!this.hass)return;const t=[...this._activityRuntime.entries()].map(([t,e])=>`${t}:${e.gen}:${e.flashTs}`).join("|");if(this._capturedSnapshotSequence===this._hassSequence&&this._capturedSnapshotDevices===this._devices&&this._capturedSnapshotActivity===t)return;const e=un(this.hass,cn(this.hass)),i=new Map,s=new Set(["sun.sun"]),n=new Set,o=new Set,r=t=>{if(!t)return;const e=t.indexOf(":");if(e<0)return void s.add(t);const i=t.slice(0,e),o=t.slice(e+1);"device"===i?n.add(o):"entity"===i&&s.add(o)},a=Fa(this._snap?.config||null).find(t=>t.id===this._config?.space);for(const t of a?.rooms||[])t.area&&o.add(t.area),r(t.settings?.temp_source),r(t.settings?.hum_source);const l=this._snap?.config?.spaces?.find(t=>t.id===this._config?.space);for(const t of l?.openings||[])t.contact&&s.add(t.contact),t.lock&&s.add(t.lock);for(const t of this._devices)for(const s of[!1,!0])i.set(Sc(t.id,s),ao(e,t,{liveStates:!1!==this._config?.live_states,showTemperature:!1!==this._config?.show_temperature,showSignal:s,activityRuntime:this._activityRuntime.get(t.id),sourceDetails:!1}));const c=xc({sourceSequence:this._hassSequence,hass:e,devices:this._devices,presentations:i,entityIds:s,deviceIds:n,areaIds:o});this._capturedSnapshotSequence=this._hassSequence,this._capturedSnapshotDevices=this._devices,this._capturedSnapshotActivity=t,this._visibleDeviceSnapshot&&"steady"!==this._continuity.state?this._candidateDeviceSnapshot=c:(this._visibleDeviceSnapshot=c,this._candidateDeviceSnapshot=null)}get _renderDeviceSnapshot(){return this._stagedDeviceSnapshotToken===this._continuity.token?this._candidateDeviceSnapshot||this._visibleDeviceSnapshot:this._visibleDeviceSnapshot||this._candidateDeviceSnapshot}_beginContinuityCandidate(t,e,i="plan"){return this._continuityDataReady=e,this._continuityPaintToken=-1,this._stagedDeviceSnapshotToken=-1,this._continuity.beginCandidate(t,i)}_backdropRaw(){return this._snap?.config&&this._config&&Fa(this._snap.config).find(t=>t.id===this._config.space)?.bg?.href||""}_candidateBackdrop(t){return t&&this._config&&Fa(t).find(t=>t.id===this._config.space)?.bg?.href||""}_assetsReady(){const t=this._backdropRaw();return!t||this._signer.isReady(this.hass,t)}_frameFingerprint(){const t=this._snap;return Gl([t?.rev||0,t?.configFingerprint||Wl(t?.config),t?.layoutRev||0,t?.layoutFingerprint||Wl(t?.layout),this._config?.space||"",this._stageWidth,this.hass?.themes?.darkMode??this.hass?.themes?.default_theme??""])}_stageValid(){const t=this._observedStage;return!!t&&t.clientWidth>0&&t.clientHeight>0}_settleContinuityFrame(){if(!this._stageValid())return;if(!this._continuity.hasCompleteFrame&&"steady"===this._continuity.state){if(this._assetsReady())return this._renderSnapshotAt=Date.now(),void this._continuity.markCompleteFrame(this._frameFingerprint());this._beginContinuityCandidate("asset-wait",!0,"asset")}if(!this._continuityDataReady)return;if(!["holding","offline-stale","overlay-pending","overlay-visible","candidate-ready"].includes(this._continuity.state))return;const t=this._continuity.token;if(this._candidateDeviceSnapshot&&this._candidateDeviceSnapshot!==this._visibleDeviceSnapshot&&this._stagedDeviceSnapshotToken!==t)return this._stagedDeviceSnapshotToken=t,void this.requestUpdate();this._continuityPaintToken!==t&&(this._continuityPaintToken=t,this._continuity.candidateReady(t)&&this._continuity.commitAfterPaint(t,{updateComplete:()=>this.updateComplete,stageValid:()=>this.isConnected&&this._stageValid(),assetsReady:()=>this._assetsReady(),frameFingerprint:()=>this._frameFingerprint()}).then(e=>{e&&t===this._continuity.token?(this._renderSnapshotAt=Date.now(),this._candidateDeviceSnapshot&&(this._visibleDeviceSnapshot=this._candidateDeviceSnapshot),this._candidateDeviceSnapshot=null,this._stagedDeviceSnapshotToken=-1):t===this._continuity.token&&(this._continuityPaintToken=-1,this._stagedDeviceSnapshotToken=-1,this._candidateDeviceSnapshot=null,this.requestUpdate())}))}updated(){const t=this.renderRoot.querySelector(".hp-static-stage")||void 0;if(t!==this._observedStage)if(this._stageObserver?.disconnect(),this._observedStage=t,t){const e=()=>{const i=t.clientWidth;if(!(i<=0||Math.abs(i-this._stageWidth)<=.5)){if(this._stageWidth<=0)return this._stageWidth=i,void this.requestUpdate();this._pendingStageWidth=i,this._stageWidthRaf||(this._stageWidthRaf=requestAnimationFrame(()=>{this._stageWidthRaf=0;const t=this._pendingStageWidth;this._pendingStageWidth=0,!t||!this._observedStage||this._observedStage.clientWidth<=0||(Math.abs(this._observedStage.clientWidth-t)>.5?e():Math.abs(t-this._stageWidth)<=.5||(this._continuity.hasCompleteFrame&&this._beginContinuityCandidate("stage-resize",!0,"stage-size"),this._stageWidth=t,this.requestUpdate()))}))}};this._stageObserver=new ResizeObserver(e),this._stageObserver.observe(t),e()}else this._stageObserver=void 0;this._settleContinuityFrame()}async _load(t=!1){if(this.hass){if(this._loading)return this._reloadQueued=!0,void(this._forceReloadQueued||=t);this._loading=!0,this._reloadQueued=!1;try{const e=await zc(this.hass,t),i=!this._snap||this._snap.configFingerprint!==e.configFingerprint,s=!this._snap||this._snap.layoutFingerprint!==e.layoutFingerprint;if(i&&!await this._signer.prepareImage(this.hass,this._candidateBackdrop(e.config)))return this._continuity.note("asset-failed"),window.clearTimeout(this._reloadRetryTimer),void(this._reloadRetryTimer=window.setTimeout(()=>{this._load(!0)},1e3));window.clearTimeout(this._reloadRetryTimer),this._reloadRetryTimer=0,(i||s)&&this._continuity.hasCompleteFrame&&"steady"===this._continuity.state&&this._beginContinuityCandidate("structural-response",!0),i||s?this._snap=e:this._snap&&(this._snap.rev=e.rev,this._snap.layoutRev=e.layoutRev),i&&this._continuity.note("config-candidate",{configRev:e.rev}),s&&this._continuity.note("layout-candidate",{layoutRev:e.layoutRev}),this._loadedOnce=!0,this._connectionWasLost=!1,this._continuityDataReady=!0,this._refreshDevices()}catch{}finally{if(this._loading=!1,this._continuityDataReady=!0,this.requestUpdate(),this._reloadQueued){const t=this._forceReloadQueued;this._reloadQueued=!1,this._forceReloadQueued=!1,this._load(t)}}}}get _lang(){return bo(this.hass,this._config?.language)}getCardSize(){const t=Fa(this._snap?.config||null).find(t=>t.id===this._config?.space);if(t){const e=t.vb[3]/t.vb[2];return Math.max(3,Math.round(8*e))+(!1===this._config?.show_button?0:1)}return 6}_errorCard(t){return B`
${t}
- `}_referenced(){return ys(this._snap?.config)}_renderRecoveryOverlay(){if(!this._continuity.overlayVisible&&"recovery-error"!==this._continuity.state)return G;const t="connection"===this._continuity.recoveryReason;return W`
`}_referenced(){return Ss(this._snap?.config)}_renderRecoveryOverlay(){if(!this._continuity.overlayVisible&&"recovery-error"!==this._continuity.state)return G;const t="connection"===this._continuity.recoveryReason;return B`
- ${po(this._lang,t?"continuity.restore_connection":"continuity.restore_plan")}
- ${"recovery-error"===this._continuity.state?W``:G}
-
`}houseplanContinuityTrace(){return[...this._continuityHistory,...this._continuity.trace].slice(-80).map(t=>({...t,...t.stage?{stage:[...t.stage]}:{}}))}render(){if(!this._config)return G;const t=this._snap?.config;if(!t)return this._errorCard(po(this._lang,"space_card.loading"));const e=this._config.space,i=this._renderDeviceSnapshot,s=vc({hass:i?.hass||this.hass,cfg:t,layout:this._snap?.layout||{},spaceId:e,iconSize:this._config.icon_size,stageWidth:this._stageWidth,lang:this._lang,displayUrl:t=>this._signer.display(this.hass,t),assetLoaded:this._onAssetLoaded,registry:i?{revision:i.sourceSequence,authoritative:!1,access:"limited",entities:{...i.hass.entities},devices:{...i.hass.devices},lastSuccess:i.capturedAt}:sn(this.hass),devices:[...i?.devices||this._devices],presentations:i?.presentations,activityRuntime:this._activityRuntime,liveStates:!1!==this._config.live_states,showTemperature:!1!==this._config.show_temperature,showSignal:!1!==this._config.show_signal,inert:this._continuity.overlayBlocksInteraction});if(!s)return this._errorCard(po(this._lang,"space_card.not_found",{id:e}));const n=Ca(t).find(t=>t.id===e),o=void 0!==this._config.title?this._config.title:n?.title||"",r=!1!==this._config.show_button,a=this._config.button_label||po(this._lang,"space_card.button"),l=this._continuity.overlayVisible||"recovery-error"===this._continuity.state?this._continuity.recoveryReason:null;return W`
+
${wo(this._lang,t?"continuity.restore_connection":"continuity.restore_plan")}
+ ${"recovery-error"===this._continuity.state?B`
`:G}
+
`}houseplanContinuityTrace(){return[...this._continuityHistory,...this._continuity.trace].slice(-80).map(t=>({...t,...t.stage?{stage:[...t.stage]}:{}}))}render(){if(!this._config)return G;const t=this._snap?.config;if(!t)return this._errorCard(wo(this._lang,"space_card.loading"));const e=this._config.space,i=this._renderDeviceSnapshot,s=Cc({hass:i?.hass||this.hass,cfg:t,layout:this._snap?.layout||{},spaceId:e,iconSize:this._config.icon_size,stageWidth:this._stageWidth,lang:this._lang,displayUrl:t=>this._signer.display(this.hass,t),assetLoaded:this._onAssetLoaded,registry:i?{revision:i.sourceSequence,authoritative:!1,access:"limited",entities:{...i.hass.entities},devices:{...i.hass.devices},lastSuccess:i.capturedAt}:cn(this.hass),devices:[...i?.devices||this._devices],presentations:i?.presentations,activityRuntime:this._activityRuntime,liveStates:!1!==this._config.live_states,showTemperature:!1!==this._config.show_temperature,showSignal:!1!==this._config.show_signal,inert:this._continuity.overlayBlocksInteraction});if(!s)return this._errorCard(wo(this._lang,"space_card.not_found",{id:e}));const n=Fa(t).find(t=>t.id===e),o=void 0!==this._config.title?this._config.title:n?.title||"",r=!1!==this._config.show_button,a=this._config.button_label||wo(this._lang,"space_card.button"),l=this._continuity.overlayVisible||"recovery-error"===this._continuity.state?this._continuity.recoveryReason:null;return B`
- ${o?W`${o}
`:G}
+ ${o?B`${o}
`:G}
${s}
${this._renderRecoveryOverlay()}
- ${r?W`