This repository has been archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
zigate2zigpy.py
85 lines (72 loc) · 4.01 KB
/
zigate2zigpy.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
'''
Created on 26 sept. 2019
@author: doudz
quick and dirty converter from zigate.json to zigbee.db
'''
import json
import sqlite3
from binascii import unhexlify
JSON = 'zigate.json'
DB = 'zigbee.db'
def create_db(cursor):
tables = ['CREATE TABLE IF NOT EXISTS attributes (ieee ieee, endpoint_id, cluster, attrid, value)',
'CREATE TABLE IF NOT EXISTS clusters (ieee ieee, endpoint_id, cluster)',
'CREATE TABLE IF NOT EXISTS devices (ieee ieee, nwk, status)',
('CREATE TABLE IF NOT EXISTS endpoints (ieee ieee, endpoint_id, profile_id, '
'device_type device_type, status)'),
('CREATE TABLE IF NOT EXISTS group_members (group_id, ieee ieee, endpoint_id, FOREIGN KEY(group_id) '
'REFERENCES groups(group_id), FOREIGN KEY(ieee, endpoint_id) REFERENCES endpoints(ieee, endpoint_id))'),
'CREATE TABLE IF NOT EXISTS groups (group_id, name)',
('CREATE TABLE IF NOT EXISTS node_descriptors (ieee ieee, value, FOREIGN KEY(ieee) '
'REFERENCES devices(ieee))'),
'CREATE TABLE IF NOT EXISTS output_clusters (ieee ieee, endpoint_id, cluster)',
]
for query in tables:
cursor.execute(query)
indexes = ['CREATE UNIQUE INDEX IF NOT EXISTS attribute_idx ON attributes(ieee, endpoint_id, cluster, attrid)',
'CREATE UNIQUE INDEX IF NOT EXISTS cluster_idx ON clusters(ieee, endpoint_id, cluster)',
'CREATE UNIQUE INDEX IF NOT EXISTS endpoint_idx ON endpoints(ieee, endpoint_id)',
'CREATE UNIQUE INDEX IF NOT EXISTS group_idx ON groups(group_id)',
'CREATE UNIQUE INDEX IF NOT EXISTS group_members_idx ON group_members(group_id, ieee, endpoint_id)',
'CREATE UNIQUE INDEX IF NOT EXISTS ieee_idx ON devices(ieee)',
'CREATE UNIQUE INDEX IF NOT EXISTS node_descriptors_idx ON node_descriptors(ieee)',
'CREATE UNIQUE INDEX IF NOT EXISTS output_cluster_idx ON output_clusters(ieee, endpoint_id, cluster)',
]
for query in indexes:
cursor.execute(query)
conn = sqlite3.connect(DB)
cursor = conn.cursor()
create_db(cursor)
with open(JSON, 'rb') as fp:
zigate_db = json.load(fp)
for device in zigate_db.get('devices', []):
query = 'INSERT OR IGNORE INTO devices (ieee, nwk, status) VALUES (?, ?, ?)'
nwk = int(device['info']['addr'], 16)
ieee = device['info']['ieee']
ieee = ':'.join([ieee[i: i + 2] for i in range(0, len(ieee), 2)])
print('Import device ', ieee)
cursor.execute(query, (ieee, nwk, 2))
for endpoint in device.get('endpoints', []):
query = ('INSERT OR IGNORE INTO endpoints (ieee, endpoint_id, profile_id, device_type, status) '
'VALUES (?, ?, ?, ?, ?)')
cursor.execute(query, (ieee, endpoint['endpoint'], endpoint['profile'], endpoint['device'], 1))
for cluster in endpoint['in_clusters']:
query = 'INSERT OR IGNORE INTO clusters (ieee, endpoint_id, cluster) VALUES (?, ?, ?)'
cursor.execute(query, (ieee, endpoint['endpoint'], cluster))
for cluster in endpoint['out_clusters']:
query = 'INSERT OR IGNORE INTO output_clusters (ieee, endpoint_id, cluster) VALUES (?, ?, ?)'
cursor.execute(query, (ieee, endpoint['endpoint'], cluster))
for cluster in endpoint['clusters']:
for attribute in cluster['attributes']:
if 'data' not in attribute:
continue
data = attribute['data']
if 'value' in attribute and type(attribute['data']) == str and \
isinstance(attribute['data'], type(attribute['value'])):
data = unhexlify(data)
query = ('INSERT OR IGNORE INTO attributes (ieee, endpoint_id, cluster, attrid, value) '
'VALUES (?, ?, ?, ?, ?)')
cursor.execute(query, (ieee, endpoint['endpoint'], cluster['cluster'], attribute['attribute'], data))
conn.commit()
conn.close()
print('Conversion done.')