-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLivingRoomPanelNavigation.cs
More file actions
78 lines (74 loc) · 3.57 KB
/
LivingRoomPanelNavigation.cs
File metadata and controls
78 lines (74 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using CodeCasa.AutoGenerated;
using CodeCasa.NetDaemon.Utilities.Extensions;
using NetDaemon.AppModel;
using NetDaemon.HassModel;
using Reactive.Boolean;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Text.Json.Serialization;
using CodeCasa.CustomEntities.Core.Events;
using CodeCasa.CustomEntities.Core.GoogleHome;
using CodeCasa.CustomEntities.Core.InputSelect;
using NetDaemon.HassModel.Entities;
namespace CodeCasa.Automations.Apps.Dashboard
{
[NetDaemonApp]
internal class LivingRoomPanelNavigation
{
public LivingRoomPanelNavigation(
IHaContext context,
AutomationEntities automationEntities,
InputSelectEntities inputSelectEntities,
GoogleHomeAlarmEntities googleHomeAlarmEntities,
GoogleHomeTimerEntities googleHomeTimerEntities,
LivingRoomWallPanelView livingRoomWallPanelView,
IScheduler scheduler)
{
var panelInteraction = context.Events.Filter(Events.LivingRoomPanelInteractionEvent).Select(_ => true);
var proximityDetected = context.Events
.Filter<AutomationTriggeredEventData>(Events.AutomationTriggeredEvent)
.Where(e => e.Data?.EntityId == automationEntities.WebhookLivingRoomWallPanelProximityDetected.EntityId)
.Select(_ => true);
var anyInteraction = panelInteraction.Merge(proximityDetected);
var recentInteraction = anyInteraction
.Merge(
anyInteraction
.Throttle(TimeSpan.FromSeconds(30))
.Select(_ => false)
).Prepend(false);
var notificationCount =
inputSelectEntities.LivingRoomPanelNotifications.StateAllChangesWithCurrent().Select(s => string.IsNullOrEmpty(s.New?.Attributes?.Options?.FirstOrDefault()) ? 0 : s.New.Attributes.Options.Count);
var alarmCount = googleHomeAlarmEntities.Select(
gt => gt
.StateAllChangesDeserialized<SpeakerAlarmInfo>())
.CombineLatest()
.Select(speakerInfos => speakerInfos.Sum(s => s.Alarms?.Count ?? 0));
var timerCount = googleHomeTimerEntities.Select(
gt => gt
.StateAllChangesDeserialized<SpeakerTimerInfo>())
.CombineLatest()
.Select(speakerInfos => speakerInfos.Sum(s => s.Timers?.Count ?? 0));
var infoCount = notificationCount.CombineLatest(alarmCount, timerCount, (c1, c2, c3) => c1 + c2 + c3);
var infoToDisplay = infoCount
.Scan(
(prev: 0, curr: 0),
(state, newCount) => (prev: state.curr, curr: newCount)
)
.Select(state =>
{
if (state.curr == 0) return false;
if (state.curr > state.prev) return true;
return (bool?)null;
})
.Where(b => b != null).Select(b => b!.Value);
recentInteraction.Or(infoToDisplay, OperatorDistinctness.NotDistinct).SubscribeTrueFalse(
() => livingRoomWallPanelView.SelectOption(LivingRoomWallPanelView.States.Home),
() => livingRoomWallPanelView.SelectOption(LivingRoomWallPanelView.States.Idle));
}
// ReSharper disable once ClassNeverInstantiated.Local
private record AutomationTriggeredEventData
{
[JsonPropertyName("entity_id")] public string? EntityId { get; init; }
}
}
}