@@ -16,14 +16,12 @@ class GraphQlHelper:
1616 def __init__ (self , api : API , devices : Devices ):
1717 self ._api = api
1818 self ._devices = devices
19- self .access_token = ""
2019 self .mapper : GraphqlValueMapper = GraphqlValueMapper ()
2120
2221 self .subscriptions : List [Optional [asyncio .Task ]] = [None ]
2322
2423 async def async_init (self ) -> None :
2524 """Initialize the Hilo "GraphQlHelper" class."""
26- self .access_token = await self ._api .async_get_access_token ()
2725 await self .call_get_location_query (self ._devices .location_hilo_id )
2826
2927 QUERY_GET_LOCATION : str = """query getLocation($locationHiloId: String!) {
@@ -32,7 +30,7 @@ async def async_init(self) -> None:
3230 lastUpdate
3331 lastUpdateVersion
3432 devices {
35- deviceType
33+ deviceType
3634 hiloId
3735 physicalAddress
3836 connectionStatus
@@ -530,9 +528,10 @@ async def async_init(self) -> None:
530528}"""
531529
532530 async def call_get_location_query (self , location_hilo_id : str ) -> None :
531+ access_token = await self ._get_access_token ()
533532 transport = AIOHTTPTransport (
534533 url = "https://platform.hiloenergie.com/api/digital-twin/v3/graphql" ,
535- headers = {"Authorization" : f"Bearer { self . access_token } " },
534+ headers = {"Authorization" : f"Bearer { access_token } " },
536535 )
537536 client = Client (transport = transport , fetch_schema_from_transport = True )
538537 query = gql (self .QUERY_GET_LOCATION )
@@ -547,8 +546,9 @@ async def subscribe_to_device_updated(
547546 self , location_hilo_id : str , callback : callable = None
548547 ) -> None :
549548 while True : # Loop to reconnect if the connection is lost
549+ access_token = await self ._get_access_token ()
550550 transport = WebsocketsTransport (
551- url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ self . access_token } "
551+ url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ access_token } "
552552 )
553553 client = Client (transport = transport , fetch_schema_from_transport = True )
554554 query = gql (self .SUBSCRIPTION_DEVICE_UPDATED )
@@ -561,16 +561,17 @@ async def subscribe_to_device_updated(
561561 device_hilo_id = self ._handle_device_subscription_result (result )
562562 if callback :
563563 callback (device_hilo_id )
564- except Exception as e :
564+ except Exception as e :
565565 print (f"Connection lost: { e } . Reconnecting in 5 seconds..." )
566566 await asyncio .sleep (5 )
567567 self .call_get_location_query (location_hilo_id )
568568
569569 async def subscribe_to_location_updated (
570570 self , location_hilo_id : str , callback : callable = None
571571 ) -> None :
572+ access_token = await self ._get_access_token ()
572573 transport = WebsocketsTransport (
573- url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ self . access_token } "
574+ url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ access_token } "
574575 )
575576 client = Client (transport = transport , fetch_schema_from_transport = True )
576577 query = gql (self .SUBSCRIPTION_LOCATION_UPDATED )
@@ -587,6 +588,10 @@ async def subscribe_to_location_updated(
587588 asyncio .sleep (1 )
588589 await self .subscribe_to_location_updated (location_hilo_id )
589590
591+ async def _get_access_token (self ) -> str :
592+ """Get the access token."""
593+ return await self ._api .async_get_access_token ()
594+
590595 def _handle_query_result (self , result : Dict [str , Any ]) -> None :
591596 devices_values : list [any ] = result ["getLocation" ]["devices" ]
592597 attributes = self .mapper .map_query_values (devices_values )
0 commit comments