jpcornil-git / HA-mcp23017

MCP23017 implementation for Home Assistant (threadsafe, lower latency and config flow support)
16 stars 6 forks source link

Getting I/O errors - probably problem between chair and the keyboard #4

Closed darmach closed 2 years ago

darmach commented 2 years ago

Hi, I have HA OS installed on Rpi3B, and MCP23017 connected to i2c-1 bus (if I didn't mix something up - connected data pins to those right next to 5V and GND on Rpi3B GPIO)

With HA-mcp23017 installed via HACS I do get errors below in the logs:

2022-02-28 23:46:12 ERROR (SyncWorker_5) [custom_components.mcp23017] Unable to access mcp23017-0x23 ([Errno 121] Remote I/O error)
2022-02-28 23:46:13 ERROR (SyncWorker_5) [custom_components.mcp23017] Unable to access mcp23017-0x23 ([Errno 121] Remote I/O error)
2022-02-28 23:46:13 ERROR (SyncWorker_3) [custom_components.mcp23017] Unable to access mcp23017-0x23 ([Errno 121] Remote I/O error)
2022-02-28 23:46:15 ERROR (SyncWorker_5) [custom_components.mcp23017] Unable to access mcp23017-0x23 ([Errno 121] Remote I/O error)

Tried to test this with apk installed i2c-tools:

[core-ssh ~]$ apk add i2c-tools
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/aarch64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/aarch64/APKINDEX.tar.gz
(1/1) Installing i2c-tools (4.2-r1)
Executing busybox-1.33.1-r6.trigger
OK: 90 MiB in 100 packages
[core-ssh ~]$ ls -l /dev/i2c-1
crw-------    1 root     root       89,   1 Oct 12 17:24 /dev/i2c-1
[core-ssh ~]$ i2cdetect -y 1
Error: Could not open file `/dev/i2c-1': Operation not permitted
[core-ssh ~]$ whoami
root

I suspect I don't have access to this device because of container separation (this was executed in HA ssh, so in ssh addon container I guess?)

Can you advise what I might be doing wrong?

jpcornil-git commented 2 years ago

Hi @darmach,

I'm not used to HAOS (using a core install) but your issue is definitively related to access rights to the i2c bus within the container running HA, cfr. L188 (and ssh). A few questions:

pi@RPiv4:~ $ grep i2c /boot/config.txt
dtparam=i2c_arm=on
pi@RPiv4:~ $ lsmod | grep i2c
i2c_bcm2835            16384  2
i2c_dev                20480  4

I can give a try to HAOS using a spare RPi4B or a QEMU machine but not before next week.

darmach commented 2 years ago

Thanks for the response @jpcornil-git !

I enabled it manually, logging on physical console as root to get the access to /mnt/boot

In config.txt I enabled only i2c_arm - I wanted to get only the i2c-1 header active, the one (I hope I found that right) next to 5V and GND pins.

I can see i2c modules loaded:

[core-ssh ~]$ lsmod | grep i2c
i2c_dev                20480  8
i2c_bcm2835            16384  4

(Hmm, but that's a suspiciously high device count for Rpi3, right? 8 and 4?)

Also the device is visible in /dev:

[core-ssh ~]$ ls -l /dev/ | grep i2c
crw-rw-rw-    1 root     root       89,   1 Oct 12 17:24 i2c-1

No hurry at all, if you could find some time next week to test this it would be awesome!

darmach commented 2 years ago

Hi @jpcornil-git, I tested this a bit today, ran a --privileged ubuntu container on the side, and was able to i2cdetect the addres without problem.

Looks like the root cause is HA container privilege separation, can we somehow safely (or not) work around it while still using HASSOS? Else I will have to think about migration to Some Rpi linux distribution and running HA on top of that...

jpcornil-git commented 2 years ago

Note that I see here that dtparam=i2c1=on and dtparam=i2c_arm=on should be added to config.txt

jpcornil-git commented 2 years ago

From what I can see HA OS supervisor is running homeassistant container in privileged mode already, cfr. https://github.com/home-assistant/supervisor/blob/main/supervisor/docker/homeassistant.py#L133 => likely only a matter of enabling i2c in HA OS as described above