Pyatv: Push Updates

Created on 6 Jul 2020  路  10Comments  路  Source: postlund/pyatv

Is there any way to have real time push updates from the Apple TV in python code?

question

All 10 comments

Yes, the push updates API provides that:

https://pyatv.dev/development/listeners/#push-updates

Okay thanks , but do you have a complete python code on the real time push API?

Here's a simple one that hacked together now:

import sys
import asyncio
import pyatv
from pyatv.interface import PushListener


class MyPushListener(PushListener):
    def playstatus_update(self, updater, playstatus):
        print("Currently playing:\n", playstatus)

    def playstatus_error(self, updater, exception):
        print("Got an exception:", exception)


async def main():
    loop = asyncio.get_event_loop()

    atvs = await pyatv.scan(loop, identifier="device_id")
    if not atvs:
        print("No device found", file=sys.stderr)
        return

    atv = await pyatv.connect(atvs[0], loop)
    listener = MyPushListener()
    try:
        atv.push_updater.listener = listener
        atv.push_updater.start()

        print("Press ENTER to quit")
        await loop.run_in_executor(None, sys.stdin.readline)
    except:
        atv.close()


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

It doesn't do credentials handling (or pairing) so you might have to do that separately. Should work directly for tvOS and older devices if home sharing is enabled however. Just replace device_id with an identifier from atvremote scan.

Okay Thanks!! 馃榾

If you are not interested in writing any code you can get the same output from atvremote ...push_updates as well. Or in json format from atvscript ... push_updates.

https://pyatv.dev/documentation/atvremote/#push-updates
https://pyatv.dev/documentation/atvscript/#push-and-power-updates

Hi again , for the push listener in Python code how could I do a if statement on the play-status of the apple TV?

Something like this:

class MyPushListener(PushListener):
    def playstatus_update(self, updater, playstatus):
        if playstatus.artist == "Weird Al Yankovic":
            # do something

All available fields are here. This is an async callback, so remember to not do any blocking code here.

Alright thanks

Any additional questions or can we close the isssue?

I will close this issue now, feel free to create a new issue if you need further assistance 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nriley picture nriley  路  18Comments

MarkStoutjesdijk picture MarkStoutjesdijk  路  9Comments

postlund picture postlund  路  7Comments

sofakng picture sofakng  路  17Comments

Krocko picture Krocko  路  10Comments