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

changes to support guest accounts #42

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

AWare
Copy link

@AWare AWare commented May 15, 2022

This adds support for the homeId parameter as suggested by @martintoreilly in #32

This also moves homeId out of urls where used and into the param dict.

Caveats:

  • Devices and products are not filtered on getAll with a homeId.
  • I wasn't able to use the async api directly.
  • I only have access to a guest hive account, so can't test parity.

With some assistance testing this against async I think this should work in ha as afaict session.config comes from the integrations config, so by not overwriting homeID in the session, this should work?

getParams would have worked nicely if getAll filtered on homeId, but as it doesn't- could probably do with a little rethink on my part

Tested with:

import pyhiveapi as Hive

tokens = {}
hive_auth = Hive.Auth(🙊,🙊)
authData = hive_auth.login()

session = authData["AuthenticationResult"]
tokens.update({"token": session["IdToken"]})
tokens.update({"refreshToken": session["RefreshToken"]})
tokens.update({"accessToken": session["AccessToken"]})

api = Hive.API(token=session["IdToken"])
api.getProducts()
api.getDevices()
api.setHome(🙊)
api.getProducts()
api.getDevices()

Tested in async mode with

import apyhiveapi as Hive

hiveSession = Hive.session.HiveSession(username="🙈", password="🙈")
await hiveSession.login()
api = hiveSession.api
await api.getHomes()
await api.getProducts()

api.setHome("🙈")
await api.getHomes()
await api.getProducts()

@AWare
Copy link
Author

AWare commented May 18, 2022

@KJonline I'm going to be unavailable for a few weeks from the end of this week as I'm having elbow surgery- and would really like to get this in before I go. can I get a bit of a hand getting this over the line please?

@KJonline
Copy link
Contributor

@AWare unfortunately I’m not in a position to at the minute. Im moving house next week so I need to focus on getting that complete. I can help by giving support over chat. Sorry I can’t be more help.

@AWare
Copy link
Author

AWare commented May 18, 2022

@KJonline good luck with the move!

if you're happy to run the workflows, I can fix anything they suggest and then we can pick this up when it's mutually convenient

Copy link
Contributor

@KJonline KJonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had a scan through and spotted the async version of the API it looks like you haven't updated the all url to remove the hard coded params in favour of using the new getParams function.

@AWare
Copy link
Author

AWare commented May 18, 2022

Cheers, have handled that and the linter comments.

Trying to test it now, using python3 -m asyncio and the following:

import apyhiveapi as Hive

hiveSession = Hive.session.HiveSession(username="", password="")
await hiveSession.login()
api = hiveSession.api
await api.getProducts()
api.setHome("")
await api.getProducts()

and getting some weird list index out of range errors on login()- which I'll revisit tomorrow.

@AWare
Copy link
Author

AWare commented Jul 25, 2022

@KJonline would you be able to give me a hand testing this sometime please?

@KJonline
Copy link
Contributor

@AWare yep can do. I think there is still some outstanding changes on this?

@AWare
Copy link
Author

AWare commented Jul 26, 2022

I'm not 100% happy with the split between getParams and getHomeIdParam but was having problems building a harness to test it with.

@KJonline
Copy link
Contributor

@AWare im going to take a look a bit later on tonight

@Pyhass Pyhass deleted a comment from AWare Jul 27, 2022
pyhiveapi/apyhiveapi/api/hive_api.py Outdated Show resolved Hide resolved
}
if sendhomeID and self.homeID is not None:
params.update({"homeId": self.homeID})
return params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need and else if here if someone requests to the send the homeId but its actually set to None. to do this you will also need to create a new exception in the Hive exceptions file and import it into this module

Suggested change
return params
elif sendhomeID and self.homeID is None:
raise HiveInvalidHomeId
return params

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would change the behaviour of the sync api to require a homeId in the case of one home in hive. I've tried to make sure my change only effects cases where a homeID is actually set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, there's nowhere we don't want to send homeID if it isn't set; except getHomes, which doesn't mind it being set. So we don't need an arg for it.

Copy link
Contributor

@KJonline KJonline Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about if your the account owner with a single home and you don’t need to send a homeID.

is your thinking to send one anyway?

@AWare
Copy link
Author

AWare commented Jul 27, 2022

@KJonline thanks for the feedback, I've managed to get it working with the async api now, it turns out that where requests turned key: True into key=true, aiohttp turned it into an exception instead. I've updated the description with my test code.

@KJonline
Copy link
Contributor

I think the best way for me to help with testing is if I create a new temp branch to merge it into. As the code lives on your repository I don’t have access to pull the code

@AWare
Copy link
Author

AWare commented Jul 31, 2022

@KJonline I think you can just set my fork as a remote, and you should be able to push to this branch too because of my PR. Or, there is a way to check out this pr with refs https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally#modifying-an-inactive-pull-request-locally if you didn't want to push

@AWare
Copy link
Author

AWare commented Aug 20, 2022

@KJonline hi, have you had a chance to look at this yet?

@AWare
Copy link
Author

AWare commented Sep 1, 2022

@KJonline sorry to bother you, but would you be able to have another look at this please?

@martintoreilly
Copy link
Contributor

@AWare @KJonline I can try and find some time this weekend to look at this / test if useful? I've an owner and guest account for my home.

Copy link
Contributor

@KJonline KJonline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see feedback in sections of code

@@ -210,11 +266,13 @@ def getProducts(self):

def getActions(self):
"""Call the get actions endpoint."""
url = self.urls["base"] + self.urls["actions"]
url = self.urls["all"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this has been updated to point to all? getDevices & getProducts still points to the individual endpoints. if we can get all data from the all endpoint maybe we should remove the individual endpoints. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea, I did this in July. Sorry.

@@ -222,11 +264,13 @@ async def getProducts(self):
async def getActions(self):
"""Call the get actions endpoint."""
json_return = {}
url = self.urls["actions"]
url = self.urls["all"]
Copy link
Contributor

@KJonline KJonline Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here as the sync one

@@ -101,6 +100,8 @@ def __init__(
)
self.devices = {}
self.deviceList = {}
self.api = API(hiveSession=self, websession=websession)
Copy link
Contributor

@KJonline KJonline Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this has moved? doesn't really matter I don't think, the linter may complain

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc there's a race condition here. this makes sure that the api is not defined until after session has everything it needs

if self.session is not None:
self.homeID = self.session.config.homeID

def getParams(self, products=False, devices=False, actions=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing of the the sync code and the api didn't seem to work with the params when they where booleans. When I changed them to be lower case f and in quotes the api then worked as expected.

Suggested change
def getParams(self, products=False, devices=False, actions=False):
def getParams(self, products='false', devices='false', actions='false\):

The other option would be to the leave the params as booleans and update the dictionary to be as per below converting them to string and lowercasing them.

params = {
            "products": str(products).lower(),
            "devices": str(devices).lower(),
            "actions": str(actions).lower(),
        }

"devices": 'true' if devices else 'false',
"actions": 'true' if actions else 'false',
}
if self.homeID:
Copy link
Contributor

@KJonline KJonline Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seemed to have catered for it here but not in the sync api

@AWare
Copy link
Author

AWare commented Nov 1, 2022

Sorry, but I won't be continuing to work on this PR.

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.

3 participants