Script (NodeJS): Chromecast status to virtual device
Posted: Tuesday 07 June 2016 18:18
This is a simple script to get information pushed into Domoticz when chromecasts changes state to/from playing and idle.
(Just a basic script, it is possible to control chromecasts as well through https://www.npmjs.com/package/nodecastor).
My plan is to adjust the light on RGB-lists behind the tv:s depending on the source.
Create a virtual device (switch) for each Chromecast you want status on.
Install nodejs (preferably with pm2 in order to have the service running at startup).
Install the required libraries (npm install.. ): underscore, nodecastor, request.
Save this script and start it in node.js (after configuration):
The values under //Settings needs to be adapted.
IP and port to Domoticz and an array with mapping from chromecast name to virtual device id in Domoticz (can be found on http://domoticz_ip:domoticz_port/json.htm?type=devices.

(Just a basic script, it is possible to control chromecasts as well through https://www.npmjs.com/package/nodecastor).
My plan is to adjust the light on RGB-lists behind the tv:s depending on the source.
Create a virtual device (switch) for each Chromecast you want status on.
Install nodejs (preferably with pm2 in order to have the service running at startup).
Install the required libraries (npm install.. ): underscore, nodecastor, request.
Save this script and start it in node.js (after configuration):
Code: Select all
var _ = require("underscore");
var nodecastor = require('nodecastor');
var request = require('request');
//Settings
var domoticzIp = "192.168.1.200";
var domoticzPort = 8080;
var playerLookup = [
{ Name: 'Sovrum', Id: 178},
{ Name: 'Vardagsrum', Id: 179},
{ Name: 'Allrum', Id: 180},
{ Name: 'Matsal', Id: 181}
];
function deviceIsPlaying(deviceStatus) {
if (deviceStatus.applications === undefined)
return false;
var match = _.find(deviceStatus.applications, function(item, index) {
if (item.displayName != 'Backdrop')
return true;
});
return !(match === undefined);
}
function lookupDeviceId(deviceName) {
var match = _.find(playerLookup, function(item, index) {
if (item.Name === deviceName)
return true;
});
return (match === undefined) ? -1 : match.Id;
}
function updateDomoticz(playerId, isPlaying) {
if (playerId < 0)
return;
var url = "http://" + domoticzIp + ":" + domoticzPort +
"/json.htm?type=command¶m=switchlight&idx=" + playerId +
"&switchcmd=" + (isPlaying ? "On" : "Off");
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
}
function localServer() {
//Chromecast scanner
nodecastor.scan()
.on('online', function(d) {
var playerName = d.friendlyName;
var playerId = lookupDeviceId(playerName);
d.on('connect', function() {
d.status(function(err, s) {
if (!err) {
var devicePlaying = deviceIsPlaying(s);
d.on('status', function(status) {
var devicePlaying = deviceIsPlaying(s);
});
updateDomoticz(playerId, devicePlaying);
}
});
});
})
.on('offline', function(d) {
var playerName = d.friendlyName;
var playerId = lookupDeviceId(playerName);
updateDomoticz(playerId, devicePlaying);
})
.start();
}
console.log("Starting server");
localServer();
IP and port to Domoticz and an array with mapping from chromecast name to virtual device id in Domoticz (can be found on http://domoticz_ip:domoticz_port/json.htm?type=devices.
