os/hosts/giggles/home-assistant-package-wohnzimmer-tv.nix
Hendrik Sokolowski 6c7d9704bd
All checks were successful
continuous-integration/drone/push Build is passing
latest changes
2023-11-11 01:24:25 +01:00

247 lines
8.6 KiB
Nix

let
helper = import ./home-assistant-automation-helpers.nix;
entityMediaplayerFireTV = "media_player.wohnzimmer_firetv";
entityMediaplayerXBOX = "media_player.wohnzimmer_xbox";
entityMediaplayerZeppelin = "media_player.wohnzimmer_zeppelin";
entitySwitchTV = "switch.wohnzimmer_tv_steckdosenleiste_tv";
entitySwitchFireTV = "switch.wohnzimmer_tv_steckdosenleiste_firetv";
entitySwitchXBOX = "switch.wohnzimmer_tv_steckdosenleiste_xbox";
entitySwitchZeppelin = "switch.wohnzimmer_tv_steckdosenleiste_zeppelin";
entityActivitySelect = "wohnzimmer_tv_activity";
entityVolumeMute = "wohnzimmer_tv_volume_mute";
entityVolumeLevel = "wohnzimmer_tv_volume_level";
entityHarmonyActivitySelect = "select.wohnzimmer_harmony_activities";
activityOptionOff = "Aus";
activityOptionOffHarmony = "power_off";
activityOptionMusic = "Musik";
activityOptionFireTV = "FireTV";
activityOptionXBOX = "XBOX";
# because of tasmota
switchDelaySecondsZeppelin = 1;
switchDelaySecondsTV = 2;
switchDelaySecondsFireTV = 3;
switchDelaySecondsXBOXOn = 4;
switchDelaySecondsXBOXOff = 60; # because it needs longer to shut down
mkAutomationCondition = slug: triggers: conditions: actions: {
alias = "Wohnzimmer TV: ${slug}";
id = "wohnzimmer_tv_automation_${slug}";
trigger = triggers;
condition = conditions;
action = actions;
};
mkAutomation = slug: triggers: actions: (mkAutomationCondition slug triggers [] actions);
rokuButtonPressed = key: {
platform = "event";
event_type = "roku_command";
event_data = {
type = "keypress";
key = key;
};
};
mkRokuVolumeAutomation = key: action: mkAutomation
"roku_vol_${action}"
[(rokuButtonPressed key)]
[(helper.action.callService "input_number.${action}" "input_number.${entityVolumeLevel}")];
tvSelect = option: {
service = "input_select.select_option";
target.entity_id = "input_select.${entityActivitySelect}";
data.option = option;
};
harmonySelect = option: {
service = "select.select_option";
target.entity_id = entityHarmonyActivitySelect;
data.option = option;
};
tvChangedTo = stateTo: (helper.trigger.stateTo "input_select.${entityActivitySelect}" stateTo);
tvChangedNotTo = stateNotTo: (helper.trigger.stateNotTo "input_select.${entityActivitySelect}" stateNotTo);
harmonyChangedTo = stateTo: (helper.trigger.stateTo entityHarmonyActivitySelect stateTo);
harmonyChangedNotTo = stateNotTo: (helper.trigger.stateNotTo entityHarmonyActivitySelect stateNotTo);
in
{
input_boolean = {
"${entityVolumeMute}" = {
name = "Wohnzimmer TV Mute";
icon = "mdi:volume-mute";
};
};
input_number = {
"${entityVolumeLevel}" = {
name = "Wohnzimmer TV Lautstärke";
icon = "mdi:volume-high";
unit_of_measurement = "%";
min = "0.0";
max = "1.0";
step = "0.01";
};
};
input_select = {
"${entityActivitySelect}" = {
name = "Wohnzimmer TV Aktivität";
options = [
activityOptionOff
activityOptionMusic
activityOptionFireTV
activityOptionXBOX
];
};
};
media_player = [
{
platform = "universal";
name = "Wohnzimmer TV Universal";
device_class = "tv";
unique_id = "media_player_universal_wohnzimmer_tv";
children = [
entityMediaplayerFireTV
entityMediaplayerXBOX
entityMediaplayerZeppelin
];
active_child_template = ''
{% if is_state('input_select.${entityActivitySelect}', '${activityOptionMusic}') %}
${entityMediaplayerZeppelin}
{% elif is_state('input_select.${entityActivitySelect}', '${activityOptionFireTV}') %}
${entityMediaplayerFireTV}
{% elif is_state('input_select.${entityActivitySelect}', '${activityOptionXBOX}') %}
${entityMediaplayerXBOX}
{% endif %}
'';
state_template = ''
{% if is_state('input_select.${entityActivitySelect}', '${activityOptionMusic}') %}
{{ states('${entityMediaplayerZeppelin}') }}
{% elif is_state('input_select.${entityActivitySelect}', '${activityOptionFireTV}') %}
{{ states('${entityMediaplayerFireTV}') }}
{% elif is_state('input_select.${entityActivitySelect}', '${activityOptionXBOX}') %}
{{ states('${entityMediaplayerXBOX}') }}
{% else %}
off
{% endif %}
'';
commands = {
turn_on = tvSelect activityOptionFireTV;
turn_off = tvSelect activityOptionOff;
volume_set = helper.action.callService "input_number.set_value" "input_number.${entityVolumeLevel}" // {data.value = "{{ volume_level }}";};
volume_up = helper.action.callService "input_number.increment" "input_number.${entityVolumeLevel}";
volume_down = helper.action.callService "input_number.decrement" "input_number.${entityVolumeLevel}";
volume_mute = helper.action.callService "input_boolean.toggle" "input_boolean.${entityVolumeMute}";
};
attributes = {
is_volume_muted = "input_boolean.${entityVolumeMute}";
volume_level = "input_number.${entityVolumeLevel}";
};
}
];
automation = [
(mkRokuVolumeAutomation "Up" "increment")
(mkRokuVolumeAutomation "Down" "decrement")
(mkAutomation "roku_vol_mute" [(rokuButtonPressed "Right")] [(helper.action.callService "input_boolean.toggle" "input_boolean.${entityVolumeMute}")])
(mkAutomation "sync_off_to_harmony" [(tvChangedTo activityOptionOff)] [(harmonySelect activityOptionOffHarmony)])
(mkAutomation "sync_activity_to_harmony" [(tvChangedNotTo activityOptionOff)] [(harmonySelect "{{ trigger.to_state.state }}")])
(mkAutomation "sync_off_from_harmony" [(harmonyChangedTo activityOptionOffHarmony)] [(tvSelect activityOptionOff)])
(mkAutomation "sync_activity_from_harmony" [(harmonyChangedNotTo activityOptionOffHarmony)] [(tvSelect "{{ trigger.to_state.state }}")])
(
mkAutomationCondition
"set_speaker_volume"
[
(helper.trigger.state "input_number.${entityVolumeLevel}")
(helper.trigger.stateTo "input_boolean.${entityVolumeMute}" "off")
(helper.trigger.stateFrom entityMediaplayerZeppelin "unavailable")
]
[{
condition = "state";
entity_id = "input_boolean.${entityVolumeMute}";
state = "off";
}]
[(helper.action.callService "media_player.volume_set" entityMediaplayerZeppelin // {data.volume_level = "{{ states(\"input_number.${entityVolumeLevel}\") }}";})]
)
(
mkAutomation
"mute_speaker"
[(helper.trigger.stateTo "input_boolean.${entityVolumeMute}" "on")]
[(helper.action.callService "media_player.volume_set" entityMediaplayerZeppelin // {data.volume_level = "0";})]
)
(
mkAutomation
"switch_off_tv"
[
(tvChangedTo activityOptionOff)
(tvChangedTo activityOptionMusic)
]
(helper.action.delayed (helper.action.turnOff entitySwitchTV) switchDelaySecondsTV)
)
(
mkAutomation
"switch_off_firetv"
[
(tvChangedTo activityOptionOff)
(tvChangedTo activityOptionMusic)
(tvChangedTo activityOptionXBOX)
]
(helper.action.delayed (helper.action.turnOff entitySwitchFireTV) switchDelaySecondsFireTV)
)
(
mkAutomation
"switch_off_xbox"
[
(tvChangedTo activityOptionOff)
(tvChangedTo activityOptionMusic)
(tvChangedTo activityOptionFireTV)
]
(helper.action.delayed (helper.action.turnOff entitySwitchXBOX) switchDelaySecondsXBOXOff)
)
(
mkAutomation
"switch_off_zeppelin"
[(tvChangedTo activityOptionOff)]
(helper.action.delayed (helper.action.turnOff entitySwitchZeppelin) switchDelaySecondsZeppelin)
)
(
mkAutomation
"switch_on_tv"
[
(tvChangedTo activityOptionFireTV)
(tvChangedTo activityOptionXBOX)
]
(helper.action.delayed (helper.action.turnOn entitySwitchTV) switchDelaySecondsTV)
)
(
mkAutomation
"switch_on_firetv"
[(tvChangedTo activityOptionFireTV)]
(helper.action.delayed (helper.action.turnOn entitySwitchFireTV) switchDelaySecondsFireTV)
)
(
mkAutomation
"switch_on_xbox"
[(tvChangedTo activityOptionXBOX)]
(helper.action.delayed (helper.action.turnOn entitySwitchXBOX) switchDelaySecondsXBOXOn)
)
(
mkAutomation
"switch_on_zeppelin"
[(tvChangedNotTo activityOptionOff)]
(helper.action.delayed (helper.action.turnOn entitySwitchZeppelin) switchDelaySecondsZeppelin)
)
];
}