Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using 'source' for app icon #759

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/cards/media-player-card/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,41 @@ export function getVolumeLevel(entity: MediaPlayerEntity) {
: undefined;
}

const mediaIconMapping = {
app_name: {
spotify: "mdi:spotify",
"google podcasts": "mdi:google-podcast",
plex: "mdi:plex",
soundcloud: "mdi:soundcloud",
youtube: "mdi:youtube",
"oto music": "mdi:music-circle",
netflix: "mdi:netflix",
},
source: {
"google podcasts": "mdi:google-podcast",
"oto music": "mdi:music-circle",
netflix: "mdi:netflix",
ps5: "mdi:sony-playstation",
ps4: "mdi:sony-playstation",
playstation: "mdi:sony-playstation",
plex: "mdi:plex",
soundcloud: "mdi:soundcloud",
spotify: "mdi:spotify",
twitch: "mdi:twitch",
youtube: "mdi:youtube",
},
};

export function computeMediaIcon(config: MediaPlayerCardConfig, entity: MediaPlayerEntity): string {
var icon = config.icon || stateIcon(entity);

if (![UNAVAILABLE, UNKNOWN, OFF].includes(entity.state) && config.use_media_info) {
var app = entity.attributes.app_name?.toLowerCase();
switch (app) {
case "spotify":
return "mdi:spotify";
case "google podcasts":
return "mdi:google-podcast";
case "plex":
return "mdi:plex";
case "soundcloud":
return "mdi:soundcloud";
case "youtube":
return "mdi:youtube";
case "oto music":
return "mdi:music-circle";
case "netflix":
return "mdi:netflix";
default:
return icon;
}
Object.keys(mediaIconMapping).forEach((attribute) => {
var app = entity.attributes[attribute]?.toLowerCase();
if (app) {
icon = mediaIconMapping[attribute][app];
}
});
Comment on lines +96 to +101
Copy link
Owner

@piitaya piitaya Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear about the rules priority because it depends of the order of the attributes.
I think we can do like this (I just wrote a pseudo code but you got the idea 🙂)

for each app_name
    if app_name === entity.attribute.app_name:
           return app_name icon
for each source
    if app_name === entity.attribute.source:
           return source icon

}
return icon;
}
Expand Down