Aller au contenu

Mode horloge

set_clock_mode

Set the clock mode of the device. Time is set automatically during connection via Bluetooth.

Parameters:

Name Type Description Default
style int

The clock style (0-8). Defaults to 1.

1
date str

The date to display (DD/MM/YYYY). Defaults to today.

''
show_date bool

Whether to show the date. Defaults to True.

True
format_24 bool

Whether to use 24-hour format. Defaults to True.

True

Raises:

Type Description
ValueError

If any parameter is out of range or invalid.

Source code in src/pypixelcolor/commands/set_clock_mode.py
 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
def set_clock_mode(style: int = 1, date="", show_date: bool = True, format_24: bool = True):
    """
    Set the clock mode of the device.
    Time is set automatically during connection via Bluetooth.

    Args:
        style (int, optional): The clock style (0-8). Defaults to 1.
        date (str, optional): The date to display (DD/MM/YYYY). Defaults to today.
        show_date (bool, optional): Whether to show the date. Defaults to True.
        format_24 (bool, optional): Whether to use 24-hour format. Defaults to True.

    Raises:
        ValueError: If any parameter is out of range or invalid.
    """
    if isinstance(show_date, str):
        show_date = show_date.lower() in ("true", "1", "yes", "on")
    if isinstance(format_24, str):
        format_24 = format_24.lower() in ("true", "1", "yes", "on")

    # Process date
    if not date:
        now = datetime.now()
        day, month, year = now.day, now.month, now.year % 100
        day_of_week = now.weekday() + 1
    else:
        try:
            day, month, year = map(int, date.split("/"))
            day_of_week = datetime(year, month, day).weekday() + 1
        except (ValueError, IndexError) as e:
            raise ValueError(f"Invalid date format: {e}")

    # Validate ranges
    if int(style) not in range(0, 9):
        raise ValueError("Clock style must be between 0 and 8")
    if int(day_of_week) not in range(1, 8):
        raise ValueError("Day of week must be between 1 and 7")
    if int(year) not in range(0, 100):
        raise ValueError("Year must be between 0 and 99")
    if int(month) not in range(1, 13):
        raise ValueError("Month must be between 1 and 12")
    if int(day) not in range(1, 32):
        raise ValueError("Day must be between 1 and 31")

    # Build byte sequence using direct bytes constructions
    header = bytes([
        11, # Command length 
        0,  # Reserved
        6,  # Command ID
        1   # Command type ID
    ])

    params = bytes([
        int(style) & 0xFF,                # Clock style
        1 if bool(format_24) else 0,      # 24-hour format
        1 if bool(show_date) else 0,      # Show date
    ])

    date_bytes = bytes([
        year & 0xFF,                   # Year
        month & 0xFF,                  # Month
        day & 0xFF,                    # Day
        day_of_week & 0xFF,            # Day of week
    ])

    payload = header + params + date_bytes
    return single_window_plan("set_clock_mode", payload)

set_time

Set the device time.

Parameters:

Name Type Description Default
hour Optional[int]

Hour to set (0-23). If None, uses current hour.

None
minute Optional[int]

Minute to set (0-59). If None, uses current minute.

None
second Optional[int]

Second to set (0-59). If None, uses current second.

None

Raises:

Type Description
ValueError

If any parameter is out of range.

Note

Command is the same as get_device_info.

Source code in src/pypixelcolor/commands/set_time.py
 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
def set_time(hour: Optional[int] = None, minute: Optional[int] = None, second: Optional[int] = None):
    """
    Set the device time.

    Args:
        hour: Hour to set (0-23). If None, uses current hour.
        minute: Minute to set (0-59). If None, uses current minute.
        second: Second to set (0-59). If None, uses current second.

    Raises:
        ValueError: If any parameter is out of range.

    Note:
        Command is the same as `get_device_info`.
    """
    # Get current time if any component is None
    if hour is None or minute is None or second is None:
        now = datetime.now()
        hour = now.hour if hour is None else hour
        minute = now.minute if minute is None else minute
        second = now.second if second is None else second

    # Validate
    if not (0 <= int(hour) <= 23):
        raise ValueError("Hour must be between 0 and 23")
    if not (0 <= int(minute) <= 59):
        raise ValueError("Minute must be between 0 and 59")
    if not (0 <= int(second) <= 59):
        raise ValueError("Second must be between 0 and 59")

    # Build command
    cmd = bytes([
        8,              # Command length
        0,              # Reserved
        1,              # Command ID
        0x80,           # Command type ID
        int(hour),      # Hour
        int(minute),    # Minute
        int(second),    # Second
        0               # Reserved
    ])
    return single_window_plan("set_time", cmd)