RasplexMon: Cronjob to control Sonos, Philips HUE and send iOS-Push

Hi there Plex-Community. 
 
I just finished my Cronjob for the Raspberry-Plex-Box under my TV and thought i share my resulting python-script. Nothing fancy, but a nice way to ad some small home-automation-features to the Rasplex.
 
Have a look: RasplexMon
 
![post-83834-0-75166300-1411234181.jpg|500x347](upload://dSCudULgPBQejYcRuSfoXI2nBQd.jpg)
 
What it does: 
  1. The Conjob runs every minute on the Pi and has no notable impact on the performance.
  2. It Checks if there is an active Session. If yes, it logs the session (timestamp connected user, show/movie-title) and sends a push-notification to my iOS device using pushover. 
  3. If i am the user with the active session, the script reduces the brightness of my Philips HUE lightbulb in the living-room and sets the Volume of the Sonos speaker in the living-room to zero. 
  4. If the script has already seen the active session it will not try to notify me (or control the lights/audio) again - until three hours have passed. Thus it will work again the next day, if i resume a show which i quit a day earlier after just having watched half of it.  
![post-83834-0-15990100-1411236243.jpg|500x342](upload://rUzB52AnwDjM7uXWTqkA9qLFb9A.jpg)
 
What inside: 
  • The script itself is heavily based on the plexMon.py by Victor Wieczorek. I looked at the plexwatch-solution but found it to bloated.
  • The SoCo-Library controls the Sonos Speaker. 
  • Phue, a Python library for the Philips Hue system, ist controlling the light. 
 
 
What I did: 
  • I connected via SSH and remounted the /flash-directory ( mount -o rw,remount /flash ) to have write-access.
  • Copied my script and all python dependencies to the folder (i did not use pip or any install scripts just copied all the modules i needed).
  • Setup a cron with crontab: 
    ![post-83834-0-61140700-1411234965.png|500x77](upload://bhlksQnZnzBe0q4GT9ixoXApWW6.png)
  • Used /storage/downloads/ a my logfile-directory because its writable. 
This is a typical logfile-Entry if friends are watching
2014-09-19 15:23:11 Tony watches A Great Hollyday in Sweden
 
This is a Logfile-Entry with actions, if I have an active session
2014-09-20 16:43:31 JohnDow watches To Protect And Infect, Part 2
2014-09-20 16:43:32 Attempting to connect to the bridge...
2014-09-20 16:43:32 Using ip: 192.168.1.109
2014-09-20 16:43:32 Using username from config: 2f724e2f724e2f724e2f724e
2014-09-20 16:43:32 Sending SetVolume [(u'InstanceID', 0), (u'Channel', u'Master'), (u'DesiredVolume', 0)] to 192.168.1.105
2014-09-20 16:43:32 Starting new HTTP connection (1): 192.168.1.105
2014-09-20 16:43:32 Received status 200 from 192.168.1.105

I am glad for any optimization-suggestions.

These are my first steps with python so please excuse the code-quality. But everything seams to work relatively stable. Is it OK to just drop the needed python modules in the directory? Can i read the log two times (as i did in the code)? So many questions...
 
Notes
  • One would need a plexpass to read the active sessions from the Plex-Server.
  • The Pushover-App works fine, but will have a one-time-purchase after a brief trial-period
  • The HUE-Bridge has to be manually paird on first run (hit the button on the bridge)
  • I tried to pause the sonos-speaker but settled on the mute commando. I just wanted the living-room Speaker to be silent. But if i send him the pause-commando while he was part of a sonos zone, i would have been required to unjoin him beforehand, which is a bit tricky, because it can throw an exception if i would blindly send the unjoin-commando and the speaker is currently not in a zone. So i just mute him.

Really great work. Would love to add this to my home cinema.. but buying a PlexPass subscribtion just for dimming lights is a bit expensive!  :rolleyes:

Notes

  • One would need a plexpass to read the active sessions from the Plex-Server.
  • The Pushover-App works fine, but will have a one-time-purchase after a brief trial-period
  • The HUE-Bridge has to be manually paird on first run (hit the button on the bridge)
  • I tried to pause the sonos-speaker but settled on the mute commando. I just wanted the living-room Speaker to be silent. But if i send him the pause-commando while he was part of a sonos zone, i would have been required to unjoin him beforehand, which is a bit tricky, because it can throw an exception if i would blindly send the unjoin-commando and the speaker is currently not in a zone. So i just mute him.
Why would you need a PlexPass, surely you mean Plex Login.

I have a nifty little gadget for controlling lights and sound, it's called a finger!

Nice bit of coding though.

Regards

Why would you need a PlexPass, surely you mean Plex Login.


Well, if i really do not need a PlexPass subscription, I'd like to test this script with my own setup.

Unfortunately my 'coding skills' are pretty basic, so I'd really appreciate a step-by-step instruction of how to implement this.

Why would you need a PlexPass, surely you mean Plex Login.

I have a nifty little gadget for controlling lights and sound, it's called a finger!

Nice bit of coding though.

Regards

As far as i know, the queried PMS-URL "/status/sessions" is only available to plex pass users. 

If there is no active pass-subscription linked to your account, you get a 403 Forbidden if you access the session-urls.

“That’s not all. As of today, even if you don’t have a Plex Pass (and really, why wouldn’t you?), we’ve made Multiuser and Now Playing free for everyone. So now everyone you share with gets their own view history, and you can see what’s being streamed, in real time!”


They opened multiuser and Now Playing for people without subscription… is it now possible to use your script without paying for PlexPass?

Does this make the lights turn on again upon stop / pause?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.