feat(netd): attempt to fix timeout issue with spotifyplus
All checks were successful
Discord / discord commits (push) Has been skipped
All checks were successful
Discord / discord commits (push) Has been skipped
This commit is contained in:
parent
35e91cab7f
commit
f7f8b2ec19
6 changed files with 260 additions and 237 deletions
|
@ -1,5 +1,3 @@
|
|||
using System;
|
||||
|
||||
using HomeAssistantGenerated;
|
||||
|
||||
using NetDaemon.AppModel;
|
||||
|
@ -16,13 +14,7 @@ namespace NetDaemonConfig.Apps.Spotify.PauseUnpause
|
|||
[NetDaemonApp]
|
||||
public class PauseUnpause
|
||||
{
|
||||
public PauseUnpause(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PauseUnpauseData>(
|
||||
"spotify_pause_unpause",
|
||||
(e) =>
|
||||
{
|
||||
try
|
||||
private static void CallBack(PauseUnpauseData e, Services services)
|
||||
{
|
||||
if (e.pause)
|
||||
{
|
||||
|
@ -37,13 +29,12 @@ namespace NetDaemonConfig.Apps.Spotify.PauseUnpause
|
|||
deviceId: Globals.DefaultDevId);
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
|
||||
public PauseUnpause(IHaContext ha, Services services)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + e.ToString(),
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
ha.RegisterServiceCallBack<PauseUnpauseData>(
|
||||
"spotify_pause_unpause",
|
||||
(e) => Globals.RunSpotifyCallback(services, e, CallBack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
|
@ -31,13 +32,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayAlbum
|
|||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
};
|
||||
|
||||
public PlayAlbum(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PlayAlbumData>(
|
||||
"spotify_play_album",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
private async Task CallBack(PlayAlbumData e, Services services)
|
||||
{
|
||||
string uri;
|
||||
|
||||
|
@ -107,13 +102,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayAlbum
|
|||
delay: 0.50
|
||||
);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
||||
public PlayAlbum(IHaContext ha, Services services)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + e.ToString(),
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
ha.RegisterServiceCallBack<PlayAlbumData>(
|
||||
"spotify_play_album",
|
||||
(e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using HomeAssistantGenerated;
|
||||
|
||||
|
@ -24,13 +24,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayArtist
|
|||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
};
|
||||
|
||||
public PlayArtist(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PlayArtistData>(
|
||||
"spotify_play_artist",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
private async Task CallBack(PlayArtistData e, Services services)
|
||||
{
|
||||
SpotifyplusSearchArtistsResponse? result = (
|
||||
await services.Spotifyplus.SearchArtistsAsync(
|
||||
|
@ -55,13 +49,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayArtist
|
|||
delay: 0.50
|
||||
);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
||||
public PlayArtist(IHaContext ha, Services services)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + e.ToString(),
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
ha.RegisterServiceCallBack<PlayArtistData>(
|
||||
"spotify_play_artist",
|
||||
(e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
|
@ -31,13 +32,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayPlaylist
|
|||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
};
|
||||
|
||||
public PlayPlaylist(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PlayPlaylistData>(
|
||||
"spotify_play_playlist",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
private async Task CallBack(PlayPlaylistData e, Services services)
|
||||
{
|
||||
string query = e?.playlist ?? throw new TargetException("Query not found.");
|
||||
|
||||
|
@ -89,13 +84,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayPlaylist
|
|||
delay: 0.50
|
||||
);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
||||
public PlayPlaylist(IHaContext ha, Services services)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + e.ToString(),
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
ha.RegisterServiceCallBack<PlayPlaylistData>(
|
||||
"spotify_play_playlist",
|
||||
(e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using HomeAssistantGenerated;
|
||||
|
||||
|
@ -24,13 +24,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlaySong
|
|||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
};
|
||||
|
||||
public PlaySong(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PlaySongData>(
|
||||
"spotify_play_song",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
private async Task CallBack(PlaySongData e, Services services)
|
||||
{
|
||||
SpotifyplusSearchTracksResponse? result = (
|
||||
await services.Spotifyplus.SearchTracksAsync(
|
||||
|
@ -56,13 +50,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlaySong
|
|||
delay: 0.50
|
||||
);
|
||||
}
|
||||
catch (Exception error)
|
||||
|
||||
public PlaySong(IHaContext ha, Services services)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + e.ToString(),
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
ha.RegisterServiceCallBack<PlaySongData>(
|
||||
"spotify_play_song",
|
||||
(e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using HomeAssistantGenerated;
|
||||
|
||||
namespace NetDaemonConfig.Apps.Spotify.Types
|
||||
{
|
||||
|
@ -6,6 +10,60 @@ namespace NetDaemonConfig.Apps.Spotify.Types
|
|||
{
|
||||
public const string DefaultDevId = "homie";
|
||||
public const string DefaultEntityId = "media_player.spotifyplus";
|
||||
|
||||
public static void LogExceptions(Services services, Action callback, string extraInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
callback();
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + extraInfo,
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
|
||||
public static void RunSpotifyCallback<Params>(Services services, Params e, Action<Params, Services> callback)
|
||||
{
|
||||
void Callable() { callback(e, services); }
|
||||
|
||||
// I do this because SpotifyPlus sometimes takes a failed call to successfully reach the speaker
|
||||
try { Callable(); }
|
||||
catch (Exception error)
|
||||
{
|
||||
Console.WriteLine(error.ToString());
|
||||
LogExceptions(services, Callable, e?.ToString() ?? "");
|
||||
}
|
||||
}
|
||||
|
||||
public static async void LogAsyncExceptions(Services services, Func<Task> callback, string extraInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
await callback();
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
services.Notify.PersistentNotification(
|
||||
message: error.Message + "\n" + extraInfo,
|
||||
title: "Erreur Spotify");
|
||||
}
|
||||
}
|
||||
|
||||
public static async void RunAsyncSpotifyCallback<Params>(Services services, Params e, Func<Params, Services, Task> callback)
|
||||
{
|
||||
async Task Callable() { await callback(e, services); }
|
||||
|
||||
// I do this because SpotifyPlus sometimes takes a failed call to successfully reach the speaker
|
||||
try { await Callable(); }
|
||||
catch (Exception error)
|
||||
{
|
||||
Console.WriteLine(error.ToString());
|
||||
LogAsyncExceptions(services, Callable, e?.ToString() ?? "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://jsonformatter.org/yaml-to-json
|
||||
|
|
Loading…
Add table
Reference in a new issue