diff --git a/dist/spotify-card.js b/dist/spotify-card.js index deeef7a..7cd9abe 100644 --- a/dist/spotify-card.js +++ b/dist/spotify-card.js @@ -438,7 +438,7 @@ found at http://polymer.github.io/PATENTS.txt paper-slider { width: auto; } - `}};t([J({type:Object})],Gt.prototype,"hass",void 0),t([Z()],Gt.prototype,"_config",void 0),t([Z()],Gt.prototype,"_toggle",void 0),Gt=t([B("spotify-card-editor")],Gt);class Jt{constructor(t){this.playlists=[],this.devices=[],this.chromecast_devices=[],this.state_ttl=4e3,this.last_state_update_time=0,this.loading=!1,this.parent=t}is_loading(){return setTimeout(this.set_loading_off,100),this.loading}set_loading_off(){this.loading=!1}is_loaded(){return void 0!==this.playlists.length}getPlaybackOptions(t){return{uri:t,force_playback:"playing"==this.parent.getSpotifyEntityState(),random_song:this.parent.config.always_play_random_song||!1,account:this.parent.config.account}}playUri(t){const e=this.getCurrentPlayer();if(e)this.playUriOnConnectDevice(e.id,t);else{const e=this.parent.config.default_device;if(e){const i=this.devices.filter(t=>t.name==e);if(i.length>0)return this.playUriOnConnectDevice(i[0].id,t);{const i=this.chromecast_devices.filter(t=>t.friendly_name==e);if(i.length>0)return this.playUriOnCastDevice(i[0].friendly_name,t);console.error("Could not find default_device:"+e)}}console.error("No active device nor default device in settings")}}transferPlaybackToCastDevice(t){this.parent.hass.callService("spotcast","start",{device_name:t,force_playback:!0,account:this.parent.config.account})}transferPlaybackToConnectDevice(t){this.parent.hass.callService("spotcast","start",{spotify_device_id:t,force_playback:!0,account:this.parent.config.account})}playUriOnCastDevice(t,e){const i=Object.assign(Object.assign({},this.getPlaybackOptions(e)),{device_name:t});this.parent.hass.callService("spotcast","start",i)}playUriOnConnectDevice(t,e){const i=Object.assign(Object.assign({},this.getPlaybackOptions(e)),{spotify_device_id:t});this.parent.hass.callService("spotcast","start",i)}async updateState(){if(!((new Date).getTime()-this.last_state_update_timethis.entitiesUpdated(t)))}connectedCallback(){super.connectedCallback(),this.spotcast_connector=new Jt(this),this.doSubscribeEntities()}updated(t){super.updated(t),this.doSubscribeEntities()}disconnectedCallback(){super.disconnectedCallback(),this.unsubscribe_entitites&&this.unsubscribe_entitites()}entitiesUpdated(t){let e=!1;for(const i in t)i.startsWith("media_player")&&((i.startsWith("media_player.spotify_")||i==this.config.spotify_entity)&&(this.spotify_installed=!0,this.spotify_state=t[i]),e=!0);e&&!document.hidden&&(this.fetch_time_out&&clearTimeout(this.fetch_time_out),this.fetch_time_out=setTimeout(()=>{this.spotcast_connector.updateState().then(()=>{this.requestUpdate()})},500))}getSpotifyEntityState(){return this.spotify_state?this.spotify_state.state:""}setConfig(t){let e="";if(t.limit&&"number"!=typeof t.limit&&(e="limit"),t.playlist_type&&!It.includes(t.playlist_type)&&(e="playlist_type"),t.country_code&&"string"!=typeof t.country_code&&(e="country_code"),t.height&&"number"!=typeof t.height&&(e="height"),t.display_style&&!Bt.includes(t.display_style)&&(e="display_style"),t.darkmode&&"boolean"!=typeof t.darkmode&&(e="darkmode"),t.show_error||""!=e)throw new Error(Ft("common.invalid_configuration")+": "+e);t.test_gui&&function(){var t=document.querySelector("home-assistant");if(t=(t=(t=(t=(t=(t=(t=(t=t&&t.shadowRoot)&&t.querySelector("home-assistant-main"))&&t.shadowRoot)&&t.querySelector("app-drawer-layout partial-panel-resolver"))&&t.shadowRoot||t)&&t.querySelector("ha-panel-lovelace"))&&t.shadowRoot)&&t.querySelector("hui-root")){var e=t.lovelace;return e.current_view=t.___curView,e}return null}().setEditMode(!0),this.config=Object.assign({},t)}spotifyDeviceSelected(t){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToConnectDevice(t.id);const e=this.spotcast_connector.playlists[0];console.log("spotifyDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnConnectDevice(t.id,e.uri)}chromecastDeviceSelected(t){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToCastDevice(t.friendly_name);const e=this.spotcast_connector.playlists[0];console.log("chromecastDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnCastDevice(t.friendly_name,e.uri)}onShuffleSelect(){var t,e;"playing"==(null===(t=this.spotify_state)||void 0===t?void 0:t.state)&&this.hass.callService("media_player","shuffle_set",{entity_id:this.spotify_state.entity_id,shuffle:!(null===(e=this.spotcast_connector.player)||void 0===e?void 0:e.shuffle_state)})}handlePlayPauseEvent(t,e){t.stopPropagation(),this.spotify_state&&this.hass.callService("media_player",e,{entity_id:this.spotify_state.entity_id})}onPauseSelect(t){this.handlePlayPauseEvent(t,"media_pause")}onResumeSelect(t){this.handlePlayPauseEvent(t,"media_play")}render(){var t,e,i,s;let n=z``;var o;this.spotcast_installed||this.hass.connection&&void 0!==(o=this.hass.connection,Ct(o,"_srv",Nt,Tt)).state.spotcast&&(this.spotcast_installed=!0),this.config.show_warning&&(n=this.showWarning(Ft("common.show_warning"))),this.spotcast_installed||(n=this.showWarning(Ft("common.show_missing_spotcast"))),this.spotify_installed||(n=this.showWarning(Ft("common.show_missing_spotify")));let r=z`
Loading...
`;!this.spotcast_connector.is_loading()&&this.spotcast_installed?this.spotcast_connector.fetchPlaylists().then(()=>{this.requestUpdate()}):r="grid"==(null===(t=this.config.display_style)||void 0===t?void 0:t.toLowerCase())?this.generateGridView():this.generateListView();const a=this.spotcast_connector.getCurrentPlayer(),c=null!==(e=null==a?void 0:a.name)&&void 0!==e?e:Ft("common.choose_player");return z` + `}};t([J({type:Object})],Gt.prototype,"hass",void 0),t([Z()],Gt.prototype,"_config",void 0),t([Z()],Gt.prototype,"_toggle",void 0),Gt=t([B("spotify-card-editor")],Gt);class Jt{constructor(t){this.playlists=[],this.devices=[],this.chromecast_devices=[],this.state_ttl=4e3,this.last_state_update_time=0,this.loading=!1,this.parent=t}is_loading(){return setTimeout(this.set_loading_off,100),this.loading}set_loading_off(){this.loading=!1}is_loaded(){return void 0!==this.playlists.length}getPlaybackOptions(t){return{uri:t,force_playback:"playing"==this.parent.getSpotifyEntityState(),random_song:this.parent.config.always_play_random_song||!1,account:this.parent.config.account}}playUri(t){const e=this.getCurrentPlayer();if(e)this.playUriOnConnectDevice(e.id,t);else{const e=this.parent.config.default_device;if(e){const i=this.devices.filter(t=>t.name==e);if(i.length>0)return this.playUriOnConnectDevice(i[0].id,t);{const i=this.chromecast_devices.filter(t=>t.friendly_name==e);if(i.length>0)return this.playUriOnCastDevice(i[0].friendly_name,t);console.error("Could not find default_device:"+e)}}console.error("No active device nor default device in settings")}}transferPlaybackToCastDevice(t){this.parent.hass.callService("spotcast","start",{device_name:t,force_playback:!0,account:this.parent.config.account})}transferPlaybackToConnectDevice(t){this.parent.hass.callService("spotcast","start",{spotify_device_id:t,force_playback:!0,account:this.parent.config.account})}playUriOnCastDevice(t,e){const i=Object.assign(Object.assign({},this.getPlaybackOptions(e)),{device_name:t});this.parent.hass.callService("spotcast","start",i)}playUriOnConnectDevice(t,e){const i=Object.assign(Object.assign({},this.getPlaybackOptions(e)),{spotify_device_id:t});this.parent.hass.callService("spotcast","start",i)}async updateState(){if(!((new Date).getTime()-this.last_state_update_timethis.entitiesUpdated(t)))}connectedCallback(){super.connectedCallback(),this.spotcast_connector=new Jt(this),this.doSubscribeEntities()}updated(t){super.updated(t),this.doSubscribeEntities()}disconnectedCallback(){super.disconnectedCallback(),this.unsubscribe_entitites&&this.unsubscribe_entitites()}entitiesUpdated(t){let e=!1;for(const i in t)i.startsWith("media_player")&&((i.startsWith("media_player.spotify_")||i==this.config.spotify_entity)&&(this.spotify_installed=!0,this.spotify_state=t[i]),e=!0);e&&!document.hidden&&(this.fetch_time_out&&clearTimeout(this.fetch_time_out),this.fetch_time_out=setTimeout(()=>{this.spotcast_connector.updateState().then(()=>{this.requestUpdate()})},500))}getSpotifyEntityState(){return this.spotify_state?this.spotify_state.state:""}setConfig(t){let e="";if(t.limit&&"number"!=typeof t.limit&&(e="limit"),t.playlist_type&&!It.includes(t.playlist_type)&&(e="playlist_type"),t.country_code&&"string"!=typeof t.country_code&&(e="country_code"),t.height&&"number"!=typeof t.height&&(e="height"),t.display_style&&!Bt.includes(t.display_style)&&(e="display_style"),t.darkmode&&"boolean"!=typeof t.darkmode&&(e="darkmode"),t.show_error||""!=e)throw new Error(Ft("common.invalid_configuration")+": "+e);t.test_gui&&function(){var t=document.querySelector("home-assistant");if(t=(t=(t=(t=(t=(t=(t=(t=t&&t.shadowRoot)&&t.querySelector("home-assistant-main"))&&t.shadowRoot)&&t.querySelector("app-drawer-layout partial-panel-resolver"))&&t.shadowRoot||t)&&t.querySelector("ha-panel-lovelace"))&&t.shadowRoot)&&t.querySelector("hui-root")){var e=t.lovelace;return e.current_view=t.___curView,e}return null}().setEditMode(!0),this.config=Object.assign({},t)}spotifyDeviceSelected(t){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToConnectDevice(t.id);const e=this.spotcast_connector.playlists[0];console.log("spotifyDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnConnectDevice(t.id,e.uri)}chromecastDeviceSelected(t){if(this.spotcast_connector.getCurrentPlayer())return this.spotcast_connector.transferPlaybackToCastDevice(t.friendly_name);const e=this.spotcast_connector.playlists[0];console.log("chromecastDeviceSelected playing first playlist"),this.spotcast_connector.playUriOnCastDevice(t.friendly_name,e.uri)}onShuffleSelect(){var t,e;"playing"==(null===(t=this.spotify_state)||void 0===t?void 0:t.state)&&this.hass.callService("media_player","shuffle_set",{entity_id:this.spotify_state.entity_id,shuffle:!(null===(e=this.spotcast_connector.player)||void 0===e?void 0:e.shuffle_state)})}handlePlayPauseEvent(t,e){t.stopPropagation(),this.spotify_state&&this.hass.callService("media_player",e,{entity_id:this.spotify_state.entity_id})}onPauseSelect(t){this.handlePlayPauseEvent(t,"media_pause")}onResumeSelect(t){this.handlePlayPauseEvent(t,"media_play")}render(){var t,e,i,s;let n=z``;var o;this.spotcast_installed||this.hass.connection&&void 0!==(o=this.hass.connection,Ct(o,"_srv",Nt,Tt)).state.spotcast&&(this.spotcast_installed=!0),this.config.show_warning&&(n=this.showWarning(Ft("common.show_warning"))),this.spotcast_installed||(n=this.showWarning(Ft("common.show_missing_spotcast"))),this.spotify_installed||(n=this.showWarning(Ft("common.show_missing_spotify")));let r=z`
Loading...
`;!this.spotcast_connector.is_loading()&&this.spotcast_installed?this.spotcast_connector.fetchPlaylists().then(()=>{this.requestUpdate()}):r="grid"==(null===(t=this.config.display_style)||void 0===t?void 0:t.toLowerCase())?this.generateGridView():this.generateListView();const a=this.spotcast_connector.getCurrentPlayer(),c=null!==(e=null==a?void 0:a.name)&&void 0!==e?e:Ft("common.choose_player");return z` ${this.config.hide_warning?"":n} `}generateListView(){var t;if(this.spotcast_connector.is_loaded()){const e=[];for(let i=0;ithis.spotcast_connector.playUri(s.uri)}> - - - ${n?this.generateButtonForCurrent():z`
- - - - -
`} - -

${s.name}

- `)}return z`
${e}
`}return z``}generateGridIconForCurrent(){var t;return"playing"==(null===(t=this.spotify_state)||void 0===t?void 0:t.state)?z` + `}generateListView(){var t;if(this.spotcast_connector.is_loaded()){const e=[];for(let i=0;i0){const i=(null===(t=this.spotify_state)||void 0===t?void 0:t.attributes.media_playlist)===s.name;e.push(z`
this.spotcast_connector.playUri(s.uri)}> + + + ${i?this.generateButtonForCurrent():z`
+ + + + +
`} + +

${s.name}

+
`)}}return z`
${e}
`}return z``}generateGridIconForCurrent(){var t;return"playing"==(null===(t=this.spotify_state)||void 0===t?void 0:t.state)?z` `:z` - `}generateGridView(){var t,e;if(!this.spotcast_connector.is_loaded())return z``;const i=[];for(let s=0;sthis.spotcast_connector.playUri(n.uri)}> - -
- ${o?this.generateGridIconForCurrent():z` - this.spotcast_connector.playUri(n.uri)}> - - - - `} -
- `)}const s=this.config.grid_covers_per_row?this.config.grid_covers_per_row:3;return z`
+ `}generateGridView(){var t,e;if(!this.spotcast_connector.is_loaded())return z``;const i=[];for(let s=0;s0){const s=(null===(t=this.spotify_state)||void 0===t?void 0:t.attributes.media_playlist)===n.name;null===(e=this.spotify_state)||void 0===e||e.attributes.media_playlist,n.name,i.push(z`
this.spotcast_connector.playUri(n.uri)}> + +
+ ${s?this.generateGridIconForCurrent():z` + this.spotcast_connector.playUri(n.uri)}> + + + + `} +
+
`)}}const s=this.config.grid_covers_per_row?this.config.grid_covers_per_row:3;return z`
${i}
`}showWarning(t){return z`${t}`}static get styles(){return[Zt.generalStyles,Zt.listStyles,Zt.gridStyles]}};Kt.generalStyles=tt` *:focus { diff --git a/src/const.ts b/src/const.ts index d433303..f176d36 100755 --- a/src/const.ts +++ b/src/const.ts @@ -1 +1 @@ -export const CARD_VERSION = '2.0.0'; +export const CARD_VERSION = '2.0.1'; diff --git a/src/spotify-card.ts b/src/spotify-card.ts index eb29d55..3d1e7c3 100755 --- a/src/spotify-card.ts +++ b/src/spotify-card.ts @@ -349,7 +349,8 @@ export class SpotifyCard extends LitElement { const result: TemplateResult[] = []; for (let i = 0; i < this.spotcast_connector.playlists.length; i++) { const item = this.spotcast_connector.playlists[i]; - if (item.images.length>0) { // Skip playlists with no images as they are either empty or not available + if (item.images.length > 0) { + // Skip playlists with no images as they are either empty or not available const playing = this.spotify_state?.attributes.media_playlist === item.name; result.push(html`
this.spotcast_connector.playUri(item.uri)}> @@ -366,7 +367,7 @@ export class SpotifyCard extends LitElement {

${item.name}

`); - } + } } return html`
${result}
`; } @@ -395,7 +396,8 @@ export class SpotifyCard extends LitElement { const result: TemplateResult[] = []; for (let i = 0; i < this.spotcast_connector.playlists.length; i++) { const item = this.spotcast_connector.playlists[i]; - if (item.images.length>0) { // Skip playlists with no images as they are either empty or not available + if (item.images.length > 0) { + // Skip playlists with no images as they are either empty or not available const playing = this.spotify_state?.attributes.media_playlist === item.name; this.spotify_state?.attributes.media_playlist === item.name;