feat(hass): add voice command to play a spotify artist's music
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-10-05 02:57:07 -04:00
parent e13ae2af57
commit 291f42cf30
5 changed files with 107 additions and 33 deletions

View file

@ -2,7 +2,6 @@
config,
lib,
pkgs,
self,
...
}: let
inherit (lib) getExe;
@ -27,13 +26,6 @@ in {
environment.systemPackages = [turnOnUE];
services.home-assistant = {
customComponents = builtins.attrValues {
inherit
(self.legacyPackages.${pkgs.system}.hass-components)
spotifyplus
;
};
extraComponents = [
"mpd"

View file

@ -1,13 +1,10 @@
{
pkgs,
self,
...
}: {
{pkgs, ...}: {
imports = [
./assist.nix
./bluetooth.nix
./firmware.nix
./frontend.nix
./spotify.nix
./timer.nix
];
@ -51,12 +48,6 @@
services.home-assistant = {
enable = true;
package = pkgs.home-assistant.override {
packageOverrides = _: super: {
inherit (self.packages.${pkgs.system}) urllib3;
};
};
extraComponents = [
"androidtv_remote"
"caldav"
@ -64,7 +55,6 @@
"holiday"
"isal"
"met"
"spotify"
"switchbot"
"upnp"
"yamaha_musiccast"

View file

@ -275,4 +275,34 @@ in [
'');
};
}
{
spec = {
name = "play_artist";
description = "Use this function to play music from an artist";
parameters = {
type = "object";
properties.query = {
type = "string";
description = "The query";
};
required = ["query"];
};
};
function = {
type = "script";
sequence = [
{
service = "script.play_artist";
data = {
criteria = "{{ query }}";
};
}
];
};
}
]

View file

@ -0,0 +1,75 @@
{
config,
lib,
pkgs,
self,
...
}: let
inherit (lib) getExe;
in {
systemd.services.home-assistant.preStart = let
WorkingDirectory = "/var/lib/hass";
creds = config.sops.secrets.spotifyd.path;
in
getExe (pkgs.writeShellApplication {
name = "spotify-plus-creds";
text = ''
cp -f ${creds} ${WorkingDirectory}/.storage/SpotifyWebApiPython_librespot_credentials.json
'';
});
services.home-assistant = {
# Needed for spotifyplus
package = pkgs.home-assistant.override {
packageOverrides = _: super: {
inherit (self.packages.${pkgs.system}) urllib3;
};
};
customComponents = builtins.attrValues {
inherit
(self.legacyPackages.${pkgs.system}.hass-components)
spotifyplus
;
};
extraComponents = [
"spotify"
];
config = {
script.play_artist = {
alias = "Spotify - Play Artist";
sequence = [
{
sequence = [
{
action = "spotifyplus.search_artists";
data = {
entity_id = "media_player.spotifyplus";
criteria = ''{{ criteria }}'';
limit = 1;
};
response_variable = "sp_results";
}
{
action = "spotifyplus.player_media_play_context";
data = {
entity_id = "media_player.spotifyplus";
context_uri = ''
{% for item in sp_results.result | dictsort %}
{% if item[0] == 'items' %}
{{ item[1][0].uri }}
{% break %}
{% endif %}
{%- endfor %}
'';
};
}
];
}
];
};
};
};
}

View file

@ -1,10 +1,8 @@
{
config,
lib,
pkgs,
...
}: let
inherit (lib) getExe;
inherit (config.vars) mainUser;
in {
hardware.bluetooth = {
@ -91,15 +89,4 @@ in {
volume_normalisation = false;
};
};
systemd.services.home-assistant.preStart = let
WorkingDirectory = "/var/lib/hass";
creds = config.sops.secrets.spotifyd.path;
in
getExe (pkgs.writeShellApplication {
name = "spotify-plus-creds";
text = ''
cp -f ${creds} ${WorkingDirectory}/.storage/SpotifyWebApiPython_librespot_credentials.json
'';
});
}