stockholm/makefu/2configs/home/ham/lib/default.nix

135 lines
3.5 KiB
Nix
Raw Normal View History

2020-02-04 17:26:59 +00:00
let
prefix = "/ham";
in
{
inherit prefix;
say = let
# returns a list of actions to be performed on an mpd to say something
tts = { message, entity }:
[
{
2022-01-30 22:51:32 +00:00
service = "sonos.snapshot";
target.entity_id = entity;
2020-02-04 17:26:59 +00:00
}
{
2022-01-30 22:51:32 +00:00
service = "tts.google_say";
2020-02-04 17:26:59 +00:00
data = {
2022-09-23 21:41:30 +00:00
entity_id = entity;
2022-01-30 22:51:32 +00:00
inherit message;
language = "de";
2020-02-04 17:26:59 +00:00
};
}
2022-01-30 22:51:32 +00:00
#{ wait_template = "{{ is_state('${entity}' , 'playing') }}";
# timeout = "00:00:02";
#}
#{ wait_template = "{{ not is_state('${entity}' , 'playing') }}";
# timeout = "00:01:00";
#}
{ delay.seconds = 1; }
{ delay = ''
{% set duration = state_attr("${entity}","media_duration") or 0 %}
{% set seconds = (duration % 60 ) %}
2022-01-30 22:51:32 +00:00
{% set minutes = (duration / 60)|int % 60 %}
{% set hours = (duration / 3600)|int %}
{{ "%02i:%02i:%02i"|format(hours, minutes, seconds)}}
'';
2020-02-04 17:26:59 +00:00
}
{
2022-01-30 22:51:32 +00:00
service = "sonos.restore";
target.entity_id = entity;
2020-02-04 17:26:59 +00:00
}
];
in
{
2022-01-30 22:51:32 +00:00
living_room = message: tts {
inherit message;
entity = "media_player.living_room";
};
office = message: tts {
2020-02-04 17:26:59 +00:00
inherit message;
2022-01-30 22:51:32 +00:00
entity = "media_player.office";
2020-02-04 17:26:59 +00:00
};
2022-09-23 21:41:30 +00:00
bedroom = message: tts {
inherit message;
entity = "media_player.bedroom";
};
2020-02-04 17:26:59 +00:00
};
2021-08-31 13:14:37 +00:00
2020-03-03 20:19:19 +00:00
zigbee.prefix = "/ham/zigbee";
2021-08-31 13:14:37 +00:00
btn_cycle_light = light: btn: halfbright:
let
maxbright = 255;
transition = 0.2; # seconds
in
# this function implements a simple state machine based on the state and brightness of the light (light must support brightness
{
alias = "Cycle through states of ${light} via button ${btn}";
trigger = {
platform = "state";
entity_id = "sensor.${btn}_click";
to = "single";
};
action = {
choose = [
{
# state 0: off to half
conditions = {
condition = "template";
value_template = ''{{ states("${light}") == "off" }}'';
};
sequence = [
{
service = "light.turn_on";
data = {
entity_id = light;
brightness = halfbright;
};
}
];
}
{
# state 1: half to full
conditions = {
condition = "template";
value_template = ''{{ states('${light}') == 'on' and ( ${toString (halfbright - 1)} <= state_attr("${light}","brightness") <= ${toString (halfbright + 1)})}}'';
};
sequence = [
{
service = "light.turn_on";
data = {
entity_id = light;
brightness = maxbright;
};
}
];
}
{
# state 2: full to off
conditions = {
condition = "template";
# TODO: it seems like the devices respond with brightness-1 , maybe off-by-one somewhere?
value_template = ''{{ states("${light}") == "on" and state_attr("${light}","brightness") >= ${toString (maxbright - 1)}}}'';
};
sequence = [
{
service = "light.turn_off";
data = {
entity_id = light;
};
}
];
}
];
# default: on to off
# this works because state 0 checks for "state == off"
default = [{
service = "light.turn_off";
data = {
entity_id = light;
};
}];
};
};
2020-02-04 17:26:59 +00:00
}