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

Add functions to obtain miotspec information #11

Merged
merged 5 commits into from
Dec 8, 2022

Conversation

rytilahti
Copy link
Contributor

@rytilahti rytilahti commented Nov 16, 2022

This extends the public API by adding a new module MiotSpec with the following new class methods:

  • get_specs() to get list of all available specs, containing device urns and version information
  • get_spec_for_urn(device_urn) to get a spec for the given device urn
  • get_standard_types(type) to get list of standardized elements for a given "type" (devices, properties, actions, ..)
  • get_standard_type_spec(type_urn) to get the spec for a standardized element

These methods can be used to obtain information to create generic interfaces based on the response metadata, see rytilahti/python-miio#1581 for an example use-case.

The cli tool is extended with a new sub-command (miot) that can be used to access this functionality (unfortunately, the existing get-devices has now to be called explicitly...):

❯ micloud
Usage: micloud [OPTIONS] COMMAND [ARGS]...

  Tool for fetching xiaomi cloud information.

Options:
  --help  Show this message and exit.

Commands:
  get-devices  Get device information, including tokens.
  miot         Commands for miotspec fetching.

which allows accessing the new functionalities:

❯ micloud miot
Usage: micloud miot [OPTIONS] COMMAND [ARGS]...

  Commands for miotspec fetching.

Options:
  --help  Show this message and exit.

Commands:
  get-spec       Return a device spec for the given URN.
  get-type-spec  Return a type spec for given type URN.
  specs          Return all specs filtered by the given status.
  types          Return available standard URNs for type.

The new commands return JSON so that the output can be passed to jq or other tools that can consume JSON.

Open questions:

  • Naming of the methods?
  • If wanted, the old way of calling cli tool without get-devices could be added
  • Session re-using? I added reset=True to reset the session for every request to avoid passing all extra headers potentially added by the existing functionality

URLs to fetch the data were found at https://gist.github.com/marcelrv/20509106e97b4c7dd8c9056aefda2332 – thanks @marcelrv

This extends the public API by adding the following new methods to MiCloud:
* miot_get_specs() to get list of all available specs, containing device urns and version information
* miot_get_spec(device_urn) to get a spec for the given device urn
* miot_get_standard_types(type) to get list of standardized elements for a given type (devices, properties, actions, ..)
* miot_get_standard_type_spec(type_urn) to get the spec for a standardized element

The cli tool is extended with a new group ("miot") that can be used to access this functionality.
@Squachen
Copy link
Owner

Thank you for the PR, I'll try to make time to look it over and merge ASAP. At the latest this weekend!

@rytilahti
Copy link
Contributor Author

There is no hurry, take your time :-)

I started to wonder if I should shift the new methods into a new class as they are clearly a bit out-of-place currently, but using the existing fetch functionalities (to keep the useragents etc. consistent) would necessitate some refactoring. Let me know what you think and I'll make the necessary adjustments.

@Squachen
Copy link
Owner

Yeah, fell free to do any refactoring you see fit. If you want to move common code to a new parent or utility class to be able to put miot-functions in its own class, that's fine by me. :)

@rytilahti
Copy link
Contributor Author

I reorganized the code a bit by moving the miotspec methods into their own module, and extracted the session creation into an utils function. Feel free to take a look and let me know what you think @Squachen.

rytilahti added a commit to rytilahti/python-miio that referenced this pull request Nov 30, 2022
rytilahti added a commit to rytilahti/python-miio that referenced this pull request Dec 5, 2022
@Squachen Squachen merged commit 1c5920f into Squachen:master Dec 8, 2022
@rytilahti rytilahti deleted the feat/add_miotspec branch December 8, 2022 15:13
rytilahti added a commit to rytilahti/python-miio that referenced this pull request Dec 8, 2022
rytilahti added a commit to rytilahti/python-miio that referenced this pull request Jan 6, 2023
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

Successfully merging this pull request may close these issues.

2 participants