Skip to content

Commit

Permalink
Merge branch 'fix/optimisation'
Browse files Browse the repository at this point in the history
  • Loading branch information
tbouron committed Jan 25, 2020
2 parents c7eec7c + f736d8a commit 345fb2b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
4 changes: 3 additions & 1 deletion MMM-Sonos.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
.metadata {
display: inline-flex;
}

.metadata > span {
display: flex;
justify-items: center;
}
.metadata > span:not(:last-child) {
margin-right: 0.5em;
}
.metadata .group-name {
max-width: 150px;
}

.feather {
width: 1.2em;
Expand Down
68 changes: 38 additions & 30 deletions MMM-Sonos.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Module.register('MMM-Sonos', {
defaults: {
animationSpeed: 1000,
showFullGroupName: false,
showArtist: true,
showAlbum: true,
showMetadata: true
Expand All @@ -23,48 +24,51 @@ Module.register('MMM-Sonos', {

socketNotificationReceived: function (id, payload) {
Log.log(`Notification received: ${id}`, payload);
let currentItem;

switch (id) {
case 'SET_SONOS_GROUPS':
this.items = payload;
this.updateDom(this.config.animationSpeed);
break;
case 'SET_SONOS_CURRENT_TRACK':
currentItem = this.items.hasOwnProperty(payload.group.ID) ? this.items[payload.group.ID] : {};
this.items[payload.group.ID] = {
...currentItem,
group: payload.group,
track: payload.track,
};
this.updateDom(this.config.animationSpeed);
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
track: payload.track,
};
this.updateDom(this.config.animationSpeed);
}
break;
case 'SET_SONOS_VOLUME':
currentItem = this.items.hasOwnProperty(payload.group.ID) ? this.items[payload.group.ID] : {};
this.items[payload.group.ID] = {
...currentItem,
group: payload.group,
volume: payload.volume
};
this.updateDom();
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
volume: payload.volume
};
this.updateDom();
}
break;
case 'SET_SONOS_MUTE':
currentItem = this.items.hasOwnProperty(payload.group.ID) ? this.items[payload.group.ID] : {};
this.items[payload.group.ID] = {
...currentItem,
group: payload.group,
isMuted: payload.isMuted
};
this.updateDom();
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
isMuted: payload.isMuted
};
this.updateDom();
}
break;
case 'SET_SONOS_PLAY_STATE':
currentItem = this.items.hasOwnProperty(payload.group.ID) ? this.items[payload.group.ID] : {};
this.items[payload.group.ID] = {
...currentItem,
group: payload.group,
state: payload.state
};
this.updateDom(this.config.animationSpeed);
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
state: payload.state
};
this.updateDom(this.config.animationSpeed);
}
break;
default:
Log.info(`Notification with ID "${id}" unsupported. Ignoring...`);
Expand Down Expand Up @@ -117,10 +121,14 @@ Module.register('MMM-Sonos', {
volume = `${this.getIcon(item.volume < 50 ? 'volume-1' : 'volume-2', 'dimmed')}&nbsp;<span>${item.volume}</span>`;
}

const groupName = this.config.showFullGroupName
? item.group.ZoneGroupMember.map(member => member.ZoneName).join(' + ')
: item.group.Name;

const metadata = document.createElement('div');
metadata.className = 'metadata small normal';
metadata.innerHTML =
`<span>${this.getIcon('speaker', 'dimmed')}&nbsp;<span>${item.group.Name}</span></span>` +
`<span>${this.getIcon('speaker', 'dimmed')}&nbsp;<span class="group-name ticker">${groupName}</span></span>` +
'&nbsp;' +
`<span>${volume}</span>` +
'&nbsp;' +
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ then add the module to your MagicMirror's configuration. Here is an example:
position: 'top_left',
config: {
animationSpeed: Number,
showFullGroupName: Boolean,
showArtist: Boolean,
showAlbum: Boolean,
showMetadata: Boolean
Expand All @@ -49,6 +50,7 @@ then add the module to your MagicMirror's configuration. Here is an example:
| Configuration key | Description | Default | Required |
| --- | --- | --- | --- |
| animationSpeed | Animation speed to display/hide the module when tracks change. This value is in _milliseconds_ | 1000 | No |
| showArtist | Whether or not display the artist name | `true` | No |
| showAlbum | Whether or not display the album name | `true` | No |
| showMetadata | Whether or not display the track metadata, i.e. room where it's played, length, volume | `true` | No |
| showFullGroupName | Whether or not to display all devices in the group. If false, the group name will be `<coordinator-name> +<number-other-devices>`, e.g. `Kitchen +2`. | `false` | No |
| showArtist | Whether or not to display the artist name | `true` | No |
| showAlbum | Whether or not to display the album name | `true` | No |
| showMetadata | Whether or not to display the track metadata, i.e. room where it's played, length, volume | `true` | No |
4 changes: 2 additions & 2 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = NodeHelper.create({

setGroups(groups) {
Promise.all(groups.map(group => {
const sonos = new Sonos(group.host);
const sonos = group.CoordinatorDevice();
return Promise.all([
sonos.currentTrack(),
sonos.getCurrentState(),
Expand Down Expand Up @@ -98,7 +98,7 @@ module.exports = NodeHelper.create({
groups.forEach(group => {
console.log(`Registering listeners for group "${group.Name}" (host "${group.host}")`);

const sonos = new Sonos(group.host);
const sonos = group.CoordinatorDevice();

sonos.on('Mute', isMuted => {
console.log('This speaker is %s.', isMuted ? 'muted' : 'unmuted')
Expand Down

0 comments on commit 345fb2b

Please sign in to comment.