Skip to content

Commit 5215cda

Browse files
Add more try-catches
1 parent b3f3cf5 commit 5215cda

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

streams/spotify_volume_handler.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,47 @@ def on_child_event(self, event_type):
121121

122122
def update_amplipi_volume(self):
123123
"""Update AmpliPi's volume via the Spotify client volume slider"""
124-
spotify_volume = self.spotify.volume
125-
if spotify_volume is None:
126-
return
127-
128-
if abs(spotify_volume - self.shared_volume) <= self.tolerance:
129-
if self.debug:
130-
logger.debug("Ignored minor Spotify -> AmpliPi change")
131-
return
132-
133-
delta = float(spotify_volume - self.shared_volume)
134-
self.amplipi.consume_status(requests.patch(
135-
"http://localhost/api/zones",
136-
json={
137-
"zones": [zone["id"] for zone in self.amplipi.connected_zones],
138-
"update": {"vol_delta_f": delta, "mute": False},
139-
},
140-
timeout=5,
141-
).json())
142-
self.shared_volume = spotify_volume
124+
try:
125+
spotify_volume = self.spotify.volume
126+
if spotify_volume is None:
127+
return
128+
129+
if abs(spotify_volume - self.shared_volume) <= self.tolerance:
130+
if self.debug:
131+
logger.debug("Ignored minor Spotify -> AmpliPi change")
132+
return
133+
134+
delta = float(spotify_volume - self.shared_volume)
135+
self.amplipi.consume_status(requests.patch(
136+
"http://localhost/api/zones",
137+
json={
138+
"zones": [zone["id"] for zone in self.amplipi.connected_zones],
139+
"update": {"vol_delta_f": delta, "mute": False},
140+
},
141+
timeout=5,
142+
).json())
143+
self.shared_volume = spotify_volume
144+
except Exception as e:
145+
logger.exception(f"Exception: {e}")
143146

144147
def update_spotify_volume(self):
145148
"""Update Spotify's volume slider to match AmpliPi"""
146-
amplipi_volume = self.amplipi.volume
147-
if amplipi_volume is None:
148-
return
149-
150-
if abs(amplipi_volume - self.shared_volume) <= self.tolerance:
151-
if self.debug:
152-
logger.debug("Ignored minor AmpliPi -> Spotify change")
153-
return
154-
155-
url = f"http://localhost:{self.spotify.api_port}"
156-
new_vol = int(amplipi_volume * 100)
157-
requests.post(url + '/player/volume', json={"volume": new_vol}, timeout=5)
158-
self.shared_volume = amplipi_volume
149+
try:
150+
amplipi_volume = self.amplipi.volume
151+
if amplipi_volume is None:
152+
return
153+
154+
if abs(amplipi_volume - self.shared_volume) <= self.tolerance:
155+
if self.debug:
156+
logger.debug("Ignored minor AmpliPi -> Spotify change")
157+
return
158+
159+
url = f"http://localhost:{self.spotify.api_port}"
160+
new_vol = int(amplipi_volume * 100)
161+
requests.post(url + '/player/volume', json={"volume": new_vol}, timeout=5)
162+
self.shared_volume = amplipi_volume
163+
except Exception as e:
164+
logger.exception(f"Exception: {e}")
159165

160166

161167
if __name__ == "__main__":
@@ -184,7 +190,7 @@ def update_spotify_volume(self):
184190
logger.exception("Exiting...")
185191
break
186192
except Exception as e:
187-
logger.exception(f"Error: {e}")
193+
logger.exception(f"Exception: {e}")
188194
sleep(5)
189195
continue
190196
sleep(2)

0 commit comments

Comments
 (0)