-
Notifications
You must be signed in to change notification settings - Fork 454
Kiosk mode: camera motion detection for screen wake #4497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nstefanelli
wants to merge
16
commits into
home-assistant:main
Choose a base branch
from
nstefanelli:kiosk-pr2-camera-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8036a8d
Add L10n strings for camera detection settings (PR2)
nstefanelli 200c390
Add MotionSensitivity enum and camera detection settings to KioskSett…
nstefanelli a74b5cb
test(kiosk): add unit tests for MotionSensitivity and camera settings
nstefanelli 74f8a88
feat(kiosk): add camera motion detector
nstefanelli 095fd0c
feat(kiosk): add presence detector with Vision framework
nstefanelli dd9b498
feat(kiosk): add camera detection manager coordinator
nstefanelli 0581846
feat(kiosk): integrate camera detection into KioskModeManager
nstefanelli 3806049
feat(kiosk): add camera detection settings section
nstefanelli c61eca0
chore(kiosk): add camera detection files to Xcode project
nstefanelli 42b3cda
fix(kiosk): address code review findings for camera detection
nstefanelli 3b45927
fix(kiosk): move camera frame processing off main thread
nstefanelli 267e0fa
fix(kiosk): add explicit init() to KioskSettings
nstefanelli 1a084ce
feat(kiosk): scope down to motion-only per maintainer request
nstefanelli cc28363
fix(kiosk): address camera detection review feedback
nstefanelli cf7af84
chore(swiftgen): regenerate after rebase onto upstream/main
nstefanelli 0da0b68
Merge branch 'main' into kiosk-pr2-camera-detection
bgoncal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
Sources/App/Kiosk/Camera/KioskCameraDetectionManager.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import AVFoundation | ||
| import Combine | ||
| import Foundation | ||
| import Shared | ||
| import UIKit | ||
|
|
||
| // MARK: - Kiosk Camera Detection Manager | ||
|
|
||
| /// Coordinates camera-based motion detection for kiosk mode | ||
| @MainActor | ||
| public final class KioskCameraDetectionManager: ObservableObject { | ||
| // MARK: - Singleton | ||
|
|
||
| public static let shared = KioskCameraDetectionManager() | ||
|
|
||
| // MARK: - Published State | ||
|
|
||
| /// Whether any camera detection is currently active | ||
| @Published public private(set) var isActive: Bool = false | ||
|
|
||
| /// Current motion detected state | ||
| @Published public private(set) var motionDetected: Bool = false | ||
|
|
||
| /// Camera authorization status | ||
| @Published public private(set) var authorizationStatus: AVAuthorizationStatus = .notDetermined | ||
|
|
||
| // MARK: - Callbacks | ||
|
|
||
| /// Called when motion is detected (for wake trigger) | ||
| public var onMotionDetected: (() -> Void)? | ||
|
|
||
| // MARK: - Private | ||
|
|
||
| private var settings: KioskSettings { KioskModeManager.shared.settings } | ||
| private let motionDetector = KioskCameraMotionDetector() | ||
| private var cancellables = Set<AnyCancellable>() | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| private init() { | ||
| setupBindings() | ||
| checkAuthorizationStatus() | ||
| } | ||
|
|
||
| deinit { | ||
| cancellables.forEach { $0.cancel() } | ||
| cancellables.removeAll() | ||
| } | ||
|
|
||
| // MARK: - Public Methods | ||
|
|
||
| /// Start camera detection based on current settings. | ||
| /// `isActive` reflects the underlying detector state (bound in setupBindings), | ||
| /// so it stays false if the detector bails out (e.g. camera permission denied). | ||
| public func start() { | ||
| Current.Log.info("Starting camera detection manager") | ||
|
|
||
| if settings.cameraMotionEnabled { | ||
| motionDetector.start() | ||
| } | ||
| } | ||
|
|
||
| /// Stop all camera detection | ||
| public func stop() { | ||
| Current.Log.info("Stopping camera detection manager") | ||
| motionDetector.stop() | ||
| } | ||
|
|
||
| /// Restart detection (e.g., after settings change) | ||
| public func restart() { | ||
| stop() | ||
| start() | ||
| } | ||
|
|
||
| /// Request camera authorization | ||
| public func requestAuthorization() async -> Bool { | ||
| let granted = await motionDetector.requestAuthorization() | ||
| checkAuthorizationStatus() | ||
| return granted | ||
| } | ||
|
|
||
| // MARK: - Private Methods | ||
|
|
||
| private func checkAuthorizationStatus() { | ||
| authorizationStatus = AVCaptureDevice.authorizationStatus(for: .video) | ||
| } | ||
|
|
||
| private func setupBindings() { | ||
| motionDetector.$isActive | ||
| .receive(on: DispatchQueue.main) | ||
| .sink { [weak self] active in | ||
| self?.isActive = active | ||
| } | ||
| .store(in: &cancellables) | ||
|
|
||
| motionDetector.$motionDetected | ||
| .receive(on: DispatchQueue.main) | ||
| .sink { [weak self] detected in | ||
| self?.motionDetected = detected | ||
| if detected { | ||
| self?.handleMotionDetected() | ||
| } | ||
| } | ||
| .store(in: &cancellables) | ||
|
|
||
| motionDetector.$authorizationStatus | ||
| .receive(on: DispatchQueue.main) | ||
| .sink { [weak self] status in | ||
| self?.authorizationStatus = status | ||
| } | ||
| .store(in: &cancellables) | ||
| } | ||
|
|
||
| private func handleMotionDetected() { | ||
| Current.Log.info("Camera motion detected") | ||
| onMotionDetected?() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.