Using the File Editor add-on API to automate file edition: working with Ingress

Hello!
I have two Home Assistant installations which I’d like to configure in bulk; both of them are Raspberry Pi HassOS. So far, I’ve experimented with the File Editor add-on API to read and write files from a script, efectively emulating the front-end.

I managed to do so in a trial Docker on Windows installation:
curl http://192.168.0.47:3218/api/file?filename=/config/automations.yaml for reading.
curl -d "filename=/config/automations.yaml&text=$(cat automations.yaml)" http://192.168.0.47:3218/api/save for writing.

However, when I tried to do the same in my Raspberry Pi installations, I ran into some differences:

  • The request URL now looks like http://192.168.0.138:8123/api/hassio_ingress/KhCFTsCcTBwgtZLDBFrEYEyuJOD5TvDjsNWsJwBHyMw/api/file?filename=/config/automations.yaml.
    I assume the string after hassio_ingress is some kind of add-on identificator, as it doesn’t vary between sessions or users.
  • There’s a cookie named ingress_session.

I’d like to know what to do to get those values, or if there is an alternative way of making this work. I searched for Ingress documentation and only found this. I also checked out the File Editor/Configurator add-on page, but couldn’t find anything there regarding these.

I’m running Home Assistant 0.115.4, HassOS 4.13 and File Editor 5.1.0.

Thanks in advance!

It should be the same as

and

Thank you for your reply.
I get connection refused if I try that on my HassOS installation. My knowledge of Ingress, HassOS and add-ons is limited; but as far as I know, there wouldn’t be any application listening on 3218 on a HassOS installation, am I correct?

I’m not sure, I don’t know enough about the file editor. I do know that ingress is just used to serve up the UI and nothing more. The file editor could be watching on that port, it really depends on how the addon was built. It could also be built into the HA api. Have you tried the command against port 8123?

I doubt that the api is inaccessible. The addon mentions using the API, just doesn’t provide examples how to use the api. I think you just need to find the port and you’ll be good to go.

Looking through the setup… I see port 8099 being used. Don’t know if that’s for the API.

1 Like

I did a quick trial and it hangs when I request to 8099, but I think it’s worth further investigation. Thank you for your replies!

Supposedly, from what I’ve read in “Presenting your addon”, port 8099 is where the add-on should provide the web interface - so I guess the API is available there, but it also says it only accepts connections from the container.
I also read this in the add-on documentation:

  • This add-on can only be used via Ingress and has no direct access.

And:

This add-on is allowed to access your running Home Assistant instance directly via the Home Assistant API. This mode handles authentication for the add-on as well, which enables an add-on to interact with Home Assistant without the need for additional authentication tokens.

This makes me think that, effectively, the add-on’s API can only be reached through the Home Assistant API, on 8123 and going through Ingress.

Just talked with the dev. Unfortunately, the addon is not built for this use case.

Thank you.

Unfortunately, the addon is not built for this use case.

But that doesn’t mean you can’t use it that way, does it? It seems that if I can figure out how to create valid values for those two Ingress codes, I can access the add-on API.

The command

curl --cookie "ingress_session=0fc5fefd26cb1ef0910a12d97edd3670e9d3319000e972676ba2778ec93140ec9cfe54b3b9cca5484dc402534f0f401c34f867fb5e4a44506442cb08e573447c" http://192.168.0.138:8123/api/hassio_ingress/KhCFTsCcTBwgtZLDBFrEYEyuJOD5TvDjsNWsJwBHyMw/api/file?filename=/config/automations.yaml

returned, indeed, the correct contents of automations.yaml.

Maybe an alternative approach to not having to mess with ingress: how about creating a custom add-on for the configurator that doesn’t use ingress? A while back I did publish the standalone configurator here. I’m unsure if that still works in the current state (haven’t updated it in a while, and it’s not using the latest packaged version). But it might be a good starting point. That way you could just let it run on some port and use basic HTTP authentication. :man_shrugging:

2 Likes

Yes, it does indeed work. I tried it with a Docker over Windows HA installation not too long ago, and that does allow me to read and write files with simple commands. I’ll explore that alternative. Thank you!

So I solved the Ingress issue! This curl returns a JSON message containing the value of a valid Ingress cookie.

curl "http://<HA host>:<HA port>/api/hassio/ingress/session" \
  -X "POST" \
  -H "Connection: keep-alive" \
  -H "Content-Length: 0" \
  -H "Pragma: no-cache" \
  -H "Cache-Control: no-cache" \
  -H "authorization: Bearer <access token>" \
  -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" \
  -H "Accept: */*" \
  -H "Origin: http://<HA host>:<HA port>" \
  -H "Referer: http://<HA host>:<HA port>/core_configurator" \
  -H "Accept-Language: es-ES,es;q=0.9" \
  --compressed \
  --insecure
1 Like

I generated a long lived access token to use it with the command but I’m getting 401 unauthorized response any help?