feat(netd): attempt to fix timeout issue with spotifyplus
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2025-02-19 12:21:51 -05:00
parent 35e91cab7f
commit f7f8b2ec19
6 changed files with 260 additions and 237 deletions

View file

@ -1,5 +1,3 @@
using System;
using HomeAssistantGenerated; using HomeAssistantGenerated;
using NetDaemon.AppModel; using NetDaemon.AppModel;
@ -16,13 +14,7 @@ namespace NetDaemonConfig.Apps.Spotify.PauseUnpause
[NetDaemonApp] [NetDaemonApp]
public class PauseUnpause public class PauseUnpause
{ {
public PauseUnpause(IHaContext ha, Services services) private static void CallBack(PauseUnpauseData e, Services services)
{
ha.RegisterServiceCallBack<PauseUnpauseData>(
"spotify_pause_unpause",
(e) =>
{
try
{ {
if (e.pause) if (e.pause)
{ {
@ -37,13 +29,12 @@ namespace NetDaemonConfig.Apps.Spotify.PauseUnpause
deviceId: Globals.DefaultDevId); deviceId: Globals.DefaultDevId);
} }
} }
catch (Exception error)
public PauseUnpause(IHaContext ha, Services services)
{ {
services.Notify.PersistentNotification( ha.RegisterServiceCallBack<PauseUnpauseData>(
message: error.Message + "\n" + e.ToString(), "spotify_pause_unpause",
title: "Erreur Spotify"); (e) => Globals.RunSpotifyCallback(services, e, CallBack)
}
}
); );
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
using FuzzySharp; using FuzzySharp;
using FuzzySharp.Extractor; using FuzzySharp.Extractor;
@ -31,13 +32,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayAlbum
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayAlbum(IHaContext ha, Services services) private async Task CallBack(PlayAlbumData e, Services services)
{
ha.RegisterServiceCallBack<PlayAlbumData>(
"spotify_play_album",
async (e) =>
{
try
{ {
string uri; string uri;
@ -107,13 +102,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayAlbum
delay: 0.50 delay: 0.50
); );
} }
catch (Exception error)
public PlayAlbum(IHaContext ha, Services services)
{ {
services.Notify.PersistentNotification( ha.RegisterServiceCallBack<PlayAlbumData>(
message: error.Message + "\n" + e.ToString(), "spotify_play_album",
title: "Erreur Spotify"); (e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
}
}
); );
} }
} }

View file

@ -1,6 +1,6 @@
using System;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
using HomeAssistantGenerated; using HomeAssistantGenerated;
@ -24,13 +24,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayArtist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayArtist(IHaContext ha, Services services) private async Task CallBack(PlayArtistData e, Services services)
{
ha.RegisterServiceCallBack<PlayArtistData>(
"spotify_play_artist",
async (e) =>
{
try
{ {
SpotifyplusSearchArtistsResponse? result = ( SpotifyplusSearchArtistsResponse? result = (
await services.Spotifyplus.SearchArtistsAsync( await services.Spotifyplus.SearchArtistsAsync(
@ -55,13 +49,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayArtist
delay: 0.50 delay: 0.50
); );
} }
catch (Exception error)
public PlayArtist(IHaContext ha, Services services)
{ {
services.Notify.PersistentNotification( ha.RegisterServiceCallBack<PlayArtistData>(
message: error.Message + "\n" + e.ToString(), "spotify_play_artist",
title: "Erreur Spotify"); (e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
}
}
); );
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
using FuzzySharp; using FuzzySharp;
using FuzzySharp.Extractor; using FuzzySharp.Extractor;
@ -31,13 +32,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlayPlaylist
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlayPlaylist(IHaContext ha, Services services) private async Task CallBack(PlayPlaylistData e, Services services)
{
ha.RegisterServiceCallBack<PlayPlaylistData>(
"spotify_play_playlist",
async (e) =>
{
try
{ {
string query = e?.playlist ?? throw new TargetException("Query not found."); string query = e?.playlist ?? throw new TargetException("Query not found.");
@ -89,13 +84,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlayPlaylist
delay: 0.50 delay: 0.50
); );
} }
catch (Exception error)
public PlayPlaylist(IHaContext ha, Services services)
{ {
services.Notify.PersistentNotification( ha.RegisterServiceCallBack<PlayPlaylistData>(
message: error.Message + "\n" + e.ToString(), "spotify_play_playlist",
title: "Erreur Spotify"); (e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
}
}
); );
} }
} }

View file

@ -1,6 +1,6 @@
using System;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
using HomeAssistantGenerated; using HomeAssistantGenerated;
@ -24,13 +24,7 @@ namespace NetDaemonConfig.Apps.Spotify.PlaySong
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}; };
public PlaySong(IHaContext ha, Services services) private async Task CallBack(PlaySongData e, Services services)
{
ha.RegisterServiceCallBack<PlaySongData>(
"spotify_play_song",
async (e) =>
{
try
{ {
SpotifyplusSearchTracksResponse? result = ( SpotifyplusSearchTracksResponse? result = (
await services.Spotifyplus.SearchTracksAsync( await services.Spotifyplus.SearchTracksAsync(
@ -56,13 +50,12 @@ namespace NetDaemonConfig.Apps.Spotify.PlaySong
delay: 0.50 delay: 0.50
); );
} }
catch (Exception error)
public PlaySong(IHaContext ha, Services services)
{ {
services.Notify.PersistentNotification( ha.RegisterServiceCallBack<PlaySongData>(
message: error.Message + "\n" + e.ToString(), "spotify_play_song",
title: "Erreur Spotify"); (e) => Globals.RunAsyncSpotifyCallback(services, e, CallBack)
}
}
); );
} }
} }

View file

@ -1,4 +1,8 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using HomeAssistantGenerated;
namespace NetDaemonConfig.Apps.Spotify.Types namespace NetDaemonConfig.Apps.Spotify.Types
{ {
@ -6,6 +10,60 @@ namespace NetDaemonConfig.Apps.Spotify.Types
{ {
public const string DefaultDevId = "homie"; public const string DefaultDevId = "homie";
public const string DefaultEntityId = "media_player.spotifyplus"; 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 // https://jsonformatter.org/yaml-to-json