openHAB2 : REST API : websockets: notification on item state change

Are there any examples of connecting to openHAB2 REST API using python + websockets? I’m interested in getting item state change notifications without polling item states.

I tried the python notification example (see https://github.com/appzer/openhab/wiki/Samples-REST#python) with no luck (it’s the exact same example, I just enabled tracelog, changed IP and item name):

import websocket    #need to run pip install websocket-client to get this module

class WSClient():
 
    def __init__(self):
        websocket.enableTrace(True) #True for debugging
       
    def connect(self, url):
        print("connecting to %s" % url)
        
        self.ws = websocket.WebSocketApp(url,
        on_message = self.on_message,
        on_error = self.on_error,
        on_close = self.on_close)
        self.ws.on_open = self.on_open
        self.ws.run_forever()
 
    def on_message(self, ws, message):
        print("Received:")
        print(message)
 
    def on_error(self, ws, error):
        print(error)
 
    def on_close(self, ws):
        print("connection closed")
 
    def on_open(self, ws):
        print("connected")
        
    def close(self, ws):
        ws.close()
 
if __name__ == "__main__":
    try:
        client = WSClient()
        client.connect("ws://192.168.178.42:8080/rest/items/switch_hm_motionDetection_lowBat/state?Accept=application/json")
       
    except (KeyboardInterrupt, SystemExit):
        print("System exit Received - Exiting program")
        client.close()
        print(" **** Program Ended ****")

It gives me the following output:

connecting to ws://192.168.178.42:8080/rest/items/switch_hm_motionDetection_lowBat/state?Accept=application/json
--- request header ---
GET /rest/items/switch_tvOn/state?Accept=application/json HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 192.168.178.42:8080
Origin: http://192.168.178.42:8080
Sec-WebSocket-Key: hEBfauXHBMLur7tkyxZg8g==
Sec-WebSocket-Version: 13


-----------------------
--- response header ---
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 4
Server: Jetty(9.3.22.v20171030)
-----------------------
Handshake status 200
connection closed

The function on_message is never called.

Looking at the wireshark log I see the following:

Auswahl_007

State information for item switch_hm_motionDetection_lowBat (OFF) is transferred, but is not received from my websocket client.

I tried different examples using socketIO-client and now I have seen every possible error (at least it feal’s like it)…

Another attempt maybe worth mentioning:

import websocket as ws
 
  
if __name__ == "__main__":
      
    ws.enableTrace(True)
     
    ohsocket = ws.WebSocket()
    ohsocket.settimeout(20)
 
    ohsocket.connect("ws://192.168.178.42:8080/rest/items/", header={"X-Atmosphere-Transport":"websocket", "Accept":"application/json"})

gives me the following ouput:

--- request header ---
GET /rest/items/ HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 192.168.178.42:8080
Origin: http://192.168.178.42:8080
Sec-WebSocket-Key: bIMaDJx431bYUKLNKK1h3A==
Sec-WebSocket-Version: 13
X-Atmosphere-Transport: websocket
Accept: application/json


-----------------------
--- response header ---
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(9.3.22.v20171030)
-----------------------
Traceback (most recent call last):
  File "/home/seven/dev/workspace_eclipse/openhabExt/src/main_websockets.py", line 100, in <module>
    ohsocket.connect("ws://192.168.178.42:8080/rest/items/", header={"X-Atmosphere-Transport":"websocket", "Accept":"application/json"})
  File "/usr/local/lib/python3.4/dist-packages/websocket/_core.py", line 216, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python3.4/dist-packages/websocket/_handshake.py", line 69, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python3.4/dist-packages/websocket/_handshake.py", line 135, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d", status)
websocket._exceptions.WebSocketBadStatusException: Handshake status 200

Do you have an idea how to use python websockets with openHAB2?

Sebastian

PS: The following website did not bring light into darkness either…