Files
nix-config/modules/nixos/homeassistant/automations/default.nix
mjallen18 81b8bd9ec5 test
2025-11-21 12:14:07 -06:00

192 lines
5.5 KiB
Nix

{
config,
lib,
pkgs,
namespace,
...
}:
with lib;
let
inherit (lib.${namespace}) mkOpt;
cfg = config.${namespace}.services.home-assistant.automation;
yamlFormat = pkgs.formats.yaml { };
mkLightswitchAutomation = name: lscfg:
# Use explicit ordering to ensure proper YAML structure
listToAttrs [
(nameValuePair "actions" [
{
choose = [
{
conditions = [
{
condition = "trigger";
id = [ "on press" ];
}
];
sequence = [
{
action = "light.turn_on";
data = {
brightness_pct = lscfg.brightnessPercent;
kelvin = lscfg.lightTemperature;
transition = lscfg.transitionTime;
};
metadata = {};
target.entity_id = [ lscfg.lightEntityId ];
}
];
}
{
conditions = [
{
condition = "trigger";
id = [ "off press" ];
}
];
sequence = [
{
action = "light.turn_off";
data.transition = lscfg.transitionTime;
metadata = {};
target.entity_id = [ lscfg.lightEntityId ];
}
];
}
{
conditions = [
{
condition = "trigger";
id = [ "up press" ];
}
];
sequence = [
{
action = "light.turn_on";
data.brightness_step_pct = lscfg.brightnessStepPercent;
metadata = {};
target.entity_id = lscfg.lightEntityId;
}
];
}
{
conditions = [
{
condition = "trigger";
id = [ "down press" ];
}
];
sequence = [
{
action = "light.turn_on";
data.brightness_step_pct = -lscfg.brightnessStepPercent;
metadata = {};
target.entity_id = lscfg.lightEntityId;
}
];
}
{
conditions = [
{
condition = "trigger";
id = [ "on hold" ];
}
];
sequence = [
{
action = "light.turn_on";
data = {
brightness_pct = 100;
rgb_color = [ 255 0 0 ];
transition = 0;
};
metadata = {};
target.entity_id = [ lscfg.lightEntityId ];
}
];
}
];
}
])
(nameValuePair "alias" lscfg.alias)
(nameValuePair "conditions" [])
(nameValuePair "description" lscfg.description)
(nameValuePair "id" (toString lscfg.id))
(nameValuePair "mode" "single")
(nameValuePair "triggers" [
{
device_id = lscfg.mqttDeviceId;
domain = "mqtt";
id = "on press";
subtype = "on-press";
trigger = "device";
type = "action";
}
{
device_id = lscfg.mqttDeviceId;
domain = "mqtt";
id = "off press";
subtype = "off-press";
trigger = "device";
type = "action";
}
{
device_id = lscfg.mqttDeviceId;
domain = "mqtt";
id = "up press";
subtype = "up-press";
trigger = "device";
type = "action";
}
{
device_id = lscfg.mqttDeviceId;
domain = "mqtt";
id = "down press";
subtype = "down-press";
trigger = "device";
type = "action";
}
{
device_id = lscfg.mqttDeviceId;
domain = "mqtt";
id = "on hold";
subtype = "on-hold";
trigger = "device";
type = "action";
}
])
];
# Generate all automations as list
lightswitchAutomations = mapAttrsToList mkLightswitchAutomation cfg.lightswitch;
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" lightswitchAutomations;
};
}