Skip to content

Commit

Permalink
feature: show audio input format
Browse files Browse the repository at this point in the history
always show in volumes section and optionally in player section
  • Loading branch information
punxaphil committed Jan 3, 2024
1 parent baeefa9 commit 27dee25
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ customSources: # Main use case is probably to set tv media player to play TV sou
thumbnail: https://cdn-icons-png.flaticon.com/512/716/716429.png
dynamicVolumeSlider: true # default is false. See more in section further down.
artworkHostname: http://192.168.0.59:8123 #default is ''. Usually not needed, but depending on your setup your device might not be able to access the artwork on the default host. One example where it could be needed is if you cast the dashboard with Google Cast.
showAudioInputFormat: true # default is false. Will show the audio input format (e.g. Dolby Digital) in the player section if available. By default, it will only show if the input format in the volumes section.

# media browser specific
mediaBrowserItemsPerRow: 4 # default is 1. Use this to show items as icons.
Expand Down
38 changes: 37 additions & 1 deletion src/components/player-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { CardConfig, MediaPlayerEntityFeature } from '../types';
import { mdiVolumeMinus, mdiVolumePlus } from '@mdi/js';
import { MediaPlayer } from '../model/media-player';
import { when } from 'lit/directives/when.js';
import { until } from 'lit-html/directives/until.js';

const { SHUFFLE_SET, REPEAT_SET, PLAY, PAUSE, NEXT_TRACK, PREVIOUS_TRACK } = MediaPlayerEntityFeature;

class PlayerControls extends LitElement {
@property({attribute: false}) store!: Store;
@property({ attribute: false }) store!: Store;
private config!: CardConfig;
private activePlayer!: MediaPlayer;
private mediaControlService!: MediaControlService;
Expand All @@ -27,11 +28,15 @@ class PlayerControls extends LitElement {
this.activePlayer.state !== 'idle',
() => html`
<div class="icons">
<div class="flex-1"></div>
<ha-icon-button hide=${noUpDown} @click="${this.volDown}" .path=${mdiVolumeMinus}></ha-icon-button>
<sonos-ha-player .store=${this.store} .features=${[SHUFFLE_SET, PREVIOUS_TRACK]}></sonos-ha-player>
<sonos-ha-player .store=${this.store} .features=${[PLAY, PAUSE]} class="big-icon"></sonos-ha-player>
<sonos-ha-player .store=${this.store} .features=${[NEXT_TRACK, REPEAT_SET]}></sonos-ha-player>
<ha-icon-button hide=${noUpDown} @click="${this.volUp}" .path=${mdiVolumePlus}></ha-icon-button>
<div class="audio-input-format">
${this.config.showAudioInputFormat && until(this.getAudioInputFormat())}
</div>
</div>
`,
)}
Expand All @@ -42,6 +47,13 @@ class PlayerControls extends LitElement {
private volDown = async () => await this.mediaControlService.volumeDown(this.activePlayer);
private volUp = async () => await this.mediaControlService.volumeUp(this.activePlayer);

private async getAudioInputFormat() {
const sensors = await this.store.hassService.getRelatedEntities(this.activePlayer, 'sensor');
const audioInputFormat = sensors.find((sensor) => sensor.entity_id.includes('audio_input_format'));
return audioInputFormat && audioInputFormat.state && audioInputFormat.state !== 'No audio'
? html`<div>${audioInputFormat.state}</div>`
: '';
}
static get styles() {
return css`
.main {
Expand All @@ -61,6 +73,30 @@ class PlayerControls extends LitElement {
--mdc-icon-button-size: 5rem;
--mdc-icon-size: 5rem;
}
.audio-input-format {
flex: 1 0 0;
margin-bottom: 10px;
text-align: center;
align-self: stretch;
position: relative;
}
.audio-input-format > div {
color: var(--card-background-color);
background: var(--disabled-text-color);
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
position: absolute;
bottom: 0;
right: 0;
max-width: 100%;
font-size: smaller;
line-height: normal;
padding: 3px;
}
.flex-1 {
flex: 1;
}
`;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/editor/advanced-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export const ADVANCED_SCHEMA = [
name: 'numberOfFavoritesToShow',
valueMin: 1,
},
{
name: 'showAudioInputFormat',
selector: { boolean: {} },
},
];

class AdvancedEditor extends BaseEditor {
Expand Down
2 changes: 1 addition & 1 deletion src/sections/volumes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Volumes extends LitElement {
if (hide) {
return;
}
const relatedEntities = await this.hassService.getRelatedEntities(player);
const relatedEntities = await this.hassService.getRelatedEntities(player, 'switch', 'number', 'sensor');
return relatedEntities.map((relatedEntity: HassEntity) => {
relatedEntity.attributes.friendly_name =
relatedEntity.attributes.friendly_name?.replaceAll(player.name, '')?.trim() ?? '';
Expand Down
4 changes: 2 additions & 2 deletions src/services/hass-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class HassService {
return mediaPlayerItem;
}

async getRelatedEntities(player: MediaPlayer) {
async getRelatedEntities(player: MediaPlayer, ...entityTypes: string[]) {
return new Promise<HassEntity[]>(async (resolve, reject) => {
const subscribeMessage = {
type: 'render_template',
Expand All @@ -48,7 +48,7 @@ export default class HassService {
unsubscribe();
resolve(
response.result
.filter((item: string) => item.includes('switch') || item.includes('number'))
.filter((item: string) => entityTypes.some((type) => item.includes(type)))
.map((item) => this.hass.states[item]),
);
}, subscribeMessage);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface CardConfig extends LovelaceCardConfig {
topFavorites?: string[];
numberOfFavoritesToShow?: number;
hideBrowseMediaButton?: boolean;
showAudioInputFormat?: boolean;
}

export interface MediaArtworkOverride {
Expand Down

0 comments on commit 27dee25

Please sign in to comment.