I spent many hours trying to update Home Graph in real-time in more than 1 device.
Actually this project have 2 devices: 1 washer and 1 lamp.
The washer is perfect, real-time update when I make changes direct in database.
The lamp is the problem. If I make changes direct in database it just updates if I close the device page and open again (on Google Home App, of course). It is not updating in real-time.
I believe the problem is in this code below, because I don't know how many devices it is calling in [context.params.deviceId], but I'm not sure if this is the problem or how to solve it.
Please help me with that. I spent all night in this for nothing.
Many thanks in advance.
PS: I've followed that tutorial to make the washer work perfectly. https://codelabs.developers.google.com/codelabs/smarthome-washer/
UPDATE:
I tried something new:
I used all the code exactly as the tutorial on the link above and changed only the action device type action.devices.types.WASHER
to action.devices.types.LIGHT
or action.devices.types.SWITCH
After this test I realized that the real-time ReportState is only working with the washer and not working with the light or the switch even changing only one line of code.
So it looks like the problem is on Google, not in the code. Does anyone agree?
/**
* Send a REPORT STATE call to the homegraph when data for any device id
* has been changed.
*/
exports.reportstate = functions.database.ref('{deviceId}').onWrite(async (change, context) => {
console.info('Firebase write event triggered this cloud function');
const snapshot = change.after.val();
const requestBody = {
requestId: 'ff36a3cc', /* Any unique ID */
agentUserId: '123', /* Hardcoded user ID */
payload: {
devices: {
states: {
[context.params.deviceId]: {
on: snapshot.OnOff.on,
},
},
},
},
};
const res = await homegraph.devices.reportStateAndNotification({
requestBody
});
console.info('Report state response:', res.status, res.data);
});