Debian Codegraph @

asusrouter/modules/led.py

raw
1 """LED module."""
2
3 from __future__ import annotations
4
5 from collections.abc import Awaitable, Callable
6 from enum import IntEnum
7 from typing import Any
8
9 from asusrouter.modules.endpoint import Endpoint
10
11
12 class AsusLED(IntEnum):
13 """Asus LED state."""
14
15 UNKNOWN = -999
16 OFF = 0
17 ON = 1
18
19
20 async def set_state(
21 callback: Callable[..., Awaitable[bool]],
22 state: AsusLED,
23 **kwargs: Any,
24 ) -> bool:
25 """Set the LED state."""
26
27 # Prepare the arguments
28 arguments = {"led_val": state.value}
29
30 # Run the service
31 return await callback(
32 service="start_ctrl_led",
33 arguments=arguments,
34 apply=True,
35 expect_modify=kwargs.get("expect_modify", False),
36 )
37
38
39 async def keep_state(
40 callback: Callable[..., Awaitable[bool]],
41 state: AsusLED = AsusLED.ON,
42 **kwargs: Any,
43 ) -> bool:
44 """Keep the LED state."""
45
46 identity = kwargs.get("identity")
47
48 # Check if identity is available and if endpoints are defined
49 if identity is None or not identity.endpoints:
50 return False
51
52 # Get the sysinfo
53 sysinfo = identity.endpoints.get(Endpoint.SYSINFO)
54
55 # Only when LEDs are off and if sysinfo is available
56 if not sysinfo or state == AsusLED.ON:
57 return False
58
59 # Toggle the LED
60 await set_state(callback, AsusLED.ON, **kwargs)
61 await set_state(callback, AsusLED.OFF, **kwargs)
62
63 return True