diff --git a/dist/spotify-card.js b/dist/spotify-card.js index 327ef3f..9b3faa1 100644 --- a/dist/spotify-card.js +++ b/dist/spotify-card.js @@ -468,7 +468,7 @@ found at http://polymer.github.io/PATENTS.txt .hidden { display: none; } - `}};e([Z({type:Object})],Ue.prototype,"hass",void 0),e([K()],Ue.prototype,"config",void 0),e([K()],Ue.prototype,"_toggle",void 0),e([K()],Ue.prototype,"chromecast_devices",void 0),Ue=e([q("spotify-card-editor")],Ue);class Re{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){var i;const n=this.parent.devices.filter(t=>t.name==e),s=null===(i=this.parent.config.known_connect_devices)||void 0===i?void 0:i.filter(t=>t.name==e);if(n.length>0)return this.playUriOnConnectDevice(n[0].id,t);if(s&&s.length>0)return this.playUriOnConnectDevice(s[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(e.indexOf(":")<0&&(e="name:".concat(e)),{key:e.split(":",1)[0],pattern:new RegExp(e.slice(e.indexOf(":")+1).trim())})))&&void 0!==t?t:[],s=n.items.filter(e=>i.some(t=>t.pattern.test(e[t.key])));this.parent.playlists=s}else this.parent.playlists=n.items}catch(e){if(!(e instanceof SyntaxError))throw Error("Failed to filter playlists: "+e);this.parent.playlists=n.items}}catch(e){throw Error("Failed to fetch playlists: "+e)}finally{this.loading=!1}}}const Ye=e=>{let t=[];function i(i,n){e=n?i:Object.assign(Object.assign({},e),i);let s=t;for(let t=0;t(t.push(e),()=>{!function(e){let i=[];for(let n=0;n{if(e[t])return e[t];let s,a=0,o=Ye();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&&(n&&(s=n(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||(s&&s.then(e=>{e()}),e.removeEventListener("ready",r))}}},e[t]},We=e=>e.sendMessagePromise({type:"get_states"});function Ie(e,t){if(void 0===e)return null;const{domain:i,service:n}=t.data;return{[i]:Object.assign({},e[i],{[n]:{description:"",fields:{}}})}}function Ge(e,t){if(void 0===e)return null;const{domain:i,service:n}=t.data,s=e[i];if(!s||!(n in s))return null;const a={};return Object.keys(s).forEach(e=>{e!==n&&(a[e]=s[e])}),{[i]:a}}const qe=e=>e.sendMessagePromise({type:"get_services"}),Be=(e,t)=>Promise.all([e.subscribeEvents(t.action(Ie),"service_registered"),e.subscribeEvents(t.action(Ge),"service_removed")]).then(e=>()=>e.forEach(e=>e()));async function Ze(e){const t=await We(e),i={};for(let e=0;ee.subscribeEvents(e=>function(e,t){const i=e.state;if(void 0===i)return;const{entity_id:n,new_state:s}=t.data;if(s)e.setState({[s.entity_id]:s});else{const t=Object.assign({},i);delete t[n],e.setState(t,!0)}}(t,e),"state_changed"),Je=(e,t)=>(e=>Fe(e,"_ent",Ze,Ke))(e).subscribe(t);var Qe;function Xe(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 et(e,t){return!t||!e||(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 ${Le("common.version")} 2.3.10 `,"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:Le("common.description"),preview:!0});let tt=Qe=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 Re(this)}static async getConfigElement(){return document.createElement("spotify-card-editor")}static getStubConfig(){return{}}setConfig(e){let t="";if(e.playlist_type&&!Object.values(se).includes(e.playlist_type.toLowerCase())&&(t="playlist_type"),e.display_style&&!Object.values(ne).includes(e.display_style.toLowerCase())&&(t="display_style"),e.show_error||""!=t)throw new Error(Le("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==this.config.spotify_entity||!this.config.spotify_entity&&i.startsWith("media_player.spotify")?(this._spotify_installed=!0,this._spotify_state=e[i]):i==this._connect_player_entity_id&&(this._connect_player_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){var t,i,n;if(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)}}),e.has("player")&&(null===(t=this.player)||void 0===t?void 0:t.device)&&this.player.device.id!=(null===(n=null===(i=e.get("player"))||void 0===i?void 0:i.device)||void 0===n?void 0:n.id)){console.log("player device changed",this.player.device.id);const[,e]=this.getFilteredDevices(),t=e.find(e=>{var t;return e.id==(null===(t=this.player)||void 0===t?void 0:t.device.id)});this._connect_player_entity_id=t?t.entity_id:void 0,this._connect_player_state=void 0}}getDisplayStyle(){var e;return"grid"==(null===(e=this.config.display_style)||void 0===e?void 0:e.toLowerCase())?ne.Grid:ne.List}getPlayingState(){var e,t;return this._connect_player_state?"playing"==this._connect_player_state.state:null!==(t="playing"==(null===(e=this._spotify_state)||void 0===e?void 0:e.state))&&void 0!==t&&t}getShuffleState(){var e,t;return this._connect_player_state?this._connect_player_state.attributes.shuffle: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:""}getMediaAttribute(e){var t,i,n;return null!==(i=null===(t=this._connect_player_state)||void 0===t?void 0:t.attributes[e])&&void 0!==i?i:null===(n=this._spotify_state)||void 0===n?void 0:n.attributes[e]}isSpotcastInstalled(){var e,t;return!(!(null===(e=this.hass)||void 0===e?void 0:e.connection)||void 0===(t=this.hass.connection,Fe(t,"_srv",qe,Be)).state.spotcast)}checkIfAllowedToShow(e){var t,i;const n=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 n)if(t.test("name"in e?e.name:e.friendly_name))return!1;return!0}getDefaultDevice(){let[e,t,i]=this.getFilteredDevices();if(e=e.filter(e=>e.name==this.config.default_device),t=t.filter(e=>e.name==this.config.default_device),i=i.filter(e=>e.friendly_name==this.config.default_device),e.length>0||t.length>0||i.length>0)return this.config.default_device}getFilteredDevices(){var e;return[this.config.hide_connect_devices?[]:this.devices.filter(this.checkIfAllowedToShow,this),null!==(e=this.config.known_connect_devices)&&void 0!==e?e:[],this.config.hide_chromecast_devices?[]: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 n=e.target;let s;switch(n.localName){case"img":s=null===(i=n.parentElement)||void 0===i?void 0:i.parentElement;break;case"div":s=n;break;case"p":s=n.parentElement;break;default:console.log(n)}s.classList.add("loading"),setTimeout(()=>{s.classList.remove("loading")},1e4),this.spotcast_connector.playUri(t)}onShuffleSelect(e){var t,i;this.hass.callService("media_player","shuffle_set",{entity_id:null===(i=null!==(t=this._connect_player_state)&&void 0!==t?t:this._spotify_state)||void 0===i?void 0:i.entity_id,shuffle:!this.getShuffleState()});let n=e.target.parentElement;"svg"==(null==n?void 0:n.localName)&&(n=n.parentElement),(null==n?void 0:n.classList.contains("shuffle"))?n.classList.remove("shuffle"):null==n||n.classList.add("shuffle")}handleMediaEvent(e,t){var i;e.stopPropagation(),this._spotify_state&&this.hass.callService("media_player",t,{entity_id:(null!==(i=this._connect_player_state)&&void 0!==i?i:this._spotify_state).entity_id})}getVolume(){var e,t,i;return 100*(null===(i=null===(t=null!==(e=this._connect_player_state)&&void 0!==e?e:this._spotify_state)||void 0===t?void 0:t.attributes)||void 0===i?void 0:i.volume_level)}shouldUpdate(e){let t=!0;return e.forEach((e,i)=>{if("_spotify_state"==i||"_connect_player_state"==i){(new Date).getTime()-this._last_volume_set_time<500&&(t=!1)}}),t}handleVolumeChanged(e){var t,i;if(e.stopPropagation(),this._spotify_state){this.hass.callService("media_player","volume_set",{entity_id:null===(i=null!==(t=this._connect_player_state)&&void 0!==t?t:this._spotify_state)||void 0===i?void 0:i.entity_id,volume_level:e.target.value/100});const n=new Date;this._last_volume_set_time=n.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)}knownSpotifyConnectDeviceSelected(e,t){this.confirmDeviceSelection(e);if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToConnectDevice(t.id);const i=this.playlists[0];console.log("knownSpotifyConnectDeviceSelected 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;let n=H``;this.isSpotcastInstalled()||(n=this.showWarning(Le("common.show_missing_spotcast")));let s=H`
Loading...
`;if(this.spotcast_connector.is_loaded())switch(this.getDisplayStyle()){case ne.Grid:s=this.generateGridView();break;default:s=this.generateListView()}const a=H`