Skip to content
This repository has been archived by the owner on Oct 31, 2020. It is now read-only.

Redo request work with httpx for async #26

Closed
wants to merge 11 commits into from
Closed

Conversation

palfrey
Copy link

@palfrey palfrey commented Sep 8, 2020

This solves #22. I've done some limited local testing, but wasn't sure how to test better. I've used the following script to do testing as well as with my local home assistant

import queue
import media_player
import asyncio
import _thread
import time

def handler(self, context):
    print(self, context)

q = queue.Queue()

class Hass:
    pass

async def main():
    hass = Hass()
    hass.loop = q.get()
    config = media_player.PLATFORM_SCHEMA({"device": "<live id>", "platform": "xboxone", "host": "<xbox ip>"})

    devices = []
    add_devices = lambda device: devices.extend(device)
    await media_player.async_setup_platform(hass, config, add_devices)
    for d in devices:
        print(d.__dict__)
        await d.async_update()
        print("updated")
        print(d._xboxone.all_apps.keys())
        print(d.source_list)       
        print(d.__dict__)

def event_thread():
    loop = asyncio.new_event_loop()
    loop.set_debug(True)
    asyncio.set_event_loop(loop)
    print("event loop", loop)
    q.put(loop)
    loop.run_forever()


_thread.start_new_thread(event_thread, ())
time.sleep(2)
print("main")
asyncio.run(main())

@tuxuser
Copy link
Member

tuxuser commented Sep 10, 2020

Thanks for your contribution!

Ideally we could upstream this branch of xbox-smartglass-core-python: https://github.com/OpenXbox/xbox-smartglass-core-python/tree/task/asyncio

Leftover bugs

  • add_manager functionality does not work, cause CoreProtocol is not set until the asyncio loop is actually started..

Benefits

  • No more REST server wrapping the core library, the library functions can be called directly
  • Authentication and more important, refreshing tokens, can be handled by Home Assistant directly.

Unfortunately I didnt find a sane solution for the add_manager bug yet - assistance happily appreciated.

@tuxuser tuxuser closed this Oct 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants