Homeassistant core on Android (Guide Dec'22)

Hey there!

So i’ve been charting my holiday journey getting started with Homeassistant, which started with a few youtube videos and reading the documentation for installation onto a Raspberry PI.
Unfortunately it seems that at the moment Pi systems are basically not available, and the recommended Pi4 goes for about £100 on eBay, which seems a lot of money for a Pi ! So looking around I found the excellent guide by Alexanderhale (Home Assistant Core on Android Tablet) which proved a very VERY good place to start! kudos to alexander for setting all that out. Unfortunately my journey wasn’t quite as simple as just following the guide, as I think various things have changed since it was written. Hence, I’m writing up my installation experience in case it helps anyone else.

Sorry for the hxxps links - as a new user it wont’ let me put many links in the post…

My starting point:

  • Samsung Galaxy Note 20 Ultra (recently replaced with a Pixel 7 Pro, hence a decent powerful device for the installation) Android 13 (Phone stock OS)
  • Google Nexus 9 tablet, Android 7.1.1 (Tablet stock OS). This is hopefully going to become a dashboard device, but is being used at the moment to trial these installation steps on a much older android version

I have successfully installed HA on both of these devices, so I believe that installation of a custom ROM like LineageOS as suggested by Alexanderhale may only be required on much older devices, i’m not sure. Of course, if that’s your kind of thing then knock yourselves out :slight_smile:

Preparation

  • Installation of F-Droid software repository (steps detailed in Alexanderhale’s guide, I won’t repeat them here)
  • Installation of Termux
  • no ROOT access (decision born of practicality rather than any objection to rooting, I have a feeling that rooting may some some issues though!)

Procedure
From a clean install of termux

pkg updates
pkg upgrade

When running pkg updates, it will ask a number of times if you want to keep you existing config (option ‘n’) or update. I always opted to keep existing config, which is apparently the default option.

NOTE: At the time of writing (Dec’22), this installs Python 3-11. This is a PITA, because homeassistant (currently version 2022.12.9 on pipy) only states that it supports Python 3.10 (hxxps://github.com/home-assistant/core/issues/84117). Hey ho. Termux apparently doesn’t keep old versions of packages to save on hosting costs. Some workarounds to get old termux packages are listed here (hxxps://stackoverflow.com/questions/64853742/need-to-run-python-3-8-x-on-termux-on-android-currently-installed-with-python-3) but to be honest, I couldn’t be bothered with them, so I just got it working on Python 3.11. Boom!

pkg install python nano openssh termux-api make libjpeg-turbo
pkg install dbus-python python-apsw python-apt python-bcrypt python-cryptography python-numpy python-pillow python-tkinter python-tldp python-xcbgen
pkg install binutils rust wget 
pkg install c-ares

The first series of these are as recommended by alexanderhale’s wiki page.
The second lot is several termux-specific python packages (more on this later)
Third is binutils and rust, which are required to compile some homeassistant dependency packages now, and wget for grabbing some packages from t’internet.
Lastly c-ares is required by pycares which is required by homeassistant somewhere (but doesn’t seem to be a dependency, I only found out by running HA)

Next we get and unpack couple of packages that can’t be installed directly by pip.

wget https://files.pythonhosted.org/packages/01/50/e3015e6e03a3cf64113f509e8b86b71af37169b59ccedfcb182f3d031329/pycares-4.3.0.tar.gz
wget https://files.pythonhosted.org/packages/ff/4f/62d9859b7d4e6dc32feda67815c5f5ab4421e6909e48cbc970b6a40d60b7/aiohttp-3.8.3.tar.gz
tar -xvf pycares-4.3.0.tar.gz
tar -xvf aiohttp-3.8.3.tar.gz

Next, we create a python virtual environment for homeassistant to run in. This isn’t strictly necessary, but I followed alexanderhale’s guide and it seems to work well at isolating the HA installation, even if it does bring a couple of headaches that need to be fixed. This step is optional really.

python -m venv hass
source hass/bin/activate

We’re now in our nice new virtual environment.

The first thing that I do is to copy all of the python packages we downloaded earlier into this venv. If we didn’t create a venv this would not be necessary, so this is not necessary if you didn’t opt to create a venv.

This appears to be necessary because there a some oddities in termux which are fixed by using the termux-specific python packages. The package that lead me to this is numpy (incidentally another package that’s not a ‘dependency’ but you get immediate errors if you try to execute without it), because for the life of me I could not get a compatible version installed and compiled using pip. However copying the termux-specific package worked brilliantly.

cp -r ../usr/lib/python3.11/site-packages/* ./hass/lib/python3.11/site-packages/

Now comes the fun bit.

Aiohttp
This package was a complete pain, and one that I never solved satisfactorily, due to a tangled web of dependencies and installation errors. homeassistant requires aiohttp==3.8.1, which simply just WOULD NOT compile on my termux-python3.11 system.
However, I noticed in the aiohttp documentation that aiohttp is built to be backwards-compatible, which gave me a cunning idea…
The below commands extract the aiohttp 3.8.3 files (downloaded from pipy earlier) and rename the package version to be 3.8.1 instead, and then compile and install the hacked package. Following this, it’s recognised by the system as version 3.8.1, the HA installation proceeds nicely, and everything seems to work nicely.

Side note - i’m aware this is horrible and is good justification by itself for performing the installation in a venv.

cd aiohttp-3.8.3
sed -i s/3.8.3/3.8.1/g PKG-INFO
sed -i s/3.8.3/3.8.1/g  aiohttp/__init__.py
sed -i s/3.8.3/3.8.1/g aiohttp.egg-info/PKG-INFO
python setup.py install
cd ..

pycares
Pycares installation isn’t quite as much of a hack as aiohttp, but it requires some mods to source code to be compatible with this setup.
pycares will be installed by homeassistant during installation, but I found that if you don’t do this modification then when you boot homeassistant you get constant errors. Better to fix the problem at source (literally). The solution was given here (hxxps://github.com/saghul/pycares/issues/78#issuecomment-445508989), noting that the source file is now in a slightly different place, that answer must be old.

cd pycares-4.3.0
sed -i 's/#define HAVE_GETSERVBYPORT_R 1/\/\/#define HAVE_GETSERVBYPORT_R 1/g' deps/build-config/config_android/ares_config.h
python setup.py install
cd ..

Next, set some compilation flags required to compile homeassistant dependencies successfully. (Otherwise orjson and cryptography refuse to build)

export RUSTFLAGS=" -C lto=no"
export CARGO_BUILD_TARGET="$(rustc -vV | sed -n 's|host: ||p')"

Getting there. Install wheel and tzdata (once again, tzdata is not a dependency, but you cannot complete setup if you don’t have it installed. Solution was found here(hxxps://community.home-assistant.io/t/time-zone-error-while-installing-need-help/318807/5))
PyTurboJPEG weird installation is to cope with differences between android and linux, there seems to be some differences in how to test for the presence of the mathlib.

pip install wheel
pip install tzdata
MATHLIB=“m” pip install PyTurboJPEG==1.6.7

And you’re ready to rock.

Pip install homeassistant

Note: last time I did this I got a file permission error due to the OS halfway through installation… i just started the installation again and it worked nicely.

This setup seems to be working just about perfectly for me - hope the guide helps anyone else who’s thinking of trying this.

9 Likes

“Your scientists were so preoccupied with whether they could, they didn’t stop to think if they should.”

IMO this is not a good idea. A tablet is not a server.

Though congrats for getting it working.

This is a bottomless pit, in every update, you will face many other new challenges. I had setup my first environment like this even with a zigbee dongle but life was hard :slight_smile:

Congrats

lol… you might be right…

It was my xmas project (having never heard of HA before), and I didn’t have a Raspberry Pi lying around, but I did have a phone. Getting it up and running took so much forum-browsing looking at weird errors that it felt compelling to write it all down - especially since I got to the point where I have a single script to do all the hacks and installation. It might help some other poor soul !

FWIW, The tablet is never intended to be a server. The phone is far more powerful and seems to work extremely well, for what I need of it, which is simply evaluation of homeassistant to see if it’s something i’d like to continue with. If so, i’ll probably try to grab a 2nd hand Pi off ebay, but they are expensive at the moment, and I have the phone just lying about…

As much as anything, I was hoping that maybe someone who knows much more than me might pick up on some of the comments (e.g. tzdata not being a dependency, but being needed for HA installation to work) and see if anything needed updating in the installation configuration. I wouldn’t dare to suggest mods to the actual source as a newbie user…

1 Like
  1. chroot into ubuntu (root required)
  2. follow official guide
  3. profit

I’ve done the whole installation without errors! Thanks for that.
But how to fire up HA now?
http://homeassistant.local:8123 does not work.
Do i miss something?
TIA,
Rick

Just like you, I installed homeassistant without errors, and just like your service does not start. Port 8123 is empty.

To start homeassistant run the command
hass

Everything was fine, but on 02/08/2023 a Python update was released. After that, the hass virtual environment ceased to be created.
python -m venv hass
An error occurs:
Error: Command ‘[’/data/data/com.termux/files/home/hass1/bin/python’, ‘-m’, ‘ensurepip’, ‘–upgrade’, ‘–default-pip’]’ returned non-zero exit status 1.
Maybe someone knows how to solve this problem?

1 Like

Thanks for the instruction.
I was able to run Home Assistant server on Xiaommi Mi A2 Lite (android 10).

But I got a problem. When the Internet is lost, a lot (tens of thousands per minute) of error messages of the form begin to be displayed in the log:

│2023-02-20 01:15:17.391 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback BaseSelectorEventLoop._accept_connection(<aiohttp.web_... 0x784353a2d0>, <socket.socke...40000, 0, 41)>, None, <Server socke...00, 0, 41)>,)>, 128, None, None)                                                                                             │
│Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                            │
│  File "/data/data/com.termux/files/usr/lib/python3.11/asyncio/events.py", line 80, in _run                                                                                                                                                                                                                                                                   │
│    self._context.run(self._callback, *self._args)                                                                                                                                                                                                                                                                                                            │
│  File "/data/data/com.termux/files/usr/lib/python3.11/asyncio/selector_events.py", line 165, in _accept_connection                                                                                                                                                                                                                                           │
│    conn, addr = sock.accept()                                                                                                                                                                                                                                                                                                                                │
│                 ^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                │
│  File "/data/data/com.termux/files/usr/lib/python3.11/socket.py", line 294, in accept                                                                                                                                                                                                                                                                        │
│    fd, addr = self._accept()                                                                                                                                                                                                                                                                                                                                 │
│               ^^^^^^^^^^^^^^                                                                                                                                                                                                                                                                                                                                 │
│OSError: [Errno 22] Invalid argument                                                                                                                                                                                                                                                                                                                          │

I saw such issue on GitHub. But there is no solution.
Can someone suggest how to fix this?

1 Like

Just installed HA using your methode on my old phone (galaxy s7 with stock android 8) it did surprisingly work. but i’m wondering if this installation is some what reliable for a simple home with some automations or will it break at some point!

1 Like

Hi
I have installed but I’m not understanding how I start homeassistant, can you please give an example, how to start? best regards

You should run the hass command on the command line.

Hi,
many thanks for your answer.
when I run hass I get

~ $ hass
No command hass found, did you mean:
Command has in package has
Command pass in package pass
~ $

You must first activate the virtual environment

~ $ source ~/hass/bin/activate
(hass) ~ $ hass

Hi
That was the piece that was missing. Now HA is starting. Many thanks for your help

Best regards

Hello, thank you very much for that great tutorial. The setup went just fine, but when I start Homeassistant in the virtual environment I get the following error code:

PermissionError: [Errno 13] Permission denied: '/proc/stat/'

And the initial setup of hass halts.

Does anyone have an idea how to solve this?

Best regards

Hi Ramblingman

I want to thank you very much for your tutorial and for sharing it. Best regards

This error occurs due to access restrictions in new (10+) versions of android.
Here is an instruction on how to get around a similar error for /proc/net/psched. In the same way (by calling the command through adb shell), you can bypass the error with /proc/stat.

1 Like

I notice that HA installed in this way don’t has the addon store under settings. How can I have this store in this installation? best regards

1 Like