I was looking for a way to detect from a flow if the source of my Sonos device is the TV
Some of the flaky workarounds I tried where:
- LG webos app, is on gives false positives
- Powerplug
And currently I use the following homescript that is executed every second and will check if the Sonos speaker is playing but the length of the track is null, the result is stored in a homey variable which is then pickedup in a different flow
Hopefully this helps someone or someone has a better solution
async function sourceIsTvForSonos(name) {
const devices = await Homey.devices.getDevices();
for (const device of Object.values(devices)) {
if (device.name == name && device.class === "speaker") {
const result = await Homey.devices.getDevice({id: device.id});
const isPlaying = result.capabilitiesObj?.speaker_playing.value;
const position = result.capabilitiesObj?.speaker_position.value;
return (isPlaying == true && position == null);
}
}
return false;
}
async function setVariable(name, value) {
const variables = await Homey.logic.getVariables();
for (const variable of Object.values(variables)) {
if (variable.name == name) {
await Homey.logic.updateVariable({id: variable.id, variable: {"value": value}});
}
}
}
let result = await sourceIsTvForSonos("Woonkamer");
await setVariable("isTvOn", result);