Search code examples
google-cloud-platformgoogle-cloud-firestoregoogle-cloud-functionsactions-on-googlegoogle-smart-home

Smarthome AOG. The best way (in 2021) to integrate state of devices with firestore fields


I have this project: https://github.com/neuberfran/firebasefunction/blob/main/firebase/functions/smart-home/fulfillment.js It works well. But, for example, I want to implement a condition that if I have the garage closed and I said "close garage", the Home assistantt will alert me about it.

As shown in the photo below, I am using an rpi3/iot-device/back-end that controls the garagestate field. I need to know the best way to implement this condition, that is, read the value of the garagestate field and from that, know if I can open the garage or not:

enter image description here


Solution

  • You'd probably need to add an intermediary condition in your onExecute to return an error based on the Firestore state:

    // ...
    for (const target of command.devices) {
        const configRef = firestore.doc(`device-configs/${target.id}`)
        const targetDoc = await configRef.get()
        const {garagestate} = targetDoc.data()
        if (garagestate === false) {
           // garagestate exists and is false
           // return an error
           return {
             requestId,
             payload: {
               status: 'ERROR',
               errorCode: 'alreadyClosed'
             }
           }
        }
        // ...
    }
    // ...