Connection to Google IoT Core

Hi All,

New to Home Assistant excited to be part of this community. My first post so apologies if this is in the wrong category etc.

I had a question about MQTT functionality I wondered if anybody could help with. I am trying to connect to Google Cloud IoT Core so I can push my sensor values into a bigger cloud database. When going through the docs I am stuck on the password authentication. Google IoT Core requires the password to a JWT. Is this possible to do with the current MQTT setup or would i need to get under the hood a bit more and create a bespoke MQTT driver? The current setup is copied in below:

  port: 8883
  client_id: "projects/PROJECT_ID/locations/REGION/registries/REGISTRY_ID/devices/DEVICE_ID"
  certificate: /config/keys/roots.pem
  username: "unused"
  password: "THIS SHOULD BE A JWT"

Has anybody had any experience connecting to Google IoT core at all?



I am trying to do this myself, have any luck. My guess is that this won’t be possible as the JWT must be constantly refreshed and there is no way to do this by simply adding the JWT to the config.

I have managed to get it working but its a hack and not an optimal solution.

What i did in the end was to get the mqtt directory off the github repo into my custom_components directory so my home-assistant would use that and not the default. From there I could modify the code. I then took the python code to generate a JWT off the google website and added it into the mqtt class in the file. This needed some extra inports putting into to drag in the JWT libraries. Once done I modified the line that took the password to take that instead of look at the password line from the config file.

The drawback is that it only generates a new JWT on startup so to get around this I have made the JWT expiry 23 hrs (the max accepted by IoT Core is 24 Hrs i think). I have had added a script in to restart my home-assistant every 22 hours to regenerate the key.

As i said this is not optimal and the parameters to generate the JWT are hard-coded into the python but it does work and I am sending up both state and event data successfully.

At some point I will look at setting it up properly but I am pretty new to home-assistant and haven’t had the time to wade through the developer documentation yet.

Happy to share the code tweaks etc if this will help?

I decided to try a different approach which is to use Node Red add-on to connect to Google IoT Core. Node Res has a node trust handles the authentication for you. However, I ran into some issues because now Google requires a RS256_X509 key for devices and the Node for Google IoT only support RS256. I have not found a work around or updated version for Node Red.

Since my ultimate goal is actually to get data from HA into Firebase for my project, I am now trying to use Node Red add-on with a node that authenticates with Firebase using the Admin SDK JSON for authentication. This seems easier for my requirements but I am currently encountering an issue with the node I am using to talk to Firebase.

I will report back my results so that maybe this can help others. I do think going the Node Red route could provide the best solution as it has hooks to read and right data from HA easily and there seems to be more integrations with Google IoT core.

1 Like