Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for how to call aioharmony from within Python #23

Open
wayner9 opened this issue Feb 14, 2021 · 8 comments
Open

Examples for how to call aioharmony from within Python #23

wayner9 opened this issue Feb 14, 2021 · 8 comments

Comments

@wayner9
Copy link

wayner9 commented Feb 14, 2021

The documentation shows how to call this from the command line, as in:
python3 aioharmony --harmony_ip 192.168.1.203 show_current_activity
but how would I do this from within Python? Is it possible to get an example working .py file to do stuff like showing activities, starting activities, powering off a hub, etc? What do I import and how do I call the functions, etc

@ehendrix23
Copy link
Owner

See main.py. It pretty much shows how to use each functionality.

@zaphodb65
Copy link

Sorry, but I can't figure this out. Here is what I am trying to do - just start an activity on a given hub. Here is the code that I have tried, but it says it can't find get_client in module aioharmony. What am I doing wrong?

import aioharmony
ip_test="192.168.1.203"
prot_test="WEBSOCKETS"
show_responses="true"
kitchen_hub=get_client(ip_test,prot_test,show_responses)
kitchen_hub.start_activity("Watch TV")

@gpala7077
Copy link

I would also like some examples on using it within python. It isn't as straightforward how to use the library

@ehendrix23
Copy link
Owner

First of all, this is using asyncio so you can't just do get_client.
To connect to the Hub you first create the object:
client = HarmonyAPI(ip_address=ip_address, protocol=protocol)
and then you connect to the client:
await client.connect()
Examples of this is in the get_client() function in __main__.py

To start an activity you use:
await client.start_activity(self, activity_id)

Notice how it needs an activity_id. To get the activity_id (for example for "Watch TV") you retrieve it using:
activity_id = client.get_activity_id(args.activity)

__main__.py allows one to use it from command line, or to use as an example how to use the API.

Thus the program would be:

from aioharmony.harmonyapi import HarmonyAPI
ip_test="192.168.1.203"
prot_test="WEBSOCKETS"
kitchen_hub=HarmonyAPI(ip_address=ip_test, protocol=prot_test)
await kitchen_hub.connect()
watch_tv_activity_id = kitchen_hub.get_activity_id("Watch TV")
await kitchen_hub.start_activity(self, watch_tv_activity_id)

If you would want to use the methods in __main__ then it would be more like (not checking but should be close):

import aioharmony

async def run():
    ip_test="192.168.1.203"
    prot_test="WEBSOCKETS"
    show_responses="true"
    kitchen_hub=aioharmony.get_client(ip_test,prot_test,show_responses)
    aioharmony.start_activity(kitchen_hub, "Watch TV")

loop = asyncio.new_event_loop()
loop.run_until_complete(run())

Hope this helps a bit.

@gpala7077
Copy link

This was perfect! Thank you so much. I got it working! Great work

@gpala7077
Copy link

One last question, how does the hub.send_command() work. I assume there needs to be a way to send a command to a specific device.

@ehendrix23
Copy link
Owner

Look at send_command in __main__.py. From an API it uses send_commands.

send_commands expects an argument of SendCommand or a list of SendCommand (in case multiple commands are to be sent).
SendCommand is defined in constant.py

Overall as arguments it expects a device_id, command, and delay.

What you could do is update main.py and do like print statements in there or so (or run it through debug) to see

@wayner9
Copy link
Author

wayner9 commented Jan 8, 2022

I am still having problems getting the package to work from within my Python programs. I tried this but it gives me a self is not defined error.

import asyncio
from aioharmony.harmonyapi import HarmonyAPI
ip_test="192.168.1.203"
prot_test="WEBSOCKETS"
import asyncio

async def run():
    kitchen_hub=HarmonyAPI(ip_address=ip_test, protocol=prot_test)
    await kitchen_hub.connect()
    watch_tv_activity_id = kitchen_hub.get_activity_id("Live TV")
    return await kitchen_hub.start_activity(self, watch_tv_activity_id)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()

I have also tried changing the self argument to kitchen_hub but that doesn't work. Anyone have any ideas on how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants