1818# USA.
1919
2020
21- import functools
2221import logging
2322from datetime import datetime
2423from enum import Enum
2524
25+ from .const import DEFAULT_USERNAME , DEFAULT_MODULE_ID , DEFAULT_PORT
2626from .soapclient import MethodCallError , SoapClient
27-
28- _LOGGER = logging .getLogger (__name__ )
27+ from .helpers import auth_required
2928
3029
31- def auth_required (fn ):
32- @functools .wraps (fn )
33- def _wrap (device , * args , ** kwargs ):
34- if not device .client .authenticated :
35- device .client .authenticate ()
36- _LOGGER .debug ("Device authenticated" )
37- return fn (device , * args , ** kwargs )
38-
39- return _wrap
30+ _LOGGER = logging .getLogger (__name__ )
4031
4132
4233def DeviceFactory (
43- * , client = None , hostname = None , password = None , username = "Admin" , port = 80
34+ * ,
35+ client = None ,
36+ hostname = None ,
37+ password = None ,
38+ username = DEFAULT_USERNAME ,
39+ port = DEFAULT_PORT ,
4440):
4541 client = client or SoapClient (
4642 hostname = hostname , password = password , username = username , port = port
@@ -53,11 +49,15 @@ def DeviceFactory(
5349
5450 if "Audio Renderer" in module_types :
5551 cls = Siren
56- # 'Optical Recognition', 'Environmental Sensor', 'Camera']
52+
5753 elif "Camera" in module_types :
54+ # Other posible values for camera (needs testing):
55+ # 'Optical Recognition', 'Environmental Sensor', 'Camera'
5856 cls = Camera
57+
5958 elif "Motion Sensor" in module_types :
6059 cls = Motion
60+
6161 else :
6262 raise TypeError (module_types )
6363
@@ -73,15 +73,16 @@ def __init__(
7373 client = None ,
7474 hostname = None ,
7575 password = None ,
76- username = "Admin" ,
77- port = 80 ,
76+ username = DEFAULT_USERNAME ,
77+ port = DEFAULT_PORT ,
78+ module_id = DEFAULT_MODULE_ID ,
7879 ):
7980 self .client = client or SoapClient (
8081 hostname = hostname , password = password , username = username , port = port
8182 )
83+ self .module_id = module_id
84+
8285 self ._info = None
83- self ._module_id = None
84- self ._controller = None
8586
8687 @property
8788 def info (self ):
@@ -90,59 +91,38 @@ def info(self):
9091
9192 return self ._info
9293
93- @property
94- def module_id (self ):
95- if not self ._module_id :
96- self ._module_id = self .info ["ModuleTypes" ].find (self .MODULE_TYPE ) + 1
97-
98- return self ._module_id
99-
100- @property
101- def controller (self ):
102- # NOTE: not sure about this
103- return self .module_id
104-
10594 def call (self , * args , ** kwargs ):
106- kwargs ["ModuleID" ] = kwargs .get ("ModuleID" ) or self .module_id
107- kwargs ["Controller" ] = kwargs .get ("Controller" ) or self .controller
108-
95+ kwargs ["ModuleID" ] = self .module_id
10996 return self .client .call (* args , ** kwargs )
11097
111- # def get_info(self):
112- # info = self.client.device_info()
113-
114- # if isinstance(info["ModuleTypes"], str):
115- # info["ModuleTypes"] = [info["ModuleTypes"]]
116-
117- # dev = set(info["ModuleTypes"])
118- # req = set(self.REQUIRED_MODULE_TYPES)
119- # if not req.issubset(dev):
120- # raise TypeError(
121- # f"device '{self.client.hostname}' is not a "
122- # f"{self.__class__.__name__}",
123- # )
98+ def is_authenticated (self ):
99+ return self .client .is_authenticated ()
124100
125- # return info
101+ def authenticate (self ):
102+ return self .client .authenticate ()
126103
127104
128105class Camera (Device ):
129106 MODULE_TYPE = "Camera"
107+ DEFAULT_SCHEMA = "http://"
108+ DEFAULT_STREAM_PATH = "/play1.sdp"
109+ DEFAULT_PICTURE_PATH = "/image/jpeg.cgi"
130110
131111 def __init__ (self , * args , ** kwargs ):
132112 super ().__init__ (* args , ** kwargs )
133113 self ._base_url = (
134- "http://"
114+ { self . DEFAULT_SCHEMA }
135115 + f"{ self .client .username .lower ()} :{ self .client .password } @"
136116 + f"{ self .client .hostname } :{ self .client .port } "
137117 )
138118
139119 @property
140120 def stream_url (self ):
141- return f"{ self ._base_url } /play1.sdp "
121+ return f"{ self ._base_url } { self . DEFAULT_STREAM_PATH } "
142122
143123 @property
144124 def picture_url (self ):
145- return f"{ self ._base_url } /image/jpeg.cgi "
125+ return f"{ self ._base_url } { self . DEFAULT_PICTURE_PATH } "
146126
147127
148128class Motion (Device ):
@@ -167,15 +147,15 @@ def backoff(self):
167147
168148 # @backoff.setter
169149 # def backoff(self, seconds):
170- # self.client. call(
171- # "SetMotionDetectorSettings", ModuleID=1, Backoff=self._backoff
150+ # self.call(
151+ # "SetMotionDetectorSettings", Backoff=self._backoff
172152 # )
173153 # _LOGGER.warning("set backoff property has no effect")
174154
175155 # def authenticate(self):
176156 # super().authenticate()
177157
178- # res = self.client. call("GetMotionDetectorSettings", ModuleID=1 )
158+ # res = self.call("GetMotionDetectorSettings")
179159 # try:
180160 # self._backoff = int(res["Backoff"])
181161 # except (ValueError, TypeError, KeyError):
0 commit comments