Can HA set the Do Not Disturb on my IP phone using FreePBX?

I have an IP phone in my home office tied into my office’s system that uses FreePBX. I’d like to set the DND on that phone to on/off based on if my computer is locked/unlocked. I already have a trigger in HA for my computer’s lock status, I just need to find a way for HA to talk to FreePBX.

I asked the FreePBX community for a solution and this was the only response I got. I’m not smart enough to understand or implement that solution without a little help from the HA community. Has anyone done anything like this or willing to give me some guidance?

My main question would be how to use an HA automation to issue an SSH command and what that command would look like. If I can figure that out I should be able to get this working.

You should be able to use a command line switch for this.

You’ll need to set up SSH public key authentication. See this guide:

Probably something like this:

# Example configuration.yaml entry
switch:
  - platform: command_line
    switches:
      phone_dnd:
        command_on: "ssh -o UserKnownHostsFile=/config/.ssh/known_hosts <INSERT HOST USERNAME>@<INSERT HOST ADDRESS> -i /config/.ssh/id_ed25519 'lgaetz-dnd.php set <ext#>'"
        command_off: "ssh -o UserKnownHostsFile=/config/.ssh/known_hosts <INSERT HOST USERNAME>@<INSERT HOST ADDRESS> -i /config/.ssh/id_ed25519 'lgaetz-dnd.php unset <ext#>'"

You’ll need to copy lgaetz-dnd.php to your FreePBX system, make it executable (chmod +x lgaetz-dnd.php) and put it somewhere in your $PATH (or specify the full path to lgaetz-dnd.php in the SSH commands).

I have no experience with FreePBX, so I’m afraid I can’t help you further.

Thanks @ondras12345! I think I can figure it out with what you told me. I’ll report back once I get a chance to play with it.

I am so close on this, but can’t quite get it.

If I enter this command in the Home Assistant SSH & Web Terminal it works and changes the DND
ssh -o UserKnownHostsFile=/root/.ssh/known_hosts PBX [email protected] -i /root/.ssh/id_rsa '/usr/bin/php /etc/lgaetz-dnd.php set 320'

I have this in my configuration.yaml

switch:
  #Controlling the DND on my desk phone
  - platform: command_line
    switches:
      phone_dnd:
        command_on: "ssh -o UserKnownHostsFile=/root/.ssh/known_hosts PBX [email protected] -i /root/.ssh/id_rsa '/usr/bin/php /etc/lgaetz-dnd.php set 320'"
        command_off: "ssh -o UserKnownHostsFile=/root/.ssh/known_hosts PBX [email protected] -i /root/.ssh/id_rsa '/usr/bin/php /etc/lgaetz-dnd.php unset 320'"

My switch shows up in HA
Untitled

But when I try to turn the switch ‘on’ I get this error in my logs.

2021-10-25 09:13:20 ERROR (SyncWorker_16) [homeassistant.components.command_line] Command failed: ssh -o UserKnownHostsFile=/root/.ssh/known_hosts PBX [email protected] -i /root/.ssh/id_rsa '/usr/bin/php /etc/lgaetz-dnd.php set 320'
2021-10-25 09:13:20 ERROR (SyncWorker_16) [homeassistant.components.command_line.switch] Command failed: ssh -o UserKnownHostsFile=/root/.ssh/known_hosts PBX [email protected] -i /root/.ssh/id_rsa '/usr/bin/php /etc/lgaetz-dnd.php set 320'

Any ideas?

The guide I linked recommended putting the ssh keys and the known_hosts file in /config/.ssh instead of /root/.ssh. This is because /root will be different inside the Home Assistant container (where your command line switch’s commands run) than on the host OS.

I attempted that at first but ran into issues on step 2 of the guide, so I attempted to follow the guide verbatim. I now see how that was an issue.

I tried it again using the /config/.ssh location

  ~ ssh-keygen -t rsa                                                                                                                                                                                                                   
Generating public/private rsa key pair.                                                                                                                                                                                                  
Enter file in which to save the key (/root/.ssh/id_rsa): /config/.ssh                                                                                                                                                                    
/config/.ssh already exists.                                                                                                                                                                                                             
Overwrite (y/n)? y                                                                                                                                                                                                                       
Enter passphrase (empty for no passphrase):                                                                                                                                                                                              
Enter same passphrase again:                                                                                                                                                                                                             
Your identification has been saved in /config/.ssh                                                                                                                                                                                       
Your public key has been saved in /config/.ssh.pub

But on step 2 when I try to copy to my PBX it takes the /root/.ssh-id_rsa key that I created. When I originally tried this I received an error. Do I need to use a different command to tell it to use the /config/.ssh key?

➜  ~ ssh-copy-id PBX [email protected]                                                                                                                                                                                           
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

ssh-copy-id has the -i flag, same as ssh. Try ssh-copy-id -i /config/.ssh/id_rsa PBX [email protected]

Also, you have told ssh-keygen to generate an rsa key and save it as /config/.ssh. You should have made a new directory mkdir /config/.ssh and put the key in /config/.ssh/id_rsa.

IT WORKS!

Thank you so much for your help and patience with me! My wife especially thanks you since she won’t need to listen to my phone ring when I’m not there. :laughing: