-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_sync.py
More file actions
75 lines (59 loc) · 2.02 KB
/
device_sync.py
File metadata and controls
75 lines (59 loc) · 2.02 KB
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
#!/usr/bin/python
import json
import os
import sys
import re
# Variables
install_location = '/home/pi/.homeassistant'
def import_entity():
global entities
try:
os.popen('cp core.entity_registry core.entity_registry.bak')
except:
sys.exit('ERROR - Could not create backup of core.entity_registry')
with open('core.entity_registry') as json_file:
entities = json.load(json_file)
def import_devices():
global devices
try:
os.popen('cp core.device_registry core.device_registry.bak')
except:
sys.exit('ERROR - Could not create backup of core.device_registry')
with open('core.device_registry') as json_file:
devices = json.load(json_file)
def write_devices():
global devices
print('Writing the device registry')
try:
with open('core.device_registry', 'w') as outfile:
json.dump(devices, outfile, indent=2)
except:
sys.exit('ERROR - Could not write device registry')
def main():
os.chdir(install_location + '/.storage')
import_entity()
import_devices()
# Identify devices with more than one entity
device_to_update = []
for entity in entities['data']['entities']:
if re.search("^switch.*", entity['entity_id']) or re.search("^light.*", entity['entity_id']) or re.search("^binary_sensor.*", entity['entity_id']):
device_to_update.append(entity)
print(entity)
for device in devices['data']['devices']:
for d in device_to_update:
if d['device_id'] == device['id']:
print('Device: ' + device['id'])
if device['name_by_user']:
print('Old name: ' + device['name_by_user'])
try:
device['name_by_user'] = d['name']
print('New name: ' + device['name_by_user'])
except:
break
write_devices()
print('Success')
if __name__ == '__main__':
try:
main()
except:
print('Fatal error in main loop')