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

Get possible device commands/arguments via API #846

Closed
fhempy opened this issue Oct 29, 2020 · 3 comments
Closed

Get possible device commands/arguments via API #846

fhempy opened this issue Oct 29, 2020 · 3 comments

Comments

@fhempy
Copy link
Contributor

fhempy commented Oct 29, 2020

Hi,

I want to use python-miio via API. To always support all integrated devices I would like to do a generic integration where I would like to get all possible commands via API. Is that possible?

I found a way to get device type and the possible commands, but not the options:

for device_class in DeviceGroupMeta.device_classes:
    print(device_class.get_device_group().name + ", cmds: " + str(device_class.get_device_group().commands.keys()))

It would be great if you could provide an API to get all possible commands and options for a device. Thanks!

@rytilahti
Copy link
Owner

rytilahti commented Nov 9, 2020

Hi,

how about using annotations (e.g., Vacuum.add_timer.__annotations__) which also contain the types where available? Another option could be using the inspect class, but annotations & docstrings are something I would personally use.

>>> from miio import Vacuum
>>> Vacuum.add_timer
<function Vacuum.add_timer at 0x7f33563f4040>
>>> Vacuum.add_timer.__annotations__
{'cron': <class 'str'>, 'command': <class 'str'>, 'parameters': <class 'str'>}
>>> Vacuum.add_timer.__doc__
'Add a timer.\n\n        :param cron: schedule in cron format\n        :param command: ignored by the vacuum.\n        :param parameters: ignored by the vacuum.'

It would be great if there were a nicer API for this as well as for getting the available commands, so contributions are welcome!

@fhempy
Copy link
Contributor Author

fhempy commented Nov 28, 2020

Thanks for the hint with annotations. I could finally manage to get everything I need via annotations. Find my implementation here:
https://github.com/dominikkarall/fhem_pythonbinding/blob/master/FHEM/bindings/python/lib/miio/miio.py#L34-L52

I'm using inspect to get all details of the function including parameters. There are still a few methods which don't use annotations for their parameters. I'm going to create PRs if I come across one.

@fhempy fhempy closed this as completed Nov 28, 2020
@rytilahti
Copy link
Owner

Glad you got it working, PRs to add annotations are welcome! :-)

In the future, I think that the Device class should have introspection capabilities built-in, basically wrapping the annotation data (and @command() given information). Something like commands() giving a list of CommandMeta objects containing all the necessary information in a nice format to easily create dynamic UIs.

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

No branches or pull requests

2 participants