150 lines
4.2 KiB
Nix
Executable File
150 lines
4.2 KiB
Nix
Executable File
{
|
|
config,
|
|
lib,
|
|
namespace,
|
|
...
|
|
}:
|
|
with lib;
|
|
let
|
|
inherit (lib.${namespace}) mkOpt;
|
|
cfg = config.${namespace}.services.home-assistant.automation;
|
|
|
|
automationToYaml = lscfg: ''
|
|
- id: '${toString lscfg.id}'
|
|
alias: ${lscfg.alias}
|
|
description: '${lscfg.description}'
|
|
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
|
|
conditions: []
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- on press
|
|
sequence:
|
|
- action: light.turn_on
|
|
data:
|
|
brightness_pct: ${toString lscfg.brightnessPercent}
|
|
color_temp_kelvin: ${toString lscfg.lightTemperature}
|
|
transition: ${toString lscfg.transitionTime}
|
|
metadata: {}
|
|
target:
|
|
entity_id:
|
|
- ${lscfg.lightEntityId}
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- off press
|
|
sequence:
|
|
- action: light.turn_off
|
|
data:
|
|
transition: ${toString lscfg.transitionTime}
|
|
metadata: {}
|
|
target:
|
|
entity_id:
|
|
- ${lscfg.lightEntityId}
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- up press
|
|
sequence:
|
|
- action: light.turn_on
|
|
data:
|
|
brightness_step_pct: ${toString lscfg.brightnessStepPercent}
|
|
metadata: {}
|
|
target:
|
|
entity_id: ${lscfg.lightEntityId}
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- down press
|
|
sequence:
|
|
- action: light.turn_on
|
|
data:
|
|
brightness_step_pct: ${toString (-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}
|
|
mode: single
|
|
'';
|
|
|
|
lightswitchAutomations = concatStringsSep "\n" (
|
|
mapAttrsToList (_: automationToYaml) 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";
|
|
deviceAction = mkOpt types.str "" "device action";
|
|
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 = lib.mkIf config.${namespace}.services.home-assistant.enable {
|
|
environment.etc."hass/lightswitch-automations.yaml" = {
|
|
text = lightswitchAutomations;
|
|
user = "hass";
|
|
group = "hass";
|
|
};
|
|
};
|
|
}
|