mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-10 04:32:50 -06:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. --> https://github.com/user-attachments/assets/e79c6e4d-13ff-405a-9463-02e597ce4996
60 lines
1.9 KiB
Swift
60 lines
1.9 KiB
Swift
import CoreMotion
|
|
import Foundation
|
|
import PromiseKit
|
|
|
|
public class ActivitySensor: SensorProvider {
|
|
public enum ActivityError: Error {
|
|
case unauthorized
|
|
case unavailable
|
|
case noData
|
|
}
|
|
|
|
public let request: SensorProviderRequest
|
|
public required init(request: SensorProviderRequest) {
|
|
self.request = request
|
|
}
|
|
|
|
public func sensors() -> Promise<[WebhookSensor]> {
|
|
firstly {
|
|
Self.latestMotionActivity()
|
|
}.map { activity in
|
|
with(WebhookSensor(name: "Activity", uniqueID: WebhookSensorId.activity.rawValue)) {
|
|
$0.State = activity.activityTypes.first
|
|
$0.Attributes = [
|
|
"Confidence": activity.confidence.description,
|
|
"Types": activity.activityTypes,
|
|
]
|
|
$0.Icon = activity.icons.first
|
|
}
|
|
}.map {
|
|
[$0]
|
|
}
|
|
}
|
|
|
|
private static func latestMotionActivity() -> Promise<CMMotionActivity> {
|
|
guard Current.motion.isAuthorized() else {
|
|
return .init(error: ActivityError.unauthorized)
|
|
}
|
|
|
|
guard Current.motion.isActivityAvailable() else {
|
|
Current.Log.warning("Activity is not available")
|
|
return .init(error: ActivityError.unavailable)
|
|
}
|
|
|
|
let (promise, seal) = Promise<CMMotionActivity>.pending()
|
|
let end = Current.date()
|
|
let start = Current.calendar().date(byAdding: .minute, value: -10, to: end)!
|
|
let queue = OperationQueue.main
|
|
Current.motion.queryStartEndOnQueueHandler(start, end, queue) { activities, error in
|
|
if let latestActivity = activities?.last {
|
|
seal.fulfill(latestActivity)
|
|
} else if let error {
|
|
seal.reject(error)
|
|
} else {
|
|
seal.reject(ActivityError.noData)
|
|
}
|
|
}
|
|
return promise
|
|
}
|
|
}
|