112 lines
3.4 KiB
Nix
Executable File
112 lines
3.4 KiB
Nix
Executable File
{
|
|
config,
|
|
lib,
|
|
namespace,
|
|
...
|
|
}:
|
|
with lib;
|
|
let
|
|
inherit (lib.${namespace}) mkOpt mkBoolOpt;
|
|
cfg = config.${namespace}.services.home-assistant.automation;
|
|
|
|
automationToYaml = mlcfg: ''
|
|
- id: '${toString mlcfg.id}'
|
|
alias: ${mlcfg.alias}
|
|
description: '${mlcfg.description}'
|
|
triggers:
|
|
- device_id: ${mlcfg.motion-sensor.mqttDeviceId}
|
|
entity_id: ${mlcfg.motion-sensor.mqttEntityId}
|
|
domain: binary_sensor
|
|
id: occupied
|
|
subtype: on_press
|
|
trigger: device
|
|
type: occupied
|
|
for:
|
|
hours: 0
|
|
minutes: 0
|
|
seconds: 0
|
|
- device_id: ${mlcfg.motion-sensor.mqttDeviceId}
|
|
entity_id: ${mlcfg.motion-sensor.mqttEntityId}
|
|
domain: binary_sensor
|
|
id: vacant
|
|
subtype: off_press
|
|
trigger: device
|
|
type: not_occupied
|
|
for:
|
|
hours: 0
|
|
minutes: 0
|
|
seconds: 5
|
|
conditions: []
|
|
actions:
|
|
- choose:
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- occupied
|
|
sequence:
|
|
- action: light.turn_on
|
|
data:
|
|
brightness_pct: ${toString mlcfg.light.brightnessPercent}
|
|
kelvin: ${toString mlcfg.light.lightTemperature}
|
|
transition: ${toString mlcfg.light.transitionTime}
|
|
metadata: {}
|
|
target:
|
|
entity_id:
|
|
- ${mlcfg.light.entityId}
|
|
- conditions:
|
|
- condition: trigger
|
|
id:
|
|
- vacant
|
|
sequence:
|
|
- action: light.turn_off
|
|
data:
|
|
transition: ${toString mlcfg.light.transitionTime}
|
|
metadata: {}
|
|
target:
|
|
entity_id:
|
|
- ${mlcfg.light.entityId}
|
|
mode: single
|
|
'';
|
|
|
|
motionLightAutomations = concatStringsSep "\n" (
|
|
mapAttrsToList (_: automationToYaml) cfg.motion-light
|
|
);
|
|
in
|
|
{
|
|
options.${namespace}.services.home-assistant.automation = {
|
|
motion-light = 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";
|
|
motion-sensor = {
|
|
mqttDeviceId = mkOpt types.str "" "mqtt device id of the motion sensor";
|
|
mqttEntityId = mkOpt types.str "" "mqtt entity id of the motion sensor";
|
|
};
|
|
light = {
|
|
entityId = 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";
|
|
};
|
|
switch = {
|
|
enable = mkBoolOpt false "is it a binary switch light";
|
|
deviceId = mkOpt types.str "" "device id of the switch";
|
|
entityId = mkOpt types.str "" "entity id of the switch";
|
|
};
|
|
};
|
|
}
|
|
)) { } "motion light automations";
|
|
};
|
|
|
|
config = lib.mkIf config.${namespace}.services.home-assistant.enable {
|
|
environment.etc."hass/motion-light-automations.yaml" = {
|
|
text = motionLightAutomations;
|
|
user = "hass";
|
|
group = "hass";
|
|
};
|
|
};
|
|
}
|