pepe: home-assistant refactoring

This commit is contained in:
Ingolf Wagner 2020-04-12 19:57:49 +02:00
parent 8eed107a3e
commit 55bf10631d
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
2 changed files with 69 additions and 12 deletions

View file

@ -103,7 +103,11 @@ in {
view_overview = { view_overview = {
name = "Übersicht"; name = "Übersicht";
view = true; view = true;
entities = [ "group.today" ]; entities = [ "group.today" "group.all_sensors" ];
};
all_sensors = {
name = "Sensor Overview";
control = "hidden";
}; };
}; };

View file

@ -2,7 +2,7 @@
let let
# allow new devices to join # allow new devices to join
enablePairing = true; enablePairing = false;
device = "/dev/ttyACM0"; device = "/dev/ttyACM0";
dataFolder = "/srv/zigbee/data"; dataFolder = "/srv/zigbee/data";
@ -27,10 +27,17 @@ let
"motion_sensor_7".id = "0x00158d0002f9a6aa"; "motion_sensor_7".id = "0x00158d0002f9a6aa";
"motion_sensor_8".id = "0x00158d0002f04637"; "motion_sensor_8".id = "0x00158d0002f04637";
}; };
door = {
"door_sensor_1".id = "0x00158d000312dc52";
"door_sensor_2".id = "0x00158d000316d5bf";
"door_sensor_3".id = "0x00158d0002f9516f";
"door_sensor_4".id = "0x00158d00031383b9";
"door_sensor_5".id = "0x00158d0003120d3e";
};
}; };
# todo : rename with allSensors # todo : generate automatically
allSensors = with sensors; buttons // temperature // motion; allSensors = with sensors; buttons // temperature // motion // door;
zigBee2MqttConfig = { zigBee2MqttConfig = {
@ -76,9 +83,11 @@ in {
services.homeAssistantConfig = { services.homeAssistantConfig = {
# group.unknown.entities = [ "sensor.button_1" ]; # define sensors
# --------------
sensor = let sensor = let
# define button sensors
buttons = with lib; buttons = with lib;
mapAttrsToList (name: mapAttrsToList (name:
{ ... }: [{ { ... }: [{
@ -90,6 +99,7 @@ in {
value_template = "{{ value_json.click }}"; value_template = "{{ value_json.click }}";
}]) sensors.buttons; }]) sensors.buttons;
# define temperature sensors
temperature = with lib; temperature = with lib;
mapAttrsToList (name: mapAttrsToList (name:
{ ... }: [ { ... }: [
@ -122,6 +132,7 @@ in {
} }
]) sensors.temperature; ]) sensors.temperature;
# define meta information sensors
informations = lib.mapAttrsToList (name: informations = lib.mapAttrsToList (name:
{ ... }: [ { ... }: [
{ {
@ -146,8 +157,12 @@ in {
in lib.flatten (buttons ++ temperature ++ informations); in lib.flatten (buttons ++ temperature ++ informations);
# define binary sensors
# ---------------------
binary_sensor = let binary_sensor = let
# define motion sensors
# ---------------------
motion = lib.mapAttrsToList (name: motion = lib.mapAttrsToList (name:
{ ... }: { { ... }: {
name = name; name = name;
@ -159,13 +174,31 @@ in {
payload_off = false; payload_off = false;
value_template = "{{ value_json.occupancy }}"; value_template = "{{ value_json.occupancy }}";
}) sensors.motion; }) sensors.motion;
in lib.flatten (motion);
# define door sensors
# -------------------
door = lib.mapAttrsToList (name:
{ ... }: {
name = name;
platform = "mqtt";
device_class = "door";
state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state";
payload_on = false;
payload_off = true;
value_template = "{{ value_json.contact}}";
}) sensors.door;
in lib.flatten (motion ++ door);
# create groups
# -------------
# to have nice panels for every device
group = let group = let
information = name: [ "sensor.battery_${name}" "sensor.link_${name}" ]; information = name: [ "sensor.battery_${name}" "sensor.link_${name}" ];
sensor = lib.mapAttrs' (name: sensorButtons = lib.mapAttrs' (name:
{ ... }: { { ... }: {
name = name; name = name;
value = { value = {
@ -187,7 +220,7 @@ in {
}; };
}) (sensors.temperature); }) (sensors.temperature);
binarySensor = lib.mapAttrs' (name: sensorMotions = lib.mapAttrs' (name:
{ ... }: { { ... }: {
name = name; name = name;
value = { value = {
@ -196,6 +229,15 @@ in {
}; };
}) (sensors.motion); }) (sensors.motion);
sensorDoors = lib.mapAttrs' (name:
{ ... }: {
name = name;
value = {
control = "hidden";
entities = [ "binary_sensor.${name}" ] ++ (information name);
};
}) (sensors.door);
views = { views = {
view_sensors = { view_sensors = {
name = "Sensoren"; name = "Sensoren";
@ -206,8 +248,18 @@ in {
}; };
}; };
in views // sensor // binarySensor // sensorTemperature; in views // sensorButtons // sensorMotions // sensorTemperature
// sensorDoors // {
all_sensors.entities =
(lib.mapAttrsToList (name: { ... }: "sensor.${name}")
(sensors.buttons // sensors.temperature))
++ (lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}")
(sensors.motion // sensors.door));
};
# create automation
# -----------------
# todo : this belongs in the home-assistant.nix
automation = let automation = let
lights = map (button: { lights = map (button: {
@ -281,10 +333,11 @@ in {
}; };
virtualisation.docker.enable = true; # manage zigbee2mqtt
# ------------------
# todo : einen eigenen container bauen mit dockerTool : https://nixos.wiki/wiki/Docker # todo : einen eigenen container bauen mit dockerTool : https://nixos.wiki/wiki/Docker
virtualisation.docker.enable = true;
systemd.services."zigbee2mqtt" = { systemd.services."zigbee2mqtt" = {
enable = true; enable = true;
description = description =