|
| 1 | +import contextlib |
| 2 | +import logging |
1 | 3 | import time
|
2 | 4 |
|
3 |
| -from signalrgb.client import SignalRGBClient |
| 5 | +from signalrgb.client import SignalRGBClient, SignalRGBException |
| 6 | +from signalrgb.model import Effect |
4 | 7 |
|
5 | 8 |
|
6 |
| -def cycle_effects(client, duration=10): |
| 9 | +def cycle_effects(client: SignalRGBClient, duration: int = 10) -> None: |
7 | 10 | """
|
8 | 11 | Cycle through all available effects, applying each for a specified duration.
|
9 | 12 |
|
10 | 13 | Args:
|
11 | 14 | client (SignalRGBClient): An initialized SignalRGBClient object.
|
12 | 15 | duration (int): The duration in seconds to apply each effect. Defaults to 10 seconds.
|
13 | 16 | """
|
14 |
| - print("Fetching available effects...") |
15 | 17 | effects = client.get_effects()
|
16 | 18 |
|
17 |
| - print(f"Found {len(effects)} effects. Starting cycle...") |
18 | 19 | for effect in effects:
|
19 | 20 | effect_name = effect.attributes.name
|
20 |
| - print(f"Applying effect: {effect_name}") |
21 | 21 |
|
22 | 22 | try:
|
23 | 23 | client.apply_effect_by_name(effect_name)
|
24 |
| - print( |
25 |
| - f"Effect '{effect_name}' applied successfully. Waiting for {duration} seconds..." |
26 |
| - ) |
27 | 24 | time.sleep(duration)
|
28 |
| - except Exception as e: |
29 |
| - print(f"Error applying effect '{effect_name}': {str(e)}") |
| 25 | + except SignalRGBException as e: |
| 26 | + logging.warning("Failed to apply effect %s: %s", effect_name, e) |
30 | 27 |
|
31 | 28 |
|
32 |
| -def main(): |
| 29 | +def main() -> None: |
33 | 30 | # Initialize the SignalRGB client
|
34 | 31 | client = SignalRGBClient(host="hyperia.home", port=16038)
|
| 32 | + logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") |
| 33 | + |
| 34 | + # Initialize initial_effect to None |
| 35 | + initial_effect: Effect | None = None |
35 | 36 |
|
36 | 37 | try:
|
37 | 38 | # Get the initial effect
|
38 | 39 | initial_effect = client.get_current_effect()
|
39 |
| - print(f"Initial effect: {initial_effect.attributes.name}") |
40 | 40 |
|
41 | 41 | # Cycle through effects
|
42 | 42 | cycle_effects(client, duration=5) # Change duration as needed
|
43 | 43 |
|
44 |
| - except Exception as e: |
45 |
| - print(f"An error occurred: {str(e)}") |
| 44 | + except SignalRGBException as e: |
| 45 | + logging.exception("Error during effect cycling: %s", e) |
46 | 46 |
|
47 | 47 | finally:
|
48 | 48 | # Restore the initial effect
|
49 |
| - try: |
50 |
| - client.apply_effect_by_name(initial_effect.attributes.name) |
51 |
| - print(f"Restored initial effect: {initial_effect.attributes.name}") |
52 |
| - except Exception as e: |
53 |
| - print(f"Error restoring initial effect: {str(e)}") |
| 49 | + with contextlib.suppress(SignalRGBException): |
| 50 | + if initial_effect: |
| 51 | + client.apply_effect_by_name(initial_effect.attributes.name) |
54 | 52 |
|
55 | 53 |
|
56 | 54 | if __name__ == "__main__":
|
|
0 commit comments