Skip to content

Commit

Permalink
Merge pull request #54541 from dotlambda/home-assistant-0.86
Browse files Browse the repository at this point in the history
home-assistant: 0.85.1 -> 0.86.4
  • Loading branch information
peterhoeg committed Feb 6, 2019
2 parents e73718e + f85453f commit 7003a28
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 62 deletions.
79 changes: 71 additions & 8 deletions nixos/modules/services/misc/home-assistant.nix
Expand Up @@ -6,9 +6,18 @@ let
cfg = config.services.home-assistant;

# cfg.config != null can be assumed here
configFile = pkgs.writeText "configuration.json"
configJSON = 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));
configFile = pkgs.runCommand "configuration.yaml" { } ''
${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out
'';

lovelaceConfigJSON = pkgs.writeText "ui-lovelace.json"
(builtins.toJSON cfg.lovelaceConfig);
lovelaceConfigFile = pkgs.runCommand "ui-lovelace.yaml" { } ''
${pkgs.remarshal}/bin/json2yaml -i ${lovelaceConfigJSON} -o $out
'';

availableComponents = pkgs.home-assistant.availableComponents;

Expand Down Expand Up @@ -45,6 +54,8 @@ let
defaultConfig = {
homeassistant.time_zone = config.time.timeZone;
http.server_port = (toString cfg.port);
} // optionalAttrs (cfg.lovelaceConfig != null) {
lovelace.mode = "yaml";
};

in {
Expand Down Expand Up @@ -99,6 +110,53 @@ in {
'';
};

configWritable = mkOption {
default = false;
type = types.bool;
description = ''
Whether to make <filename>configuration.yaml</filename> writable.
This only has an effect if <option>config</option> is set.
This will allow you to edit it from Home Assistant's web interface.
However, bear in mind that it will be overwritten at every start of the service.
'';
};

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 <filename>ui-lovelace.yaml</filename> as a Nix attribute set.
Setting this option will automatically add
<literal>lovelace.mode = "yaml";</literal> to your <option>config</option>.
Beware that setting this option will delete your previous <filename>ui-lovelace.yaml</filename>
'';
};

lovelaceConfigWritable = mkOption {
default = false;
type = types.bool;
description = ''
Whether to make <filename>ui-lovelace.yaml</filename> writable.
This only has an effect if <option>lovelaceConfig</option> is set.
This will allow you to edit it from Home Assistant's web interface.
However, bear in mind that it will be overwritten at every start of the service.
'';
};

package = mkOption {
default = pkgs.home-assistant;
defaultText = "pkgs.home-assistant";
Expand Down Expand Up @@ -144,12 +202,17 @@ in {
systemd.services.home-assistant = {
description = "Home Assistant";
after = [ "network.target" ];
preStart = lib.optionalString (cfg.config != null) ''
config=${cfg.configDir}/configuration.yaml
rm -f $config
${pkgs.remarshal}/bin/json2yaml -i ${configFile} -o $config
chmod 444 $config
'';
preStart = optionalString (cfg.config != null) (if cfg.configWritable then ''
cp --no-preserve=mode ${configFile} "${cfg.configDir}/configuration.yaml"
'' else ''
rm -f "${cfg.configDir}/configuration.yaml"
ln -s ${configFile} "${cfg.configDir}/configuration.yaml"
'') + optionalString (cfg.lovelaceConfig != null) (if cfg.lovelaceConfigWritable then ''
cp --no-preserve=mode ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
'' else ''
rm -f "${cfg.configDir}/ui-lovelace.yaml"
ln -s ${lovelaceConfigFile} "${cfg.configDir}/ui-lovelace.yaml"
'');
serviceConfig = {
ExecStart = "${package}/bin/hass --config '${cfg.configDir}'";
User = "hass";
Expand Down
18 changes: 16 additions & 2 deletions nixos/tests/home-assistant.nix
Expand Up @@ -50,6 +50,18 @@ in {
}
];
};
lovelaceConfig = {
title = "My Awesome Home";
views = [ {
title = "Example";
cards = [ {
type = "markdown";
title = "Lovelace";
content = "Welcome to your **Lovelace UI**.";
} ];
} ];
};
lovelaceConfigWritable = true;
};
};
};
Expand All @@ -59,8 +71,10 @@ in {
$hass->waitForUnit("home-assistant.service");
# The config is specified using a Nix attribute set,
# but then converted from JSON to YAML
$hass->succeed("test -f ${configDir}/configuration.yaml");
# converted from JSON to YAML, and linked to the config dir
$hass->succeed("test -L ${configDir}/configuration.yaml");
# The lovelace config is copied because lovelaceConfigWritable = true
$hass->succeed("test -f ${configDir}/ui-lovelace.yaml");
# Check that Home Assistant's web interface and API can be reached
$hass->waitForOpenPort(8123);
Expand Down

0 comments on commit 7003a28

Please sign in to comment.