4545from pyhilo .device import DeviceAttribute , HiloDevice , get_device_attributes
4646from pyhilo .exceptions import InvalidCredentialsError , RequestError
4747from pyhilo .util .state import (
48+ StateDict ,
4849 WebsocketDict ,
4950 WebsocketTransportsDict ,
5051 get_state ,
@@ -75,7 +76,7 @@ def __init__(
7576 self ._backoff_refresh_lock_ws = asyncio .Lock ()
7677 self ._request_retries = request_retries
7778 self ._state_yaml : str = DEFAULT_STATE_FILE
78- self .state = get_state ( self . _state_yaml )
79+ self .state : StateDict = {}
7980 self .async_request = self ._wrap_request_method (self ._request_retries )
8081 self .device_attributes = get_device_attributes ()
8182 self .session : ClientSession = session
@@ -152,14 +153,14 @@ def dev_atts(
152153 else attribute ,
153154 )
154155
155- def _get_fid_state (self ) -> bool :
156+ async def _get_fid_state (self ) -> bool :
156157 """Looks up the cached state to define the firebase attributes
157158 on the API instances.
158159
159160 :return: Whether or not we have cached firebase state
160161 :rtype: bool
161162 """
162- self .state = get_state (self ._state_yaml )
163+ self .state = await get_state (self ._state_yaml )
163164 fb_state = self .state .get ("firebase" , {})
164165 if fb_fid := fb_state .get ("fid" ):
165166 self ._fb_fid = fb_fid
@@ -171,14 +172,14 @@ def _get_fid_state(self) -> bool:
171172 return True
172173 return False
173174
174- def _get_android_state (self ) -> bool :
175+ async def _get_android_state (self ) -> bool :
175176 """Looks up the cached state to define the android device token
176177 on the API instances.
177178
178179 :return: Whether or not we have cached android state
179180 :rtype: bool
180181 """
181- self .state = get_state (self ._state_yaml )
182+ self .state = await get_state (self ._state_yaml )
182183 android_state = self .state .get ("android" , {})
183184 if token := android_state .get ("token" ):
184185 self ._device_token = token
@@ -187,18 +188,18 @@ def _get_android_state(self) -> bool:
187188
188189 async def _get_device_token (self ) -> None :
189190 """Retrieves the android token if it's not cached."""
190- if not self ._get_android_state ():
191+ if not await self ._get_android_state ():
191192 await self .android_register ()
192193
193194 async def _get_fid (self ) -> None :
194195 """Retrieves the firebase state if it's not cached."""
195- if not self ._get_fid_state ():
196+ if not await self ._get_fid_state ():
196197 self ._fb_id = "" .join (
197198 random .SystemRandom ().choice (string .ascii_letters + string .digits )
198199 for _ in range (FB_ID_LEN )
199200 )
200201 await self .fb_install (self ._fb_id )
201- self ._get_fid_state ()
202+ await self ._get_fid_state ()
202203
203204 async def _async_request (
204205 self , method : str , endpoint : str , host : str = API_HOSTNAME , ** kwargs : Any
@@ -366,7 +367,7 @@ async def post_devicehub_negociate(self) -> tuple[str, str]:
366367 resp = await self .async_request ("post" , url )
367368 ws_url = resp .get ("url" )
368369 ws_token = resp .get ("accessToken" )
369- set_state (
370+ await set_state (
370371 self ._state_yaml ,
371372 "websocket" ,
372373 {
@@ -397,7 +398,7 @@ async def get_websocket_params(self) -> None:
397398 "available_transports" : transport_dict ,
398399 "full_ws_url" : self .full_ws_url ,
399400 }
400- set_state (self ._state_yaml , "websocket" , websocket_dict )
401+ await set_state (self ._state_yaml , "websocket" , websocket_dict )
401402
402403 async def fb_install (self , fb_id : str ) -> None :
403404 LOG .debug ("Posting firebase install" )
@@ -422,7 +423,7 @@ async def fb_install(self, fb_id: str) -> None:
422423 raise RequestError (err ) from err
423424 LOG .debug (f"FB Install data: { resp } " )
424425 auth_token = resp .get ("authToken" , {})
425- set_state (
426+ await set_state (
426427 self ._state_yaml ,
427428 "firebase" ,
428429 {
@@ -463,7 +464,7 @@ async def android_register(self) -> None:
463464 LOG .error (f"Android registration error: { msg } " )
464465 raise RequestError
465466 token = msg .split ("=" )[- 1 ]
466- set_state (
467+ await set_state (
467468 self ._state_yaml ,
468469 "android" ,
469470 {
0 commit comments