1import ./make-test.nix ({ pkgs, ... }:
2
3let
4 configDir = "/var/lib/foobar";
5 apiPassword = "some_secret";
6 mqttPassword = "another_secret";
7
8in {
9 name = "home-assistant";
10 meta = with pkgs.stdenv.lib; {
11 maintainers = with maintainers; [ dotlambda ];
12 };
13
14 nodes = {
15 hass =
16 { pkgs, ... }:
17 {
18 environment.systemPackages = with pkgs; [
19 mosquitto
20 ];
21 services.home-assistant = {
22 inherit configDir;
23 enable = true;
24 package = pkgs.home-assistant.override {
25 extraPackages = ps: with ps; [ hbmqtt ];
26 };
27 config = {
28 homeassistant = {
29 name = "Home";
30 time_zone = "UTC";
31 latitude = "0.0";
32 longitude = "0.0";
33 elevation = 0;
34 };
35 frontend = { };
36 http.api_password = apiPassword;
37 mqtt = { # Use hbmqtt as broker
38 password = mqttPassword;
39 };
40 binary_sensor = [
41 {
42 platform = "mqtt";
43 state_topic = "home-assistant/test";
44 payload_on = "let_there_be_light";
45 payload_off = "off";
46 }
47 ];
48 };
49 };
50 };
51 };
52
53 testScript = ''
54 startAll;
55 $hass->waitForUnit("home-assistant.service");
56
57 # The config is specified using a Nix attribute set,
58 # but then converted from JSON to YAML
59 $hass->succeed("test -f ${configDir}/configuration.yaml");
60
61 # Check that Home Assistant's web interface and API can be reached
62 $hass->waitForOpenPort(8123);
63 $hass->succeed("curl --fail http://localhost:8123/states");
64 $hass->succeed("curl --fail -H 'x-ha-access: ${apiPassword}' http://localhost:8123/api/ | grep -qF 'API running'");
65
66 # Toggle a binary sensor using MQTT
67 $hass->succeed("curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}' | grep -qF '\"state\": \"off\"'");
68 $hass->waitUntilSucceeds("mosquitto_pub -V mqttv311 -t home-assistant/test -u homeassistant -P '${mqttPassword}' -m let_there_be_light");
69 $hass->succeed("curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}' | grep -qF '\"state\": \"on\"'");
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 # Check that no errors were logged
77 # The timer can get out of sync due to Hydra's load, so this error is ignored
78 $hass->fail("cat ${configDir}/home-assistant.log | grep -vF 'Timer got out of sync' | grep -qF ERROR");
79 '';
80})