feat(netd): add try catch to handle errors properly
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-10-09 14:24:07 -04:00
parent a1c608ee0a
commit 3ad658406b
5 changed files with 130 additions and 74 deletions

View file

@ -9,7 +9,6 @@
./timer.nix ./timer.nix
]; ];
# TODO: netd: make error handling with HA notifs
# TODO: nix: add HA options for custom_sentences and stuff # TODO: nix: add HA options for custom_sentences and stuff
services.home-assistant = { services.home-assistant = {

View file

@ -4,6 +4,7 @@ using HomeAssistantGenerated;
using NetDaemon.AppModel; using NetDaemon.AppModel;
using NetDaemon.HassModel; using NetDaemon.HassModel;
using NetDaemon.HassModel.Integration; using NetDaemon.HassModel.Integration;
using System;
using System.Text.Json; using System.Text.Json;
record PlayAlbumData(string? artist, string? album); record PlayAlbumData(string? artist, string? album);
@ -23,6 +24,8 @@ public class PlayAlbum
ha.RegisterServiceCallBack<PlayAlbumData>( ha.RegisterServiceCallBack<PlayAlbumData>(
"spotify_play_album", "spotify_play_album",
async (e) => async (e) =>
{
try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await ha.CallServiceWithResponseAsync(
"spotifyplus", "spotifyplus",
@ -38,10 +41,11 @@ public class PlayAlbum
} }
)).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchAlbumsResponse>(_jsonOptions);
string? uri = result?.Result?.Items?[0]?.Uri; string uri = result?.Result?.Items?[0]?.Uri ??
throw new NullReferenceException(
$"The album {e?.album}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
);
if (uri is not null)
{
ha.CallService( ha.CallService(
"spotifyplus", "spotifyplus",
"player_media_play_context", "player_media_play_context",
@ -56,6 +60,18 @@ public class PlayAlbum
} }
); );
} }
catch (Exception error)
{
ha.CallService(
"notify",
"persistent_notification",
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
}
} }
); );
} }

View file

@ -4,6 +4,7 @@ using HomeAssistantGenerated;
using NetDaemon.AppModel; using NetDaemon.AppModel;
using NetDaemon.HassModel; using NetDaemon.HassModel;
using NetDaemon.HassModel.Integration; using NetDaemon.HassModel.Integration;
using System;
using System.Text.Json; using System.Text.Json;
record PlayArtistData(string? artist); record PlayArtistData(string? artist);
@ -23,6 +24,8 @@ public class PlayArtist
ha.RegisterServiceCallBack<PlayArtistData>( ha.RegisterServiceCallBack<PlayArtistData>(
"spotify_play_artist", "spotify_play_artist",
async (e) => async (e) =>
{
try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await ha.CallServiceWithResponseAsync(
"spotifyplus", "spotifyplus",
@ -38,10 +41,9 @@ public class PlayArtist
} }
)).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchArtistsResponse>(_jsonOptions);
string? uri = result?.Result?.Items?[0]?.Uri; string uri = result?.Result?.Items?[0]?.Uri ??
throw new NullReferenceException($"The artist {e?.artist} could not be found.");
if (uri is not null)
{
ha.CallService( ha.CallService(
"spotifyplus", "spotifyplus",
"player_media_play_context", "player_media_play_context",
@ -56,6 +58,18 @@ public class PlayArtist
} }
); );
} }
catch (Exception error)
{
ha.CallService(
"notify",
"persistent_notification",
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
}
} }
); );
} }

View file

@ -28,7 +28,9 @@ public class PlayPlaylist
"spotify_play_playlist", "spotify_play_playlist",
async (e) => async (e) =>
{ {
string? query = e?.playlist; try
{
string query = e?.playlist ?? throw new NullReferenceException("Query not found.");
var result = (await ha.CallServiceWithResponseAsync( var result = (await ha.CallServiceWithResponseAsync(
"spotifyplus", "spotifyplus",
@ -41,17 +43,17 @@ public class PlayPlaylist
} }
)).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions);
List<PlaylistsItem>? myPlaylists = result?.Result?.Items; List<PlaylistsItem> myPlaylists = result?.Result?.Items ??
throw new NullReferenceException($"No playlists found for query {query}");
if (query is not null && myPlaylists is not null)
{
ExtractedResult<PlaylistsItem> match = Process.ExtractOne<PlaylistsItem>( ExtractedResult<PlaylistsItem> match = Process.ExtractOne<PlaylistsItem>(
new PlaylistsItem { Name = query.ToLower() }, new PlaylistsItem { Name = query.ToLower() },
myPlaylists, myPlaylists,
new Func<PlaylistsItem, string>((item) => (item.Name ?? "").ToLower()) new Func<PlaylistsItem, string>((item) => (item.Name ?? "").ToLower())
); );
string uri = match.Value!.Uri!; string uri = match.Value?.Uri ??
throw new NullReferenceException($"No matches found for query {query}");
// We search outside the user's playlists if the score is too low // We search outside the user's playlists if the score is too low
if (match.Score < 85) if (match.Score < 85)
@ -70,13 +72,11 @@ public class PlayPlaylist
} }
)).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusPlaylistResponse>(_jsonOptions);
string? potentialUri = otherResult?.Result?.Items?[0]?.Uri; string potentialUri = otherResult?.Result?.Items?[0]?.Uri ??
throw new NullReferenceException($"No public matches found for query {query}");
if (potentialUri is not null)
{
uri = potentialUri; uri = potentialUri;
} }
}
ha.CallService( ha.CallService(
"spotifyplus", "spotifyplus",
@ -92,6 +92,18 @@ public class PlayPlaylist
} }
); );
} }
catch (Exception error)
{
ha.CallService(
"notify",
"persistent_notification",
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
}
} }
); );
} }

View file

@ -4,6 +4,7 @@ using HomeAssistantGenerated;
using NetDaemon.AppModel; using NetDaemon.AppModel;
using NetDaemon.HassModel; using NetDaemon.HassModel;
using NetDaemon.HassModel.Integration; using NetDaemon.HassModel.Integration;
using System;
using System.Text.Json; using System.Text.Json;
record PlaySongData(string? artist, string? song); record PlaySongData(string? artist, string? song);
@ -22,6 +23,8 @@ public class PlaySong
ha.RegisterServiceCallBack<PlaySongData>( ha.RegisterServiceCallBack<PlaySongData>(
"spotify_play_song", "spotify_play_song",
async (e) => async (e) =>
{
try
{ {
var result = (await ha.CallServiceWithResponseAsync( var result = (await ha.CallServiceWithResponseAsync(
"spotifyplus", "spotifyplus",
@ -37,10 +40,10 @@ public class PlaySong
} }
)).Value.Deserialize<SpotifyplusSearchTracksResponse>(_jsonOptions); )).Value.Deserialize<SpotifyplusSearchTracksResponse>(_jsonOptions);
string? uri = result?.Result?.Items?[0]?.Uri; string uri = result?.Result?.Items?[0]?.Uri ?? throw new NullReferenceException(
$"The song {e?.song}{(e?.artist is null ? "" : $" by {e?.artist}")} could not be found."
);
if (uri is not null)
{
ha.CallService( ha.CallService(
"spotifyplus", "spotifyplus",
"player_media_play_tracks", "player_media_play_tracks",
@ -55,6 +58,18 @@ public class PlaySong
} }
); );
} }
catch (Exception error)
{
ha.CallService(
"notify",
"persistent_notification",
data: new PersistentNotificationCreateParameters
{
Message = error.Message,
Title = "Erreur Spotify",
}
);
}
} }
); );
} }