-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (90 loc) · 3.93 KB
/
Copy pathmain.py
File metadata and controls
111 lines (90 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""
Screen Light Sync - Main Entry Point
Python application for real-time screen color synchronization with Home Assistant smart lights
"""
import time
import argparse
import sys
from typing import Optional
from src.screen_capture import ScreenCapture, ScreenLightSync, ColorMode
from src.ha_controller import HomeAssistantController
from src.config import UPDATE_INTERVAL, BRIGHTNESS, COLOR_CHANGE_THRESHOLD, RATE_LIMIT_INTERVAL
def main():
"""Main function"""
parser = argparse.ArgumentParser(description='Screen Light Sync - Real-time screen color synchronization with smart lights')
parser.add_argument('--mode',
choices=['average', 'dominant', 'center'],
default='average',
help='Color extraction mode (default: average)')
parser.add_argument('--interval',
type=float,
default=UPDATE_INTERVAL,
help=f'Update interval in seconds (default: {UPDATE_INTERVAL})')
parser.add_argument('--fast',
action='store_true',
help='Fast mode - use center capture area')
parser.add_argument('--test',
action='store_true',
help='Test Home Assistant connection')
parser.add_argument('--monitor',
type=int,
default=1,
help='Monitor index to capture (default: 1)')
args = parser.parse_args()
# Initialize Home Assistant controller
ha_controller = HomeAssistantController()
# Test connection if requested
if args.test:
print("Testing connection to Home Assistant...")
if ha_controller.test_connection():
print("✓ Connection successful!")
else:
print("✗ Connection failed!")
sys.exit(1)
return
# Set color mode
color_mode = args.mode
if args.fast:
color_mode = ColorMode.CENTER
print("Fast mode enabled - using center color extraction")
# Initialize screen light sync
sync = ScreenLightSync(ha_controller, color_mode)
print(f"Starting Screen Light Sync...")
print(f"Mode: {color_mode}")
print(f"Monitor: {args.monitor}")
print(f"Update interval: {args.interval}s")
print("Press Ctrl+C to stop")
try:
with ScreenCapture(args.monitor) as screen:
while True:
start_time = time.time()
# Capture screen
if args.fast:
# Fast mode - capture center area only
capture_area = screen.get_center_area()
image_array = screen.capture_screen(capture_area)
else:
# Full screen capture
image_array = screen.capture_screen()
# Extract color
color = sync.extract_color(image_array)
# Check if we should update
current_time = time.time()
should_update = (sync.should_update_color(color, COLOR_CHANGE_THRESHOLD) and
current_time - sync.last_update_time > RATE_LIMIT_INTERVAL)
if should_update:
sync.update_light(color, BRIGHTNESS)
# Calculate processing time
processing_time = time.time() - start_time
if processing_time > 0.1: # Only show if > 100ms
print(f"Processing time: {processing_time:.3f}s")
# Sleep for specified interval
time.sleep(args.interval)
except KeyboardInterrupt:
print("\n✓ Screen Light Sync stopped")
except Exception as e:
print(f"✗ Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()