From 9ff8fe83991925a6b08c7f1f7ab56896197abee7 Mon Sep 17 00:00:00 2001 From: mjallen18 Date: Fri, 21 Nov 2025 11:20:01 -0600 Subject: [PATCH] testing --- .../homeassistant/automations/default.nix | 195 ++++++++++++++++++ systems/x86_64-linux/nuc-nixos/default.nix | 14 +- 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 modules/nixos/homeassistant/automations/default.nix diff --git a/modules/nixos/homeassistant/automations/default.nix b/modules/nixos/homeassistant/automations/default.nix new file mode 100644 index 0000000..d13cc90 --- /dev/null +++ b/modules/nixos/homeassistant/automations/default.nix @@ -0,0 +1,195 @@ +{ + config, + lib, + namespace, + ... +}: +with lib; +let + inherit (lib.${namespace}) mkOpt; + cfg = config.${namespace}.services.home-assistant.automation; + + yamlFormat = pkgs.formats.yaml { }; + + mkLightswitchAutomation = name: lscfg: { + id = toString lscfg.id; + alias = lscfg.alias; + description = lscfg.description; + + triggers = [ + { + domain = "mqtt"; + device_id = lscfg.mqttDeviceId; + type = "action"; + subtype = "on-press"; + trigger = "device"; + id = "on press"; + } + { + domain = "mqtt"; + device_id = lscfg.mqttDeviceId; + type = "action"; + subtype = "off-press"; + trigger = "device"; + id = "off press"; + } + { + domain = "mqtt"; + device_id = lscfg.mqttDeviceId; + type = "action"; + subtype = "up-press"; + trigger = "device"; + id = "up press"; + } + { + domain = "mqtt"; + device_id = lscfg.mqttDeviceId; + type = "action"; + subtype = "down-press"; + trigger = "device"; + id = "down press"; + } + { + domain = "mqtt"; + device_id = lscfg.mqttDeviceId; + type = "action"; + subtype = "on-hold"; + trigger = "device"; + id = "on hold"; + } + ]; + + conditions = []; + + actions = [ + { + choose = [ + { + conditions = [ + { + condition = "trigger"; + id = [ "on press" ]; + } + ]; + sequence = [ + { + action = "light.turn_on"; + metadata = {}; + data = { + transition = lscfg.transitionTime; + brightness_pct = lscfg.brightnessPercent; + kelvin = lscfg.lightTemperature; + }; + target.entity_id = [ lscfg.lightEntityId ]; + } + ]; + } + { + conditions = [ + { + condition = "trigger"; + id = [ "off press" ]; + } + ]; + sequence = [ + { + action = "light.turn_off"; + metadata = {}; + data.transition = lscfg.transitionTime; + target.entity_id = [ lscfg.lightEntityId ]; + } + ]; + } + { + conditions = [ + { + condition = "trigger"; + id = [ "up press" ]; + } + ]; + sequence = [ + { + action = "light.turn_on"; + metadata = {}; + data.brightness_step_pct = lscfg.brightnessStepPercent; + target.entity_id = lscfg.lightEntityId; + } + ]; + } + { + conditions = [ + { + condition = "trigger"; + id = [ "down press" ]; + } + ]; + sequence = [ + { + action = "light.turn_on"; + metadata = {}; + data.brightness_step_pct = -lscfg.brightnessStepPercent; + target.entity_id = lscfg.lightEntityId; + } + ]; + } + { + conditions = [ + { + condition = "trigger"; + id = [ "on hold" ]; + } + ]; + sequence = [ + { + action = "light.turn_on"; + metadata = {}; + data = { + transition = 0; + brightness_pct = 100; + rgb_color = [ 255 38 0 ]; + }; + target.entity_id = [ lscfg.lightEntityId ]; + } + ]; + } + ]; + } + ]; + + mode = "single"; + }; + + # Generate all automations as list + lightswitchAutomations = mapAttrsToList mkLightswitchAutomation cfg.lightswitch; + + # YAML format generator + yamlFormat = pkgs.formats.yaml { }; +in +{ + options.${namespace}.services.home-assistant.automation = { + lightswitch = + mkOpt + (types.attrsOf ( + types.submodule { + options = { + id = mkOpt types.int 0 "Automation Id"; + alias = mkOpt types.str "" "Alias/Friendly Name"; + description = mkOpt types.str "" "Automation Description"; + mqttDeviceId = mkOpt types.str "" "mqtt device id of the sitch"; + lightEntityId = mkOpt types.str "" "home assistant entity id for the lights"; + transitionTime = mkOpt types.int 2 "light transition time in seconds"; + brightnessPercent = mkOpt types.int 100 "brightness percentage when turned on"; + lightTemperature = mkOpt types.int 6000 "light temperature when turned on"; + brightnessStepPercent = mkOpt types.int 10 "dimmer step in brightness percent"; + }; + } + )) + { } + "lightswitch automations"; + }; + + config = { + environment.etc."lightswitch-automations.yaml".source = + yamlFormat.generate "lightswitch-automations.yaml" allAutomations; + }; +} \ No newline at end of file diff --git a/systems/x86_64-linux/nuc-nixos/default.nix b/systems/x86_64-linux/nuc-nixos/default.nix index 445ab0b..6f0b47f 100644 --- a/systems/x86_64-linux/nuc-nixos/default.nix +++ b/systems/x86_64-linux/nuc-nixos/default.nix @@ -67,7 +67,19 @@ # # Services # # # ################################################### - services.home-assistant.enable = true; + services.home-assistant = { + enable = true; + automation = { + lightswitch = { + living-room-lights = { + id = "1741726347213"; + alias = "Living Room Light Switch"; + mqttDeviceId = "958ff6b3c9df0db21e418aaf6410b996"; + lightEntityId = "light.living_room_lights"; + }; + }; + }; + }; # ################################################### # # User # #