maybe?
This commit is contained in:
@@ -10,158 +10,113 @@ let
|
||||
inherit (lib.${namespace}) mkOpt;
|
||||
cfg = config.${namespace}.services.home-assistant.automation;
|
||||
|
||||
yamlFormat = pkgs.formats.yaml { };
|
||||
|
||||
mkLightswitchAutomation = name: lscfg: {
|
||||
id = 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 = 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 ];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
|
||||
mode = "single";
|
||||
};
|
||||
|
||||
# Generate all automations as list
|
||||
lightswitchAutomations = mapAttrsToList mkLightswitchAutomation cfg.lightswitch;
|
||||
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}
|
||||
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 = {
|
||||
|
||||
Reference in New Issue
Block a user