-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindowOpen.swift
More file actions
70 lines (58 loc) · 2.46 KB
/
WindowOpen.swift
File metadata and controls
70 lines (58 loc) · 2.46 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
//
// WindowOpen.swift
//
//
// Created by Julian Kahnert on 01.07.24.
//
import Foundation
import HAModels
import Shared
public struct WindowOpen: Automatable {
public var isActive = true
public let name: String
public let windowContact: ContactSensorDevice
public let notificationWait: Duration
public var triggerEntityIds: Set<EntityId> {
[windowContact.contactSensorId]
}
public init(_ name: String, windowContact: ContactSensorDevice, notificationWait: Duration = .minutes(15)) {
self.name = name
self.windowContact = windowContact
self.notificationWait = notificationWait
}
public func shouldTrigger(with event: HomeEvent, using hm: HomeManagable) async throws -> Bool {
guard case let HomeEvent.change(item) = event,
windowContact.contactSensorId == item.entityId else {
return false
}
return true
}
public func execute(using hm: HomeManagable) async throws {
log.debug("Executing 'WindowOpen' automation")
let isWindowOpen = try await windowContact.isContactOpen(with: hm)
// Clear window state when window closes early
guard isWindowOpen else {
log.debug("Skipping 'WindowOpen' automation, window is closed")
await hm.setWindowOpenState(entityId: windowContact.contactSensorId, to: nil)
return
}
// Set window state for tracking
let opened = Date()
let state = WindowOpenState(entityId: windowContact.contactSensorId, opened: opened, maxOpenDuration: notificationWait.timeInterval)
await hm.setWindowOpenState(entityId: windowContact.contactSensorId, to: state)
let end = opened.addingTimeInterval(notificationWait.timeInterval)
log.debug("Start sleeping for \(notificationWait.description) before sending notification")
var shouldWait = true
while shouldWait {
let waitSeconds = min(60, end.timeIntervalSinceNow)
try await Task.sleep(for: .seconds(waitSeconds))
if end.timeIntervalSinceNow <= 5 {
shouldWait = false
}
}
log.debug("Start sending notification")
let message = "\(windowContact.contactSensorId.name) (\(windowContact.contactSensorId.placeId))"
await hm.sendNotification(title: "🪟 offen", message: message, id: windowContact.contactSensorId.windowNotificationId)
log.debug("End sending notification")
}
}