mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-01-22 14:45:27 +00:00
nixos/home-assistant: fix tests
This commit is contained in:
parent
8dd2327bbd
commit
a68c7e0fa7
|
@ -2,69 +2,53 @@ import ./make-test-python.nix ({ pkgs, ... }:
|
||||||
|
|
||||||
let
|
let
|
||||||
configDir = "/var/lib/foobar";
|
configDir = "/var/lib/foobar";
|
||||||
apiPassword = "some_secret";
|
mqttPassword = "secret";
|
||||||
mqttPassword = "another_secret";
|
|
||||||
hassCli = "hass-cli --server http://hass:8123 --password '${apiPassword}'";
|
|
||||||
in {
|
in {
|
||||||
name = "home-assistant";
|
name = "home-assistant";
|
||||||
meta = with pkgs.stdenv.lib; {
|
meta = with pkgs.stdenv.lib; {
|
||||||
maintainers = with maintainers; [ dotlambda ];
|
maintainers = with maintainers; [ dotlambda ];
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes = {
|
nodes.hass = { pkgs, ... }: {
|
||||||
hass =
|
environment.systemPackages = with pkgs; [ mosquitto ];
|
||||||
{ pkgs, ... }:
|
services.home-assistant = {
|
||||||
{
|
inherit configDir;
|
||||||
environment.systemPackages = with pkgs; [
|
enable = true;
|
||||||
mosquitto home-assistant-cli
|
config = {
|
||||||
];
|
homeassistant = {
|
||||||
services.home-assistant = {
|
name = "Home";
|
||||||
inherit configDir;
|
time_zone = "UTC";
|
||||||
enable = true;
|
latitude = "0.0";
|
||||||
package = pkgs.home-assistant.override {
|
longitude = "0.0";
|
||||||
extraPackages = ps: with ps; [ hbmqtt ];
|
elevation = 0;
|
||||||
};
|
};
|
||||||
config = {
|
frontend = {};
|
||||||
homeassistant = {
|
# uses embedded mqtt broker
|
||||||
name = "Home";
|
mqtt.password = mqttPassword;
|
||||||
time_zone = "UTC";
|
binary_sensor = [{
|
||||||
latitude = "0.0";
|
platform = "mqtt";
|
||||||
longitude = "0.0";
|
state_topic = "home-assistant/test";
|
||||||
elevation = 0;
|
payload_on = "let_there_be_light";
|
||||||
auth_providers = [
|
payload_off = "off";
|
||||||
{
|
}];
|
||||||
type = "legacy_api_password";
|
logger = {
|
||||||
api_password = apiPassword;
|
default = "info";
|
||||||
}
|
logs."homeassistant.components.mqtt" = "debug";
|
||||||
];
|
|
||||||
};
|
|
||||||
frontend = { };
|
|
||||||
mqtt = { # Use hbmqtt as broker
|
|
||||||
password = mqttPassword;
|
|
||||||
};
|
|
||||||
binary_sensor = [
|
|
||||||
{
|
|
||||||
platform = "mqtt";
|
|
||||||
state_topic = "home-assistant/test";
|
|
||||||
payload_on = "let_there_be_light";
|
|
||||||
payload_off = "off";
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
lovelaceConfig = {
|
|
||||||
title = "My Awesome Home";
|
|
||||||
views = [ {
|
|
||||||
title = "Example";
|
|
||||||
cards = [ {
|
|
||||||
type = "markdown";
|
|
||||||
title = "Lovelace";
|
|
||||||
content = "Welcome to your **Lovelace UI**.";
|
|
||||||
} ];
|
|
||||||
} ];
|
|
||||||
};
|
|
||||||
lovelaceConfigWritable = true;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
lovelaceConfig = {
|
||||||
|
title = "My Awesome Home";
|
||||||
|
views = [{
|
||||||
|
title = "Example";
|
||||||
|
cards = [{
|
||||||
|
type = "markdown";
|
||||||
|
title = "Lovelace";
|
||||||
|
content = "Welcome to your **Lovelace UI**.";
|
||||||
|
}];
|
||||||
|
}];
|
||||||
|
};
|
||||||
|
lovelaceConfigWritable = true;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
testScript = ''
|
testScript = ''
|
||||||
|
@ -77,28 +61,13 @@ in {
|
||||||
with subtest("Check that Home Assistant's web interface and API can be reached"):
|
with subtest("Check that Home Assistant's web interface and API can be reached"):
|
||||||
hass.wait_for_open_port(8123)
|
hass.wait_for_open_port(8123)
|
||||||
hass.succeed("curl --fail http://localhost:8123/lovelace")
|
hass.succeed("curl --fail http://localhost:8123/lovelace")
|
||||||
assert "API running" in hass.succeed(
|
|
||||||
"curl --fail -H 'x-ha-access: ${apiPassword}' http://localhost:8123/api/"
|
|
||||||
)
|
|
||||||
with subtest("Toggle a binary sensor using MQTT"):
|
with subtest("Toggle a binary sensor using MQTT"):
|
||||||
assert '"state": "off"' in hass.succeed(
|
# wait for broker to become available
|
||||||
"curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}'"
|
|
||||||
)
|
|
||||||
hass.wait_until_succeeds(
|
hass.wait_until_succeeds(
|
||||||
"mosquitto_pub -V mqttv311 -t home-assistant/test -u homeassistant -P '${mqttPassword}' -m let_there_be_light"
|
"mosquitto_sub -V mqttv311 -t home-assistant/test -u homeassistant -P '${mqttPassword}' -W 1 -t '*'"
|
||||||
)
|
|
||||||
assert '"state": "on"' in hass.succeed(
|
|
||||||
"curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}'"
|
|
||||||
)
|
|
||||||
with subtest("Toggle a binary sensor using hass-cli"):
|
|
||||||
assert '"state": "on"' in hass.succeed(
|
|
||||||
"${hassCli} --output json state get binary_sensor.mqtt_binary_sensor"
|
|
||||||
)
|
)
|
||||||
hass.succeed(
|
hass.succeed(
|
||||||
"${hassCli} state edit binary_sensor.mqtt_binary_sensor --json='{\"state\": \"off\"}'"
|
"mosquitto_pub -V mqttv311 -t home-assistant/test -u homeassistant -P '${mqttPassword}' -m let_there_be_light"
|
||||||
)
|
|
||||||
assert '"state": "off"' in hass.succeed(
|
|
||||||
"curl http://localhost:8123/api/states/binary_sensor.mqtt_binary_sensor -H 'x-ha-access: ${apiPassword}'"
|
|
||||||
)
|
)
|
||||||
with subtest("Print log to ease debugging"):
|
with subtest("Print log to ease debugging"):
|
||||||
output_log = hass.succeed("cat ${configDir}/home-assistant.log")
|
output_log = hass.succeed("cat ${configDir}/home-assistant.log")
|
||||||
|
@ -107,5 +76,9 @@ in {
|
||||||
|
|
||||||
with subtest("Check that no errors were logged"):
|
with subtest("Check that no errors were logged"):
|
||||||
assert "ERROR" not in output_log
|
assert "ERROR" not in output_log
|
||||||
|
|
||||||
|
# example line: 2020-06-20 10:01:32 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on home-assistant/test: b'let_there_be_light'
|
||||||
|
with subtest("Check we received the mosquitto message"):
|
||||||
|
assert "let_there_be_light" in output_log
|
||||||
'';
|
'';
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{ stdenv, lib, fetchurl, fetchFromGitHub, fetchpatch, python3, protobuf3_6
|
{ stdenv, nixosTests, lib, fetchurl, fetchFromGitHub, fetchpatch, python3, protobuf3_6
|
||||||
|
|
||||||
# Look up dependencies of specified components in component-packages.nix
|
# Look up dependencies of specified components in component-packages.nix
|
||||||
, extraComponents ? [ ]
|
, extraComponents ? [ ]
|
||||||
|
@ -123,6 +123,9 @@ in with py.pkgs; buildPythonApplication rec {
|
||||||
|
|
||||||
passthru = {
|
passthru = {
|
||||||
inherit (py.pkgs) hass-frontend;
|
inherit (py.pkgs) hass-frontend;
|
||||||
|
tests = {
|
||||||
|
inherit (nixosTests) home-assistant;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
meta = with lib; {
|
meta = with lib; {
|
||||||
|
|
Loading…
Reference in a new issue