lukasroegner / homebridge-apple-tv-remote

Plugin for controlling Apple TVs in homebridge.
MIT License
131 stars 13 forks source link

[Feature Request] Add MQTT Support #28

Open RaymondMouthaan opened 4 years ago

RaymondMouthaan commented 4 years ago

Hi @lukasroegner,

First of all, thanks for the great work on this project! I've been looking for such solution for quit sometime. Mostly I am interested when the Apple TV gets status played, paused or stopped. I use this status to control some lights.

Currently I am using the api to get the status every 500 ms and based on isPlaying I control lights to turn on or off, which works fine. However it would be great to have the status published to a MQTT broker, instead polling the api. Likewise to control commands via MQTT.

The guys from homebridge-mqttthing do this for many other accessories, perhaps you could add mqtt support as well for homebridge-apple-tv-remote?

Another question I have, is there a way the get pause state as well via the api?

Thanks in advance!

lukasroegner commented 4 years ago

Hi @RaymondMouthaan,

Thanks for the feedback. I can understand that MQTT support would be great in your case. However, it is a really specific feature, which might not be used by other users at all. I'll leave this issue open and we can checke whether other users are also interested. If that's the case, I can implement an MQTT endpoint for the API.

The isPlaying property is true when the Apple TV is playing and false if an audio track or movie is paused or stopped, or no media is loaded.

RaymondMouthaan commented 4 years ago

@lukasroegner, thanks for your quick reply!

Hope others have interest in mqtt support as well, it just would integrate with platforms like hassio and Node-RED a little better :-) My JavaScript / TypeScript skills are not so great, otherwise I could do a PR for it.

Indeed, I noted that there is no distinction between pause and stop. That fine for me, however last night I tested the api and noticed that isPlaying and isOn is not always in sync with the Apple TV. Meaning while playing a movie, the api response was isPlaying: false and vice versa ...

Another thing I noticed is, when both switches disabled

<...>

   "isOnOffSwitchEnabled": false,
   "onOffSwitchName": "Apple TV Switch",
   "isPlayPauseSwitchEnabled": false,
   "playPauseSwitchName": "Apple TV Play/Pause"
<...>

Requesting the api gives the following errors:

[4/21/2020, 4:05:33 PM] [Apple TV Platform] [AppleTV-Livingroom] Getting power state...
[4/21/2020, 4:05:33 PM] Error: connect EHOSTUNREACH fd00::1:bb:7b14:f2b:19aa:49152
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
[4/21/2020, 4:05:33 PM] Error: connect EHOSTUNREACH fd00::1:bb:7b14:f2b:19aa:49152
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
[4/21/2020, 4:05:33 PM] Error: connect EHOSTUNREACH fd00::1:bb:7b14:f2b:19aa:49152
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
[4/21/2020, 4:05:33 PM] Error: connect EHOSTUNREACH fd00::1:bb:7b14:f2b:19aa:49152
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

While with both switches enabled, these errors do not occur. Is this behaviour as designed?

Greets, Ray

iMartzen commented 4 years ago

Hello @lukasroegner ! I would like to have MQTT support too šŸ„‡

RaymondMouthaan commented 4 years ago

Perhaps a configuration like this:

{
    "devices": [
        {
            "name": "AppleTV-Livingroom",
            "credentials": "",
            "mqttUrl": "mqtt://<ip_address|hostname>:<port>",
            "mqttUsername": "<username>",
            "mqttPassword": "<password>",
            "mqttLog": true,
            "onOffSwitch": {
                "name": "Apple TV Switch",
                "switchEnabled": true,
                "mqttEnabled": true,
                "topics": {
                    "getOn": {
                        "topic": "homebridge/onoff-switch/get",
                        "apply": "if (JSON.parse( message ).hasOwnProperty('state')) { return JSON.parse( message ).state; }"
                    },
                    "setOn": {
                        "topic": "homebridge/onoff-switch/set",
                        "apply": "return JSON.stringify( { state: message } );"
                    }
                }
            },
            "playPauseSwitch": {
                "name": "Apple TV Play/Pause",
                "switchEnabled": true,
                "mqttEnabled": true,
                "topics": {
                    "getOn": {
                        "topic": "homebridge/playpause-switch/get",
                        "apply": "if (JSON.parse( message ).hasOwnProperty('state')) { return JSON.parse( message ).state; }"
                    },
                    "setOn": {
                        "topic": "homebridge/playpause-switch/set",
                        "apply": "return JSON.stringify( { state: message } );"
                    }
                }
            }
        }
    ]
}
lukebrannon1 commented 4 years ago

