From 275e8cf6c7911d91a34d9441a88f3b833c5bc76d Mon Sep 17 00:00:00 2001 From: FL550 Date: Fri, 11 Dec 2020 10:28:47 +0100 Subject: [PATCH] Bump version --- dist/spotify-card.js | 3 ++- dist/spotify-card.js.map | 2 +- package.json | 4 ++-- src/const.ts | 2 +- src/spotify-card.ts | 3 ++- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dist/spotify-card.js b/dist/spotify-card.js index cd0691f..7fcd979 100644 --- a/dist/spotify-card.js +++ b/dist/spotify-card.js @@ -456,7 +456,7 @@ found at http://polymer.github.io/PATENTS.txt paper-slider { width: auto; } - `}};e([Z({type:Object})],Ue.prototype,"hass",void 0),e([J()],Ue.prototype,"config",void 0),e([J()],Ue.prototype,"_toggle",void 0),e([J()],Ue.prototype,"chromecast_devices",void 0),Ue=e([B("spotify-card-editor")],Ue);class je{constructor(e){this.state_ttl=4e3,this.last_state_update_time=0,this.loading=!1,this.parent=e}is_loading(){return setTimeout(this.set_loading_off,100),this.loading}set_loading_off(){this.loading=!1}is_loaded(){return 0!==this.parent.playlists.length}getPlaybackOptions(e){return{uri:e,force_playback:"playing"==this.parent.getSpotifyEntityState(),random_song:this.parent.config.always_play_random_song||!1,account:this.parent.config.account}}playUri(e){const t=this.getCurrentPlayer();if(t)this.playUriOnConnectDevice(t.id,e);else{const t=this.parent.config.default_device;if(t)this.startPlaybackOnDevice(t,e);else{if(!(this.parent.devices.length>0))throw new Error("No device available for playback");{const t=this.parent.devices[0].name;this.startPlaybackOnDevice(t,e)}}}}startPlaybackOnDevice(e,t){const i=this.parent.devices.filter(t=>t.name==e);if(i.length>0)return this.playUriOnConnectDevice(i[0].id,t);{const i=this.parent.chromecast_devices.filter(t=>t.friendly_name==e);if(i.length>0)return this.playUriOnCastDevice(i[0].friendly_name,t);throw new Error("Could not find device: "+e)}}transferPlaybackToCastDevice(e){this.parent.hass.callService("spotcast","start",{device_name:e,force_playback:!0,account:this.parent.config.account})}transferPlaybackToConnectDevice(e){this.parent.hass.callService("spotcast","start",{spotify_device_id:e,force_playback:!0,account:this.parent.config.account})}playUriOnCastDevice(e,t){const i=Object.assign(Object.assign({},this.getPlaybackOptions(t)),{device_name:e});this.parent.hass.callService("spotcast","start",i)}playUriOnConnectDevice(e,t){const i=Object.assign(Object.assign({},this.getPlaybackOptions(t)),{spotify_device_id:e});this.parent.hass.callService("spotcast","start",i)}async updateState(){if(!((new Date).getTime()-this.last_state_update_time{let t=[];function i(i,s){e=s?i:Object.assign(Object.assign({},e),i);let n=t;for(let t=0;t(t.push(e),()=>{!function(e){let i=[];for(let s=0;s{if(e[t])return e[t];let n,a=0,o=Re();const r=()=>i(e).then(e=>o.setState(e,!0)),l=()=>r().catch(t=>{if(e.connected)throw t});return e[t]={get state(){return o.state},refresh:r,subscribe(t){a++,1===a&&(s&&(n=s(e,o)),e.addEventListener("ready",l),l());const i=o.subscribe(t);return void 0!==o.state&&setTimeout(()=>t(o.state),0),()=>{i(),a--,a||(n&&n.then(e=>{e()}),e.removeEventListener("ready",r))}}},e[t]},Fe=e=>e.sendMessagePromise({type:"get_states"});function We(e,t){if(void 0===e)return null;const{domain:i,service:s}=t.data;return{[i]:Object.assign({},e[i],{[s]:{description:"",fields:{}}})}}function Ie(e,t){if(void 0===e)return null;const{domain:i,service:s}=t.data,n=e[i];if(!n||!(s in n))return null;const a={};return Object.keys(n).forEach(e=>{e!==s&&(a[e]=n[e])}),{[i]:a}}const qe=e=>e.sendMessagePromise({type:"get_services"}),Be=(e,t)=>Promise.all([e.subscribeEvents(t.action(We),"service_registered"),e.subscribeEvents(t.action(Ie),"service_removed")]).then(e=>()=>e.forEach(e=>e()));async function Ge(e){const t=await Fe(e),i={};for(let e=0;ee.subscribeEvents(e=>function(e,t){const i=e.state;if(void 0===i)return;const{entity_id:s,new_state:n}=t.data;if(n)e.setState({[n.entity_id]:n});else{const t=Object.assign({},i);delete t[s],e.setState(t,!0)}}(t,e),"state_changed"),Je=(e,t)=>(e=>Ye(e,"_ent",Ge,Ze))(e).subscribe(t);var Ke;function Qe(e,t){if(!t||!oe(t)&&!oe(e)&&e.length!=t.length)return!0;for(const i in e)if(e[i].id!=t[i].id)return!0;return!1}function Xe(e,t){return!t||(e.state!=t.state||e.attributes.shuffle!=t.attributes.shuffle||e.attributes.media_title!=t.attributes.media_title||e.attributes.media_artist!=t.attributes.media_artist||e.attributes.volume_level!=t.attributes.volume_level)}console.info(`%c SPOTIFY-CARD \n%c ${He("common.version")} 2.3.4 `,"color: orange; font-weight: bold; background: black","color: white; font-weight: bold; background: dimgray"),window.customCards=window.customCards||[],window.customCards.push({type:"spotify-card",name:"Spotify Card",description:He("common.description"),preview:!0});let et=Ke=class extends ie{constructor(){super(),this.playlists=[],this.devices=[],this.chromecast_devices=[],this._spotify_installed=!1,this._fetch_time_out=0,this._last_volume_set_time=0,this.spotcast_connector=new je(this)}static async getConfigElement(){return document.createElement("spotify-card-editor")}static getStubConfig(){return{}}setConfig(e){let t="";if(e.playlist_type&&!Object.values(ne).includes(e.playlist_type.toLowerCase())&&(t="playlist_type"),e.display_style&&!Object.values(se).includes(e.display_style.toLowerCase())&&(t="display_style"),e.show_error||""!=t)throw new Error(He("common.invalid_configuration")+": "+t);this.config=e}connectedCallback(){super.connectedCallback(),this.doSubscribeEntities(),this.updateSpotcast()}doSubscribeEntities(){var e;(null===(e=this.hass)||void 0===e?void 0:e.connection)&&!this._unsubscribe_entitites&&this.isConnected&&(this._unsubscribe_entitites=Je(this.hass.connection,e=>this.entitiesUpdated(e)))}entitiesUpdated(e){let t=!1;for(const i in e)i.startsWith("media_player")&&((i.startsWith("media_player.spotify")||i==this.config.spotify_entity)&&(this._spotify_installed=!0,this._spotify_state=e[i]),t=!0);t&&!document.hidden&&this.updateSpotcast()}updateSpotcast(){this._fetch_time_out&&clearTimeout(this._fetch_time_out),this._fetch_time_out=setTimeout(async()=>{this.hass&&this.isSpotcastInstalled()&&!this.spotcast_connector.is_loading()&&(await this.spotcast_connector.updateState(),await this.spotcast_connector.fetchPlaylists())},500)}disconnectedCallback(){super.disconnectedCallback(),this._unsubscribe_entitites&&(this._unsubscribe_entitites(),this._unsubscribe_entitites=void 0)}updated(e){super.updated(e),this.updateComplete.then(()=>{for(const e of this.renderRoot.querySelectorAll("[data-spotify-image-url]")){const t=new Image;t.onload=function(t){var i;null===(i=e.firstElementChild)||void 0===i||i.replaceWith(t.srcElement)},e.dataset.spotifyImageUrl&&(t.src=e.dataset.spotifyImageUrl)}})}getDisplayStyle(){var e;return"grid"==(null===(e=this.config.display_style)||void 0===e?void 0:e.toLowerCase())?se.Grid:se.List}getPlayingState(){var e,t;return null!==(t="playing"==(null===(e=this._spotify_state)||void 0===e?void 0:e.state))&&void 0!==t&&t}getShuffleState(){var e,t;return null!==(t=null===(e=this.player)||void 0===e?void 0:e.shuffle_state)&&void 0!==t&&t}getSpotifyEntityState(){return this._spotify_state?this._spotify_state.state:""}isSpotcastInstalled(){var e,t;return!(!(null===(e=this.hass)||void 0===e?void 0:e.connection)||void 0===(t=this.hass.connection,Ye(t,"_srv",qe,Be)).state.spotcast)}checkIfAllowedToShow(e){var t,i;const s=null!==(i=null===(t=this.config.filter_devices)||void 0===t?void 0:t.map(e=>new RegExp(e+"$")))&&void 0!==i?i:[];for(const t of s)if(t.test("name"in e?e.name:e.friendly_name))return!1;return!0}getDefaultDevice(){let[e,t]=this.getFilteredDevices();if(e=e.filter(e=>e.name==this.config.default_device),t=t.filter(e=>e.friendly_name==this.config.default_device),e.length>0||t.length>0)return this.config.default_device}getFilteredDevices(){return[this.devices.filter(this.checkIfAllowedToShow,this),this.chromecast_devices.filter(this.checkIfAllowedToShow,this)]}getPlaylists(){return this.playlists}isThisPlaylistPlaying(e){var t;return(null===(t=this._spotify_state)||void 0===t?void 0:t.attributes.media_playlist)===e.name}startUri(e,t){var i;const s=e.target;let n;switch(s.localName){case"img":n=null===(i=s.parentElement)||void 0===i?void 0:i.parentElement;break;case"div":n=s;break;case"p":n=s.parentElement;break;default:console.log(s)}n.classList.add("loading"),setTimeout(()=>{n.classList.remove("loading")},1e4),this.spotcast_connector.playUri(t)}onShuffleSelect(e){var t,i;this.hass.callService("media_player","shuffle_set",{entity_id:null===(t=this._spotify_state)||void 0===t?void 0:t.entity_id,shuffle:!(null===(i=this.player)||void 0===i?void 0:i.shuffle_state)});let s=e.target.parentElement;"svg"==(null==s?void 0:s.localName)&&(s=s.parentElement),(null==s?void 0:s.classList.contains("shuffle"))?s.classList.remove("shuffle"):null==s||s.classList.add("shuffle")}handleMediaEvent(e,t){e.stopPropagation(),this._spotify_state&&this.hass.callService("media_player",t,{entity_id:this._spotify_state.entity_id})}getVolume(){var e,t;return 100*(null===(t=null===(e=this._spotify_state)||void 0===e?void 0:e.attributes)||void 0===t?void 0:t.volume_level)}shouldUpdate(e){let t=!0;return e.forEach((e,i)=>{if("_spotify_state"==i){(new Date).getTime()-this._last_volume_set_time<500&&(t=!1)}}),t}handleVolumeChanged(e){if(e.stopPropagation(),this._spotify_state){this.hass.callService("media_player","volume_set",{entity_id:this._spotify_state.entity_id,volume_level:e.target.value/100});const t=new Date;this._last_volume_set_time=t.getTime()}}confirmDeviceSelection(e){var t;const i=e.target;null===(t=null==i?void 0:i.parentElement)||void 0===t||t.classList.add("dropdown-content-hide"),setTimeout(()=>{var e;null===(e=null==i?void 0:i.parentElement)||void 0===e||e.classList.remove("dropdown-content-hide")},1e3)}spotifyDeviceSelected(e,t){this.confirmDeviceSelection(e);if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToConnectDevice(t.id);const i=this.playlists[0];console.log("spotifyDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnConnectDevice(t.id,i.uri)}chromecastDeviceSelected(e,t){this.confirmDeviceSelection(e);if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToCastDevice(t.friendly_name);const i=this.playlists[0];console.log("chromecastDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnCastDevice(t.friendly_name,i.uri)}getCurrentPlayer(){return this.spotcast_connector.getCurrentPlayer()}render(){var e,t,i,s,n,a,o,r;let l=z``;this.isSpotcastInstalled()||(l=this.showWarning(He("common.show_missing_spotcast")));let c=z`
Loading...
`;if(this.spotcast_connector.is_loaded())switch(this.getDisplayStyle()){case se.Grid:c=this.generateGridView();break;default:c=this.generateListView()}const d=z`