Add "Focus" sensor for iOS 15 & macOS 12 (#1664)

Refs #945. This will not be available until we switch to compiling builds with Xcode 13, regardless of when it is merged.

## Summary
Adds a "Focus" binary sensor which is on when focus is enabled. This updates live, even when the app isn't running, via Intents.

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#543

## Any other notes
Followup is needed for onboarding to prompt for this permission.
This commit is contained in:
Zac West
2021-06-18 12:45:09 -07:00
committed by GitHub
parent 188189ec45
commit cd6fb39c6d
16 changed files with 370 additions and 40 deletions

View File

@@ -0,0 +1,35 @@
import Foundation
import PromiseKit
final class FocusSensor: SensorProvider {
public enum FocusError: Error, Equatable {
case unauthorized
}
let request: SensorProviderRequest
init(request: SensorProviderRequest) {
self.request = request
}
func sensors() -> Promise<[WebhookSensor]> {
guard Current.focusStatus.authorizationStatus() == .authorized else {
return .init(error: FocusError.unauthorized)
}
let focusState = Current.focusStatus.status()
var sensors = [WebhookSensor]()
if let isFocused = focusState.isFocused {
sensors.append(with(WebhookSensor(
name: "Focus",
uniqueID: "focus",
icon: "mdi:moon-waning-crescent",
state: isFocused
)) {
$0.Type = "binary_sensor"
})
}
return .value(sensors)
}
}