@RaymondMouthaan @iMartzen I stumbled upon this post and figured I'd share -

Take a look at node-red-contrib-homebridge-automation if you haven't already. It allows node-red to control and monitor any homebridge accessory. I'm using it to enable MQTT control of my homebridge accessories. Just point the nodes to an MQTT in/out node, and I think you'll achieve what you're looking for.

RaymondMouthaan commented 4 years ago

@lukebrannon1, thanks for the tip, I wasn't aware of the plugin. šŸ‘ Currently I am struggling with homebridge-apple-tv-remote, which isn't stable passing the play/pause state of the AppleTV, but another solution I used to work with mqtt is by Apple's Automation feature to control a mqttthing switch, this switch publishes its state on a topic.

So basically:

when AppleTV play/pause is on
then MQTTT Switch on

when AppleTV play/pause is off
then MQTTT Switch off
martinorob commented 4 years ago

+1 for add https://flows.nodered.org/node/node-red-contrib-homebridge-automation compatibility

martinorob commented 4 years ago

every 500 ms

Hi @RaymondMouthaan , may you tell me how you get the status every 500 ms? With curl? crontab?

Thanks

RaymondMouthaan commented 4 years ago

every 500 ms

Hi @RaymondMouthaan , may you tell me how you get the status every 500 ms? With curl? crontab?

Thanks

@martinorob

homebridge-apple-tv-remote is able to expose an api (see the documentation of this plugin). You can request it from a tool like Postman or use curl or from Node-RED you can use a http request node. And case you like to request the api every x seconds, just use an inject node. Hope this helps :-)

RaymondMouthaan commented 3 years ago

Hi @RaymondMouthaan,

Thanks for the feedback. I can understand that MQTT support would be great in your case. However, it is a really specific feature, which might not be used by other users at all. I'll leave this issue open and we can checke whether other users are also interested. If that's the case, I can implement an MQTT endpoint for the API.

The isPlaying property is true when the Apple TV is playing and false if an audio track or movie is paused or stopped, or no media is loaded.

Hi @lukasroegner, did you have the chance to consider the request to add mqtt to the plugin? In a first implementation it would be great just to have the plugin publish its state to a given topic when mqtt is enabled. Perhaps when you make a start I can help testing and develop it further, but currently I just don't know where to start.

If tried node-red-contrib-homebridge-automation as a solution but somehow I can't get it to work with my multiple dockerized homebridges and Node-RED.

My current workaround is some homekit rules that turn on|off a mqttthing switch based on the state of a homebridge-apple-tv-remote switch. So basically I have 2 homebridge-apple-tv-remote switches, one for on|off and one for play|pause which control two mqttthing switches by homekit rules, which publishes its state to Mqtt topics, subscribed by Node-RED.

lukasroegner commented 3 years ago

@RaymondMouthaan I don't think that I'll implement it within the scope of this plugin, it just doesn't fit into the homebridge ecosystem. I think it does make more sense to either implement a separate nodejs application that translates MQTT to AppleTV, or a Node-RED package.

RaymondMouthaan commented 3 years ago

@RaymondMouthaan I don't think that I'll implement it within the scope of this plugin, it just doesn't fit into the homebridge ecosystem. I think it does make more sense to either implement a separate nodejs application that translates MQTT to AppleTV, or a Node-RED package.

That's sad to read :(, mqtt support just would add something extra to the plugin, just like REST does. Next to REST, is mqtt mostly used for communication in home automation and IOT.

In case of creating another nodejs app to translate to mqtt to AppleTV feels like duplication of all the work and effort you put in homebridge-apple-tv-remote + mqtt ...

But maybe I just should rest my case on this topic and close it

NorthernMan54 commented 3 years ago

I just tried homebridge-automation with this and did not see any issues using this with node-red.

image

RaymondMouthaan commented 3 years ago

@NorthernMan54 do you run multiple docker homebridge containers? If you do I am very interested in how you make the plug-in work in Node-RED.

NorthernMan54 commented 3 years ago

Sorry, Iā€™m not a docker person.