feat(netd): add try catch to handle errors properly
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
a1c608ee0a
commit
3ad658406b
5 changed files with 130 additions and 74 deletions
|
@ -9,7 +9,6 @@
|
|||
./timer.nix
|
||||
];
|
||||
|
||||
# TODO: netd: make error handling with HA notifs
|
||||
# TODO: nix: add HA options for custom_sentences and stuff
|
||||
|
||||
services.home-assistant = {
|
||||
|
|
|
@ -4,6 +4,7 @@ using HomeAssistantGenerated;
|
|||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Integration;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
record PlayAlbumData(string? artist, string? album);
|
||||
|
@ -23,6 +24,8 @@ public class PlayAlbum
|
|||
ha.RegisterServiceCallBack<PlayAlbumData>(
|
||||
"spotify_play_album",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = (await ha.CallServiceWithResponseAsync(
|
||||
"spotifyplus",
|
||||
|
@ -38,10 +41,11 @@ public class PlayAlbum
|
|||
}
|
||||
)).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(
|
||||
"spotifyplus",
|
||||
"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",
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using HomeAssistantGenerated;
|
|||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Integration;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
record PlayArtistData(string? artist);
|
||||
|
@ -23,6 +24,8 @@ public class PlayArtist
|
|||
ha.RegisterServiceCallBack<PlayArtistData>(
|
||||
"spotify_play_artist",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = (await ha.CallServiceWithResponseAsync(
|
||||
"spotifyplus",
|
||||
|
@ -38,10 +41,9 @@ public class PlayArtist
|
|||
}
|
||||
)).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(
|
||||
"spotifyplus",
|
||||
"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",
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -28,7 +28,9 @@ public class PlayPlaylist
|
|||
"spotify_play_playlist",
|
||||
async (e) =>
|
||||
{
|
||||
string? query = e?.playlist;
|
||||
try
|
||||
{
|
||||
string query = e?.playlist ?? throw new NullReferenceException("Query not found.");
|
||||
|
||||
var result = (await ha.CallServiceWithResponseAsync(
|
||||
"spotifyplus",
|
||||
|
@ -41,17 +43,17 @@ public class PlayPlaylist
|
|||
}
|
||||
)).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>(
|
||||
new PlaylistsItem { Name = query.ToLower() },
|
||||
myPlaylists,
|
||||
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
|
||||
if (match.Score < 85)
|
||||
|
@ -70,13 +72,11 @@ public class PlayPlaylist
|
|||
}
|
||||
)).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;
|
||||
}
|
||||
}
|
||||
|
||||
ha.CallService(
|
||||
"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",
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using HomeAssistantGenerated;
|
|||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Integration;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
record PlaySongData(string? artist, string? song);
|
||||
|
@ -22,6 +23,8 @@ public class PlaySong
|
|||
ha.RegisterServiceCallBack<PlaySongData>(
|
||||
"spotify_play_song",
|
||||
async (e) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = (await ha.CallServiceWithResponseAsync(
|
||||
"spotifyplus",
|
||||
|
@ -37,10 +40,10 @@ public class PlaySong
|
|||
}
|
||||
)).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(
|
||||
"spotifyplus",
|
||||
"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",
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue