Controlling the Alarm
My alarm system can be controlled by calling in by telephone and pressing buttons on the phone to enter passwords, set the alarm etc.
To control the alarm, I wanted to automate the pressing of the phone keys by using a USB modem. The script automatically dials the phone and sends dial tones at the right time interval.
This was the hardest part as I spend a day or two trying to find modem software that would still work and work on Raspberry Pi Arm CPU.
Almost nobody uses dial tone modem software anymore so most of the code is no longer working.
The only software that I found that still works on RPI is wvdial.
The following python code can be called from HA as a command switch or from the RFID python script.
It sends commands to the USB modem, which calls the alarm system on the phone line (local house call) and automatically sends key presses at the right time.
It also does other things like publish alarm status to MQTT server and sets the alarm status in HA.
import os
import sys
import time
import subprocess
from optparse import OptionParser
import RPi.GPIO as GPIO
import homeassistant.remote as remote
from homeassistant.const import STATE_ON, STATE_OFF
"""
Script to turn on and off my Bosche EZ Series Alarm using modem to call into alarm system
"""
alarm_set_time = 20
alarm_arm_time = 60
# command line options
parser = OptionParser()
parser.add_option("-s", "--state", dest="state",
help="on or off")
(switch, args) = parser.parse_args()
#::: Publish state of the alarm to mqtt server :::
def publish_mqtt(alarm_state):
command = "mosquitto_pub -r -h '192.168.0.59' -t '/alarm/state' -m '%s' " % alarm_state
os.system(command)
#::: Make sure this program is not still running :::
def file_exist(_file):
try:
with open(_file) as f: pass
except IOError:
return False
return True
f.close
if file_exist("wvdial.conf"):
print ("Script to set alarm already running will now exit")
sys.exit()
# GET ALARM STATE
GPIO.setmode(GPIO.BOARD)
pin = 8
GPIO.setup(pin, GPIO.IN)
if GPIO.input(pin):
alarm = 'on'
else:
alarm = 'off'
if switch.state == 'on' and alarm == 'off':
publish_mqtt('arming')
print("Turning on the alarm")
config = '''
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = USB Modem
Phone = ,,,###,,,1111,,,1,,,1,,,#,
ISDN = 0
Password = ''
New PPPD = yes
Username = ''
Modem = /dev/ttyACM0
Baud = 460800
'''
elif switch.state == 'off' and alarm == 'on':
publish_mqtt('disarming')
print("Turning off the alarm")
config = '''
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = USB Modem
Phone = ,,,###,,,1111,,,1,,,#,
ISDN = 0
Password = ''
New PPPD = yes
Username = ''
Modem = /dev/ttyACM0
Baud = 460800
'''
else:
print ("Alarm is already:", switch.state, " no action taken ...")
publish_mqtt(switch.state)
sys.exit()
# write wvdial config file
f = open('wvdial.conf','w')
f.write(config)
f.close()
# run wvdial
command = 'wvdial -C wvdial.conf'
print ('running wvdial: ', command)
p = subprocess.Popen(command, shell=True)
# Wait 30 seconds for dial command to finish
print ('waiting %s seconds to set alarm' % alarm_set_time)
time.sleep(alarm_set_time)
# Kill wvdial
p.kill()
os.system('killall wvdial')
# Set Alarm state
api = remote.API('localhost', '')
if switch.state == 'on':
# Wait for system to be set
time.sleep(alarm_arm_time)
remote.set_state(api, 'switch.alarm', new_state=STATE_ON)
remote.set_state(api, 'sensor.alarm', new_state=STATE_ON)
publish_mqtt('on')
else:
remote.set_state(api, 'switch.alarm', new_state=STATE_OFF)
remote.set_state(api, 'sensor.alarm', new_state=STATE_OFF)
publish_mqtt('off')
os.remove("wvdial.conf")
print ('Finished setting the alarm', switch.state)