I hope this is the right location for this Topic if not please let me know.
We are currently using a python script (outside of openHAB) to receive XML phone state changes, and post MQTT messages to a subscribed ESP8266 to sound a buzzer and flash a light when the phone rings.
I’m sure it’s possible to do this using openHAB but I don’t see a binding or any other method to make OH subscribe to XML notifications. Here is the python script we are using if it helps.
Anyone have any advice on how to do this in openHAB?
from waitress import serve
from pyramid.config import Configurator
from pyramid.response import Response
import xml.etree.ElementTree as ElementTree
import paho.mqtt.client as mqtt
def ip2ext(ip):
return {'10.240.196.86': '2445', # user
}[ip]
def offHookHandler(elem):
phoneip = elem.find('PhoneIP')
try:
ext = ip2ext(phoneip.text)
return ext
except KeyError:
return None
def onHookHandler(elem):
phoneip = elem.find('PhoneIP')
try:
ext = ip2ext(phoneip.text)
return ext
except KeyError:
return None
def incomingCallHandler(elem):
phoneip = elem.find('PhoneIP')
try:
ext = ip2ext(phoneip.text)
return ext
except KeyError:
return None
def callStateChangeHandler(elem):
phoneip = elem.find('PhoneIP')
try:
ext = ip2ext(phoneip.text)
return ext
except KeyError:
return None
def catcher(request):
# parse the XML sent by the phone
root = ElementTree.fromstring(request.body)
msg = ''
# start the remote ringer if there is an IncomingCallEvent
elem = root.find('IncomingCallEvent')
if elem is not None:
ext = incomingCallHandler(elem)
if ext:
msg = '1'
# stop it if the phone is picked up
elem = root.find('OffHookEvent')
if elem is not None:
ext = offHookHandler(elem)
if ext:
msg = '0'
# alternatively stop it if the dialing party hangs up,
elem = root.find('CallStateChangeEvent')
state = ''
if elem is not None:
for child in root:
state = child.attrib['CallState']
if state == 'Disconnected':
ext = callStateChangeHandler(elem)
# ext is returned only if the state is 'Disconnected'
if ext:
msg = '0'
# build a new MQTT Client (for each request)
# This is not efficeint, but, over localhost and for a low demand
# service, it should be fine
if msg:
client = mqtt.Client('Ringer')
client.connect('127.0.0.1')
client.loop()
client.publish('ringer/' + ext, msg)
client.disconnect()
return Response('')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('catcher', '/')
config.add_view(catcher, route_name='catcher')
app = config.make_wsgi_app()
serve(app, host='0.0.0.0', port=8081)