Created
August 26, 2018 07:36
-
-
Save twosky2000/dc13951ed71dbc6a3ad7ee0fc3f5eefc to your computer and use it in GitHub Desktop.
Little Script for get ets4 efs file to integrate in home assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import csv | |
import yaml | |
tags = None | |
namecount = dict() | |
class KNXDeviceManager(yaml.YAMLObject): | |
yaml_tag = u'!KNXDeviceManager' | |
def __init__(self,listing= []): | |
self.Devices = listing | |
self.ETS_NAME = "test" | |
def filtertype(self,markedtype): | |
sublist = [] | |
for dev in self.Devices : | |
tainted = False | |
for entry in dev.xadresses : | |
if (( hasattr(entry.name,"type")) and ( getattr(entry.name,"type") == markedtype)): | |
tainted = True | |
if tainted: | |
sublist.append(dev) | |
return sublist | |
def filtertags(self,tag): | |
sublist = [] | |
for dev in self.Devices: | |
tainted = False | |
for entry in dev.xadresses : | |
contain = False | |
for tg in tag: | |
for tags in dev.name: | |
if (tags == tg): | |
contain = True | |
for tags in dev.removed_tags: | |
if (tags == tg): | |
contain = True | |
if contain: | |
tainted = True | |
if tainted: | |
sublist.append(dev) | |
return sublist | |
def addAddress(self,Object): | |
tmp = KNXEntry(Object) | |
merge = None | |
for dev in self.Devices: | |
if (dev.name == tmp.name.name): | |
dev.xadresses.append( tmp ) | |
return | |
self.Devices.append(KNXDevice(tmp)) | |
def lenunknow(self): | |
i = 0 | |
tmp = [] | |
for dev in self.Devices : | |
tainted = False | |
for entry in dev.xadresses : | |
if not hasattr(entry.name,"type") : | |
i += 1 | |
tainted = True | |
if tainted: | |
tmp.append(dev) | |
return tmp | |
def dump_save(self): | |
i = 0 | |
tmp = [] | |
for dev in self.Devices : | |
if hasattr(dev.name,"type") : | |
i += 1 | |
tmp.append(dev) | |
return KNXDeviceManager(tmp) | |
class KNXName(yaml.YAMLObject): | |
yaml_tag = u'!KNX_Name' | |
def __init__(self,strInput): | |
self.originalName = strInput # Keep original for debug output | |
self.removed_tags = list() # keep the known tag for some manipulation | |
self.name = list() # Device Name | |
self.mountPoint = "unknown_typ" # is used for key value of home assistant config. normaly group_address or group_address_state | |
for index, tag in enumerate(strInput): | |
if (tag != ""): | |
if (namecount.get(tag)): | |
namecount[tag] +=1 | |
else: | |
namecount[tag] =1 | |
if (tags.get(tag)): | |
tmptag = tags.get(tag) | |
self.removed_tags.append(tag) | |
self.mountPoint = tmptag.get("mountPoint") | |
self.type = tmptag.get("type") | |
else: | |
self.name.append(tag) | |
class KNXEntry(yaml.YAMLObject): | |
yaml_tag = u'!KNXEntry' | |
def __init__(self, Object): | |
_, self.knx_address = Object[0].rsplit(".",1) # This is the */*/* address, the named group addresses are no us till now | |
self.name = KNXName(Object[1].split(' ')) | |
# Don't have use for these values jet, commenting out helps readability | |
#self.typeName = Object[2] | |
#self.significantBit = Object[3] | |
#if (Object[4]!= ""): | |
# self.accessed_by = Object[4].split(' ') | |
class KNXDevice(yaml.YAMLObject): | |
yaml_tag = u'!KNX_Device' | |
def __init__(self,entry): | |
self.name = entry.name.name[:] # [:] copys the value so that there is no linking in the yaml | |
self.xadresses = list() # can't be a dict, because we don't know that the input data is sane and no overwrites happen | |
self.xadresses.append(entry) | |
# lazy version for a pretty string for home assistant | |
def homeassistantconfigout(iDevices): | |
outputstr = "#KNX Config\n\n" | |
for dev in iDevices : | |
outputstr +="- platform: knx\n" | |
outputstr += " name: " +" ".join(dev.name) +"\n" | |
for entry in dev.xadresses : | |
outputstr += " " + entry.name.mountPoint + " : " + entry.knx_address +"\n" | |
outputstr += "\n" | |
return outputstr | |
# Reads known tags from external file for categorisation | |
# Example test.yaml | |
#tags: | |
# E/A: | |
# mountPoint: group_address | |
# type: switch | |
# RM: | |
# mountPoint: group_address_state | |
# type: switch | |
with open("tags.yaml", 'r') as stream: | |
try: | |
tags = yaml.load(stream).get("tags") | |
except yaml.YAMLError as exc: | |
print(exc) | |
# Reading the efs file as csv with european encoding | |
with open("test.csv", "r", encoding="ISO-8859-1") as csvfile: | |
reader = csv.reader(csvfile, delimiter='\t') | |
config = KNXDeviceManager() | |
# First line is a title, so spezial treatment | |
length = 0 | |
for indexR, row in enumerate(reader): | |
if (indexR == 0): | |
config.ETS_NAME= row[0] | |
else: | |
config.addAddress(row) | |
length = indexR | |
# Some examples and tests | |
# print(yaml.dump(config.lenunknow(),default_flow_style=False,allow_unicode=True)) | |
# print(len(config.Devices)) | |
# print(homeassistantconfigout(config.filtertype("switch"))) | |
# print(yaml.dump(config.Devices,default_flow_style=False,allow_unicode=True)) | |
# Prints every tag and how many time it accures. Helps pick tags for categorisation by name | |
for key, value in sorted(namecount.items(), key=lambda x: x[1]): | |
print('%s: %s' % (key, value)) | |
# makes a version name like Fellmann_EFH_08.26.18.yaml | |
savename = config.ETS_NAME.replace(' ','_') + "_" + datetime.datetime.now().strftime("%x").replace('/','.') + ".yaml" | |
print(savename) | |
with open(savename,"w+") as yaml_file: | |
yaml.dump(config,yaml_file,default_flow_style=False,allow_unicode=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment