Skip to content

Commit

Permalink
Updated to version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga committed Aug 19, 2019
1 parent f54ea04 commit cdfab32
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
43 changes: 35 additions & 8 deletions MMM-GoogleCast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Module.register("MMM-GoogleCast",{
defaults: {
device: null
device: null,
hide: false,
animationSpeed: 3000,
},

getStyles: function() {
Expand Down Expand Up @@ -29,11 +31,26 @@ Module.register("MMM-GoogleCast",{
{
this.app = payload.app;
this.volume = payload.volume;
if (this.app == null)
if (this.config.device == payload.id)
{
this.media = false;
}
this.updateDom();
if (this.app == null)
{
this.media = false;
if (this.config.hide)
{
this.hide(this.config.animationSpeed)
}
else
{
this.updateDom();
}
}
if (this.app && this.config.hide)
{
this.updateDom();
this.show(this.config.animationSpeed);
}
}
}
else if (payload.type == "mediaStatus")
{
Expand All @@ -51,7 +68,17 @@ Module.register("MMM-GoogleCast",{
{
this.media = true;
}
this.updateDom()
if (this.config.device == payload.id)
{
if (this.media == false && this.config.hide)
{
this.hide(this.config.animationSpeed)
}
else
{
this.updateDom()
}
}
}
else if (payload.type == "status")
{
Expand Down Expand Up @@ -116,7 +143,7 @@ Module.register("MMM-GoogleCast",{
main.appendChild(spinning);
}
main.style = "text-align: center; line-height: 19px";
}
}
else
{
main.classList.add("main");
Expand Down Expand Up @@ -236,7 +263,7 @@ Module.register("MMM-GoogleCast",{
else
{
volumeBar.style = "background-color: white; border: 4px solid white; vertical-align: bottom; border-radius: 5px; overflow: hidden; height: " + (this.volume * 100) + "px";
// volumeBar.style = "background-color: white; border: 4px solid white; vertical-align: bottom; border-radius: 5px; height: " + (this.volume * 100) + "%";
// volumeBar.style = "background-color: white; border: 4px solid white; vertical-align: bottom; border-radius: 5px; height: " + (this.volume * 100) + "%";
}
volumeDiv.appendChild(volumeBar);
deviceStatus.appendChild(volumeDiv);
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ID: zzzzzz-zzzzz-zzzzzzz-zzzzzz

As you can see, you can also link audio groups apart from individual video and speakers devices. Note the ID of the device you want to link to your mirror. If no devices are detected, go to "Issues" section below

**Step 3**: Copy and paste this in your ``config.js`` file, replacing the ``device`` property with the ID of the device you want to link to your mirror:
**Step 3**: Add the module section to your ``config.js`` file, replacing the ``device`` property with the ID of the device you want to link to your mirror:
```
{
module: 'MMM-GoogleCast',
Expand All @@ -67,6 +67,16 @@ As you can see, you can also link audio groups apart from individual video and s
}
},
```
Available options in ``config.js`` for advanced users:

| **Option** | **Default** | **Description** |
|--------------------|-------------|--------------------------------------------------------------------------------------------|
| ``device`` | ``null`` | Specifies the device to link in your mirror |
| ``hide`` | ``false`` | Setting this to true will hide the module if no app is connected to the Google Cast device |
| ``animationSpeed`` | ``3000`` | Sets the speed of the animations while hiding/showing the module |

You can also run multiple instances of the module, one for every device do you want to track. Copy multiple times the snippet above
in your ``config.js`` (one for the device) and change the ID of the device in each instance.

And everything is done! You should now see the media that's being played in your mirror

Expand Down
14 changes: 8 additions & 6 deletions lib/Cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
oldState = None
OpenedApp = None
VolumeLevel = None
deviceId = None

class MMConfig:
CONFIG_DATA = json.loads(sys.argv[1])
Expand Down Expand Up @@ -53,7 +54,7 @@ def new_media_status(self, status):
# print("AlbumArtist:" + str(status.album_artist))
# print("State:" + str(status.player_state))
# print("ImageURL:" + str(link))
toNode("mediaStatus", {"title": status.title, "artist": status.artist, "album": status.album_name, "albumArtist": status.album_artist, "state": status.player_state, "image": link})
toNode("mediaStatus", {"id": deviceId, "title": status.title, "artist": status.artist, "album": status.album_name, "albumArtist": status.album_artist, "state": status.player_state, "image": link})

class StatusListener:
def __init__(self, name, cast):
Expand All @@ -68,7 +69,7 @@ def new_cast_status(self, status):
# if __debug__:
# print("VolumeLevel:" + str(status.volume_level))
# print("ConnectedApp:" + str(status.display_name))
toNode("deviceStatus", {"volume": status.volume_level, "app": status.display_name})
toNode("deviceStatus", {"id": deviceId, "volume": status.volume_level, "app": status.display_name})
if VolumeLevel != status.volume_level or OpenedApp != status.display_name:
# if __debug__:
# print("VolumeLevel:" + str(status.volume_level))
Expand All @@ -77,22 +78,23 @@ def new_cast_status(self, status):
VolumeLevel = status.volume_level
else:
OpenedApp = status.display_name
toNode("deviceStatus", {"volume": status.volume_level, "app": status.display_name})
toNode("deviceStatus", {"id": deviceId, "volume": status.volume_level, "app": status.display_name})

def shutdown(self, signum):
toNode("status", 'Shutdown: Closing MMM-GoogleCast Python backend...')
quit()
try:
try:
deviceId = MMConfig.getDeviceId()
devices = pychromecast.get_chromecasts()
device = next(cc for cc in devices
if str(cc.device.uuid) == MMConfig.getDeviceId())
if str(cc.device.uuid) == deviceId)
device.start()
listenerMedia = StatusMediaListener(device.name, device)
device.media_controller.register_status_listener(listenerMedia)
listenerCast = StatusListener(device.name, device)
device.register_status_listener(listenerCast)
except:
toNode("status", "Device not found or unreachable")
toNode("status", "Device '" + str(deviceId) + "' not found or unreachable")
quit()
toNode("status", "Cast.py loaded successfully")
signal.signal(signal.SIGINT, shutdown)
Expand Down
9 changes: 3 additions & 6 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module.exports = NodeHelper.create({
pyshell.on('message', function (message) {
if (message.hasOwnProperty('deviceStatus'))
{
self.sendSocketNotification('deviceStatus', {type: "deviceStatus", app: message.deviceStatus.app, volume: message.deviceStatus.volume})
self.sendSocketNotification('deviceStatus', {type: "deviceStatus", id: message.deviceStatus.id, app: message.deviceStatus.app, volume: message.deviceStatus.volume})
}
if (message.hasOwnProperty('mediaStatus'))
{
self.sendSocketNotification('mediaStatus', {type: "mediaStatus", albumArtist: message.mediaStatus.albumArtist, image: message.mediaStatus.image,
self.sendSocketNotification('mediaStatus', {type: "mediaStatus", id: message.mediaStatus.id, albumArtist: message.mediaStatus.albumArtist, image: message.mediaStatus.image,
title: message.mediaStatus.title, state: message.mediaStatus.state, album: message.mediaStatus.album, artist: message.mediaStatus.artist});
}
if (message.hasOwnProperty('status'))
Expand All @@ -41,10 +41,7 @@ module.exports = NodeHelper.create({
socketNotificationReceived: function(notification, payload) {
if(notification === 'CONFIG') {
this.config = payload
if(!pythonStarted) {
pythonStarted = true;
this.python_start();
};
this.python_start();
};
}
});

0 comments on commit cdfab32

Please sign in to comment.