-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
213 lines (185 loc) Β· 8.67 KB
/
plugin.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
<plugin key="SonosAPI" name="Sonos API" author="Nick Baring" version="0.3">
<params>
<param field="Mode1" label="Ipadress" width="200px" required="true"/>
<param field="Mode2" label="Port" width="200px" required="true"/>
<param field="Mode3" label="Volume adjustment in %" width="200px" required="true"/>
</params>
</plugin>
"""
import Domoticz
import requests
import json
import time
class SonosAPI:
def __init__(self):
# Initialize a session for making HTTP requests
self._session = requests.Session()
def onStart(self):
# Called when the plugin is started
Domoticz.Log("Sonos API started.")
# Declare Devices variable
global Devices
# Assumes device is on on start
self.device_online = True
# Read options from Domoticz GUI
self.readOptions()
self.favoriteList()
# Check if there are no existing devices
if len(Devices) != 3:
# Example: Create devices
self.createDevices()
else:
options = {
"LevelNames": self.all_favorites_for_device,
"LevelOffHidden": "false",
"SelectorStyle": "1"
}
#Devices[1].Update(Options={"LevelNames": self.all_favorites_for_device})
Domoticz.Heartbeat(15)
def onStop(self):
# Called when the plugin is stopped
Domoticz.Log("Sonos API stopped.")
def readOptions(self):
# Read options from Domoticz GUI
if Parameters["Mode1"]:
self.ipadress = Parameters["Mode1"]
if "Mode2" in Parameters and Parameters["Mode2"]:
self.port = Parameters["Mode2"]
if "Mode3" in Parameters and Parameters["Mode3"]:
self.volumeAdjustment = Parameters["Mode3"]
else:
Domoticz.Error("Ipadress or port not configured")
def favoriteList(self): # Gets all favorites from Sonos.
favorite_response = requests.get(f'http://{self.ipadress}:{self.port}/favorites')
if favorite_response.status_code == 200:
json_response = favorite_response.json()
self.level_names = {0: "Off"} # Default level 0 to "Off"
# Update the dictionary with the response, starting from level 10
self.level_names.update({(i + 1) * 10: name for i, name in enumerate(json_response)})
# Accumulate values of key-value pairs
self.all_favorites_for_device = "|".join(self.level_names.values())
# Log accumulated values
Domoticz.Log("All values: {}".format(self.all_favorites_for_device))
for level, name in self.level_names.items():
Domoticz.Log("Level {}: {}".format(level, name))
else:
Domoticz.Log("Failed to fetch favorites. Status code: {}".format(favorite_response.status_code))
def createDevices(self):
# Create devices for sonos
options = {
"LevelNames": self.all_favorites_for_device,
"LevelOffHidden": "false",
"SelectorStyle": "1"
}
Domoticz.Device(Name="Favorites", Unit=1, TypeName="Selector Switch", Image=8, Options=options, Used=1).Create()
options = {
"LevelNames": "π|π|βββ|βΊββ|βΊβΊβ|π",
"LevelOffHidden": "false",
"SelectorStyle": "0"
}
Domoticz.Device(Name="Control", Unit=2, TypeName="Selector Switch", Image=8, Options=options, Used=1).Create()
Domoticz.Device(Name="Current playing", Unit=3, TypeName="Text", Used=1).Create()
def get_play_state(self,data): # Gets the state of the device, play or shuffle and sets it accordingly
shuffle_value = data["playMode"]["shuffle"]
if shuffle_value == True: # If state is Shuffle set mode in Domoticz to shuffel else play/pause
Devices[2].Update(nValue=50, sValue="50")
else:
Devices[2].Update(nValue=30, sValue="30")
def get_current_playing(self): # Gets current artist and number
response = requests.get(f'http://{self.ipadress}:{self.port}/state')
data = json.loads(response.text) # Extract text content before parsing
current_track = data.get("currentTrack", {})
artist = current_track.get("artist")
album = current_track.get("album")
title = current_track.get("title")
playbackState = data.get("playbackState")
if playbackState != "PLAYING":
Devices[3].Update(nValue=1, sValue="")
elif artist is not None and title is not None and album is not None:
combined_info = f"{artist} - {album} - {title}"
Devices[3].Update(nValue=1, sValue=combined_info)
elif artist is not None and title is not None:
combined_info = f"{artist} - {title}"
Devices[3].Update(nValue=1, sValue=combined_info)
#Domoticz.Log(combined_info)
elif artist is not None:
Devices[3].Update(nValue=1, sValue=artist)
#Domoticz.Log(artist)
elif title is not None:
Devices[3].Update(nValue=1, sValue=title)
#Domoticz.Log(title)
elif album is not None:
Devices[3].Update(nValue=1, sValue=album)
else:
Devices[3].Update(nValue=1, sValue="Artist, album and title are not available")
#Domoticz.Log("Artist and title are not available.")
def onheartbeat(self):
Domoticz.Log("Heartbeat Sonos")
if self.device_online == True:
try:
response = requests.get(f'http://{self.ipadress}:{self.port}/state')
data = json.loads(response.text) # Extract text content before parsing
self.get_play_state(data)
self.get_current_playing()
except requests.RequestException as e:
# Handle HTTP request errors
Domoticz.Error(f"HTTP request error: {e}")
Devices[1].Update(nValue=0,sValue="0") # Sets favorite device to "Off"
self.device_online = False
def onCommand(self, unit, command, level, hue):
try:
self.device_online = True #When oncommand is triggered the get state heartbeat starts again
if unit == 1: # Favorite list device
level_name = self.level_names.get(level) #gets name of the level to be used in the API call
if level == 0:
response = requests.get(
f'http://{self.ipadress}:{self.port}/pause')
Devices[1].Update(nValue=level,sValue=str(level))
Domoticz.Log(response.url)
else:
response = requests.get(
f'http://{self.ipadress}:{self.port}/favorite/{level_name}')
Devices[1].Update(nValue=level,sValue=str(level))
Domoticz.Log(response.url)
response.raise_for_status()
if unit == 2: # Sonos Controll device
if level == 0:
response = requests.get(
f'http://{self.ipadress}:{self.port}/volume/-{self.volumeAdjustment}')
elif level == 10:
response = requests.get(
f'http://{self.ipadress}:{self.port}/volume/+{self.volumeAdjustment}')
elif level == 20:
response = requests.get(
f'http://{self.ipadress}:{self.port}/previous')
time.sleep(1)
self.get_current_playing()
elif level == 30:
response = requests.get(
f'http://{self.ipadress}:{self.port}/playpause')
elif level == 40:
response = requests.get(
f'http://{self.ipadress}:{self.port}/next')
time.sleep(1)
self.get_current_playing()
elif level == 50:
response = requests.get(
f'http://{self.ipadress}:{self.port}/shuffle/toggle')
response.raise_for_status()
except requests.exceptions.RequestException as e:
Domoticz.Error(f"Request failed: {e}")
except Exception as e:
Domoticz.Error(f"An unexpected error occurred: {e}")
# Create an instance of the SonosAPI class
_plugin = SonosAPI()
def onStart():
_plugin.onStart()
def onStop():
_plugin.onStop()
def onHeartbeat():
_plugin.onheartbeat()
def onCommand(unit, command, level, hue):
_plugin.onCommand(unit, command, level, hue)
def onConfigurationChanged():
_plugin.readOptions()