diff options
| author | Joe Mou <dev@mou.fo> | 2024-06-22 02:22:00 -0400 |
|---|---|---|
| committer | Joe Mou <dev@mou.fo> | 2024-10-09 13:48:40 -0400 |
| commit | 8f787568eaed80f360e059f9a16883ab4ea6e5c5 (patch) | |
| tree | 46d1346e646e2331a7708e4065df50563b05209d /hostnix/elmo/home-assistant.nix | |
| parent | 1abb43173e05aad3f5c9b7ac85c80ebf14749fb0 (diff) | |
Refactor HA helper functions
Based on https://github.com/nathan-gs/nix-conf/blob/main/lib/ha.nix
(which goes further in using the module system; see
https://nathan.gs/2023/12/09/adding-helper-functions-to-nixos/ )
Diffstat (limited to 'hostnix/elmo/home-assistant.nix')
| -rw-r--r-- | hostnix/elmo/home-assistant.nix | 80 |
1 files changed, 45 insertions, 35 deletions
diff --git a/hostnix/elmo/home-assistant.nix b/hostnix/elmo/home-assistant.nix index d97e7a1..db270dd 100644 --- a/hostnix/elmo/home-assistant.nix +++ b/hostnix/elmo/home-assistant.nix @@ -1,4 +1,4 @@ -{ pkgs, ... }: +{ lib, pkgs, ... }: { services.postgresql = { @@ -28,7 +28,36 @@ tuya_local ]; - config = { + config = let + + ha = { + trigger = { + at = time: { + platform = "time"; + at = time; + }; + }; + + action = { + on = entity: { + service = "${lib.head (lib.strings.splitString "." entity)}.turn_on"; + target.entity_id = entity; + }; + + off = entity: { + service = "${lib.head (lib.strings.splitString "." entity)}.turn_off"; + target.entity_id = entity; + }; + + toggle = entity: { + service = "${lib.head (lib.strings.splitString "." entity)}.toggle"; + target.entity_id = entity; + }; + }; + }; + + in + { default_config = { }; http = { server_host = "::1"; @@ -77,12 +106,7 @@ }; } ]; - turn_off = [ - { - service = "climate.turn_off"; - target.entity_id = "climate.air_conditioner_1"; - } - ]; + turn_off = [ (ha.action.off "climate.air_conditioner_1") ]; }; }; } @@ -424,17 +448,9 @@ }; }; - service = service: target_id: { - inherit service; - target.entity_id = target_id; - }; - - setTemperatureAt = at: temperature: { - alias = "Thermostat ${at}"; - trigger = { - inherit at; - platform = "time"; - }; + setTemperatureAt = time: temperature: { + alias = "Thermostat ${time}"; + trigger = [ (ha.trigger.at time) ]; action = [ { service = "climate.set_temperature"; @@ -455,8 +471,8 @@ event = "leave"; }; action = [ - (service "light.turn_off" "light.panel_light") - (service "climate.turn_off" "climate.bedroom_cool") + (ha.action.off "light.panel_light") + (ha.action.off "climate.bedroom_cool") ]; } @@ -482,34 +498,28 @@ } ]; action = [ - (service "light.turn_on" "light.panel_light") + (ha.action.on "light.panel_light") ]; } { alias = "Grow light morning"; - trigger = { - platform = "time"; - at = "08:00"; - }; + trigger = [ (ha.trigger.at "08:00") ]; action = [ - (service "switch.turn_on" "light.grow_light") - (service "switch.turn_on" "switch.wakko") + (ha.action.on "light.grow_light") + (ha.action.on "switch.wakko") ]; } { alias = "Grow light night"; - trigger = { - platform = "time"; - at = "20:00"; - }; - action = [ (service "switch.turn_off" "switch.wakko") ]; + trigger = [ (ha.trigger.at "20:00") ]; + action = [ (ha.action.off "switch.wakko") ]; } (button "binary_sensor.bedroom_button_1" - (service "switch.toggle" "switch.wakko")) + (ha.action.toggle "switch.wakko")) (button "binary_sensor.bedroom_button_2" - (service "light.toggle" "light.panel_light")) + (ha.action.toggle "light.panel_light")) (setTemperatureAt "06:00" 71) (setTemperatureAt "12:00" 70) |
