-
Notifications
You must be signed in to change notification settings - Fork 9
extract home ID from jwt instead of /me during auth
#150
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
base: main
Are you sure you want to change the base?
Changes from all commits
f20e789
4ad60a6
68cf473
3a961cc
fe26a6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| from typing import Self | ||
| from urllib.parse import urlencode | ||
|
|
||
| import jwt | ||
| import orjson | ||
| from aiohttp import ClientResponseError | ||
| from aiohttp.client import ClientSession | ||
|
|
@@ -123,8 +124,8 @@ async def async_init(self) -> None: | |
| self._device_activation_status = await self.login_device_flow() | ||
| else: | ||
| self._device_ready() | ||
| get_me = await self.get_me() | ||
| self._home_id = get_me.homes[0].id | ||
|
|
||
| await self._refresh_auth() | ||
|
|
||
| @property | ||
| def device_activation_status(self) -> DeviceActivationStatus: | ||
|
|
@@ -239,9 +240,7 @@ async def _check_device_activation(self) -> bool: | |
| self._token_expiry = time.time() + float(response["expires_in"]) | ||
| self._refresh_token = response["refresh_token"] | ||
|
|
||
| get_me = await self.get_me() | ||
| self._home_id = get_me.homes[0].id | ||
|
|
||
| self._set_home_id_from_access_token() | ||
| return True | ||
|
|
||
| raise TadoError(f"Login failed. Reason: {request.reason}") | ||
|
|
@@ -302,8 +301,23 @@ async def login(self) -> None: | |
| self._token_expiry = time.time() + float(response["expires_in"]) | ||
| self._refresh_token = response["refresh_token"] | ||
|
|
||
| get_me = await self.get_me() | ||
| self._home_id = get_me.homes[0].id | ||
| self._set_home_id_from_access_token() | ||
|
|
||
| def _set_home_id_from_access_token(self) -> None: | ||
| """Decode the access token and set the home ID.""" | ||
| if self._access_token is None: | ||
| raise TadoError("Access token is not available for decoding") | ||
|
|
||
| try: | ||
| jwt_data = jwt.decode( | ||
| self._access_token, | ||
| options={"verify_signature": False, "verify_exp": False}, | ||
| ) | ||
| self._home_id = int(jwt_data["tado_homes"][0]["id"]) | ||
| except (KeyError, TypeError, ValueError, jwt.DecodeError) as err: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, DecodeError is a subclass of InvalidTokenError, which is a subclass of PyJWTError, which is a subclass of Exception |
||
| raise TadoError( | ||
| "Failed to decode access token and extract home ID" | ||
| ) from err | ||
|
|
||
| async def check_request_status( | ||
| self, response_error: ClientResponseError, *, login: bool = False | ||
|
|
@@ -367,6 +381,8 @@ async def _refresh_auth(self) -> None: | |
| self._token_expiry = time.time() + float(response["expires_in"]) | ||
| self._refresh_token = response["refresh_token"] | ||
|
|
||
| self._set_home_id_from_access_token() | ||
|
|
||
| _LOGGER.debug("Tado token refreshed") | ||
|
|
||
| async def get_me(self) -> GetMe: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImd0eSI6WyJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6Z3JhbnQtdHlwZTpkZXZpY2VfY29kZSIsInJlZnJlc2hfdG9rZW4iXSwia2lkIjoiNmNmM2E5NDg4YzA3YmJlMjQ1YzVlNjVkNGZkNTQ3OTAifQ.eyJhdWQiOlsicGFydG5lciJdLCJleHAiOjE3NzE3NzI0MTIsImlhdCI6MTc3MTc3MTgxMiwiaXNzIjoidGFkbyIsIm5iZiI6MTc3MTc3MTgxMiwic3ViIjoiYTRlNzU1MzAtMjU4Zi00NWVkLWE3NWMtMTRlMDdmZjU4MmQzIiwianRpIjoiMjljNDYwM2EtOWZiOS00NjE1LTgxM2ItNWM1NzBhN2M0ZTZjIiwiZW1haWwiOiJ1c2VyQGRvbWFpbi50bGQiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsInJvbGVzIjpbXSwiYXV0aF90aW1lIjoxNzcxNzcxODEwLCJhcHBsaWNhdGlvbklkIjoiMWJiNTAwNjMtNmIwYy00ZDExLWJkOTktMzg3ZjRhOTFjYzQ2IiwidGlkIjoiZjRjMzU0MWYtNzhjNC00ZWJiLWIwZDYtYmJkNzc4NzRjMTJiIiwic2lkIjoiN2JlZGEyMTQtNjU3Ni00NGUyLTllNWYtNjg1MjVmYjk5NzJjIiwidGFkb19ob21lcyI6W3siaWQiOjF9XSwibG9jYWxlIjoiZW5fVVMiLCJ0YWRvX3Njb3BlIjpbImhvbWUudXNlciIsImlkZW50aXR5OnJlYWQiXSwidGFkb191c2VybmFtZSI6InVzZXJAZG9tYWluLnRsZCIsIm5hbWUiOiJ0ZXN0X3VzZXIiLCJ0YWRvX2NsaWVudF9pZCI6InRhZG8tZGV2aWNlLWxpbmtpbmcifQ.H6wFUeoCJoKzqRKa-Ootqiex4ERZwEKkrIJEKg1PnBBQ9Iq3gHsV0iPfY2SQpme35VZwcWC7jbs1FVSwjrRk1L0VxaN7d2D0QXtoWj48_k9AG81LJcdkiuYdDRpL5X39leFcMdMb9EARvZSVUNQvfCOGFlwG_fVrKs5ZyM5dzlR7Weq-XdYzYyZv2awcRjfWJlQbpV-lOZa3Utk24ME6ztIn4xeQvgm_2JdIXqsFJQE5jVh-zO6LdhFW9rHVPyHuvXWA7Fww2kz1MJx6yt-rljxQXZdP09WKOkoWD8GhWB-nwZe3mvgirp_XU97CquL79b19MseDtGzb1RqFK4tyF5SCpN4U0D9gH6oibWQtA9-xiOCbnJTQ2Q2U-NLNGxveiJgV2H5F67uulpotKPxz1poAIAL-fMmfrvxcV4T9RntCktu8hBWfuVb6M6l3oIaylRyt21M3G-tMi_I-9NUpKq0LH6tnBx_UaLkN6i5rPfHlZTaaw8erNAnjYcssP-ya |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we do this step here now? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because otherwise async_init would be dependent on my.tado.com. I figured the call to get_me here was used to get an access token from the supplied refresh token, so I directly moved that here. Or did I understand that wrong?
In any case, we need an acces token to extract the home ID, so if _home_id should be accessible right after init, this is necessary. Otherwise, if we say it is not relevant at this point, we can of course wait for the first call to _request, which will automatically call _refresh_auth and set the home id with this (L384).