Skip to content

Commit

Permalink
Fix: Add support for group_members, introduced in hass 2022.5
Browse files Browse the repository at this point in the history
  • Loading branch information
punxaphil committed May 1, 2022
1 parent 1e5038e commit 82d32ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { css, html, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import { getEntityName } from '../utils';
import { getEntityName, getGroupMembers } from '../utils';
import { CustomSonosCard } from '../main';
import { PlayerGroup } from '../types';
import { styleMap } from 'lit-html/directives/style-map.js';
Expand All @@ -18,7 +18,7 @@ class Group extends LitElement {
/^ - /g,
'',
);
const speakerList = stateObj.attributes.sonos_group
const speakerList = getGroupMembers(stateObj)
.map((speaker: string) => getEntityName(this.main.hass, config, speaker))
.join(' + ');
return html`
Expand Down
6 changes: 3 additions & 3 deletions src/components/player.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { css, html, LitElement } from 'lit';
import { property, state } from 'lit/decorators.js';
import { getEntityName } from '../utils';
import { getEntityName, getGroupMembers } from '../utils';

import { CardConfig, Members } from '../types';
import { HomeAssistant } from 'custom-card-helpers';
Expand Down Expand Up @@ -31,10 +31,10 @@ class Player extends LitElement {
this.mediaControlService = this.main.mediaControlService;
this.hassService = this.main.hassService;
const entityAttributes = this.getEntityAttributes();
const isGroup = entityAttributes.sonos_group.length > 1;
const isGroup = getGroupMembers(this.hass.states[this.entityId]).length > 1;
let allVolumes = [];
if (isGroup) {
allVolumes = entityAttributes.sonos_group.map((member: string) =>
allVolumes = getGroupMembers(this.hass.states[this.entityId]).map((member: string) =>
this.getVolumeTemplate(member, getEntityName(this.hass, this.config, member), isGroup, true),
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ export function getEntityName(hass: HomeAssistant, config: CardConfig, entity: s
return name;
}

export function getGroupMembers(state: { attributes: { [p: string]: any } }) {
return state.attributes.sonos_group || state.attributes.group_members;
}

export function getMediaPlayers(config: CardConfig, hass: HomeAssistant) {
if (config.entities) {
return [...new Set(config.entities)].sort().filter((player) => hass.states[player]);
} else {
return Object.values(hass.states)
.filter((state) => state.attributes.sonos_group)
.filter(getGroupMembers)
.map((state) => state.entity_id)
.sort();
}
Expand All @@ -35,8 +39,7 @@ export function createPlayerGroups(mediaPlayers: string[], hass: HomeAssistant,
function createGroupMasters(hass: HomeAssistant, player: string, mediaPlayers: string[]) {
const state = hass.states[player];
try {
const stateAttributes = state.attributes;
const sonosGroup = stateAttributes.sonos_group.filter((member: string) => mediaPlayers.indexOf(member) > -1);
const sonosGroup = getGroupMembers(state).filter((member: string) => mediaPlayers.indexOf(member) > -1);
const isGrouped = sonosGroup?.length > 1;
const isMasterInGroup = isGrouped && sonosGroup && sonosGroup[0] === player;
return !isGrouped || isMasterInGroup;
Expand All @@ -49,7 +52,7 @@ function createGroupMasters(hass: HomeAssistant, player: string, mediaPlayers: s
function createGroupArray(hass: HomeAssistant, groupMaster: string, mediaPlayers: string[], config: CardConfig) {
const state = hass.states[groupMaster];
try {
const membersArray = state.attributes.sonos_group.filter((member: string) => {
const membersArray = getGroupMembers(state).filter((member: string) => {
return member !== groupMaster && mediaPlayers.indexOf(member) > -1;
});
return {
Expand Down

0 comments on commit 82d32ff

Please sign in to comment.