Use Samba to configure Home Assistant
This article is a continuation from Install Home Assistant on RaspberryPi
What I’ll cover
In this section I’ll start from an existing installation of Home Assistant done in a python virtual environment (on a RaspberryPi, but this doesn’t really matter) running in the “homeassistant” user account (so isolated from the root user for safety reasons).
In particular I’ll explain how to access our Home Assistant system with Samba to browse its filesystem and modify any file from there without using ssh (if it’s something you want to do quickly using Windows Explorer on a Windows machine for example).
Anyway, you may wonder:
Why Samba?
As it stands (after the first article Install Home Assistant on RaspberryPi), our Home Assistant system is only accessible directly from the machine or ssh(ing) into it.
To configure Home Assistant in the future we’ll need to access and edit some configuration files… and this is something that you’ll find yourself doing quite often, especially during the initial setup.
You can clearly do this on the system as I just mention, but is nice to have the possibility to do it directly from a system equipped with nice text editor.
This is what Samba will allow us to do.
Without Samba
Without Samba is still possible to access the configuration file we need to. This is how:
After you ssh(ed) into the machine with the user “pi” (in our case), you’ll need to switch to the “homeassistant” user.
Switch User
So, now that you are in the system at least, type this command to log in as “homeassistant”:
1 |
sudo -u homeassistant -H -s |
The prompt should show you the success of this step:
1 2 |
pi@homeassistant:~ $ sudo -u homeassistant -H -s homeassistant@homeassistant:/home/pi $ |
Note the “homeassistant@” in the last line.
Despite we are not the “homeassistant” user, we are still in the “pi”‘s home folder: “/home/pi”
We want to switch to our own (“homeassistant”) home folder.
Just type:
1 |
cd ~ |
to achieve this.
To verify that you are not in the “homeassistant” folder, you can use the “pwd” command:
1 2 |
homeassistant@homeassistant:~ $ pwd /home/homeassistant |
perfect!
Edit the configuration file
The file we are mainly interested in is “configuration.yaml” inside the homeassistant config dir that if you remember from the previous post is located in: “/home/homeassistant/.homeassistant”
that we specified with the line:
1 |
ExecStart=/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant" |
… ok? (check the previous post if you don’t remember this detail.)
Anyway, this is it (2nd file):
1 2 3 4 5 6 7 8 9 10 11 12 |
homeassistant@homeassistant:~ $ ls -go .homeassistant/ total 8732 -rw-r--r-- 1 2 Sep 13 22:52 automations.yaml -rw-r--r-- 1 1678 Sep 13 22:52 configuration.yaml -rw-r--r-- 1 0 Sep 13 22:52 customize.yaml drwxr-xr-x 2 4096 Sep 13 22:52 deps -rw-r--r-- 1 0 Sep 13 22:52 groups.yaml -rw-r--r-- 1 7207 Sep 20 19:17 home-assistant.log -rw-r--r-- 1 8908800 Sep 20 19:24 home-assistant_v2.db -rw-r--r-- 1 0 Sep 13 22:52 scripts.yaml -rw-r--r-- 1 157 Sep 13 22:52 secrets.yaml drwxr-xr-x 2 4096 Sep 13 22:55 tts |
Ok, let’s enter the “.homeassistant” folder for convenience:
1 2 |
homeassistant@homeassistant:~ $ cd .homeassistant/ homeassistant@homeassistant:~/.homeassistant $ |
and we can now edit the configuration file.
If you want to use “nano” to edit it, this is the command:
1 |
nano configuration.yaml |
Let’s Samba now!
Access Home Assistant folders with Samba
As I mentioned above from now on a lot of work will involve editing the Home Assistant configuration file. As you just saw, doing this with a shell is not something everyone enjoys necessarily and many people would probably prefer to use a visual editor like notepad++, sublime text or whatever from a desktop environment to edit the files.
Log into the machine
Yes, initially we still need to do some configuration directly on the machine… but will worth it.
Start logging in into your Home Assistant device with an ssh connection.
reminder from the previous post (if you are using the IP address):
1 |
ssh pi@192.168.1.6 |
Install Samba
We’ll install Samba with apt-get. It’s good practice to update apt database before installing a package, so to install Samba, let’s first update apt:
1 |
sudo apt-get update |
and then install Samba:
1 |
sudo apt-get install samba |
We have Samba! Let’s configure it now.
Edit Samba Configuration
we need now to configure Samba and you should find the configuration file: “/etc/samba/smb.conf”
Before editing this file you may want to keep a copy of the original. That’s the right momente to do this:
1 |
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.original |
and then proceed to edit “smb.conf“.
I’ll use “nano” to edit it (but feel free to use any other text editor you may be conformable with):
1 |
sudo nano -cl /etc/samba/_smb.conf |
Before changing anything I want just to point to one particular section:
The workgroup section (the one for example Windows will find the server in).
Double check this if you have any problems finding the device in your network at the end of the setup.
This is how it looks like (I include the line numbers as a reference):
1 2 3 4 5 6 |
24 [global] 25 26 ## Browsing/Identification ### 27 28 # Change this to the workgroup/NT-domain name your Samba server will part of 29 workgroup = WORKGROUP |
ok, that aside, add this bit at the end of the file:
1 2 3 4 |
[homeassistant] path = /home/homeassistant/.homeassistant valid users = homeassistant read only = no |
This bit creates a share directory that points to the folder containing the configuration files for Home Assistant. The valid users option has been set to allow only the user homeassistant to access the share.
Save the file (CTRL+O) and close it (CTRL+X).
Set Samba User Password
Now we will just tell samba to create a password for the user homeassistant to allow him to access the share:
1 |
sudo smbpasswd -a homeassistant |
just type any password you want to use to allow homeassistant to access through samba:
1 2 3 4 |
pi@homeassistant:~ $ sudo smbpasswd -a homeassistant New SMB password: Retype new SMB password: Added user homeassistant. |
Restart Samba Service
To restart Samba Service and reload the modified configuration you can use:
1 |
sudo service smbd restart |
(or just restart the system…)
I like also to check the status immediately after:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
pi@homeassistant:~ $ sudo service smbd status ● smbd.service - Samba SMB Daemon Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2018-09-25 18:27:05 UTC; 8s ago Docs: man:smbd(8) man:samba(7) man:smb.conf(5) Main PID: 1133 (smbd) Status: "smbd: ready to serve connections..." CGroup: /system.slice/smbd.service ├─1133 /usr/sbin/smbd ├─1134 /usr/sbin/smbd ├─1135 /usr/sbin/smbd └─1137 /usr/sbin/smbd |
Connect to Samba
From a different machine (the same one you are using ssh is fine) you can then check that the new network resource is there.
In my case (a Windows machine) I can find my Home Assistant shared folder into the Network section:
If you try to enter into it’s shared “homeassistant” folder, you will be prompted for the username and password and you’ll have to use the one you added to samba few moments ago:
and after that you should be able to browse your remote folder:
as you can see amongst these files there is the “configuration.yaml” that is the one that we’ll end up modifying the majority of the time… but you can now do it using a powerful text editor from your remote machine if you prefer!
Job Done!
We are ready to jump into out Home Assistant configuration now. Stay tuned for the following articles.
If you enjoyed this article, found it interesting or just learned something new (or simply liked it) please share it, comment or feel free to if you think I deserved it or you simply want to show your appreciation :). In any case thanks for passing by and until next time!