mirror of
https://github.com/home-assistant/iOS.git
synced 2026-05-02 05:08:25 -05:00
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:
35
Sources/Shared/API/Webhook/Sensors/FocusSensor.swift
Normal file
35
Sources/Shared/API/Webhook/Sensors/FocusSensor.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user