1
1
"""Module for the IIDManager class."""
2
2
import logging
3
+ from typing import TYPE_CHECKING , Dict , Optional , Union
4
+
5
+ if TYPE_CHECKING :
6
+ from .characteristic import Characteristic
7
+ from .service import Service
8
+
9
+ ServiceOrCharType = Union [Service , Characteristic ]
3
10
4
11
logger = logging .getLogger (__name__ )
5
12
6
13
7
14
class IIDManager :
8
15
"""Maintains a mapping between Service/Characteristic objects and IIDs."""
9
16
10
- def __init__ (self ):
17
+ def __init__ (self ) -> None :
11
18
"""Initialize an empty instance."""
12
19
self .counter = 0
13
- self .iids = {}
14
- self .objs = {}
20
+ self .iids : Dict [ "ServiceOrCharType" , int ] = {}
21
+ self .objs : Dict [ int , "ServiceOrCharType" ] = {}
15
22
16
- def assign (self , obj ) :
23
+ def assign (self , obj : "ServiceOrCharType" ) -> None :
17
24
"""Assign an IID to given object. Print warning if already assigned.
18
25
19
26
:param obj: The object that will be assigned an IID.
@@ -32,23 +39,23 @@ def assign(self, obj):
32
39
self .iids [obj ] = iid
33
40
self .objs [iid ] = obj
34
41
35
- def get_iid_for_obj (self , obj ) :
42
+ def get_iid_for_obj (self , obj : "ServiceOrCharType" ) -> int :
36
43
"""Get the IID for the given object.
37
44
38
45
Override this method to provide custom IID assignment.
39
46
"""
40
47
self .counter += 1
41
48
return self .counter
42
49
43
- def get_obj (self , iid ) :
50
+ def get_obj (self , iid : int ) -> "ServiceOrCharType" :
44
51
"""Get the object that is assigned the given IID."""
45
52
return self .objs .get (iid )
46
53
47
- def get_iid (self , obj ) :
54
+ def get_iid (self , obj : "ServiceOrCharType" ) -> int :
48
55
"""Get the IID assigned to the given object."""
49
56
return self .iids .get (obj )
50
57
51
- def remove_obj (self , obj ) :
58
+ def remove_obj (self , obj : "ServiceOrCharType" ) -> Optional [ int ] :
52
59
"""Remove an object from the IID list."""
53
60
iid = self .iids .pop (obj , None )
54
61
if iid is None :
@@ -57,7 +64,7 @@ def remove_obj(self, obj):
57
64
del self .objs [iid ]
58
65
return iid
59
66
60
- def remove_iid (self , iid ) :
67
+ def remove_iid (self , iid : int ) -> Optional [ "ServiceOrCharType" ] :
61
68
"""Remove an object with an IID from the IID list."""
62
69
obj = self .objs .pop (iid , None )
63
70
if obj is None :
0 commit comments