nixos/home-assistant: fix tests
This commit is contained in:
parent
8dd2327bbd
commit
a68c7e0fa7
@ -2,28 +2,18 @@ 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, ... }:
|
|
||||||
{
|
|
||||||
environment.systemPackages = with pkgs; [
|
|
||||||
mosquitto home-assistant-cli
|
|
||||||
];
|
|
||||||
services.home-assistant = {
|
services.home-assistant = {
|
||||||
inherit configDir;
|
inherit configDir;
|
||||||
enable = true;
|
enable = true;
|
||||||
package = pkgs.home-assistant.override {
|
|
||||||
extraPackages = ps: with ps; [ hbmqtt ];
|
|
||||||
};
|
|
||||||
config = {
|
config = {
|
||||||
homeassistant = {
|
homeassistant = {
|
||||||
name = "Home";
|
name = "Home";
|
||||||
@ -31,25 +21,20 @@ in {
|
|||||||
latitude = "0.0";
|
latitude = "0.0";
|
||||||
longitude = "0.0";
|
longitude = "0.0";
|
||||||
elevation = 0;
|
elevation = 0;
|
||||||
auth_providers = [
|
|
||||||
{
|
|
||||||
type = "legacy_api_password";
|
|
||||||
api_password = apiPassword;
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
frontend = {};
|
frontend = {};
|
||||||
mqtt = { # Use hbmqtt as broker
|
# uses embedded mqtt broker
|
||||||
password = mqttPassword;
|
mqtt.password = mqttPassword;
|
||||||
};
|
binary_sensor = [{
|
||||||
binary_sensor = [
|
|
||||||
{
|
|
||||||
platform = "mqtt";
|
platform = "mqtt";
|
||||||
state_topic = "home-assistant/test";
|
state_topic = "home-assistant/test";
|
||||||
payload_on = "let_there_be_light";
|
payload_on = "let_there_be_light";
|
||||||
payload_off = "off";
|
payload_off = "off";
|
||||||
}
|
}];
|
||||||
];
|
logger = {
|
||||||
|
default = "info";
|
||||||
|
logs."homeassistant.components.mqtt" = "debug";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
lovelaceConfig = {
|
lovelaceConfig = {
|
||||||
title = "My Awesome Home";
|
title = "My Awesome Home";
|
||||||
@ -65,7 +50,6 @@ in {
|
|||||||
lovelaceConfigWritable = true;
|
lovelaceConfigWritable = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
testScript = ''
|
testScript = ''
|
||||||
start_all()
|
start_all()
|
||||||
@ -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…
x
Reference in New Issue
Block a user