Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 25, 2023
1 parent 38fea39 commit 3648ff4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pyhap/iid_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
from .characteristic import Characteristic
from .service import Service

ServiceOrCharType = Union[Service, Characteristic]


class IIDManager:
"""Maintains a mapping between Service/Characteristic objects and IIDs."""

def __init__(self) -> None:
"""Initialize an empty instance."""
self.counter = 0
self.iids: Dict[Union[Service, Characteristic], int] = {}
self.objs: Dict[int, Union[Service, Characteristic]] = {}
self.iids: Dict["ServiceOrCharType", int] = {}
self.objs: Dict[int, "ServiceOrCharType"] = {}

def assign(self, obj: Union[Service, Characteristic]) -> None:
def assign(self, obj: "ServiceOrCharType") -> None:
"""Assign an IID to given object. Print warning if already assigned.
:param obj: The object that will be assigned an IID.
Expand All @@ -38,23 +40,23 @@ def assign(self, obj: Union[Service, Characteristic]) -> None:
self.iids[obj] = iid
self.objs[iid] = obj

def get_iid_for_obj(self, obj: Union[Service, Characteristic]) -> int:
def get_iid_for_obj(self, obj: "ServiceOrCharType") -> int:
"""Get the IID for the given object.
Override this method to provide custom IID assignment.
"""
self.counter += 1
return self.counter

def get_obj(self, iid: int) -> Union[Service, Characteristic]:
def get_obj(self, iid: int) -> "ServiceOrCharType":
"""Get the object that is assigned the given IID."""
return self.objs.get(iid)

def get_iid(self, obj: Union[Service, Characteristic]) -> int:
def get_iid(self, obj: "ServiceOrCharType") -> int:
"""Get the IID assigned to the given object."""
return self.iids.get(obj)

def remove_obj(self, obj: Union[Service, Characteristic]) -> Optional[int]:
def remove_obj(self, obj: "ServiceOrCharType") -> Optional[int]:
"""Remove an object from the IID list."""
iid = self.iids.pop(obj, None)
if iid is None:
Expand All @@ -63,7 +65,7 @@ def remove_obj(self, obj: Union[Service, Characteristic]) -> Optional[int]:
del self.objs[iid]
return iid

def remove_iid(self, iid: int) -> Optional[Union[Service, Characteristic]]:
def remove_iid(self, iid: int) -> Optional["ServiceOrCharType"]:
"""Remove an object with an IID from the IID list."""
obj = self.objs.pop(iid, None)
if obj is None:
Expand Down

0 comments on commit 3648ff4

Please sign in to comment.