|
4 | 4 | import xbmcaddon |
5 | 5 | import sys |
6 | 6 | import requests |
| 7 | +from requests.auth import HTTPBasicAuth |
7 | 8 | import json |
8 | 9 |
|
9 | 10 | addon_handle = int(sys.argv[1]) |
10 | 11 | xbmcplugin.setContent(addon_handle, 'videos') |
11 | 12 | addonID = 'script.domoticz.scenes' |
12 | | -addonVersion = '0.0.10' |
13 | | -addonDate = "4/1/2021" |
14 | | - |
15 | | - |
| 13 | +addonVersion = '0.0.21' |
| 14 | +addonDate = "26/10/2023" |
16 | 15 |
|
17 | 16 | __addon__ = xbmcaddon.Addon() |
18 | 17 | __addonname__ = __addon__.getAddonInfo('name') |
|
30 | 29 | domoticz_user = xbmcaddon.Addon(id=addonID).getSetting('domoticz_user') |
31 | 30 | domoticz_pass = xbmcaddon.Addon(id=addonID).getSetting('domoticz_pass') |
32 | 31 | domoticz_port = xbmcaddon.Addon(id=addonID).getSetting('domoticz_port') |
| 32 | +domoticz_ssl = xbmcaddon.Addon(id=addonID).getSetting('SSL') |
| 33 | +domoticz_ignore_ssl = xbmcaddon.Addon(id=addonID).getSetting('ignore_SSL') |
33 | 34 | domoticz_group = xbmcaddon.Addon(id=addonID).getSetting('group') |
34 | 35 |
|
35 | 36 |
|
36 | | -def get_base_url(host, port, useSsl, username=None, password=None): |
37 | | - if useSsl is True: |
38 | | - base_url = "https://" + host + ":" + str(port) |
39 | | - else: |
40 | | - base_url = "http://" + host + ":" + str(port) |
| 37 | +def domoticz_submit(query): |
| 38 | + server = domoticz_host |
| 39 | + port = domoticz_port |
| 40 | + usr = domoticz_user |
| 41 | + pwd = domoticz_pass |
| 42 | + ssl = domoticz_ssl |
| 43 | + server = server.replace("http://", "") |
| 44 | + server = server.replace("https://", "") |
41 | 45 |
|
42 | | - if username is not None: |
43 | | - setcreds = 'username=' + username + '&password=' + password + '&' |
| 46 | + if ssl == "true": |
| 47 | + protocol = "https://" |
44 | 48 | else: |
45 | | - setcreds = '' |
| 49 | + protocol = "http://" |
46 | 50 |
|
47 | | - url = base_url + "/json.htm?" + setcreds |
48 | | - return url |
| 51 | + url = protocol + server + ":" + str(port) + "/json.htm?" + query |
| 52 | + |
| 53 | + if domoticz_ignore_ssl == "true": |
| 54 | + requests.packages.urllib3.disable_warnings() |
| 55 | + if not usr and not pwd: |
| 56 | + r = requests.get(url=url, verify=False) |
| 57 | + else: |
| 58 | + r = requests.get(url=url, auth=HTTPBasicAuth(usr, pwd), verify=False) |
| 59 | + else: |
| 60 | + if not usr and not pwd: |
| 61 | + r = requests.get(url=url, verify=True) |
| 62 | + else: |
| 63 | + r = requests.get(url=url, auth=HTTPBasicAuth(usr, pwd), verify=False) |
49 | 64 |
|
| 65 | + if r.status_code == 200: |
| 66 | + result = r.text |
| 67 | + data = json.loads(result) |
| 68 | + return data |
50 | 69 |
|
51 | | -def get_scenes(base_url): |
52 | | - url = base_url + 'type=scenes' |
53 | | - try: |
54 | | - result = requests.get(url) |
55 | | - except: |
56 | | - return -1 |
57 | 70 |
|
58 | | - answer = result.content |
59 | | - jsonResult = json.loads(answer) |
60 | | - deviceResult = jsonResult['result'] |
61 | | - return(deviceResult) |
| 71 | +def domoticz_get_version(): |
| 72 | + query = "type=command¶m=getversion" |
| 73 | + r = domoticz_submit(query=query) |
| 74 | + return r |
62 | 75 |
|
63 | 76 |
|
64 | | -def get_favorite_devices_dict(base_url): |
65 | | - url = base_url + 'type=devices&used=true&filter=all&favorite=1' |
66 | | - try: |
67 | | - result = requests.get(url) |
68 | | - except: |
69 | | - return -1 |
| 77 | +def domoticz_scenes_and_groups(): |
| 78 | + domoticz_version = domoticz_get_version() |
| 79 | + if domoticz_version['Revision'] > 15453 or domoticz_version['version'] == "2023.2 (build 15457)": |
| 80 | + query = "type=command¶m=getscenes" |
| 81 | + else: |
| 82 | + query = 'type=scenes' |
70 | 83 |
|
71 | | - answer = result.content |
| 84 | + r = domoticz_submit(query=query) |
72 | 85 |
|
73 | | - jsonResult = json.loads(answer) |
74 | | - deviceResult = jsonResult['result'] |
75 | | - return(deviceResult) |
| 86 | + devices = r['result'] |
| 87 | + scenes_and_groups = [] |
| 88 | + groups_list = [] |
| 89 | + scenes_list = [] |
| 90 | + for dev in devices: |
| 91 | + scenes_and_groups.append(dev) |
| 92 | + if dev['Type'] == "Group": |
| 93 | + groups_list.append(dev) |
| 94 | + elif dev['Type'] == "Scene": |
| 95 | + scenes_list.append(dev) |
76 | 96 |
|
| 97 | + data = {"Groups": {"result": groups_list}, "Scenes": {"result": scenes_list}, "Groups and Scenes": {"result": scenes_and_groups}} |
| 98 | + return data |
77 | 99 |
|
78 | | -def get_all_switches(base_url): |
79 | | - url = base_url + 'type=devices&filter=light&used=true&order=Name' |
80 | | - try: |
81 | | - result = requests.get(url) |
82 | | - except: |
83 | | - return -1 |
84 | 100 |
|
85 | | - answer = result.content |
| 101 | +def domoticz_favorites(): |
| 102 | + domoticz_version = domoticz_get_version() |
| 103 | + if domoticz_version['Revision'] > 15453 or domoticz_version['version'] == "2023.2 (build 15457)": |
| 104 | + query = "type=command¶m=getdevices&used=true&filter=all&favorite=1" |
| 105 | + else: |
| 106 | + query = 'type=devices&used=true&filter=all&favorite=1' |
| 107 | + r = domoticz_submit(query=query) |
| 108 | + return r |
86 | 109 |
|
87 | | - jsonResult = json.loads(answer) |
88 | | - deviceResult = jsonResult['result'] |
89 | | - return(deviceResult) |
90 | 110 |
|
| 111 | +def domoticz_light_switches(): |
| 112 | + domoticz_version = domoticz_get_version() |
| 113 | + if domoticz_version['Revision'] > 15453 or domoticz_version['version'] == "2023.2 (build 15457)": |
| 114 | + query = "type=command¶m=getdevices&filter=light&used=true&order=Name" |
| 115 | + else: |
| 116 | + query = 'type=devices&filter=light&used=true&order=Name' |
| 117 | + r = domoticz_submit(query=query) |
| 118 | + return r |
91 | 119 |
|
92 | | -def switch_scene(base_url, idx): |
93 | | - url = base_url + "type=command¶m=switchscene&idx=" + str(idx) + "&switchcmd=On" |
94 | | - requests.get(url=url) |
95 | 120 |
|
| 121 | +def domoticz_start_scene(idx): |
| 122 | + query = "type=command¶m=switchscene&idx=" + str(idx) + "&switchcmd=On" |
| 123 | + r = domoticz_submit(query=query) |
| 124 | + return r |
96 | 125 |
|
97 | | -def switch_switch(base_url, idx): |
98 | | - url = base_url + "type=command¶m=switchlight&idx=" + str(idx) + "&switchcmd=Toggle" |
99 | | - requests.get(url=url) |
100 | 126 |
|
| 127 | +def domoticz_toggle_group(idx): |
| 128 | + query = "type=command¶m=switchscene&idx=" + str(idx) + "&switchcmd=Toggle" |
| 129 | + r = domoticz_submit(query=query) |
| 130 | + return r |
101 | 131 |
|
102 | | -def switch_dimmer(base_url, idx, state): |
103 | | - url = base_url + "type=command¶m=switchlight&idx=" + str(idx) + "&switchcmd=Set%20Level&level=" + state |
104 | | - requests.get(url=url) |
105 | 132 |
|
| 133 | +def domoticz_toggle_switch(idx): |
| 134 | + query = "type=command¶m=switchlight&idx=" + str(idx) + "&switchcmd=Toggle" |
| 135 | + r = domoticz_submit(query=query) |
| 136 | + return r |
106 | 137 |
|
107 | | -def get_list(optionsDict): |
108 | | - end_list = __addon__.getLocalizedString(30499) |
109 | 138 |
|
| 139 | +def create_optionsList(optionsDict): |
110 | 140 | optionsList = [] |
111 | 141 | for line in optionsDict: |
112 | 142 | optionsList.append(line['Name']) |
113 | | - optionsList.append(end_list) |
114 | 143 | return optionsList |
115 | 144 |
|
116 | 145 |
|
117 | | -base_url = get_base_url(host=domoticz_host, port=domoticz_port, useSsl=False, username=domoticz_user, password=domoticz_pass) |
118 | | - |
119 | | -if str(domoticz_group) == '0': |
120 | | - optionsDict = get_scenes(base_url=base_url) |
121 | | - optionsList = get_list(optionsDict=optionsDict) |
122 | | - |
123 | | -elif str(domoticz_group) == '1': |
124 | | - optionsDict = get_all_switches(base_url=base_url) |
125 | | - optionsList = get_list(optionsDict=optionsDict) |
126 | | - |
127 | | -elif str(domoticz_group) == '2': |
128 | | - optionsDict = get_favorite_devices_dict(base_url=base_url) |
129 | | - optionsList = get_list(optionsDict=optionsDict) |
130 | | - |
131 | | -else: |
132 | | - optionsDict = get_all_switches(base_url=base_url) |
133 | | - optionsList = get_list(optionsDict=optionsDict) |
134 | | - |
135 | | -title = __addon__.getLocalizedString(30498) |
136 | | - |
137 | | -answer = xbmcgui.Dialog().select(heading=title, list=optionsList) |
138 | | -action = optionsList[answer] |
139 | | - |
140 | | - |
141 | 146 | def get_idx(optionsDict, action): |
142 | | - for line in optionsDict: |
| 147 | + for line in optionsDict['result']: |
143 | 148 | Name = line['Name'] |
144 | 149 | if Name == action: |
145 | 150 | idx = line['idx'] |
146 | 151 | return idx |
147 | 152 |
|
148 | 153 |
|
| 154 | +def get_group_scene_idx_type(optionsDict, action): |
| 155 | + for dev in optionsDict['result']: |
| 156 | + if dev['Name'] == action: |
| 157 | + if "Type" in dev: |
| 158 | + return dev['idx'], dev['Type'] |
| 159 | + |
| 160 | + |
149 | 161 | def get_favorites_idx(optionsDict, action): |
150 | | - for line in optionsDict: |
| 162 | + for line in optionsDict['result']: |
151 | 163 | Name = line['Name'] |
152 | 164 | if Name == action: |
153 | 165 | idx = line['idx'] |
154 | 166 | type = line['Type'] |
155 | 167 | return idx, type |
156 | 168 |
|
157 | 169 |
|
158 | | -def run(): |
159 | | - end_list = __addon__.getLocalizedString(30499) |
| 170 | +def create_optionsDict(): |
| 171 | + if str(domoticz_group) == '0': |
| 172 | + data = domoticz_scenes_and_groups() |
| 173 | + optionsDict = data["Groups and Scenes"] |
| 174 | + optionsList = create_optionsList(optionsDict=optionsDict['result']) |
| 175 | + |
| 176 | + elif str(domoticz_group) == '1': |
| 177 | + optionsDict = domoticz_light_switches() |
| 178 | + optionsList = create_optionsList(optionsDict=optionsDict['result']) |
| 179 | + |
| 180 | + elif str(domoticz_group) == '2': |
| 181 | + optionsDict = domoticz_favorites() |
| 182 | + optionsList = create_optionsList(optionsDict=optionsDict['result']) |
| 183 | + |
| 184 | + else: |
| 185 | + optionsDict = domoticz_light_switches() |
| 186 | + optionsList = create_optionsList(optionsDict=optionsDict['result']) |
| 187 | + |
| 188 | + return optionsDict, optionsList |
| 189 | + |
| 190 | + |
| 191 | +title = __addon__.getLocalizedString(30498) |
160 | 192 |
|
161 | | - if action != end_list: |
| 193 | +domoticz_version = domoticz_get_version() |
| 194 | +optionsDict, optionsList = create_optionsDict() |
| 195 | + |
| 196 | +answer = xbmcgui.Dialog().select(heading=title, list=optionsList) |
| 197 | +if answer == -1: |
| 198 | + action = None |
| 199 | +else: |
| 200 | + action = optionsList[answer] |
| 201 | + |
| 202 | + |
| 203 | +def run(): |
| 204 | + if action and action in optionsList: |
162 | 205 | if str(domoticz_group) == '0': |
163 | | - idx = get_idx(optionsDict=optionsDict, action=action) |
164 | | - switch_scene(base_url=base_url, idx=idx) |
| 206 | + idx, groupType = get_group_scene_idx_type(optionsDict=optionsDict, action=action) |
| 207 | + if groupType == "Scene": |
| 208 | + domoticz_start_scene(idx=idx) |
| 209 | + if groupType == "Group": |
| 210 | + domoticz_toggle_group(idx=idx) |
165 | 211 | elif str(domoticz_group) == '1': |
166 | 212 | idx = get_idx(optionsDict=optionsDict, action=action) |
167 | | - switch_switch(base_url=base_url, idx=idx) |
| 213 | + domoticz_toggle_switch(idx=idx) |
168 | 214 | elif str(domoticz_group) == '2': |
169 | 215 | idx, type = get_favorites_idx(optionsDict=optionsDict, action=action) |
170 | 216 | if type == 'Scene': |
171 | | - switch_scene(base_url=base_url, idx=idx) |
| 217 | + domoticz_start_scene(idx=idx) |
| 218 | + if type == 'Group': |
| 219 | + domoticz_toggle_group(idx=idx) |
172 | 220 | if type == 'Light/Switch': |
173 | | - switch_switch(base_url=base_url, idx=idx) |
| 221 | + domoticz_toggle_switch(idx=idx) |
174 | 222 | if type == 'Color Switch': |
175 | | - switch_switch(base_url=base_url, idx=idx) |
| 223 | + domoticz_toggle_switch(idx=idx) |
176 | 224 | else: |
177 | 225 | idx = get_idx(optionsDict=optionsDict, action=action) |
178 | | - switch_switch(base_url=base_url, idx=idx) |
| 226 | + domoticz_toggle_switch(idx=idx) |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
0 commit comments