1import ./make-test.nix ({ pkgs, ... }:
2
3let
4 configDir = "/var/lib/foobar";
5 apiPassword = "secret";
6
7in {
8 name = "home-assistant";
9 meta = with pkgs.stdenv.lib; {
10 maintainers = with maintainers; [ dotlambda ];
11 };
12
13 nodes = {
14 hass =
15 { config, pkgs, ... }:
16 {
17 environment.systemPackages = with pkgs; [
18 mosquitto
19 ];
20 services.home-assistant = {
21 inherit configDir;
22 enable = true;
23 package = pkgs.home-assistant.override {
24 extraPackages = ps: with ps; [ hbmqtt ];
25 };
26 config = {
27 homeassistant = {
28 name = "Home";
29 time_zone = "UTC";
30 latitude = "0.0";
31 longitude = "0.0";
32 elevation = 0;
33 };
34 frontend = { };
35 http.api_password = apiPassword;
36 mqtt = { }; # Use hbmqtt as broker
37 binary_sensor = [
38 {
39 platform = "mqtt";
40 state_topic = "home-assistant/test";
41 payload_on = "let_there_be_light";
42 payload_off = "off";
43 }
44 ];
45 };
46 };
47 };
48 };
49
50 testScript = ''
51 startAll;
52 $hass->waitForUnit("home-assistant.service");
53
54 # Since config is specified using a Nix attribute set,
55 # configuration.yaml is a link to the Nix store
56 $hass->succeed("test -L ${configDir}/configuration.yaml");
57
58 # Check that Home Assistant's web interface and API can be reached
59 $hass->waitForOpenPort(8123);
60 $hass->succeed("curl --fail http://localhost:8123/states");
61 $hass->succeed("curl --fail -H 'x-ha-access: ${apiPassword}' http://localhost:8123/api/ | grep -qF 'API running'");
62
63 # Toggle a binary sensor using MQTT
64 $hass->succeed("curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}' | grep -qF '\"state\": \"off\"'");
65 $hass->waitUntilSucceeds("mosquitto_pub -V mqttv311 -t home-assistant/test -u homeassistant -P '${apiPassword}' -m let_there_be_light");
66 $hass->succeed("curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}' | grep -qF '\"state\": \"on\"'");
67
68 # Check that no errors were logged
69 $hass->fail("cat ${configDir}/home-assistant.log | grep -qF ERROR");
70
71 # Print log to ease debugging
72 my $log = $hass->succeed("cat ${configDir}/home-assistant.log");
73 print "\n### home-assistant.log ###\n";
74 print "$log\n";
75 '';
76})