use device name instead of ID

This commit is contained in:
2023-09-26 01:07:01 +02:00
parent 527200abe7
commit 1c9706485c
4 changed files with 28 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ type alarm struct {
type MorningAlarmConfig struct {
SpotifyClientID string `json:"spotifyClientId"`
SpotifyClientSecret string `json:"spotifyClientSecret"`
DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
RedirectURL string `json:"redirectUrl"`
ListenAddr string `json:"listenAddr"`

View File

@@ -114,7 +114,14 @@ func (ma *MorningAlarm) setupSpotify() error {
// Can still return an error even if the playback was started successfully.
func (ma *MorningAlarm) playWakeUpMusic() (bool, error) {
playContextURI := spotify.URI("spotify:playlist:" + ma.config.PlaylistID)
deviceID := spotify.ID(ma.config.DeviceID)
devID, err := ma.getDeviceIDFromName(ma.config.DeviceName)
if err != nil {
return false, err
}
deviceID := spotify.ID(devID)
playlist, err := ma.sp.GetPlaylistItems(context.Background(), spotify.ID(ma.config.PlaylistID))
@@ -160,3 +167,19 @@ func (ma *MorningAlarm) getAvailableDevices() ([]Device, error) {
return availableDevices, nil
}
func (ma *MorningAlarm) getDeviceIDFromName(name string) (string, error) {
dev, err := ma.getAvailableDevices()
if err != nil {
return "", err
}
for _, device := range dev {
if device.Name == name {
return device.ID, nil
}
}
return "", fmt.Errorf("device with name %s not found", name)
}

View File

@@ -106,7 +106,7 @@ func (ma *MorningAlarm) setupWebserver() {
"timezoneOffsetInH": offset / 60 / 60,
"alarms": len(ma.cr.Entries()),
"wakeupPlaylist": ma.config.PlaylistID,
"deviceId": ma.config.DeviceID,
"deviceName": ma.config.DeviceName,
}
if nextIn != nil {