File: led.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 1,396 bytes parent folder | download
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
"""LED module."""

from __future__ import annotations

from collections.abc import Awaitable, Callable
from enum import IntEnum
from typing import Any

from asusrouter.modules.endpoint import Endpoint


class AsusLED(IntEnum):
    """Asus LED state."""

    UNKNOWN = -999
    OFF = 0
    ON = 1


async def set_state(
    callback: Callable[..., Awaitable[bool]],
    state: AsusLED,
    **kwargs: Any,
) -> bool:
    """Set the LED state."""

    # Prepare the arguments
    arguments = {"led_val": state.value}

    # Run the service
    return await callback(
        service="start_ctrl_led",
        arguments=arguments,
        apply=True,
        expect_modify=kwargs.get("expect_modify", False),
    )


async def keep_state(
    callback: Callable[..., Awaitable[bool]],
    state: AsusLED = AsusLED.ON,
    **kwargs: Any,
) -> bool:
    """Keep the LED state."""

    identity = kwargs.get("identity")

    # Check if identity is available and if endpoints are defined
    if identity is None or not identity.endpoints:
        return False

    # Get the sysinfo
    sysinfo = identity.endpoints.get(Endpoint.SYSINFO)

    # Only when LEDs are off and if sysinfo is available
    if not sysinfo or state == AsusLED.ON:
        return False

    # Toggle the LED
    await set_state(callback, AsusLED.ON, **kwargs)
    await set_state(callback, AsusLED.OFF, **kwargs)

    return True