feat(hass): extend options of services with configFiles
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-10-09 23:14:05 -04:00
parent 56eeb7745c
commit 8bcb9c43ad
6 changed files with 125 additions and 61 deletions

View file

@ -1,4 +1,4 @@
{pkgs, ...}: { {pkgs, self, ...}: {
imports = [ imports = [
./assist.nix ./assist.nix
./bluetooth.nix ./bluetooth.nix
@ -7,9 +7,9 @@
./netdaemon ./netdaemon
./spotify.nix ./spotify.nix
./timer.nix ./timer.nix
];
# TODO: nix: add HA options for custom_sentences and stuff self.nixosModules.ha-plus
];
services.home-assistant = { services.home-assistant = {
enable = true; enable = true;

View file

@ -3,31 +3,18 @@
dracul-ha-src, dracul-ha-src,
material-rounded-theme-src, material-rounded-theme-src,
material-symbols-src, material-symbols-src,
lib,
pkgs, pkgs,
... ...
}: let }: let
inherit (lib) concatStringsSep getExe;
inherit (pkgs.writers) writeYAML; inherit (pkgs.writers) writeYAML;
themes = [
"${caule-themes-src}/themes/caule-themes-pack-1.yaml"
"${dracul-ha-src}/themes/dracul-ha.yaml"
"${material-rounded-theme-src}/themes/material_rounded.yaml"
];
in { in {
systemd.services.home-assistant.preStart = let
WorkingDirectory = "/var/lib/hass";
in
getExe (pkgs.writeShellApplication {
name = "ha-themes";
text = ''
mkdir -p ${WorkingDirectory}/themes
cp -f ${concatStringsSep " " themes} ${WorkingDirectory}/themes
'';
});
services.home-assistant = { services.home-assistant = {
configFiles = {
"themes/caule.yaml".source = "${caule-themes-src}/themes/caule-themes-pack-1.yaml";
"themes/dracul-ha.yaml".source = "${dracul-ha-src}/themes/dracul-ha.yaml";
"themes/material_rounded.yaml".source = "${material-rounded-theme-src}/themes/material_rounded.yaml";
};
customLovelaceModules = builtins.attrValues { customLovelaceModules = builtins.attrValues {
inherit inherit
(pkgs.home-assistant-custom-lovelace-modules) (pkgs.home-assistant-custom-lovelace-modules)

View file

@ -1,27 +1,9 @@
{ {
config, config,
lib,
pkgs, pkgs,
self, self,
... ...
}: let }: {
inherit (lib) getExe;
inherit (pkgs.writers) writeYAML;
in {
systemd.services.home-assistant.preStart = let
WorkingDirectory = "/var/lib/hass";
creds = config.sops.secrets.spotifyd.path;
spotify = writeYAML "assist_spotify.yaml" (import ./spotify-sentences.nix);
in
getExe (pkgs.writeShellApplication {
name = "spotify-files";
text = ''
mkdir -p ${WorkingDirectory}/custom_sentences/en
cp -f ${spotify} ${WorkingDirectory}/custom_sentences/en/assist_spotify.yaml
cp -f ${creds} ${WorkingDirectory}/.storage/SpotifyWebApiPython_librespot_credentials.json
'';
});
services.home-assistant = { services.home-assistant = {
customComponents = builtins.attrValues { customComponents = builtins.attrValues {
inherit inherit
@ -34,6 +16,12 @@ in {
"spotify" "spotify"
]; ];
customSentences."assist_spotify" = import ./spotify-sentences.nix;
configFiles.
".storage/SpotifyWebApiPython_librespot_credentials.json"
.source = config.sops.secrets.spotifyd.path;
config.intent_script = { config.intent_script = {
PlayAlbum = { PlayAlbum = {
async_action = "false"; async_action = "false";

View file

@ -1,11 +1,6 @@
# From https://github.com/don86nl/ha_intents/blob/main/config/packages/assist_timers.yaml # From https://github.com/don86nl/ha_intents/blob/main/config/packages/assist_timers.yaml
{ {lib, ...}: let
lib, inherit (lib) concatStrings concatStringsSep;
pkgs,
...
}: let
inherit (lib) concatStrings concatStringsSep getExe;
inherit (pkgs.writers) writeYAML;
mkTimer = id: { mkTimer = id: {
"assist_timer${toString id}" = { "assist_timer${toString id}" = {
@ -43,20 +38,9 @@
timer_media_location = "/path/to/file.mp3"; timer_media_location = "/path/to/file.mp3";
}; };
in { in {
systemd.services.home-assistant.preStart = let
WorkingDirectory = "/var/lib/hass";
timer = writeYAML "assist_timers.yaml" (import ./timer-sentences.nix);
in
getExe (pkgs.writeShellApplication {
name = "timer-files";
text = ''
mkdir -p ${WorkingDirectory}/custom_sentences/en
cp -f ${timer} ${WorkingDirectory}/custom_sentences/en/assist_timers.yaml
'';
});
services.home-assistant = { services.home-assistant = {
customSentences."assist_timers" = import ./timer-sentences.nix;
config = { config = {
homeassistant.customize."script.assist_timerstart" = {inherit settings;}; homeassistant.customize."script.assist_timerstart" = {inherit settings;};

View file

@ -4,6 +4,7 @@ self: {
desktop = import ./desktop self; desktop = import ./desktop self;
docker = import ./docker self.inputs.khepri; docker = import ./docker self.inputs.khepri;
esphome-plus = import ./esphome-plus; esphome-plus = import ./esphome-plus;
ha-plus = import ./ha-plus;
kmscon = import ./kmscon; kmscon = import ./kmscon;
nvidia = import ./nvidia; nvidia = import ./nvidia;
plymouth = import ./plymouth; plymouth = import ./plymouth;

View file

@ -0,0 +1,104 @@
{
config,
lib,
pkgs,
...
}: let
inherit (lib) any attrValues concatMapStringsSep getExe mapAttrs' mkDefault mkDerivedConfig mkIf mkOption nameValuePair replaceStrings types;
cfg = config.services.home-assistant;
format = pkgs.formats.yaml {};
configFilesList = attrValues cfg.configFiles;
in {
options.services.home-assistant = {
configFiles = mkOption {
default = {};
description = ''
Set of files that have to be linked in the configuration directory.
'';
type = types.attrsOf (types.submodule (
{
name,
config,
options,
...
}: {
options = {
enable = mkOption {
type = types.bool;
default = true;
description = ''
Whether this file should be generated. This
option allows specific files to be disabled.
'';
};
target = mkOption {
type = types.str;
description = ''
Name of symlink (relative to config directory).
Defaults to the attribute name.
'';
};
text = mkOption {
default = null;
type = types.nullOr types.lines;
description = "Text of the file.";
};
source = mkOption {
type = types.path;
description = "Path of the source file.";
};
};
config = {
target = mkDefault name;
source = mkIf (config.text != null) (
let
name' = "haConf-" + replaceStrings ["/"] ["-"] name;
in
mkDerivedConfig options.text (pkgs.writeText name')
);
};
}
));
};
customSentences = mkOption {
type = types.attrsOf (types.submodule {
freeformType = format.type;
options.language = mkOption {
type = types.str;
};
});
};
};
config = mkIf cfg.enable {
systemd.services.home-assistant =
mkIf (
cfg.configFiles != {} && any (c: c.enable) configFilesList
) {
preStart = let
inherit (cfg) configDir;
mkLink = configFile: ''
mkdir -p ${configDir}/${dirOf configFile.target}
cp -rf ${configFile.source} ${configDir}/${configFile.target}
'';
in
getExe (pkgs.writeShellApplication {
name = "home-assistant-pre-start";
text = concatMapStringsSep "\n" mkLink configFilesList;
});
};
services.home-assistant.configFiles = mapAttrs' (n: v:
nameValuePair "custom_sentences/${v.language}/${n}.yaml" {
source = format.generate n v;
})
cfg.customSentences;
};
}