-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
112 lines (92 loc) · 3.62 KB
/
utils.py
File metadata and controls
112 lines (92 loc) · 3.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import logging
import exceptions
import json
# States that trigger a Domoticz widget update
stateSet = {
"core:ClosureState",
"core:OpenClosedState",
"core:LuminanceState",
"core:DeploymentState"
}
# Supported uiClasses for io:// and rts:// devices
_SUPPORTED_IO_CLASSES = {
"RollerShutter",
"LightSensor",
"ExteriorScreen",
"Screen",
"Awning",
"Pergola",
"GarageDoor",
"Gate",
"Window",
"VenetianBlind",
"ExteriorVenetianBlind"
}
def filter_devices(Data):
logging.debug("start filter devices")
# OPT 6: retourneer lege lijst in plaats van None zodat callers
# veilig len() / iteratie kunnen gebruiken zonder TypeError.
# OPT 6b: efficiënte check via any() i.p.v. json.dumps van de volledige dataset
if not any("uiClass" in str(d.get("definition", {})) for d in Data):
logging.error("filter_devices: missing uiClass in response")
logging.debug(str(Data))
return []
filtered_devices = []
for device in Data:
ui_class = device["definition"]["uiClass"]
device_url = device["deviceURL"]
logging.debug("filter_devices: Device name: " + device["label"] + " Device class: " + ui_class)
# OPT 7: expliciete haakjes toegevoegd zodat de operator-prioriteit van
# 'and'/'or' geen onverwachte filter-resultaten geeft
is_io_or_rts = device_url.startswith("io://") or device_url.startswith("rts://")
is_supported_io = ui_class in _SUPPORTED_IO_CLASSES and is_io_or_rts
is_pod = ui_class == "Pod" and device_url.startswith("internal://")
if is_supported_io or is_pod:
filtered_devices.append(device)
logging.debug("supported device found: " + str(device))
else:
logging.debug("unsupported device found: " + str(device))
logging.debug("finished filter devices")
return filtered_devices
def filter_events(Data):
"""Filters relevant (DeviceStateChangedEvent / DeviceState) events out of an events list."""
logging.debug("start filter events")
filtered_events = []
for event in Data:
if event["name"] in ("DeviceStateChangedEvent", "DeviceState"):
logging.debug(
"get_events: add event: URL: '" + event["deviceURL"] +
"' num states: '" + str(len(event["deviceStates"])) + "'"
)
for event_state in event["deviceStates"]:
logging.debug(
" get_events: eventname: '" + event_state["name"] +
"' with value: '" + str(event_state["value"]) + "'"
)
filtered_events.append(event)
logging.debug("finished filter events")
return filtered_events
def filter_states(Data):
"""Filters relevant state data from a device setup API reply."""
logging.debug("start filter states")
filtered_states = []
for device in Data:
device_url = device["deviceURL"]
device_class = device["definition"]["uiClass"]
if "states" not in device:
continue
state_list = [
state for state in device["states"]
if state["name"] in stateSet
]
if state_list:
filtered_states.append({
"deviceURL": device_url,
"deviceStates": state_list,
"deviceClass": device_class,
"name": "DeviceState"
})
logging.debug("Device state: " + str(filtered_states[-1]))
return filtered_states
# OPT 8: handle_response verwijderd — was een duplicaat van de methode in
# tahoma.py en werd nergens aangeroepen vanuit utils.py