From a0a707993bd84fbee2a2e9905e7d47a46c2c13c9 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Mon, 1 Jun 2020 23:20:15 +0200 Subject: [PATCH] home-assistant: thin out stuff which is not needed anymore --- configs/pepe/home-assistant.nix | 250 ++++++--------- configs/pepe/home-assistant/chaospott.nix | 2 - configs/pepe/home-assistant/kodi.nix | 2 - configs/pepe/home-assistant/light-control.nix | 8 - configs/pepe/home-assistant/mpd.nix | 24 -- configs/pepe/home-assistant/sonoff.nix | 48 ++- configs/pepe/home-assistant/stocks.nix | 31 +- configs/pepe/home-assistant/timer.nix | 19 -- configs/pepe/home-assistant/workday.nix | 1 - .../home-assistant/zigbee2mqtt/buttons.nix | 1 - .../pepe/home-assistant/zigbee2mqtt/doors.nix | 11 +- .../home-assistant/zigbee2mqtt/lights.nix | 129 ++++---- .../home-assistant/zigbee2mqtt/motion.nix | 124 ++++---- .../home-assistant/zigbee2mqtt/temperatur.nix | 1 - modules/services/home-assistant.nix | 290 ++---------------- 15 files changed, 268 insertions(+), 673 deletions(-) diff --git a/configs/pepe/home-assistant.nix b/configs/pepe/home-assistant.nix index aaa1f4d..84124d7 100644 --- a/configs/pepe/home-assistant.nix +++ b/configs/pepe/home-assistant.nix @@ -19,67 +19,95 @@ in { services.homeAssistantConfig = { # turn on to edit GUI - # lovelace = {}; + lovelace = { }; homeassistant = { + + name = "Home"; latitude = 51.444847; longitude = 6.967006; elevation = 116; + unit_system = "metric"; + time_zone = config.time.timeZone; auth_providers = [{ type = "trusted_networks"; trusted_networks = [ config.module.cluster.services.tinc."private".networkSubnet ]; }]; + }; + # needed, don't know why + default_config = { }; + config = { }; + + # to grab metrics via prometheus prometheus.namespace = "hass"; # manual state # ------------ - input_select.situation = { + # all scenes + input_select.scene = { icon = "mdi:brightness-auto"; options = [ "default" "night" "outside" ]; }; + # scenes controlled by buttons + input_select.scene_button = { + icon = "mdi:brightness-auto"; + options = [ "default" "night" ]; + }; input_boolean.situation_toggle.icon = "mdi:toggle-switch"; automation = [ + { + # This automation script runs when the thermostat mode selector is changed. + # It publishes its value to the same MQTT topic it is also subscribed to. + alias = "Mqtt publish Scene"; + trigger = { + platform = "state"; + entity_id = "input_select.scene"; + }; + action = { + service = "mqtt.publish"; + data_template = { + topic = "control/lights/set"; + #retain = true; + payload_template = + ''{"scene":"{{ states('input_select.scene')}}"}''; + }; + }; + } + # control situation with buttons { - alias = "set night scene"; + alias = "Toggle Button (controlled) Scene"; trigger = { platform = "state"; entity_id = "input_boolean.situation_toggle"; - from = "off"; - to = "on"; }; action = { - service = "mqtt.publish"; - data = { - topic = "control/lights/set"; - payload = builtins.toJSON { scene = "night"; }; + service = "input_select.select_next"; + data.entity_id = "input_select.scene_button"; + }; + } + { + alias = "Button Scene -> Scene"; + trigger = { + platform = "state"; + entity_id = "input_select.scene_button"; + }; + action = { + service = "input_select.select_option"; + data_template = { + entity_id = "input_select.scene"; + option = "{{ states('input_select.scene_button') }}"; }; }; } { - alias = "set default scene"; - trigger = { - platform = "state"; - entity_id = "input_boolean.situation_toggle"; - from = "on"; - to = "off"; - }; - action = { - service = "mqtt.publish"; - data = { - topic = "control/lights/set"; - payload = builtins.toJSON { scene = "default"; }; - }; - }; - } - { - alias = "set outside scene"; + alias = "Set Scene To Outside when opening door"; trigger = [ { platform = "state"; @@ -97,10 +125,10 @@ in { } ]; action = { - service = "mqtt.publish"; + service = "input_select.select_option"; data = { - topic = "control/lights/set"; - payload = builtins.toJSON { scene = "outside"; }; + entity_id = "input_select.scene"; + option = "outside"; }; }; } @@ -111,44 +139,8 @@ in { 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 = [ ]; - }; - "${name}_bright" = { - control = "hidden"; - name = "${description} Tages Beleuchtung"; - view = false; - entities = [ ]; - }; - "view_${name}" = { - name = description; - view = true; - entities = [ - "group.${name}" - "group.${name}_bright" - "group.${name}_essential" - "group.${name}_lights" - "group.${name}_present" - ]; - }; }; create_rooms = rooms: lib.foldr (a: b: a // b) { } (map create_room rooms); @@ -176,118 +168,60 @@ in { 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" - ]; - }; - - all_room_presents = { - name = "Alle Anwesenheits Raum Sensoren"; - entities = [ - "group.floor_room_present" - "group.bed_room_present" - "group.living_room_present" - "group.kitchen_room_present" - "group.bath_room_present" - ]; - }; - - outside = { - name = "Draußen"; - entities = [ ]; - }; # overview # -------- - all_sensors = { - name = "Alle Sensoren"; - control = "hidden"; - }; + all_sensors = { name = "Alle Sensoren"; }; today = { - control = "hidden"; name = "Today"; - view = false; - entities = [ - "input_select.situation" - "input_select.room_present" - "input_boolean.presents_tracking" - ]; + entities = [ "input_select.scene" ]; }; - view_overview = { - name = "Übersicht"; - view = true; - entities = [ "group.today" ]; - }; - # other stuff # ----------- - tv = { - name = "TV"; - view = false; - }; - all_lights = { - name = "Alle Lampen"; - view = false; - }; + tv = { name = "TV"; }; + all_lights = { name = "Alle Lampen"; }; 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 ]; - # }; - #}) - - ]; - - }; + #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 ]; + # # }; + # #}) + # ]; + #}; }; diff --git a/configs/pepe/home-assistant/chaospott.nix b/configs/pepe/home-assistant/chaospott.nix index 0099c7f..ede1b58 100644 --- a/configs/pepe/home-assistant/chaospott.nix +++ b/configs/pepe/home-assistant/chaospott.nix @@ -42,10 +42,8 @@ in { group = { "${name}" = { name = "ChaosPott (Essen)"; - control = "hidden"; entities = [ "sensor.${name}_aerie" "sensor.${name}_cellar" ]; }; - view_overview.entities = [ "group.${name}" ]; }; }; diff --git a/configs/pepe/home-assistant/kodi.nix b/configs/pepe/home-assistant/kodi.nix index 388a89e..d6c7a6a 100644 --- a/configs/pepe/home-assistant/kodi.nix +++ b/configs/pepe/home-assistant/kodi.nix @@ -2,8 +2,6 @@ services.homeAssistantConfig = { - group.view_overview.entities = [ "media_player.kodi" ]; - media_player = [{ platform = "kodi"; host = "127.0.0.1"; diff --git a/configs/pepe/home-assistant/light-control.nix b/configs/pepe/home-assistant/light-control.nix index 5b271d5..e6ef40f 100644 --- a/configs/pepe/home-assistant/light-control.nix +++ b/configs/pepe/home-assistant/light-control.nix @@ -1,11 +1,4 @@ { pkgs, lib, config, ... }: { - #systemd.services."light-control" = { - # wantedBy = [ "multi-user.target" ]; - # environment = { RUST_LOG = "rust_iot=trace"; }; - # script = '' - # ${pkgs.light-control}/bin/rust-iot ${lightControlConfig} - # ''; - #}; services.mqtt.light-control.enable = true; services.mqtt.light-control.config = { @@ -112,7 +105,6 @@ }; }; in [ - (light { topic = "zigbee2mqtt/light_1"; rooms = [ "floor_room" ]; diff --git a/configs/pepe/home-assistant/mpd.nix b/configs/pepe/home-assistant/mpd.nix index c7650e8..80b4cc6 100644 --- a/configs/pepe/home-assistant/mpd.nix +++ b/configs/pepe/home-assistant/mpd.nix @@ -3,35 +3,11 @@ { services.homeAssistantConfig = { - group.view_overview.entities = [ "media_player.mpd" ]; - media_player = [{ platform = "mpd"; host = "localhost"; }]; - script.turn_all_off.sequence = [{ - alias = "turn mpd off"; - service = "media_player.turn_off"; - data.entity_id = "media_player.mpd"; - }]; - - script.turn_all_on.sequence = [ - { - alias = "turn mpd on"; - service = "media_player.turn_on"; - data.entity_id = "media_player.mpd"; - } - { - alias = "Adjust volume"; - service = "media_player.volume_set"; - data = { - entity_id = "media_player.mpd"; - volume_level = "0.90"; - }; - } - ]; - }; } diff --git a/configs/pepe/home-assistant/sonoff.nix b/configs/pepe/home-assistant/sonoff.nix index 98ec9d1..e492f6b 100644 --- a/configs/pepe/home-assistant/sonoff.nix +++ b/configs/pepe/home-assistant/sonoff.nix @@ -2,43 +2,37 @@ let sonoffSwitches = { - "pal01" = { - label = "Schlafzimmer Lampe Links"; - icon = "mdi:lightbulb"; - groups = [ "all_lights" "bed_room_lights" "bed_room_bright" "bed_room" ]; - }; + #"pal01" = { + # label = "Schlafzimmer Lampe Links"; + # icon = "mdi:lightbulb"; + # groups = [ "bed_room" ]; + #}; "pal02" = { label = "Drucker / Scanner"; icon = "mdi:printer"; groups = [ "bed_room" "today" ]; }; - "pal03" = { - label = "Wohnzimmer Lampe"; - icon = "mdi:lightbulb"; - groups = [ "all_lights" "living_room_lights" "living_room" ]; - }; - "pal04" = { - label = "Schlafzimmer Lampe Rechts"; - icon = "mdi:lightbulb"; - groups = [ "all_lights" "bed_room_lights" "bed_room_bright" "bed_room" ]; - }; + #"pal03" = { + # label = "Wohnzimmer Lampe"; + # icon = "mdi:lightbulb"; + # groups = [ "living_room" ]; + #}; + #"pal04" = { + # label = "Schlafzimmer Lampe Rechts"; + # icon = "mdi:lightbulb"; + # groups = [ "bed_room" ]; + #}; "pal05" = { label = "TV"; icon = "mdi:television"; - groups = [ "tv" "living_room" "today" ]; + groups = [ "tv" ]; device = "tv"; }; - "pal06" = { - label = "Küchen Lampe"; - icon = "mdi:lightbulb"; - groups = [ - "all_lights" - "kitchen_room" - "kitchen_room_bright" - "kitchen_room_essential" - "kitchen_room_lights" - ]; - }; + #"pal06" = { + # label = "Küchen Lampe"; + # icon = "mdi:lightbulb"; + # groups = ["kitchen_room"]; + #}; "pal07" = { label = "Nummer 7"; }; "pal08" = { label = "Nummer 8"; }; }; diff --git a/configs/pepe/home-assistant/stocks.nix b/configs/pepe/home-assistant/stocks.nix index d2e9073..7afd2dd 100644 --- a/configs/pepe/home-assistant/stocks.nix +++ b/configs/pepe/home-assistant/stocks.nix @@ -97,25 +97,18 @@ in { ]) stocks)); }; - group = { - "stocks" = { - name = "Aktien"; - view = true; - control = "hidden"; - entities = map ({ name, ... }: "group.stock_${name}") stocks; - }; - } // (builtins.listToAttrs (map ({ name, friendly_name, own ? { }, ... }: { - name = "stock_${name}"; - value = { - name = friendly_name; - control = "hidden"; - entities = [ - "sensor.stock_${name}" - "sensor.stock_${name}_change" - "sensor.stock_${name}_change_percent" - ] ++ (lib.optional (own != { }) "sensor.stock_${name}_profit"); - }; - }) stocks)); + group = (builtins.listToAttrs (map + ({ name, friendly_name, own ? { }, ... }: { + name = "stock_${name}"; + value = { + name = "${friendly_name} Aktie"; + entities = [ + "sensor.stock_${name}" + "sensor.stock_${name}_change" + "sensor.stock_${name}_change_percent" + ] ++ (lib.optional (own != { }) "sensor.stock_${name}_profit"); + }; + }) stocks)); }; diff --git a/configs/pepe/home-assistant/timer.nix b/configs/pepe/home-assistant/timer.nix index 663a179..3109d04 100644 --- a/configs/pepe/home-assistant/timer.nix +++ b/configs/pepe/home-assistant/timer.nix @@ -81,9 +81,7 @@ group = { timer_wakeup = { - view = false; name = "Arbeits Aufwachen"; - control = "hidden"; entities = [ "input_boolean.wakeup" "input_datetime.wakeup" @@ -92,23 +90,12 @@ }; timer_leave = { - view = false; name = "Leave Time"; - control = "hidden"; entities = [ "input_boolean.leave" "input_datetime.leave" ]; }; timer_return = { - view = false; name = "Nach Hause kommen"; - control = "hidden"; - entities = [ "input_boolean.return" "input_datetime.return" ]; - }; - - timer_sleep = { - view = false; - name = "Einschlafen"; - control = "hidden"; entities = [ "input_boolean.sleep" "input_datetime.sleep" ]; }; @@ -123,12 +110,6 @@ today.entities = [ "sensor.date" "sensor.time" ]; - view_overview.entities = [ - "group.timer_wakeup" - "group.timer_leave" - "group.timer_return" - "group.timer_sleep" - ]; }; automation = [ diff --git a/configs/pepe/home-assistant/workday.nix b/configs/pepe/home-assistant/workday.nix index 971258c..9424231 100644 --- a/configs/pepe/home-assistant/workday.nix +++ b/configs/pepe/home-assistant/workday.nix @@ -34,7 +34,6 @@ group = { today.entities = [ "binary_sensor.before_workday" "binary_sensor.workday" ]; - view_overview.entities = [ "group.is_workday" ]; }; }; diff --git a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix index f445db7..fb24cb9 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix @@ -118,7 +118,6 @@ in { hold ]; in { - control = "hidden"; entities = [ "sensor.${name}" ] ++ (entityIds states) ++ [ "sensor.battery_${name}" "sensor.link_${name}" ]; }) allDevices; diff --git a/configs/pepe/home-assistant/zigbee2mqtt/doors.nix b/configs/pepe/home-assistant/zigbee2mqtt/doors.nix index 9a6bb34..3f84faa 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/doors.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/doors.nix @@ -9,7 +9,7 @@ let }; "door_sensor_2" = { id = "0x00158d000316d5bf"; - groups = [ "floor_room" "floor_room_present" "outside" ]; + groups = [ "floor_room" ]; }; "window_sensor_3" = { id = "0x00158d0002f9516f"; @@ -17,7 +17,7 @@ let }; "door_sensor_4" = { id = "0x00158d00031383b9"; - groups = [ "floor_room" "floor_room_present" ]; + groups = [ "floor_room" ]; }; "window_sensor_5" = { id = "0x00158d0003120d3e"; @@ -76,19 +76,15 @@ in { # create groups # ------------- group = let - # to have nice panels for every device sensorGroups = lib.mapAttrs (name: { ... }: { - control = "hidden"; entities = [ "binary_sensor.${name}" "sensor.battery_${name}" "sensor.link_${name}" - ]; }) allDevices; - # sort lights into given groups. sortedInGroups = let groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: @@ -96,12 +92,11 @@ in { map (groupName: { "${groupName}" = "binary_sensor.${name}"; }) groups) allDevices)); in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - in sortedInGroups // sensorGroups // { - all_sensors.entities = lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices; }; + }; } diff --git a/configs/pepe/home-assistant/zigbee2mqtt/lights.nix b/configs/pepe/home-assistant/zigbee2mqtt/lights.nix index 02375f0..32b9e6b 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/lights.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/lights.nix @@ -14,28 +14,22 @@ let }; "light_2" = { id = "0x7cb03eaa0a0387b9"; - groups = [ "floor_room" "floor_room_lights" ]; + groups = [ "floor_room" ]; }; "light_3" = { id = "0x7cb03eaa0a033a86"; - groups = [ "living_room" "living_room_essential" "living_room_lights" ]; + groups = [ "living_room" ]; }; "light_4" = { id = "0x7cb03eaa0a04aabf"; - groups = [ - "bath_room" - "bath_room_bright" - "bath_room_essential" - "bath_room_lights" - ]; + groups = [ "bath_room" ]; }; "light_5" = { id = "0x7cb03eaa0a0346e4"; }; "light_6" = { id = "0x7cb03eaa0a034b46"; }; "light_7" = { id = "0x7cb03eaa0a033b4f"; }; "light_8" = { id = "0x7cb03eaa0a0384d3"; - groups = - [ "bed_room" "bed_room_essential" "bed_room_lights" "bed_room_bright" ]; + groups = [ "bed_room" ]; }; }; @@ -51,70 +45,69 @@ in { }; }) allDevices; - services.homeAssistantConfig = { + #services.homeAssistantConfig = { - light = lib.mapAttrsToList (name: - { ... }: { - platform = "mqtt"; - name = name; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - command_topic = "zigbee2mqtt/${name}/set"; - value_template = "{{ value_json.click }}"; - brightness = true; - color_temp = true; - schema = "json"; - }) allDevices; + # light = lib.mapAttrsToList (name: + # { ... }: { + # platform = "mqtt"; + # name = name; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # command_topic = "zigbee2mqtt/${name}/set"; + # value_template = "{{ value_json.click }}"; + # brightness = true; + # color_temp = true; + # schema = "json"; + # }) allDevices; - sensor = with lib; - mapAttrsToList (name: - { ... }: { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - value_template = "{{ value_json.linkquality}}"; - }) allDevices; + # sensor = with lib; + # mapAttrsToList (name: + # { ... }: { + # name = "link_${name}"; + # platform = "mqtt"; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # icon = "mdi:signal"; + # value_template = "{{ value_json.linkquality}}"; + # }) allDevices; - binary_sensor = lib.mapAttrsToList (name: - { ... }: { - name = "update_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - payload_on = true; - payload_off = false; - value_template = "{{ value_json.update_available }}"; - }) allDevices; + # binary_sensor = lib.mapAttrsToList (name: + # { ... }: { + # name = "update_${name}"; + # platform = "mqtt"; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # payload_on = true; + # payload_off = false; + # value_template = "{{ value_json.update_available }}"; + # }) allDevices; - # create groups - # ------------- - group = let + # # create groups + # # ------------- + # group = let - # to have nice panels for every device - lightGroups = lib.mapAttrs (name: - { ... }: { - control = "hidden"; - entities = [ - "light.${name}" - "sensor.link_${name}" - "binary_sensor.update_${name}" - ]; - }) allDevices; + # # to have nice panels for every device + # lightGroups = lib.mapAttrs (name: + # { ... }: { + # entities = [ + # "light.${name}" + # "sensor.link_${name}" + # "binary_sensor.update_${name}" + # ]; + # }) allDevices; - # sort lights into given groups. - sortedInGroups = let - groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: - { groups ? [ ], ... }: - map (groupName: { "${groupName}" = "light.${name}"; }) groups) - allDevices)); - in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; + # # sort lights into given groups. + # sortedInGroups = let + # groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: + # { groups ? [ ], ... }: + # map (groupName: { "${groupName}" = "light.${name}"; }) groups) + # allDevices)); + # in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - in sortedInGroups // lightGroups // { - all_lights.entities = - lib.mapAttrsToList (name: { ... }: "light.${name}") allDevices; - }; - }; + # in sortedInGroups // lightGroups // { + # all_lights.entities = + # lib.mapAttrsToList (name: { ... }: "light.${name}") allDevices; + # }; + #}; } diff --git a/configs/pepe/home-assistant/zigbee2mqtt/motion.nix b/configs/pepe/home-assistant/zigbee2mqtt/motion.nix index 188cbd1..90a0ef2 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/motion.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/motion.nix @@ -51,71 +51,63 @@ in { }; }) allDevices; - services.homeAssistantConfig = { - - # define meta information sensors - sensor = lib.flatten (lib.mapAttrsToList (name: - { ... }: [ - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) allDevices); - - binary_sensor = lib.mapAttrsToList (name: - { ... }: { - name = name; - platform = "mqtt"; - device_class = "motion"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - payload_on = true; - payload_off = false; - value_template = "{{ value_json.occupancy }}"; - }) allDevices; - - # create groups - # ------------- - group = let - - # to have nice panels for every device - sensorGroups = lib.mapAttrs (name: - { ... }: { - control = "hidden"; - entities = [ - "binary_sensor.${name}" - "sensor.battery_${name}" - "sensor.link_${name}" - ]; - }) allDevices; - - # sort lights into given groups. - sortedInGroups = let - groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: - { groups ? [ ], ... }: - map (groupName: { "${groupName}" = "binary_sensor.${name}"; }) groups) - allDevices)); - in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - - in sortedInGroups // sensorGroups // { - - all_sensors.entities = - lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices; - }; - }; + #services.homeAssistantConfig = { + # # define meta information sensors + # sensor = lib.flatten (lib.mapAttrsToList (name: + # { ... }: [ + # { + # name = "battery_${name}"; + # platform = "mqtt"; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # unit_of_measurement = "%"; + # icon = "mdi:battery-10"; + # value_template = "{{ value_json.battery }}"; + # } + # { + # name = "link_${name}"; + # platform = "mqtt"; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # icon = "mdi:signal"; + # unit_of_measurement = "lqi"; + # value_template = "{{ value_json.linkquality }}"; + # } + # ]) allDevices); + # binary_sensor = lib.mapAttrsToList (name: + # { ... }: { + # name = name; + # platform = "mqtt"; + # device_class = "motion"; + # state_topic = "zigbee2mqtt/${name}"; + # availability_topic = "zigbee2mqtt/bridge/state"; + # payload_on = true; + # payload_off = false; + # value_template = "{{ value_json.occupancy }}"; + # }) allDevices; + # # create groups + # # ------------- + # group = let + # # to have nice panels for every device + # sensorGroups = lib.mapAttrs (name: + # { ... }: { + # entities = [ + # "binary_sensor.${name}" + # "sensor.battery_${name}" + # "sensor.link_${name}" + # ]; + # }) allDevices; + # # sort lights into given groups. + # sortedInGroups = let + # groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: + # { groups ? [ ], ... }: + # map (groupName: { "${groupName}" = "binary_sensor.${name}"; }) groups) + # allDevices)); + # in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; + # in sortedInGroups // sensorGroups // { + # all_sensors.entities = + # lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices; + # }; + #}; } diff --git a/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix b/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix index a6e0c7e..f1a2464 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix @@ -83,7 +83,6 @@ in { # to have nice panels for every device sensorGroups = lib.mapAttrs (name: { ... }: { - control = "hidden"; entities = [ "sensor.${name}" "sensor.humidity_${name}" diff --git a/modules/services/home-assistant.nix b/modules/services/home-assistant.nix index 312df18..99aa6e8 100644 --- a/modules/services/home-assistant.nix +++ b/modules/services/home-assistant.nix @@ -6,280 +6,32 @@ let cfg = config.services.homeAssistantConfig; + mkMagicMergeOption = { description ? "", example ? { }, default ? { }, ... }: + mkOption { + inherit example description default; + type = with lib.types; + let + valueType = nullOr (oneOf [ + bool + int + float + str + (attrsOf valueType) + (listOf valueType) + ]) // { + description = ""; + emptyValue.value = { }; + }; + in valueType; + }; + in { - options.services.homeAssistantConfig = mkOption { - default = null; - type = with types; - nullOr (submodule { - options = { - group = mkOption { - default = null; - example = { - schlafzimmer = { - view = false; - entities = [ "switch.pal01" ]; - }; - }; - type = with types; - nullOr (attrsOf (submodule { - options = { - name = mkOption { - default = null; - type = with types; nullOr str; - description = '' - (string)(Optional)Name of the group. - ''; - }; - view = mkOption { - default = false; - type = with types; bool; - description = '' - (boolean)(Optional)If yes then the entry will be shown as a view (tab) at the top. Groups that are set to view: true cannot be used as entities in other views. - ''; - }; - icon = mkOption { - default = null; - type = with types; nullOr str; - description = '' - (string)(Optional)If the group is a view, this icon will show at the top in the frontend instead of the name. If the group is a view and both name and icon have been specified, the icon will appear at the top of the frontend and the name will be displayed as the mouse-over text. If it’s not a view, then the icon shows when this group is used in another group. - ''; - }; - control = mkOption { - default = null; - type = with types; nullOr str; - description = '' - (string)(Optional)Set value to hidden. If hidden then the group switch will be hidden. - ''; - }; - entities = mkOption { - default = [ ]; - type = with types; listOf str; - description = '' - (list)(Required)Array or comma delimited string, list of entities to group. - ''; - }; - all = mkOption { - default = false; - type = with types; bool; - description = '' - (boolean)(Optional)Set this to true if the group state should only turn on if all grouped entities are on. - ''; - }; - - }; - })); - }; - homeassistant = mkOption { - type = with types; - (submodule { - options = { - name = mkOption { - default = "Home"; - type = with types; str; - }; - time_zone = mkOption { - default = config.time.timeZone; - type = with types; str; - }; - latitude = mkOption { - default = 52.464031; - type = with types; float; - }; - longitude = mkOption { - default = 13.381925; - type = with types; float; - }; - elevation = mkOption { - default = 34; - type = with types; int; - }; - unit_system = mkOption { - default = "metric"; - type = with types; str; - }; - whitelist_external_dirs = mkOption { - default = null; - type = with types; nullOr (listOf str); - }; - auth_providers = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - customize = mkOption { - type = with types; - attrsOf (submodule { - options = { - friendly_name = mkOption { type = with types; str; }; - entity_picture = mkOption { - default = null; - type = with types; nullOr str; - }; - icon = mkOption { - default = null; - type = with types; nullOr str; - }; - }; - }); - }; - }; - }); - }; - introduction = mkOption { - default = { }; - type = with types; attrs; - }; - frontend = mkOption { - default = { }; - type = with types; attrs; - }; - config = mkOption { - default = { }; - type = with types; attrs; - }; - http = mkOption { - default = null; - type = with types; nullOr attrs; - }; - discovery = mkOption { - default = null; - type = with types; nullOr attrs; - }; - history = mkOption { - default = null; - type = with types; nullOr attrs; - }; - logbook = mkOption { - default = null; - type = with types; nullOr attrs; - }; - logger = mkOption { - default = { }; - type = with types; attrs; - }; - lovelace = mkOption { - default = { mode = "yaml"; }; - type = with types; nullOr attrs; - }; - map = mkOption { - default = null; - type = with types; nullOr attrs; - }; - mqtt = mkOption { - default = null; - type = with types; nullOr attrs; - }; - sun = mkOption { - default = null; - type = with types; nullOr attrs; - }; - - switch = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - binary_sensor = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - light = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - sensor = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - prometheus = mkOption { - default = null; - type = with types; nullOr attrs; - }; - automation = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - media_player = mkOption { - default = null; - type = with types; nullOr (listOf attrs); - }; - mysensors = mkOption { - default = null; - type = with types; nullOr attrs; - }; - script = mkOption { - default = null; - type = with types; - nullOr (attrsOf (submodule { - options = { - alias = mkOption { - default = null; - type = with types; nullOr str; - }; - sequence = mkOption { - default = [ ]; - type = with types; listOf attrs; - }; - }; - })); - }; - input_number = mkOption { - default = null; - type = with types; nullOr (attrsOf attrs); - }; - input_text = mkOption { - default = null; - type = with types; nullOr (attrsOf attrs); - }; - input_select = mkOption { - default = null; - type = with types; nullOr (attrsOf attrs); - }; - input_boolean = mkOption { - default = null; - type = with types; nullOr (attrsOf attrs); - }; - input_datetime = mkOption { - default = null; - type = with types; nullOr (attrsOf attrs); - }; - calendar = mkOption { - default = [ ]; - type = with types; listOf attrs; - }; - shell_command = mkOption { - default = null; - type = with types; nullOr (attrsOf str); - }; - - }; - }); + options.services.homeAssistantConfig = mkMagicMergeOption { description = '' home-assistant configuration ''; }; - config = mkIf (cfg != null) { - services.home-assistant.config = let - - sanitize = configuration: - lib.getAttr (builtins.typeOf configuration) { - bool = configuration; - int = configuration; - string = configuration; - str = configuration; - float = configuration; - list = map sanitize configuration; - set = let - stripped = lib.flip lib.filterAttrs configuration (name: value: - name != "_module" && name != "_ref" && value != null); - recursiveSanitized = lib.mapAttrs (lib.const sanitize) stripped; - in if (length (attrNames configuration) == 0) then - null - else - recursiveSanitized; - }; - - in sanitize cfg; - - }; + config = mkIf (cfg != null) { services.home-assistant.config = cfg; }; }