-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_home_assistant.py
36 lines (30 loc) · 1.12 KB
/
to_home_assistant.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
#!/usr/bin/env python3
import yaml
class MyDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
def to_home_assistant():
with open('config.yaml', 'r') as file:
try:
content = yaml.safe_load(file)
except yaml.YAMLError as e:
print(e)
return
target_list = []
for relay_number in content['relays']:
relay = content['relays'][relay_number]
target_list.append({
'platform': 'mqtt',
'command_topic': f'master/relays/{relay_number}/set',
'state_topic': f'master/relays/{relay_number}',
'payload_off': 'off',
'payload_on': 'on',
'name': relay['name'],
'unique_id': f'relay.{relay_number}',
})
with open('mqtt_relay_switches.yaml', 'w', encoding='utf8') as outfile:
yaml.dump(target_list, outfile, default_flow_style=False)
dump = yaml.dump(target_list, Dumper=MyDumper, default_flow_style=False)
print(dump)
if __name__ == '__main__':
to_home_assistant()