12
12
13
13
"""
14
14
15
+ from __future__ import annotations
16
+
15
17
import struct
16
18
17
19
from .packet import Packet
18
20
21
+ try :
22
+ from typing import Optional # adjust these as needed
23
+ except ImportError :
24
+ pass
19
25
20
26
class ColorPacket (Packet ):
21
27
"""A packet containing an RGB color value."""
22
28
23
- _FMT_PARSE = "<xx3Bx"
24
- PACKET_LENGTH = struct .calcsize (_FMT_PARSE )
29
+ _FMT_PARSE : str = "<xx3Bx"
30
+ PACKET_LENGTH : int = struct .calcsize (_FMT_PARSE )
25
31
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
26
- _FMT_CONSTRUCT = "<2s3B"
27
- _TYPE_HEADER = b"!C"
32
+ _FMT_CONSTRUCT : str = "<2s3B"
33
+ _TYPE_HEADER : bytes = b"!C"
28
34
29
- def __init__ (self , color ) :
35
+ def __init__ (self , color : Optional [ Packet ]) -> None :
30
36
"""Construct a ColorPacket from a 3-element :class:`tuple` of RGB
31
37
values, or from an int color value 0xRRGGBB.
32
38
@@ -41,22 +47,22 @@ def __init__(self, color):
41
47
raise ValueError ("Color must be an integer 0xRRGGBB or a tuple(r,g,b)" )
42
48
43
49
@classmethod
44
- def parse_private (cls , packet ) :
50
+ def parse_private (cls , packet : Optional [ Packet ]) -> Optional [ Packet ] :
45
51
"""Construct a ColorPacket from an incoming packet.
46
52
Do not call this directly; call Packet.from_bytes() instead.
47
53
pylint makes it difficult to call this method _parse(), hence the name.
48
54
"""
49
55
return cls (struct .unpack (cls ._FMT_PARSE , packet ))
50
56
51
- def to_bytes (self ):
57
+ def to_bytes (self ) -> bytes :
52
58
"""Return the bytes needed to send this packet."""
53
59
partial_packet = struct .pack (
54
60
self ._FMT_CONSTRUCT , self ._TYPE_HEADER , * self ._color
55
61
)
56
62
return self .add_checksum (partial_packet )
57
63
58
64
@property
59
- def color (self ):
65
+ def color (self ) -> tuple :
60
66
"""A :class:`tuple` ``(red, green blue)`` representing the color the
61
67
user chose in the BlueFruit Connect app."""
62
68
return self ._color
0 commit comments