From 7cc7c5374c1bbad10774d81d81e644662b06acb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=BCtz?= Date: Thu, 24 Jan 2019 15:52:05 +0100 Subject: [PATCH] nixos/home-assistant: add lovelaceConfig option --- .../modules/services/misc/home-assistant.nix | 41 +++++++++++++++++-- nixos/tests/home-assistant.nix | 11 +++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/misc/home-assistant.nix b/nixos/modules/services/misc/home-assistant.nix index 2e9aa33aeeee..48271eeacc38 100644 --- a/nixos/modules/services/misc/home-assistant.nix +++ b/nixos/modules/services/misc/home-assistant.nix @@ -8,7 +8,10 @@ let # cfg.config != null can be assumed here configFile = pkgs.writeText "configuration.json" (builtins.toJSON (if cfg.applyDefaultConfig then - (lib.recursiveUpdate defaultConfig cfg.config) else cfg.config)); + (recursiveUpdate defaultConfig cfg.config) else cfg.config)); + + lovelaceConfigFile = pkgs.writeText "ui-lovelace.json" + (builtins.toJSON cfg.lovelaceConfig); availableComponents = pkgs.home-assistant.availableComponents; @@ -45,6 +48,8 @@ let defaultConfig = { homeassistant.time_zone = config.time.timeZone; http.server_port = (toString cfg.port); + } // optionalAttrs (cfg.lovelaceConfig != null) { + lovelace.mode = "yaml"; }; in { @@ -99,6 +104,31 @@ in { ''; }; + lovelaceConfig = mkOption { + default = null; + type = with types; nullOr attrs; + # from https://www.home-assistant.io/lovelace/yaml-mode/ + example = literalExample '' + { + title = "My Awesome Home"; + views = [ { + title = "Example"; + cards = [ { + type = "markdown"; + title = "Lovelace"; + content = "Welcome to your **Lovelace UI**."; + } ]; + } ]; + } + ''; + description = '' + Your ui-lovelace.yaml as a Nix attribute set. + Setting this option will automatically add + lovelace.mode = "yaml"; to your . + Beware that setting this option will delete your previous ui-lovelace.yaml + ''; + }; + package = mkOption { default = pkgs.home-assistant; defaultText = "pkgs.home-assistant"; @@ -144,11 +174,16 @@ in { systemd.services.home-assistant = { description = "Home Assistant"; after = [ "network.target" ]; - preStart = lib.optionalString (cfg.config != null) '' - config=${cfg.configDir}/configuration.yaml + preStart = optionalString (cfg.config != null) '' + config="${cfg.configDir}/configuration.yaml" rm -f $config ${pkgs.remarshal}/bin/json2yaml -i ${configFile} -o $config chmod 444 $config + '' + optionalString (cfg.lovelaceConfig != null) '' + config="${cfg.configDir}/ui-lovelace.yaml" + rm -f $config + ${pkgs.remarshal}/bin/json2yaml -i ${lovelaceConfigFile} -o $config + chmod 444 $config ''; serviceConfig = { ExecStart = "${package}/bin/hass --config '${cfg.configDir}'"; diff --git a/nixos/tests/home-assistant.nix b/nixos/tests/home-assistant.nix index 73c1e71eb516..6e8eda30e766 100644 --- a/nixos/tests/home-assistant.nix +++ b/nixos/tests/home-assistant.nix @@ -50,6 +50,17 @@ in { } ]; }; + lovelaceConfig = { + title = "My Awesome Home"; + views = [ { + title = "Example"; + cards = [ { + type = "markdown"; + title = "Lovelace"; + content = "Welcome to your **Lovelace UI**."; + } ]; + } ]; + }; }; }; };