1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
# -*- coding: utf-8 -*-
"""
This is module specifies the Handler class.
A number of HANDLER constants have been defined here, do not USE these,
copy them instead.
"""
import re
from datetime import timedelta
from aioharmony.const import CallbackType
DEFAULT_TIMEOUT = 60
class Handler:
"""
This class is to be used to create Handler objects for registering new
callbacks through method
:meth:`aioharmony.client.HarmonyClient.register_handler`
**handler_obj:** This parameter can accept one of the following types of
objects.
* Future : result of the future will be set to the json that was received.
* Event : event will be triggered (set). Event will not be cleared, this
is the responsibility of the object listening for the event.
* Coroutine : Coroutine will be put on the task loop with one (1)
argument containing the json response.
* Callback : Callback will be called with one (1) argument containing
the json response.
:param handler_obj: Object to be called or set.
:type handler_obj: CallbackType
:param handler_name: Descriptive name for the handler. \
DEFAULT = None
:type handler_name: Optional[str]
:param resp_json: A JSON dict used to match the response upon.
DEFAULT = None
:type resp_json: dict
:param once: Only execute callback once (True) or keep callback until
removed explicitly (False)
DEFAULT = True
:type once: Optional[bool]
:param expiration: How long handler should be active once registered
before being removed.
DEFAULT = None
:type expiration: Optional[timedelta]
"""
# pylint: disable=too-many-arguments
def __init__(self,
handler_obj: CallbackType,
handler_name: str = None,
resp_json: dict = None,
once: bool = True,
expiration: timedelta = None) -> None:
self._handler_obj = handler_obj
self._handler_name = handler_name
self._resp_json = resp_json
self._once = once
self._expiration = expiration
def __copy__(self):
json_resp = dict()
json_resp.update(self._resp_json)
new_handler = Handler(
handler_obj=self._handler_obj,
handler_name=self._handler_name,
resp_json=json_resp,
once=self._once,
expiration=self._expiration
)
return new_handler
@property
def handler_obj(self) -> CallbackType:
"""
:param value: New handler_obj
:type value: CallbackType
:return: Returns handler_obj
:rtype: CallbackType
"""
return self._handler_obj
@handler_obj.setter
def handler_obj(self, value: CallbackType) -> None:
""""""
self._handler_obj = value
@property
def handler_name(self) -> str:
"""
:param value: New handler_name
:type value: str
:return: Returns handler_name
:rtype: str
"""
return self._handler_name
@handler_name.setter
def handler_name(self, value: str) -> None:
""""""
self._handler_name = value
@property
def resp_json(self) -> dict:
"""
:param value: New resp_type
:type value: dict
:return: Returns resp_type
:rtype: dict
"""
return self._resp_json
@resp_json.setter
def resp_json(self, value: dict) -> None:
""""""
self._resp_json = value
@property
def once(self) -> bool:
"""
:param value: New once
:type value: bool
:return: Returns once
:rtype: bool
"""
return self._once
@once.setter
def once(self, value: bool) -> None:
""""""
self._once = value
@property
def expiration(self) -> timedelta:
"""
:param value: New expiration
:type value: timedelta
:return: Returns expiration
:rtype: timedelta
"""
return self._expiration
@expiration.setter
def expiration(self, value: timedelta) -> None:
""""""
self._expiration = value
#
#
# Define a number of common handlers.
#
# These are NOT to be used as such, instead they HAVE TO BE copied.
#
#
def dummy_callback(message):
return message
HANDLER_NOTIFY = Handler(
handler_obj=dummy_callback,
handler_name='Notification_Received',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$')
},
once=False
)
HANDLER_START_ACTIVITY_NOTIFY_STARTED = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Starting',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 1,
},
},
once=False
)
HANDLER_STOP_ACTIVITY_NOTIFY_STARTED = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Stopping',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 0,
},
},
once=False
)
HANDLER_START_ACTIVITY_NOTIFY_INPROGRESS = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Starting_Inprogress',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 2,
},
},
once=False
)
HANDLER_START_ACTIVITY_NOTIFY_STARTED = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Starting',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 1,
},
},
once=False
)
HANDLER_STOP_ACTIVITY_NOTIFY_STARTED = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Stopping',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 0,
},
},
once=False
)
HANDLER_START_ACTIVITY_NOTIFY_INPROGRESS = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Starting_Inprogress',
resp_json={
'type': re.compile(r'^connect\.stateDigest\?notify$'),
'data': {
'activityStatus': 2,
},
},
once=False
)
HANDLER_START_ACTIVITY_FINISHED = Handler(
handler_obj=dummy_callback,
handler_name='Activity_Changed',
resp_json={
'type': re.compile(r'^harmony\.engine\?startActivityFinished$')
},
once=False
)
HANDLER_RUN_ACTIVITY = Handler(
handler_obj=dummy_callback,
handler_name='runactivity',
resp_json={
'cmd': re.compile(r'^harmony\.activityengine\?runactivity$')
},
once=False,
expiration=timedelta(seconds=DEFAULT_TIMEOUT * 5)
)
HANDLER_START_ACTIVITY_IN_PROGRESS = Handler(
handler_obj=dummy_callback,
handler_name='progress_startactivity',
resp_json={
'cmd': re.compile(r'^harmony\.engine\?startActivity$'),
'code': 100
},
once=False,
expiration=timedelta(seconds=DEFAULT_TIMEOUT * 5)
)
HANDLER_HELPDISCRETES = Handler(
handler_obj=dummy_callback,
handler_name='progress_discrete',
resp_json={
'cmd': re.compile(r'^harmony\.engine\?helpdiscretes$'),
'data': {
'isHelpDiscretes': 'true'
}
},
once=False,
expiration=timedelta(seconds=DEFAULT_TIMEOUT * 5)
)
HANDLER_START_ACTIVITY_COMPLETE = Handler(
handler_obj=dummy_callback,
handler_name='startactivity_or_discrete',
resp_json={
'cmd': re.compile(
r'(^harmony\.engine\?helpdiscretes$)|'
r'(^harmony\.engine\?startActivity$)'
)
},
once=False,
expiration=timedelta(seconds=DEFAULT_TIMEOUT * 5)
)
|