Skip to content
Snippets Groups Projects
Commit 40cf3231 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

First try of type hints

Some issues arise .. will need another round soon
parent f06d49fd
No related branches found
No related tags found
1 merge request!1First try of type hints
...@@ -28,7 +28,7 @@ from . import config ...@@ -28,7 +28,7 @@ from . import config
from . import tools from . import tools
from . import bindings from . import bindings
from .exceptions import DeviceError from .exceptions import DeviceError
from .types import UUIDT, DeviceT, EngineT, AsyncEngineT from .types import DeviceT, EngineT, AsyncEngineT
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -39,7 +39,7 @@ class Attribute(object): ...@@ -39,7 +39,7 @@ class Attribute(object):
def __init__(self, name, dev: Optional[DeviceT] = None, default: Any = None): def __init__(self, name, dev: Optional[DeviceT] = None, default: Any = None):
self.name = name self.name = name
self.default = default self.default = default
self.device = dev self.device: Optional[Device] = dev
self.__value = default self.__value = default
@property @property
...@@ -104,7 +104,7 @@ class Device(object): ...@@ -104,7 +104,7 @@ class Device(object):
def __init__( def __init__(
self, self,
dev_type: str, dev_type: str,
addr: Optional[UUIDT] = None, addr: Optional[bindings.UUID] = None,
engine: Union[EngineT, AsyncEngineT, None] = None, engine: Union[EngineT, AsyncEngineT, None] = None,
): ):
...@@ -156,11 +156,11 @@ class Device(object): ...@@ -156,11 +156,11 @@ class Device(object):
self.__version = None self.__version = None
@property @property
def address(self): def address(self) -> Optional[bindings.UUID]:
return self.__address return self.__address
@address.setter @address.setter
def address(self, value: Optional[UUIDT]): def address(self, value: Optional[bindings.UUID]):
if value is None: if value is None:
self.__address = None self.__address = None
return return
...@@ -169,60 +169,60 @@ class Device(object): ...@@ -169,60 +169,60 @@ class Device(object):
self.__address = value self.__address = value
@property @property
def url(self): def url(self) -> Optional[bindings.URL]:
return self.__url return self.__url
@url.setter @url.setter
def url(self, value): def url(self, value: Optional[str]):
if value is None: if value is None:
self.__url = None self.__url = None
else: else:
self.__url = bindings.URL(value) self.__url = bindings.URL(value)
# attributes # attributes
def new_attribute(self, name, default=None): def new_attribute(self, name: str, default: Any = None):
attr = Attribute(name,self,default) attr = Attribute(name,self,default)
self.add_attribute(attr) self.add_attribute(attr)
return attr return attr
def add_attribute(self, attr): def add_attribute(self, attr: Attribute):
if attr: if attr:
self.__attributes.append(attr) self.__attributes.append(attr)
attr.device = self attr.device = self
def del_attribute(self, attr): def del_attribute(self, attr: Attribute):
if attr: if attr:
attr.device = None attr.device = None
self.__attributes.remove(attr) self.__attributes.remove(attr)
def get_attribute(self, name): def get_attribute(self, name: str) -> Optional[Attribute]:
for attr in self.__attributes: for attr in self.__attributes:
if attr.name == name: if attr.name == name:
return attr return attr
return None return None
@property @property
def attributes(self): def attributes(self) -> Attributes:
return self.__attributes return self.__attributes
@attributes.setter @attributes.setter
def attributes(self, values): def attributes(self, values: Attributes):
if isinstance(values,Attributes): if isinstance(values,Attributes):
self.__attributes = values self.__attributes = values
else: else:
raise DeviceError("Invalid attributes list, use class Attributes)") raise DeviceError("Invalid attributes list, use class Attributes)")
def add_method(self, name, func): def add_method(self, name: str, func: Any):
self.methods.update({name:func}) self.methods.update({name:func})
def get_methods(self): def get_methods(self) -> dict:
return self.methods return self.methods
def update_alive(self): def update_alive(self):
""" update the alive timimg""" """ update the alive timimg"""
self.next_alive = time.time() + self.alive_period self.next_alive = time.time() + self.alive_period
def get_timeout(self): def get_timeout(self) -> int:
""" return Alive timeout used for isAlive msg""" """ return Alive timeout used for isAlive msg"""
return 2 * self.alive_period return 2 * self.alive_period
...@@ -313,7 +313,7 @@ class Device(object): ...@@ -313,7 +313,7 @@ class Device(object):
result.update({attr.name: attr.value}) result.update({attr.name: attr.value})
return result return result
def send_notification(self, notification, body={}): def send_notification(self, notification:str, body:dict={}):
""" queue an notification, this is just a method helper """ """ queue an notification, this is just a method helper """
if self.engine: if self.engine:
self.engine.send_notification(self,notification,body) self.engine.send_notification(self,notification,body)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment