New reconnect logic

This commit is contained in:
dmunozv04
2022-03-16 19:57:08 +01:00
parent 12328a6cdc
commit 446811f33c

35
main.py
View File

@@ -40,7 +40,7 @@ class MyPushListener(pyatv.interface.PushListener):
def playstatus_error(self, updater, exception): def playstatus_error(self, updater, exception):
print(exception) print(exception)
print("stopped") print("stopped")
# Error in exception
async def process_playstatus(playstatus, apikey, rc, web_session, categories, atv): async def process_playstatus(playstatus, apikey, rc, web_session, categories, atv):
@@ -103,7 +103,7 @@ async def connect_atv(loop, identifier, airplay_credentials):
atvs = await pyatv.scan(loop, identifier = identifier) atvs = await pyatv.scan(loop, identifier = identifier)
if not atvs: if not atvs:
print("No device found") print("No device found, will retry")
return return
config = atvs[0] config = atvs[0]
@@ -117,21 +117,30 @@ async def loop_atv(event_loop, atv_config, apikey, categories):
identifier = atv_config["identifier"] identifier = atv_config["identifier"]
airplay_credentials = atv_config["airplay_credentials"] airplay_credentials = atv_config["airplay_credentials"]
atv = await connect_atv(event_loop, identifier, airplay_credentials) atv = await connect_atv(event_loop, identifier, airplay_credentials)
if atv:
listener = MyPushListener(apikey, atv, categories)
listener = MyPushListener(apikey, atv, categories)
try:
atv.push_updater.listener = listener atv.push_updater.listener = listener
atv.push_updater.start() atv.push_updater.start()
print("Push updater started") print("Push updater started")
while True: while True:
await asyncio.sleep(1000) await asyncio.sleep(20)
#await event_loop.run_in_executor(None, sys.stdin.readline) try:
except: atv.metadata.app
atv.close() except:
print("Finished connection with ATV") print("Reconnecting to Apple TV")
asyncio.sleep(5) #reconnect to apple tv
loop_atv(event_loop, atv_config, apikey, categories) atv = await connect_atv(event_loop, identifier, airplay_credentials)
if atv:
listener = MyPushListener(apikey, atv, categories)
atv.push_updater.listener = listener
atv.push_updater.start()
print("Push updater started")
def load_config(config_file="config.json"): def load_config(config_file="config.json"):
with open(config_file) as f: with open(config_file) as f: