DietPi/PiFace/Home Assistant help required

Hello forum!

I require some assistance with a problem I have been battling with for a few days now with no success.
In short I am trying to control my PiFace Digital II board from within HASS running on the latest version of DietPi.
First of all, I have previously installed the PiFace and Home Assistant on HASSBIAN and it worked like a charm (after changing SPI speed) so I have had this working before. I wanted to try DietPi out and I have installed Home Assistant (#157) and it is working. I have installed both the pifacecommon and pifacedigitalio requirements for PiFace and I am able to control the PiFace board (simple python scripts) outside of the Home Assistant environment (pyenv) but it does not work within it.
Last night I had an inspiration based on the description for Home Assistant “Customise Python environment:”.

If you need to install additional Python modules into this pyenv environment, update Python itself or similar, you need to open a shell as user homeassistant and activate the pyenv environment:
sudo -u homeassistant bash
. /home/homeassistant/pyenv-activate.sh
pip3 install # Or whichever install/update you need to do

I followed the procedure indicated above but when I tried
pip3 install pifacecommon
pip3 install pifacedigitalio

it returned the following
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

[sudo] password for homeassistant:

Unfortunately I do not have the password for the user homeassistant as I did not create it. Note that if anyone knows the password then please let me (and others) know as that is necessary to install other python modules into pyenv?
So I just finished adding the user homeassistant into the sudo group to try and get around the issue with the password and was able to install the pifacecommon and pifacedigitalio libraries but unfortunately still, I am not able to talk to the PiFace from within HASS.

Here is a test I just ran and I think it is the same error I am getting from within HASS. Almost as if the SPI is not enabled but outside of HASS (pyenv) it works so the SPI must be enabled. Any help would be greatly appreciated and thank you in advance.

homeassistant@DietPi:/root$ . /home/homeassistant/pyenv-activate.sh
homeassistant@DietPi:~$ pip3 install pifacecommon
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pifacecommon in ./.pyenv/versions/3.8.0/lib/python3.8/site-packages (4.2.2)
homeassistant@DietPi:~$ pip3 install pifacedigitalio
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pifacedigitalio in ./.pyenv/versions/3.8.0/lib/python3.8/site-packages (3.0.5)
homeassistant@DietPi:~$ python3
Python 3.8.0 (default, Jan 7 2020, 18:53:06)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import pifacedigitalio
pifacedigital = pifacedigitalio.PiFaceDigital()
Traceback (most recent call last):
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 41, in open_fd
self.fd = posix.open(spi_device, posix.O_RDWR)
PermissionError: [Errno 13] Permission denied: ‘/dev/spidev0.0’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacedigitalio/core.py”, line 53, in init
super(PiFaceDigital, self).init(hardware_addr, bus, chip_select)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/mcp23s17.py”, line 91, in init
super(MCP23S17, self).init(bus, chip_select, speed_hz=speed_hz)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 33, in init
self.open_fd(spi_device)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 43, in open_fd
raise SPIInitError(
pifacecommon.spi.SPIInitError: I can’t see /dev/spidev0.0. Have you enabled the SPI module? (http://piface.github.io/pifacecommon/installation.html#enable-the-spi-module)


011920: Added - config.txt file located in DietPi folder
#-------I²C-------------
dtparam=i2c_arm=on
dtparam=i2c1=on

#-------SPI-------------
#dtparam=spi=off
dtparam=spi=on

#-------Serial/UART-----

NB: Enabled for 1st run only, if you want to keep this setting, please set CONFIG_SERIAL_CONSOLE_ENABLE=1 in /DietPi/dietpi.txt.

#core_freq=400
enable_uart=1

Hi,
many tanks for your report.

Hmm looks like your have issues to access /dev/spidev0.0. Pls can you check what the file system permissions are?

Hi and thanks for getting back to me. The original permissions of spidev0.0 were octal 660. I changed it to 770 but still the same results.

root@DietPi:~# cd /dev
root@DietPi:/dev# ls -l spidev0.0
crwxrwx— 1 root spi 153, 0 Jan 19 07:48 spidev0.0
root@DietPi:/dev# ls -l spidev0.1
crwxrwx— 1 root spi 153, 1 Jan 19 07:48 spidev0.1
root@DietPi:/dev#

Note that it did import the pifacedigitalio library but failed after that.

homeassistant@DietPi:/root$ . /home/homeassistant/pyenv-activate.sh
homeassistant@DietPi:~$ python3
Python 3.8.0 (default, Jan 7 2020, 18:53:06)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import pifacedigitalio
pifacedigital = pifacedigitalio.PiFaceDigital()
Traceback (most recent call last):
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 41, in open_fd
self.fd = posix.open(spi_device, posix.O_RDWR)
PermissionError: [Errno 13] Permission denied: ‘/dev/spidev0.0’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacedigitalio/core.py”, line 53, in init
super(PiFaceDigital, self).init(hardware_addr, bus, chip_select)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/mcp23s17.py”, line 91, in init
super(MCP23S17, self).init(bus, chip_select, speed_hz=speed_hz)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 33, in init
self.open_fd(spi_device)
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/spi.py”, line 43, in open_fd
raise SPIInitError(
pifacecommon.spi.SPIInitError: I can’t see /dev/spidev0.0. Have you enabled the SPI module? (http://piface.github.io/pifacecommon/installation.html#enable-the-spi-module)

can you change it to 777 for /dev/spidev0.0? You still have Permission denied

Ok, just did some experimentation and although I changed the permission to octal 777 for both spidev0.0 and spidev0.1, I noticed after a reboot that the permission went back to 660. In any case I did not reboot after a change and as below, it appears now that although spidev0.0 appears to work, there are now permission errors on /sys/class/gpio/gpio25/…
thx again for sticking with it.

homeassistant@DietPi:/dev$ ls -l spi*
crwxrwxrwx 1 root spi 153, 0 Jan 19 12:46 spidev0.0
crwxrwxrwx 1 root spi 153, 1 Jan 19 12:46 spidev0.1
homeassistant@DietPi:/dev$ . /home/homeassistant/pyenv-activate.sh
homeassistant@DietPi:~$ cd /dev
homeassistant@DietPi:/dev$ ls -l spi*
crwxrwxrwx 1 root spi 153, 0 Jan 19 12:46 spidev0.0
crwxrwxrwx 1 root spi 153, 1 Jan 19 12:46 spidev0.1
homeassistant@DietPi:/dev$ cd ~
homeassistant@DietPi:~$ python3
Python 3.8.0 (default, Jan 7 2020, 18:53:06)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import pifacedigitalio
pifacedigital = pifacedigitalio.PiFaceDigital()
Traceback (most recent call last):
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/interrupts.py”, line 385, in bring_gpio_interrupt_into_userspace
with open(GPIO_INTERRUPT_DEVICE_VALUE):
PermissionError: [Errno 13] Permission denied: ‘/sys/class/gpio/gpio25/value’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacedigitalio/core.py”, line 82, in init
self.init_board()
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacedigitalio/core.py”, line 114, in init_board
self.enable_interrupts()
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacedigitalio/core.py”, line 86, in enable_interrupts
self.gpio_interrupts_enable()
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/interrupts.py”, line 232, in gpio_interrupts_enable
bring_gpio_interrupt_into_userspace()
File “/home/homeassistant/.pyenv/versions/3.8.0/lib/python3.8/site-packages/pifacecommon/interrupts.py”, line 389, in bring_gpio_interrupt_into_userspace
with open(GPIO_EXPORT_FILE, ‘w’) as export_file:
PermissionError: [Errno 13] Permission denied: ‘/sys/class/gpio/export’

probably you would need to change permission for these files as well. Your challenge is that your commands are executed with user homeassistant but this user did not have the appropriate right. You could try to have the user added to the needed groups. Maybe that will fix the access issues.

You are correct in that it does appear to be a “permissions issue” in regards to user homeassistant as the same works perfectly under user root.
I will need to think about this some more but if anyone has some input/experience on how to add that within pyenv/homeassistant that would be great. Although I am getting better by the day, I am still learning by the minute.

thx

Probably this could help how to mange groups on Linux based environments

https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/

To display the group of user homeassistant, just run

groups homeassistant

However you would need to find out what groups you would need to add to the user homeassistant
If you know that, it can be done as follow

usermod -a -G examplegroup exampleusername

Interesting to note that there were no issues experienced when using HASSBIAN (which has been retired out) so with that I do wonder if user permissions have been addressed with the install of HASS under DietPi? Both use the pyenv environment which creates its own Python instance. As you mention (and thanks again) I will need to explore deeper into homeassistant group permissions. I might need to resurrect the old HASSBIAN build and compare.

Hi,
I don’t think it has anything to do with pyenv environment. It’s more a question of pure Linux file system permissions and authorization.

how did you setup HASS on your DietPi device?

The DietPi dev team has done a great job at creating a way of installing optimized software including HASS. When they pulled the plug on HASSBIAN I was hunting around for alternatives and fell on DietPi. Anyways, I followed the instructions and had a current working version of HASS installed in no time. Unfortunately though the PiFace board did not interface as well but I am determined to figure it out and I agree with you, it does appear to be a permissions issue of some sort. I just need to figure out what group permissions are required for user homeassistant and I should be fine. I will keep plugging away.

ok, pls keep us posted. Probably it will help others if they hit similar issues.

Good morning forum and glad to report that I discovered a fix.

Resolution: Adding the group “spi” to the user “homeassistant” resolved the issue.
I also added the group “i2c” to the user as well just in case the i2c protocol is ever required.

I hope that my persistence is able to help someone out some day.

Many thanks for reporting and investigating this issue, guys. I think we should add the HA user to SPI+I2C groups by default, as this is a common use-case. Will be done for v6.29.