Skip to content

Commit

Permalink
Fixes volume changes on tab switch
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Oct 19, 2020
1 parent bf8d5f9 commit 0857709
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
13 changes: 7 additions & 6 deletions dist/spotify-card.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/spotify-card.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spotify-card",
"author": "Niklas Fondberg <[email protected]>, Max Fermor (FL550)",
"version": "2.3.2",
"version": "2.3.3",
"description": "Spotify playlist card",
"keywords": [
"home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const CARD_VERSION = '2.3.2';
export const CARD_VERSION = '2.3.3';
25 changes: 22 additions & 3 deletions src/spotify-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class SpotifyCard extends LitElement {
private _unsubscribe_entitites?: any;
private _spotify_installed = false;
private _fetch_time_out: any = 0;
private _last_volume_set_time = 0;

constructor() {
super();
Expand Down Expand Up @@ -346,13 +347,30 @@ export class SpotifyCard extends LitElement {
return this._spotify_state?.attributes?.volume_level * 100;
}

protected shouldUpdate(changedProperties): any {
let scheduleUpdate = true;
changedProperties.forEach((_oldValue, propName) => {
// console.log(`${propName} changed. oldValue: ${_oldValue}`);
// Blocks render after a volume change, which otherwise can cause a jumping volume slider
if (propName == '_spotify_state') {
const d = new Date();
if (d.getTime() - this._last_volume_set_time < 500) {
scheduleUpdate = false;
}
}
});
return scheduleUpdate;
}

private handleVolumeChanged(ev: ValueChangedEvent): void {
//Prevents volume setting directly after page load
if (this._spotify_state && ev.timeStamp > 2500) {
ev.stopPropagation();
if (this._spotify_state) {
this.hass.callService('media_player', 'volume_set', {
entity_id: this._spotify_state.entity_id,
volume_level: ev.target.value / 100,
});
const d = new Date();
this._last_volume_set_time = d.getTime();
}
}

Expand Down Expand Up @@ -516,7 +534,8 @@ export class SpotifyCard extends LitElement {
min="0"
pin
.value=${this.getVolume()}
@value-changed=${this.handleVolumeChanged}
@click=${this.handleVolumeChanged}
@touchend=${this.handleVolumeChanged}
></paper-slider>
</div>
</div>`
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export interface Tracks {
total: number;
}

export interface ValueChangedEvent {
target?: any;
export interface ValueChangedEvent extends Event {
target: any;
timeStamp: number;
}

0 comments on commit 0857709

Please sign in to comment.