Proof of concept: "integrate" closed/proprietary devices with Android emulator and Maestro

This is a proof of concept I just got working. It’s not complete and I could use the feedback - maybe there are better approaches or someone can contribute improvements.

This is all tested on a physical Linux server. I used a second nic to add local LAN access.

The Android apps I needed all required some kind of local/broadcast access. If you’re 100% sure your app is cloud based you could skip the local lan parts.

Rationale: I have devices with broken integrations or have other issues, incomplete integrations etc.
Some of this can help with reverse engineering/debugging as well. Ideally vendors would have open APIs and documentation but that’s not always the case.

  1. Install an Android emulator. I tried several but I settled on redroid as I already use Docker and it’s well suited to running on a server. It also supports Arm apks which is great because I found a lot of IoT/automation apps do not support x86_64.

I tried:

  • Anbox - old/unmaintained, works with Intel apks only. GUI based - not great for headless
  • Waydroid - better support, including Arm but needs Wayland/GUI - not great for headless
  • Anbox Cloud - interesting but complicated and doesn’t support Arm out of the box. “Free” for personal use/Ubuntu pro. Does anyone know if Amzon Luna is using this? Headless/web based
  1. Setup bridge networking for local lan access.
  • Docker needs a networking plugin installed.
  • Docker/android networking was a bit trickier but it’s working with steps here
  • Anbox/Waydroid: you can add the second nic to the anbox0 or waydroid0 bridge as with Docker
  1. Test Android
  • Docker/local LAN it will get its own up address you can check with docker container inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $id
  • Docker: connect ipaddress:5555 should connect
  • Check networking with adb shell
  • Install and test your apps/apks
  • scrcpy is a must have for testing Docker android since there’s no other gui. Great for other things like viewing Android/Amazon tv boxes too.
  1. Install Maestro mobile UI framework
  • Maestro was really straightforward to get up and running with instructions here.
  • Maestro needs open-jdk11
  • maestro studio should start up and detect the device if adb is working.
  • Browse to http://localhost:9999
  • Click and record/run actions. You can export these to a yaml flow
  1. Create a Home Assistant Dashboard and Card
  • Default aspect ratio 50% is way too small so increase it! I gave it it’s own dashboard
  • You should be able to see and control the Android device
  • Maestro studio is a bit limited but you can select actions and run them
  • maestro studio uses http so I had to browse to the http:// port of my homeassistant for this to work
  1. TODO
  • there’s a lot left to package this up cleanly but it’s a start
  • I need to get maestro in its own container and setup through ha-proxy so https will work
  • maestro studio is an easy way to get started but the maestro cli could be tied in to homeassisitant. Maybe appium.io is a better approach if you really want to roll up your sleeves.
  • Hopefully there’s a way to look at status/results and not just playback buttons
  • SECURITY: the emulator can be booted without root access and can probably run the Docker container with fewer privileges. Maybe more can be tightened up.

2 Likes

Great work!
I thought of controlling all the “things” I couldn’t integrate via an android emulator, but never had the courage/time to get to it…

How stable does your setting proved to be?
Have you managed to get some “feedback”? (screenshots, OCR…)

I’ll probably try it as well and post my feedback as well! This kind of solution would clearly allow to “integrate” all imaginable IoT stuffs!

Interesting, thanks for sharing (didn’t know redroid yet)

Alternatively a real android device in combination with ws-scrcpy (“Web client prototype for scrcpy”)
might be an interesting approach for an integration as well.

edit: I gave it a quick try using a ws-docker container built from this fork: GitHub - cloudswave/ws-scrcpy: Web client prototype for scrcpy. and adding the “landing page” of the running instance to home assistant using GitHub - lovelylain/hass_ingress: Home Assistant ingress feature, add additional ingress panels to your Home Assistant frontend..
It worked fine, but when starting a stream session from the landing page, it opens in a separate window (it works fine apart from that, at least in the internal network, didn’t work through nabu casa). directly using the stream session url in hass_ingress leads to a white screen.
no idea if this could be fixed with some effort.

Maybe someone is interested in this brand-new project:

Unlike above ws-scrcpy (didn’t know it uses a forked modified scrcpy 1.x before) it uses standalone vanilla scrcpy /2.6.1.

Its new “Nicegui Web Interface” (since v1.3.6) works excellent already.

I managed to create a functional Dockerfile file, but no home assistant integration (yet?)

Good morning!
I was wondering if this ever got packaged. I have a use case where having a virtual android instance running in the background would be helpful.
Thanks.