I believe so. In fact, here’s the code for testing. Replace anything that you need:
var express = require('express')
, request = require('request')
, multer = require('multer');;
var app = express();
var upload = multer({ dest: '/tmp/' });
app.post('/', upload.single('thumb'), function (req, res, next) {
var payload = JSON.parse(req.body.payload);
var cURL = "http://localhost:8123/api/";
var json = { "entity_id": "light.living_room_floor_lamps" };
var cType = "application/json";
var ha_passwd = "(Home Assistant API Password Goes Here)";
console.log('Got webhook for', payload.event);
if(payload.event === 'media.play' || payload.event === 'media.resume')
{
console.log("Media is playing.");
request({
url: cURL + "services/light/turn_off",
method: "POST", json: true, body: json,
headers: { "x-ha-access" : ha_passwd, "Content-Type": cType }
}, function(error, response, body) { console.log(response); });
}
else if(payload.event === 'media.pause' || payload.event === 'media.stop')
{
console.log("Media is stopped.");
request({
url: cURL + "services/light/turn_on",
method: "POST", json: true, body: json,
headers: { "x-ha-access" : ha_passwd, "Content-Type": cType }
}, function(error, response, body) { console.log(response); });
}
res.sendStatus(200);
});
app.listen(10000);
If you then call nodejs yourscripthere.js
, you should be able to watch the output in the console as you play/pause the video. The response for turning on/off my floor lamps when I pause/play the video is not bad.
However, you might want to limit the listening to only a single client if you are planning to set up a nice cinematic experience. If you have a tablet and you are in another room, the lights can go on and off as you pause/resume playback which you probably do not want. I’ll do some research and see what I can find.
Update: As I play a TV show in Plex Web, it is instantaneous. However, it does take a second for the Home Assistant API to be executed when media is playing or pausing.
Update 1:
Okay! The list of payloads can be found in “Example Payload” in Plex Webhooks page.
Here’s an example of mine:
console.log('Got webhook for ', payload.event + ", UUID " + payload.Player.uuid );
// if(payload.Player.uuid == "(insert your UUID here.)")
// {
if(payload.event === 'media.play' || payload.event === 'media.resume')
{
console.log("Media is playing.");
request({
url: cURL + "services/light/turn_off",
method: "POST", json: true, body: json,
headers: { "x-ha-access" : ha_passwd, "Content-Type": cType }
}, function(error, response, body) { console.log(body); });
}
else if(payload.event === 'media.pause' || payload.event === 'media.stop')
{
console.log("Media is stopped.");
request({
url: cURL + "services/light/turn_on",
method: "POST", json: true, body: json,
headers: { "x-ha-access" : ha_passwd, "Content-Type": cType }
}, function(error, response, body) { console.log(body); });
// }
}
Run the script with nodejs and and watch for the UUID. Once you’ve found the UUID that is outputted by the script, go ahead and uncomment the ones that have “//” for the if() { } statement and paste the UUID that’s in between quotes with “(insert your UUID here.)”