-
Notifications
You must be signed in to change notification settings - Fork 446
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
Draft
nstefanelli
wants to merge
17
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.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c6f99ff
Add L10n strings for camera detection settings (PR2)
nstefanelli a2779e1
Add MotionSensitivity enum and camera detection settings to KioskSett…
nstefanelli 7b193c9
test(kiosk): add unit tests for MotionSensitivity and camera settings
nstefanelli e2c3c3a
feat(kiosk): add camera motion detector
nstefanelli 003bf22
feat(kiosk): add presence detector with Vision framework
nstefanelli 1ea006d
feat(kiosk): add camera detection manager coordinator
nstefanelli a93d5b6
feat(kiosk): integrate camera detection into KioskModeManager
nstefanelli 7959b54
feat(kiosk): add camera detection settings section
nstefanelli cc655cb
chore(kiosk): add camera detection files to Xcode project
nstefanelli a63b9b1
fix(kiosk): address code review findings for camera detection
nstefanelli f262028
fix(kiosk): move camera frame processing off main thread
nstefanelli 8eba154
fix(kiosk): add explicit init() to KioskSettings
nstefanelli 9922505
Merge branch 'main' into kiosk-pr2-camera-detection
nstefanelli abc22b9
feat(kiosk): scope down to motion-only per maintainer request
nstefanelli f26c83e
chore(swiftgen): regenerate cleanly without unrelated changes
nstefanelli d4d019a
Merge remote-tracking branch 'upstream/main' into kiosk-pr2-camera-de…
nstefanelli 5af1753
fix(kiosk): address camera detection review feedback
nstefanelli 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.