Created
November 11, 2024 15:12
-
-
Save KaaNee/066a97d4f6acb32b2dee558527a0355f to your computer and use it in GitHub Desktop.
Converter for openhab-things-channel-xml file into Items-File Format (Python Script)
This file contains hidden or 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 xml.etree.ElementTree as ET | |
| import sys | |
| import os | |
| # Default-Values | |
| default_xml_file = 'data.xml' | |
| default_channel_scope = 'replace-me' | |
| default_channel_prefix = 'modbus:tcp:device:mydevice' | |
| default_item_prefix = 'MyDevice_' | |
| default_group = '(mygroup)' | |
| # check for filepath and channel-scope as parameter | |
| if len(sys.argv) > 1: | |
| xml_file_path = sys.argv[1] | |
| else: | |
| xml_file_path = os.path.join(os.path.dirname(__file__), default_xml_file) | |
| # check if channel-scope as parameter is present (and use it) | |
| channel_scope = sys.argv[2] if len(sys.argv) > 2 else default_channel_scope | |
| # Check if file exists | |
| if not os.path.exists(xml_file_path): | |
| print(f"Error: File '{xml_file_path}' not found.") | |
| sys.exit(1) | |
| # Parase XML-File | |
| tree = ET.parse(xml_file_path) | |
| root = tree.getroot() | |
| # create openHAB Items Configuration | |
| for channel in root.findall('channel-type'): | |
| item_type = channel.find('item-type').text | |
| label = channel.find('label').text | |
| label_val = '\"' + label + '\"' | |
| description = channel.find('description').text if channel.find('description') is not None else label | |
| channel_id = channel.attrib['id'].replace('-channel', '') | |
| # openHAB item-name | |
| item_name = f"{default_item_prefix}_{label.replace(' ', '')}" | |
| # Output- please adapt the "<25" or similar values to the longest value of the part (shift right text in same column) | |
| print(f"{item_type:<25} {item_name:<50} {label_val:<40} {default_group:<15} {{ channel=\"{default_channel_prefix}:{channel_scope}#{channel_id}\" }}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment