@@ -590,6 +590,16 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
590590 return
591591
592592 if "data" in response_json :
593+ devices = (
594+ response_json ["data" ].get ("getLocation" , {}).get ("devices" , [])
595+ )
596+ gateways = [
597+ d for d in devices if d .get ("deviceType" ) in ("Gateway" , "Hub" )
598+ ]
599+ LOG .debug (
600+ "Gateway devices in getLocation response: %s" ,
601+ json .dumps (gateways , indent = 2 ),
602+ )
593603 self ._handle_query_result (response_json ["data" ])
594604
595605 async def subscribe_to_device_updated (
@@ -790,12 +800,55 @@ async def _get_access_token(self) -> str:
790800 return await self ._api .async_get_access_token ()
791801
792802 def _handle_query_result (self , result : Dict [str , Any ]) -> None :
793- """This receives query results and maps them to the proper device ."""
803+ """Handle the result of the GraphQL query for location and devices ."""
794804 devices_values : List [Dict [str , Any ]] = result ["getLocation" ]["devices" ]
805+
806+ for raw_device in devices_values :
807+ if raw_device .get ("deviceType" ) in ("Gateway" , "Hub" ):
808+ if self ._devices .find_device (1 ) is None :
809+ gw = self ._build_gateway_dict (raw_device )
810+ LOG .debug ("Creating gateway device from GraphQL: %s" , gw )
811+ gw_dev = self ._devices .generate_device (gw )
812+ if gw_dev not in self ._devices .devices :
813+ self ._devices .devices .append (gw_dev )
814+
795815 attributes = self .mapper .map_query_values (devices_values )
796816 self ._devices .parse_values_received (attributes )
797817
818+ def _build_gateway_dict (self , raw_device : Dict [str , Any ]) -> Dict [str , Any ]:
819+ """Build a dictionary representing the gateway device from raw GraphQL data."""
820+ hilo_id = raw_device .get ("hiloId" , "" )
821+ parts = hilo_id .split (":" )
822+ mac = parts [3 ] if len (parts ) > 3 else None
823+ if mac is None :
824+ LOG .warning ("Unable to extract MAC from hiloId: %s" , hilo_id )
825+
826+ connection_status = raw_device .get ("connectionStatus" )
827+ return {
828+ "name" : "Hilo Gateway" ,
829+ "type" : "Gateway" ,
830+ "category" : "Gateway" ,
831+ "id" : 1 ,
832+ "identifier" : mac ,
833+ "sdi" : mac ,
834+ "provider" : 1 ,
835+ "model_number" : "EQ000017" ,
836+ "sw_version" : raw_device .get ("controllerSoftwareVersion" ),
837+ "supportedAttributes" : "zigBeePairingActivated, zigBeeChannel, firmwareVersion, onlineStatus, lastStatusTime, disconnected" ,
838+ "settableAttributes" : "" ,
839+ "Disconnected" : {"value" : connection_status != "CONNECTED" },
840+ "zigBeePairingActivated" : {
841+ "value" : bool (raw_device .get ("zigBeePairingModeEnhanced" ))
842+ },
843+ "zigBeeChannel" : {"value" : raw_device .get ("zigBeeChannel" )},
844+ "firmwareVersion" : {"value" : raw_device .get ("controllerSoftwareVersion" )},
845+ "onlineStatus" : {"value" : connection_status },
846+ "lastStatusTime" : {"value" : raw_device .get ("lastConnectionTime" )},
847+ "disconnected" : {"value" : connection_status != "CONNECTED" },
848+ }
849+
798850 def _handle_device_subscription_result (self , result : Dict [str , Any ]) -> str :
851+ """Handle the result of the GraphQL subscription for device updates."""
799852 device_value : Dict [str , Any ] = result ["onAnyDeviceUpdated" ]["device" ]
800853 attributes = self .mapper .map_device_subscription_values (device_value )
801854 updated_device = self ._devices .parse_values_received (attributes )
@@ -804,6 +857,7 @@ def _handle_device_subscription_result(self, result: Dict[str, Any]) -> str:
804857 return str (device_value .get ("hiloId" ))
805858
806859 def _handle_location_subscription_result (self , result : Dict [str , Any ]) -> str :
860+ """Handle the result of the GraphQL subscription for location updates."""
807861 location_value : Dict [str , Any ] = result ["onAnyLocationUpdated" ]["location" ]
808862 attributes = self .mapper .map_location_subscription_values (location_value )
809863 updated_device = self ._devices .parse_values_received (attributes )
0 commit comments