nixos-config/configs/pepe/home-assistant.nix

461 lines
12 KiB
Nix

{ pkgs, config, lib, ... }:
let unstablePkgs = import <nixpkgs-unstable> { };
in {
imports = [
#./home-assistant/mpd.nix
#./home-assistant/timer.nix
./home-assistant/chaospott.nix
./home-assistant/kodi.nix
./home-assistant/mqtt.nix
./home-assistant/sonoff.nix
./home-assistant/stocks.nix
./home-assistant/weather.nix
./home-assistant/workday.nix
./home-assistant/zigbee2mqtt.nix
];
services.homeAssistantConfig = {
# turn on to edit GUI
# lovelace = {};
homeassistant = {
latitude = 51.444847;
longitude = 6.967006;
elevation = 116;
auth_providers = [{
type = "trusted_networks";
trusted_networks =
[ config.module.cluster.services.tinc."private".networkSubnet ];
}];
};
prometheus.namespace = "hass";
# manual state
# ------------
input_select.situation = {
icon = "mdi:brightness-auto";
options = [
# it is dark outside and I want it to be bright inside
"dark"
# it is bright outside, so no need to be bright inside
"bright"
# it is dark ouside, but I don't want it bright inside
"essential"
];
};
input_boolean.bed_room_buttons.icon = "mdi:toggle-switch";
input_boolean.situation_toggle.icon = "mdi:toggle-switch";
# room presents when I'm alone
input_boolean.alone.icon = "mdi:account";
input_select.room_present = {
icon = "mdi:map-marker";
options = [
"kitchen_room_present"
"bath_room_present"
"living_room_present"
"bed_room_present"
"floor_room_present"
"bath_room_present"
"outside"
];
};
automation = let
allRooms =
[ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ];
roomPresents = {
# group of the room that should be turned on
roomGroup,
# group of the room that should be turned off
roomOffGroup ? roomGroup,
# group of the room that that indicates presents in the room
presentsGroup,
# global situation
situation,
# brightness for dimable lights
brightness ? 255 }: [
{
alias = "presents -> turn on ${roomGroup} lights in ${situation}";
trigger = {
platform = "state";
entity_id = "group.${presentsGroup}";
from = "off";
to = "on";
};
condition = {
condition = "state";
entity_id = "input_select.situation";
state = situation;
};
action = [
{
service = "switch.turn_on";
data.entity_id = "group.${roomGroup}";
}
{
service = "light.turn_on";
data = {
entity_id = "group.${roomGroup}";
brightness = brightness;
};
}
];
}
{
alias =
"absents -> turn off ${roomOffGroup} lights in ${situation}";
trigger = {
platform = "state";
entity_id = "group.${presentsGroup}";
from = "on";
to = "off";
for.seconds = 2 * 60;
};
condition = {
condition = "and";
conditions = [
{
condition = "template";
value_template =
"{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}";
}
{
condition = "template";
value_template =
"{{ states( 'input_boolean.alone' ) == 'on' }}";
}
{
condition = "template";
value_template =
"{{ states( 'input_select.situation' ) == '${situation}' }}";
}
];
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${roomOffGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${roomOffGroup}";
}
];
}
];
situationSwitch = { fromRoomGroup, toRoomGroup, presentsGroup
, fromSituation, toSituation,
# brightness for dimable lights
brightness ? 255 }: [
{
alias =
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} on";
trigger = {
platform = "state";
entity_id = "input_select.situation";
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "on";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "switch.turn_on";
data.entity_id = "group.${toRoomGroup}";
}
{
service = "light.turn_on";
data = {
entity_id = "group.${toRoomGroup}";
brightness = brightness;
};
}
];
}
{
alias =
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} off";
trigger = {
platform = "state";
entity_id = "input_select.situation";
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "off";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
];
}
];
in lib.flatten (map roomPresents (lib.flatten (map (name: [
{
roomGroup = "${name}_lights";
presentsGroup = "${name}_present";
situation = "dark";
}
{
roomGroup = "${name}_essential";
roomOffGroup = "${name}_lights";
presentsGroup = "${name}_present";
situation = "essential";
brightness = 30;
}
]) allRooms))) ++ [
# control situation with buttons
{
alias = "set essential";
trigger = {
platform = "state";
entity_id = "input_boolean.situation_toggle";
from = "off";
to = "on";
};
action = {
service = "input_select.select_option";
data = {
entity_id = "input_select.situation";
option = "essential";
};
};
}
{
alias = "unset essential";
trigger = {
platform = "state";
entity_id = "input_boolean.situation_toggle";
from = "on";
to = "off";
};
action = {
service = "input_select.select_option";
data = {
entity_id = "input_select.situation";
option = "dark";
};
};
}
] ++ (lib.flatten (map situationSwitch (lib.flatten (map (name: [
{
presentsGroup = "${name}_present";
fromSituation = "dark";
toSituation = "essential";
fromRoomGroup = "${name}_lights";
toRoomGroup = "${name}_essential";
brightness = 30;
}
{
presentsGroup = "${name}_present";
fromSituation = "essential";
toSituation = "dark";
fromRoomGroup = "${name}_essential";
toRoomGroup = "${name}_lights";
}
]) allRooms))));
group = let
create_room = { name, description }: {
"${name}" = {
name = "${description}";
view = false;
control = "hidden";
entities = [ ];
};
"${name}_lights" = {
name = "${description} Beleuchtung";
view = false;
entities = [ ];
};
"${name}_present" = {
control = "hidden";
name = "${description} Anwesend";
view = false;
entities = [ ];
};
"${name}_essential" = {
control = "hidden";
name = "${description} Minimale Beleuchtung";
view = false;
entities = [ ];
};
"view_${name}" = {
name = description;
view = true;
entities = [
"group.${name}"
"group.${name}_lights"
"group.${name}_present"
"group.${name}_essential"
];
};
};
create_rooms = rooms:
lib.foldr (a: b: a // b) { } (map create_room rooms);
# rooms
# -----
in (create_rooms [
{
name = "floor_room";
description = "Flur";
}
{
name = "bed_room";
description = "Schlafzimmer";
}
{
name = "living_room";
description = "Wohnzimmer";
}
{
name = "kitchen_room";
description = "Küche";
}
{
name = "bath_room";
description = "Klo";
}
]) // {
view_rooms = {
name = "Räume";
view = true;
entities = [
"group.today"
"group.floor_room"
"group.bed_room"
"group.living_room"
"group.kitchen_room"
"group.bath_room"
];
};
# overview
# --------
all_sensors = {
name = "Alle Sensoren";
control = "hidden";
};
today = {
control = "hidden";
name = "Today";
view = false;
entities = [
"input_select.situation"
"input_select.room_present"
"input_boolean.alone"
];
};
view_overview = {
name = "Übersicht";
view = true;
entities = [ "group.today" ];
};
# other stuff
# -----------
tv = {
name = "TV";
view = false;
};
all_lights = {
name = "Alle Lampen";
view = false;
};
unknown = {
control = "hidden";
name = "Not Used";
view = false;
entities = [ ];
};
};
script.turn_all_off.sequence = [ ];
script.turn_all_lights_off.sequence = [ ];
};
services.home-assistant = {
enable = true;
package = unstablePkgs.home-assistant.override {
python3 = unstablePkgs.python37;
extraPackages = python: [
# todo : check which is still needed
python.netdisco
python.xmltodict
python.mpd2
# for mqtt
python.hbmqtt
python.paho-mqtt
# needed for platform workday
#(python.buildPythonPackage rec {
# pname = "holidays";
# version = "0.9.10";
# src = python.fetchPypi {
# inherit pname version;
# sha256 =
# "9f06d143eb708e8732230260636938f2f57114e94defd8fa2082408e0d422d6f";
# };
# doCheck = false;
# buildInputs = [ pkgs.dateutils ];
# propagatedBuildInputs = [ python."python-dateutil" python."six" ];
# meta = with pkgs.stdenv.lib; {
# homepage = "https://github.com/dr-prodigy/python-holidays";
# license = licenses.mit;
# description = "Generate and work with holidays in Python";
# maintainers = with maintainers; [ mrVanDalo ];
# };
#})
];
};
};
# host nginx setup
services.nginx = {
enable = true;
recommendedProxySettings = true;
virtualHosts = {
"iot.pepe.private" = {
serverAliases = [ "hass.pepe.private" "home.pepe.private" ];
locations."/" = { proxyPass = "http://pepe.private:8123"; };
};
};
};
}