Hi postlund,
I got something working - probably not nice, but - as I said - I have no experience…
But I have one problem I think you can help me very easily.
Here is the code:
#! /usr/local/bin/python3.6
"""Simple push update listener example."""
import sys
import asyncio
import aiohttp
import pyatv
import requests
from pyatv import helpers
NAME = 'Apple TV (WoZi)'
ADDRESS = '192.168.13.180'
HSGID = '00000000-5110-4664-648c-edc64667425e'
DETAILS = pyatv.AppleTVDevice(NAME, ADDRESS, HSGID)
OHHOST = '192.168.13.222'
OHPORT = '8080'
class PushListener:
def __init__(self, session):
self.session = session
self.url = "http://%s:%s/rest" % (OHHOST,OHPORT)
self.command_headers = {"Content-type": "text/plain"}
self.polling_headers = {"Accept": "application/json"}
async def _post(self, playstatus):
# This should correspond to the json data expected by the API
#payload = {
# 'title': playstatus.title
#}
#command = 'Test'
# Change address to some API endpoint
#async with self.session.post(
# self.url + '/Test_String', data=command) as resp:
# # This just prints the response - do something useful here
# print('Response: ', await resp.text())
#print('PLAYSTATUS ' + str(playstatus))
#sendStatusToItem(self, 'TestMessage', str(playstatus.title))
print("PLAYSTATE" + str(playstatus.play_state))
print("MEDIATYPE" + str(playstatus.media_type))
atvPlayState = playstatus.play_state
atvPlayStateLabel = "AUS"
atvPlayStateMediaStatus = playstatus.media_type
if atvPlayState == 0:
sendCommandToItem(self, 'Scene_Living_1', '3')
atvPlayStateLabel = "AUS"
elif atvPlayState == 3:
sendCommandToItem(self, 'Scene_Living_1', '3')
atvPlayStateLabel = "PAUSE"
elif atvPlayState == 4 or atvPlayState == 6:
sendCommandToItem(self, 'Scene_Living_1', '1')
atvPlayStateLabel = "SPIELT"
sendStatusToItem(self, 'TestMessage', str(atvPlayStateLabel) + ': ' + str(playstatus.title))
#sendCommandToItem(self, 'Test_Button', 'ON')
print (" ")
print (" ")
def playstatus_update(self, _, playstatus):
asyncio.ensure_future(self._post(playstatus))
def playstatus_error(self, updater, exception):
print('An error occurred (restarting): {0}'.format(exception))
updater.start(initial_delay=1)
def dump(obj):
for attr in dir(obj):
if hasattr( obj, attr ):
print( "obj.%s = %s" % (attr, getattr(obj, attr)))
def sendCommandToItem(self, ohItem, command):
print('STATUS')
print('ITEM: ' + ohItem)
print('COMMAND: ' + command)
requestUrl = self.url+"/items/%s" % (ohItem)
print('REQUEST: ' + requestUrl)
req = requests.post(requestUrl, data=command, headers=self.command_headers)
print('REQ: ' + str(req))
return req.status_code
def sendStatusToItem(self, ohItem, command):
print('STATUS')
print('ITEM: ' + ohItem)
print('COMMAND: ' + command)
requestUrl = self.url+"/items/%s/state" % (ohItem)
print('REQUEST: ' + requestUrl)
req = requests.put(requestUrl, data=command, headers=self.command_headers)
print('REQ: ' + str(req))
return req.status_code
async def _listen_for_push_updates(atv):
print('Starting to listen for updates')
try:
with aiohttp.ClientSession() as session:
atv.push_updater.listener = PushListener(session)
await atv.push_updater.start()
except Exception as ex:
print('Got an error: ', ex)
finally:
await atv.logout()
async def _no_device_found():
print('No Apple TV found', file=sys.stderr)
if __name__ == '__main__':
print('Connecting')
#helpers.auto_connect(_listen_for_push_updates,
# not_found=_no_device_found)
atv = pyatv.connect_to_apple_tv(DETAILS, _listen_for_push_updates)
I am ready sending items (update and status) to openhab but I do not know how to connect direct to a specified apple tv. With the helpers method everything is fine, but because I have two apple tv I have to switch off the wrong one.
Maybe you could have a look on the last line?
Thanks!
HFM
P.S.: I have another question regarding pyatv. Is it possible to find out where the focus is in menu? So that I know where to start with the commands.