-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (94 loc) · 4.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
THING_TYPE = 0
THING_ID = 1
class Thing:
id = ""
label = ""
address = ""
type = ""
def OH2_KNX_lightThingConfigParser(configFile):
# input: OpenHab2 KNX config file
# output: list of Things represented as internal Objects
thingList = []
with open(configFile, 'r') as file:
for line in file:
currentThing = Thing()
isInLabel = False
isInAddress = False
isInKNX = False
if line.isspace() or line.startswith("//"):
continue
for index, word in enumerate(line.split(" ")):
if word == "Switch":
currentThing.type = "Switch"
elif word == "Rollershutter":
currentThing.type = "Rollershutter"
if word == "":
continue
if word.startswith("{"):
isInAddress = True
if word.endswith("}"):
isInAddress = False
if (word.startswith("\"") or isInLabel) and not isInAddress and not word.endswith("\""):
isInLabel = True
currentThing.label += word.strip("\"") + " "
if word.endswith("\"") and not isInAddress:
isInLabel = False
currentThing.label += word.strip("\"")
elif not word.startswith("\"") and not word.startswith("<") and not word.startswith(
"(") and not word.startswith("{") and not word.endswith(
")") and not isInAddress and not isInLabel:
currentThing.id = word
if word.startswith("knx=") or isInKNX:
currentThing.address += word.strip("knx=\",")
if word.endswith(","):
isInKNX = True
currentThing.address += "+"
else:
isInKNX = False
thingList.append(currentThing)
return thingList
def thingObjectToOH3Channel(thing):
#outputs channel string to put in the MainUI Code for an already created Thing (everything throug MainUI)
with open("output.txt", "a") as file:
file.write(
" - id: " + thing.id + "\n channelTypeUID: knx:" + thing.type + "\n label: " + thing.label + "\n description: \"\"\n configuration:\n ga: " + thing.address + "\n")
#functions for formatting to JSON to be inserted directly into "org.openhab.core.thing.Thing.json"
def thingObjectToOH3Thing(thing):
# converts internal thingObject to a JSON String readable by OpenHab3 that describes this internal thingObject with one Channel, where the ID of the OHThing and the ID of the OHChannel are the same
with open("OH3ThingBlueprint.txt", "r") as blueprint:
with open("thing_output.txt", "a") as output:
blueprintString = blueprint.read()
result = blueprintString % {"itemType": thing.type, "id": thing.id, "channelTypeUID": thing.type.lower(), "label": thing.label, "configuration": formatChannelConfiguration(thing)}
output.write(result)
def formatChannelConfiguration(thing):
if thing.type == "Switch":
return "\"ga\": \"" + thing.address + "\""
elif thing.type == "Rollershutter":
return "\"upDown\": \"" + thing.address + "\""
# homebridge converter
def thingObjectListToHomebridgeConfigFile(thingsList):
with open("Homebridge_output.txt", "a") as output:
for thing in thingsList:
if thing.type == "Rollershutter":
output.write(
"{\n "
+ "\"name\": \"" + thing.label + "\",\n" +
" \"type\": " + "\"windowcovering\"" + ",\n" +
" \"item\": \"" + thing.id + "\",\n" +
" \"inverted\": \"true\"\n},\n"
)
elif thing.type == "Switch":
output.write(
"{\n "
+ "\"name\": \"" + thing.label + "\",\n" +
" \"type\": \"light\",\n" +
" \"item\": \"" + thing.id + "\"\n},\n"
)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
thingsList = OH2_KNX_lightThingConfigParser('EIB.txt')
#for thing in thingsList:
# print("Type: " + thing.type + ", id: " + thing.id + ", Label: " + thing.label + ", address: " + thing.address)
# if thing.type == "Switch" or thing.type == "Rollershutter":
# thingObjectToOH3Thing(thing)
thingObjectListToHomebridgeConfigFile(thingsList)