'Smart home report state SYNC new device | Requested entity was not found. [404]

I've implemented the HomeGraph API with the help of the package Google.Apis.HomeGraphService.v1 (1.50.0.2260)

It seems to work fine as well, the ReportStateAndNotification function works fine on the query, execute, and some sync requests.

But when I add a new device to my system through our app and a SYNC request is sent to Google and comes in our backend, the HomeGraph API will return an exception when sending this sync request..

-> The sync request does not throw an exception when I modify a device name in our app. It only occurs when new devices are added.

I've searched through google and multiple StackOverflow posts.. But I'm probably missing something. Most posts say check the API key etc but then the ReportStateAndNotification function should always fail, not only when the sync request comes from Google to our backend. Could anyone point me in the right direction?

Function that is used for sync requests:

public static void Send(Dictionary<string, object> deviceStateList, string requestId, string googleCustomerId)
    {
        string deviceIdList = String.Format("({0})", string.Join(", ", deviceStateList.Keys));
        try
        {
            var jsonFilePath = _appSettingsRetriever.PrivateGoogleAuthenticationFile;
            string scope = "https://www.googleapis.com/auth/homegraph";

            using (var stream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read))
            {
                GoogleCredential credentials = GoogleCredential.FromStream(stream);
                if (credentials.IsCreateScopedRequired)
                    credentials = credentials.CreateScoped(scope);

                HomeGraphServiceService service = new HomeGraphServiceService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credentials
                });

                var request = new ReportStateAndNotificationRequest
                {
                    AgentUserId = googleCustomerId,
                    RequestId = requestId,
                    Payload = new StateAndNotificationPayload
                    {
                        Devices = new ReportStateAndNotificationDevice
                        {
                            States = deviceStateList
                        }
                    }
                };                  
                _log.Debug($"Sending to HomeGraph for devices: {deviceIdList} customer: {googleCustomerId} requestId: {requestId}");
                DevicesResource.ReportStateAndNotificationRequest rp = service.Devices.ReportStateAndNotification(request);
                ReportStateAndNotificationResponse resop = rp.Execute();
            }
        }
        catch (Exception ex)
        {
            _log.Error($"Exception in ReportToHomeGraph for Customer: {googleCustomerId}. DeviceList: {deviceIdList}. JsonPath: {_appSettingsRetriever.PrivateGoogleAuthenticationFile} Exception: {ex}.");
        }
    }

Exception:

2021-09-24 14:16:13,547 [110] ERROR ReportToHomeGraph                  
 Exception in ReportToHomeGraph for Customer: 05. DeviceList: (
 fe965e6a-21ad-425f-b594-914bf63510a9, 
 1cc0ee97-a87f-44c5-a3e3-a39d159ee193, 
 618cdf94-2b31-434f-b91e-00837d155d4a
 ).
 JsonPath: C:/myfile.json Exception: The service homegraph has thrown an exception:
 Google.GoogleApiException: Google.Apis.Requests.RequestError
Requested entity was not found. [404]
Errors [
    Message[Requested entity was not found.] Location[ - ] Reason[notFound] Domain[global]
]

   at Google.Apis.Requests.ClientServiceRequest`1.<ParseResponse>d__35.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()
   at BusinessLogic.GoogleAssistant.TokenService.HomeGraph.ReportToHomeGraph.Send(Dictionary`2 deviceStateList, 
   String requestId, String googleCustomerId) in C:\Repos\GoogleAssistant
   .TokenService\HomeGraph\ReportToHomeGraph.cs:line 57.


Solution 1:[1]

When users add a new device, the first step you need to do is to issue a Request Sync to Google. This indicates the set of devices for that user has changed, and you need a new Sync request to update the data in homegraph. Google will follow this by delivering a Sync intent to your fulfillment endpoint, which you can respond with the updated set of devices.

Getting a 404 when calling Request Sync might indicate your Service Account Key might be invalid, or the agent user id you target might be wrong. Otherwise getting an error for your Sync Response might indicate it’s structured incorrectly. You can find out more about how to structure it in our examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anish Yadav