refactor(hass): move some timer logic to netd
All checks were successful
Discord / discord commits (push) Has been skipped
All checks were successful
Discord / discord commits (push) Has been skipped
This commit is contained in:
parent
ee966e467d
commit
bb1b42fd21
3 changed files with 75 additions and 176 deletions
|
@ -14,9 +14,9 @@ namespace NetDaemonConfig.Apps.Spotify.PauseUnpause
|
|||
public record PauseUnpauseData(bool pause);
|
||||
|
||||
[NetDaemonApp]
|
||||
public class PlaySong
|
||||
public class PauseUnpause
|
||||
{
|
||||
public PlaySong(IHaContext ha, Services services)
|
||||
public PauseUnpause(IHaContext ha, Services services)
|
||||
{
|
||||
ha.RegisterServiceCallBack<PauseUnpauseData>(
|
||||
"spotify_pause_unpause",
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using HomeAssistantGenerated;
|
||||
|
||||
using NetDaemon.AppModel;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
|
||||
namespace NetDaemonConfig.Apps.Timer.Setup
|
||||
{
|
||||
public record TimerFinishedEventData
|
||||
{
|
||||
[JsonPropertyName("entity_id")]
|
||||
public string? entity_id { get; init; }
|
||||
}
|
||||
|
||||
[NetDaemonApp]
|
||||
public class Setup
|
||||
{
|
||||
private readonly string _timerTarget = "media_player.music_player_daemon";
|
||||
|
||||
private readonly string _timerTtsTarget = "tts.piper";
|
||||
private readonly string _timerTtsMessage = "A set timer has finished.";
|
||||
|
||||
// TODO: private readonly string timerMediaLocation = "/path/to/file.mp3";
|
||||
|
||||
public Setup(IHaContext ha, Services services, Entities entities)
|
||||
{
|
||||
ha.Events
|
||||
.Where(x => x.EventType == "timer.finished")
|
||||
.Subscribe(x =>
|
||||
{
|
||||
if (x.DataElement.HasValue)
|
||||
{
|
||||
var data = JsonSerializer.Deserialize<TimerFinishedEventData>(x.DataElement.Value);
|
||||
|
||||
if (data?.entity_id?.StartsWith("timer.assist_timer") ?? false)
|
||||
{
|
||||
var timer = entities.Timer
|
||||
.EnumerateAll()
|
||||
.ToLookup((timer) => timer.EntityId,
|
||||
(timer) => timer)[data.entity_id].First();
|
||||
|
||||
if (timer is not null)
|
||||
{
|
||||
services.Tts.Speak(
|
||||
cache: true,
|
||||
mediaPlayerEntityId: _timerTarget,
|
||||
target: new ServiceTarget { EntityIds = [_timerTtsTarget] },
|
||||
message: _timerTtsMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
entities.Timer.EnumerateAll()
|
||||
.Where((timer) => timer.EntityId.StartsWith("timer.assist_timer"))
|
||||
.StateChanges()
|
||||
.Subscribe((timer) =>
|
||||
{
|
||||
if (timer.Old?.State != "idle" && timer.New?.State == "idle")
|
||||
{}
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
|
@ -90,180 +90,6 @@ in {
|
|||
|
||||
# Automate some logic
|
||||
automation = [
|
||||
{
|
||||
alias = "Assist - TimerReached";
|
||||
description = "Assist automation when set timer time is reached.";
|
||||
id = "assist_timerreached";
|
||||
mode = "single";
|
||||
|
||||
condition = [
|
||||
{
|
||||
alias = "Finished timer is an assist timer";
|
||||
condition = "template";
|
||||
value_template = ''{{ trigger.event.data.entity_id[:18] == 'timer.assist_timer' }}'';
|
||||
}
|
||||
];
|
||||
|
||||
trigger = [
|
||||
{
|
||||
alias = "Any timer reached";
|
||||
id = "timer_finished";
|
||||
event_type = "timer.finished";
|
||||
platform = "event";
|
||||
}
|
||||
];
|
||||
|
||||
variables = {inherit settings;};
|
||||
|
||||
action = [
|
||||
{
|
||||
alias = "Get generic variables from script";
|
||||
|
||||
variables = {
|
||||
timer_target = concatStrings [
|
||||
''{%- if settings.get('timer_target')[:13] == "" %}''
|
||||
''{{- settings.get('timer_target_default') }}''
|
||||
|
||||
''{%- elif settings.get('timer_target')[:13] == "media_player." %}''
|
||||
''{{- settings.get('timer_target') }}''
|
||||
|
||||
''{%- elif (${concatStringsSep " " [
|
||||
''settings.get('timer_target')[:7] == "sensor."''
|
||||
''or''
|
||||
''settings.get('timer_target')[:11] == "input_text.")''
|
||||
''and''
|
||||
''(states(settings.get('timer_target'))[:13] == "media_player.")''
|
||||
]}%}''
|
||||
''{{- states(settings.get('timer_target')) }}''
|
||||
|
||||
''{%- elif (${concatStringsSep " " [
|
||||
''settings.get('timer_target')[:7] == "sensor."''
|
||||
''or''
|
||||
''settings.get('timer_target')[:11] == "input_text.")''
|
||||
''and''
|
||||
''(states(settings.get('timer_target')) == "")''
|
||||
]}%}''
|
||||
''{{- settings.get('timer_target_default') }}''
|
||||
|
||||
''{%- else %}''
|
||||
''{%- set media_player_list = states.media_player | map(attribute='entity_id') | list %}''
|
||||
|
||||
''{%- if "sensor." in settings.get('timer_target') or "input_text." in target_area %}''
|
||||
''{%- set target_area = states(settings.get('timer_target')) %}''
|
||||
|
||||
''{%- else %}''
|
||||
''{%- set target_area = settings.get('timer_target') %}''
|
||||
''{%- endif %}''
|
||||
|
||||
''{%- for entity_id in media_player_list %}''
|
||||
''{%- if area_name(entity_id) | lower == target_area | lower %}''
|
||||
''{{ entity_id }}''
|
||||
''{%- endif %}''
|
||||
''{%- endfor %}''
|
||||
''{%- endif %} ''
|
||||
];
|
||||
|
||||
timer_tts = ''{{ settings.get('timer_tts') }}'';
|
||||
timer_tts_service = ''{{ settings.get('timer_tts_service') }}'';
|
||||
timer_tts_target = ''{{ settings.get('timer_tts_target') }}'';
|
||||
timer_volume = ''{{ settings.get('timer_volume') }}'';
|
||||
timer_tts_message = ''{{ settings.get('timer_tts_message') }}'';
|
||||
|
||||
timer_media_location = ''{{ settings.get('timer_media_location') }}'';
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
alias = "Media file or TTS";
|
||||
|
||||
choose = [
|
||||
{
|
||||
alias = "Media file";
|
||||
|
||||
conditions = [
|
||||
{
|
||||
alias = "Timer is a media file";
|
||||
condition = "template";
|
||||
value_template = ''{{ timer_tts == false }}'';
|
||||
}
|
||||
];
|
||||
|
||||
sequence = [
|
||||
{
|
||||
alias = "Play media";
|
||||
service = "media_player.play_media";
|
||||
target.entity_id = ''{{ timer_target }}'';
|
||||
enabled = true;
|
||||
|
||||
data = {
|
||||
announce = true;
|
||||
media_content_id = ''{{ timer_media_location }}'';
|
||||
media_content_type = "music";
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
|
||||
default = [
|
||||
{delay.seconds = 1;}
|
||||
{
|
||||
alias = "Choose TTS service";
|
||||
choose = [
|
||||
{
|
||||
conditions = [
|
||||
{
|
||||
alias = "tts.cloud_say";
|
||||
condition = "template";
|
||||
value_template = ''{{ timer_tts_service != 'tts.speak' }}'';
|
||||
}
|
||||
];
|
||||
|
||||
sequence = [
|
||||
{
|
||||
service = ''{{ timer_tts_service }}'';
|
||||
|
||||
data = {
|
||||
cache = true;
|
||||
entity_id = ''{{ timer_target }}'';
|
||||
message = concatStrings [
|
||||
''{% if timer_tts_message[:7] == "sensor." or timer_tts_message[:11] == "input_text." %}''
|
||||
''{{ states(timer_tts_message) }}''
|
||||
''{% else %}''
|
||||
''{{ timer_tts_message }}''
|
||||
''{% endif %}''
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
|
||||
default = [
|
||||
{
|
||||
service = "tts.speak";
|
||||
|
||||
target.entity_id = ''{{ timer_tts_target }}'';
|
||||
|
||||
data = {
|
||||
cache = true;
|
||||
media_player_entity_id = ''{{ timer_target }}'';
|
||||
message = concatStrings [
|
||||
''{% if timer_tts_message[:7] == "sensor." or timer_tts_message[:11] == "input_text." %}''
|
||||
''{{ states(timer_tts_message) }}''
|
||||
''{% else %}''
|
||||
''{{ timer_tts_message }}''
|
||||
''{% endif %}''
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
{
|
||||
alias = "Assist - TimerFinished";
|
||||
id = "assist_timerfinished";
|
||||
|
|
Loading…
Reference in a new issue