OpenHAB CULfw for Somfy RTS Rollershutters

Hi!

I have another working solution for OH3!
I’ve created a python script to control the nanoCUL(raw version, no error handling, no logging).
/etc/openhab/scripts/somfy.py

#!/usr/bin/python
import serial
import argparse

parser = argparse.ArgumentParser(description="Sends SOMFY RTS commands with the nanoCUL on the selected port")
parser.add_argument("shutter", help="The number of the shutter", type=str, choices=["Item1Name", "Item2Name", "Item3Name"])
parser.add_argument("command", help="The command to send", type=str.upper, choices=["UP", "DOWN", "STOP", "MY"])
args = parser.parse_args()

# Command (1 = My, 2 = Up, 4 = Down, 8 = Prog)
C="1"
if args.command == "UP":
    C="2"
elif args.command == "DOWN":
    C="4"

encryptionKey = 'A1'

if args.shutter == "Item1Name":
    rollingCode='AAA1'
    address='000001'
elif args.shutter == "Item2Name":
    rollingCode='BBB2'
    address='000002'
elif args.shutter == "Item3Name":
    rollingCode = 'CCC3'
    address = '000003'

command = 'Ys' + encryptionKey + C + '0' + rollingCode + address + "\n"
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.write(command)
ser.close()

Modify it according to your configuration:

  1. Edit the name of the Items (Item1Name, Item2Name, …)
  2. Change rolling codes, and addresses. The actual codes can be found in the SomfyCUL configuration files in /var/lib/openhab/somfycul/ (Thanks @Daniel_Weisser )
  3. Edit port (/dev/ttyUSB0)

For the script to work:

  1. pyserial should be installed: https://pythonhosted.org/pyserial/pyserial.html#installation
  2. Because Arduino resets on serial connect (check this), I’ve edited the /etc/rc.local file on my Raspberry pi to clear the hupcl setting on the port. I’ve added these lines:
# Clear the hupcl setting to prevent Arduino from reset on serial connect
stty -F /dev/ttyUSB0 -hupcl

Another solution could be to wait 2 seconds in the script after serial connect for the Arduino to restart.

The rule to call the python script:

rule "Somfy binding"
    when
	Item Item1Name received command or
	Item Item2Name received command or
	Item Item3Name received command
    then
    executeCommandLine(Duration.ofSeconds(3),"python", "/etc/openhab/scripts/somfy.py", triggeringItemName, receivedCommand.toString)
end

Hope this helps someone!