-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdomoticz_client.py
54 lines (49 loc) · 2.18 KB
/
domoticz_client.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
import Domoticz
import json
import re
import urllib.parse
import urllib.request
from urllib.error import URLError, HTTPError
class DomoticzClient:
DomoticzPort = ""
def __init__(self, domoticz_port):
self.DomoticzPort = domoticz_port
Domoticz.Debug("DomoticzClient::__init__")
def getDevicesByName(self, domoticz_devices):
domoticz_devices_by_name = {}
for device in domoticz_devices:
if "gBridge" in device['Description']:
match = re.search('gBridge:(.*)([\n|\r]?)', device['Description'])
if match:
name = match.group(1).strip()
else:
name = device['Name']
if device['Type'] == 'Group' or device['Type'] == 'Scene':
device['idx'] = 'group_' + device['idx']
domoticz_devices_by_name[name] = device
return domoticz_devices_by_name
def getLinkedDevices(self, domoticz_devices):
linked_devices = {}
for device in domoticz_devices:
if "gBridge" in device['Description'] and "linkedDevices" in device['Description']:
match = re.search('linkedDevices:(.*)([\n|\r]?)', device['Description'])
if match:
linked_ids = match.group(1).strip().split(",")
for linked_id in linked_ids:
linked_devices[linked_id] = device['idx']
return linked_devices
def fetchDevicesFromDomoticz(self):
url = "http://127.0.0.1:%d/json.htm?type=devices&filter=all&used=true&order=Name" % self.DomoticzPort
req = urllib.request.Request(url)
try:
response = urllib.request.urlopen(req).read().decode('utf-8')
except HTTPError as e:
Domoticz.Error('The server couldn\'t fetch devices from Domoticz')
Domoticz.Error('Error code: %d, reason: %s' % (e.code, e.reason))
raise
except URLError as e:
Domoticz.Error('Could not fetch devices from Domoticz, failed to reach a server: %s.' % url)
Domoticz.Error('Reason: %s' % e.reason)
raise
else:
return json.loads(response)['result']