From 291f42cf30e687d7f82ad283d524919dcbf7fd4e Mon Sep 17 00:00:00 2001 From: matt1432 Date: Sat, 5 Oct 2024 02:57:07 -0400 Subject: [PATCH] feat(hass): add voice command to play a spotify artist's music --- .../modules/home-assistant/bluetooth.nix | 8 -- .../homie/modules/home-assistant/default.nix | 14 +--- .../modules/home-assistant/docs/functions.nix | 30 ++++++++ .../homie/modules/home-assistant/spotify.nix | 75 +++++++++++++++++++ devices/homie/modules/music/default.nix | 13 ---- 5 files changed, 107 insertions(+), 33 deletions(-) create mode 100644 devices/homie/modules/home-assistant/spotify.nix diff --git a/devices/homie/modules/home-assistant/bluetooth.nix b/devices/homie/modules/home-assistant/bluetooth.nix index 6decac53..7ed044e5 100644 --- a/devices/homie/modules/home-assistant/bluetooth.nix +++ b/devices/homie/modules/home-assistant/bluetooth.nix @@ -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" diff --git a/devices/homie/modules/home-assistant/default.nix b/devices/homie/modules/home-assistant/default.nix index e39bcdc9..8017e55f 100644 --- a/devices/homie/modules/home-assistant/default.nix +++ b/devices/homie/modules/home-assistant/default.nix @@ -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" diff --git a/devices/homie/modules/home-assistant/docs/functions.nix b/devices/homie/modules/home-assistant/docs/functions.nix index f931555f..92df1127 100644 --- a/devices/homie/modules/home-assistant/docs/functions.nix +++ b/devices/homie/modules/home-assistant/docs/functions.nix @@ -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 }}"; + }; + } + ]; + }; + } ] diff --git a/devices/homie/modules/home-assistant/spotify.nix b/devices/homie/modules/home-assistant/spotify.nix new file mode 100644 index 00000000..ed5cf83d --- /dev/null +++ b/devices/homie/modules/home-assistant/spotify.nix @@ -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 %} + ''; + }; + } + ]; + } + ]; + }; + }; + }; +} diff --git a/devices/homie/modules/music/default.nix b/devices/homie/modules/music/default.nix index 1cb05350..14b182ac 100644 --- a/devices/homie/modules/music/default.nix +++ b/devices/homie/modules/music/default.nix @@ -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 - ''; - }); }