pydiagral 1.5.0b2__py3-none-any.whl → 1.5.0b5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pydiagral/api.py CHANGED
@@ -16,6 +16,8 @@ import aiohttp
16
16
 
17
17
  from .constants import API_VERSION, BASE_URL
18
18
  from .exceptions import (
19
+ APIKeyCreationError,
20
+ APIValidationError,
19
21
  AuthenticationError,
20
22
  ClientError,
21
23
  ConfigurationError,
@@ -266,7 +268,8 @@ class DiagralAPI:
266
268
  ApiKeyWithSecret: An instance of ApiKeyWithSecret containing the created API key and secret key.
267
269
 
268
270
  Raises:
269
- AuthenticationError: If the API key is not found in the response or if the created API key fails validation.
271
+ APIKeyCreationError: If the API key creation fails.
272
+ APIValidationError: If the API key validation fails.
270
273
 
271
274
  """
272
275
 
@@ -285,16 +288,16 @@ class DiagralAPI:
285
288
  set_apikey_response: ApiKeyWithSecret = ApiKeyWithSecret.from_dict(
286
289
  response_data
287
290
  )
288
- self.__apikey = set_apikey_response.api_key
291
+ self.__apikey: str = set_apikey_response.api_key
289
292
  if not self.__apikey:
290
293
  error_msg = "API key not found in response"
291
294
  _LOGGER.error(error_msg)
292
- raise AuthenticationError(error_msg)
293
- self.__secret_key = set_apikey_response.secret_key
295
+ raise APIKeyCreationError(error_msg)
296
+ self.__secret_key: str = set_apikey_response.secret_key
294
297
  if not self.__secret_key:
295
298
  error_msg = "Secret key not found in response"
296
299
  _LOGGER.error(error_msg)
297
- raise AuthenticationError(error_msg)
300
+ raise APIKeyCreationError(error_msg)
298
301
 
299
302
  _LOGGER.info("Successfully created new API key: ...%s", self.__apikey[-4:])
300
303
  # Verify if the API key is valid
@@ -303,14 +306,14 @@ class DiagralAPI:
303
306
  _LOGGER.info(
304
307
  "Successfully verified new API key: ...%s", self.__apikey[-4:]
305
308
  )
306
- except AuthenticationError as e:
309
+ except APIValidationError as e:
307
310
  _LOGGER.error("Created API key failed validation: %s", e)
308
311
  self.__apikey = None
309
312
  raise
310
313
  except DiagralAPIError as e:
311
314
  error_msg: str = f"Failed to create API key: {e!s}"
312
315
  _LOGGER.error(error_msg)
313
- raise AuthenticationError(error_msg) from e
316
+ raise APIKeyCreationError(error_msg) from e
314
317
 
315
318
  return ApiKeyWithSecret(api_key=self.__apikey, secret_key=self.__secret_key)
316
319
 
@@ -329,6 +332,7 @@ class DiagralAPI:
329
332
 
330
333
  Raises:
331
334
  ConfigurationError: If no API key is provided or if the API key is invalid.
335
+ APIValidationError: If the API key is invalid or not found in the list of valid keys.
332
336
 
333
337
  """
334
338
 
@@ -357,7 +361,7 @@ class DiagralAPI:
357
361
  if is_valid:
358
362
  _LOGGER.info("API key successfully validated")
359
363
  else:
360
- raise AuthenticationError(
364
+ raise APIValidationError(
361
365
  "API key is invalid or not found in the list of valid keys"
362
366
  )
363
367
 
@@ -400,7 +404,7 @@ class DiagralAPI:
400
404
  self.__apikey = None
401
405
  self.__secret_key = None
402
406
 
403
- async def try_connection(self, ephemeral: bool = True) -> bool:
407
+ async def try_connection(self, ephemeral: bool = True) -> TryConnectResult:
404
408
  """Test connection with the Diagral system.
405
409
 
406
410
  This method tests the connection by either using provided API credentials or generating
@@ -415,7 +419,14 @@ class DiagralAPI:
415
419
  if non-ephemeral temporary keys were generated.
416
420
 
417
421
  Raises:
418
- DiagralAPIError: If connection attempt fails or system status check fails.
422
+ APIKeyCreationError: If creation of temporary API keys fails
423
+ APIValidationError: If API key validation fails
424
+ SessionError: If the session is not initialized.
425
+ DiagralAPIError: If the request results in a 400 status code or other API errors.
426
+ AuthenticationError: If authentication fails or request results in a 401 or 403 status code.
427
+ ValidationError: If the request results in a 422 status code.
428
+ ServerError: If the request results in a 500 or 503 status code.
429
+ ClientError: If there is a network error.
419
430
 
420
431
  Note:
421
432
  If API credentials are not provided during client initialization, temporary
@@ -427,20 +438,25 @@ class DiagralAPI:
427
438
 
428
439
  result: TryConnectResult = TryConnectResult()
429
440
  api_keys_provided = bool(self.__apikey and self.__secret_key)
430
- try:
431
- # If API keys are not provided, generate temporary keys
432
- if not api_keys_provided:
433
- api_key_response: ApiKeyWithSecret = await self.set_apikey()
434
441
 
435
- # Retrieve system status to validate connection
442
+ # If API keys are not provided, generate temporary keys
443
+ if not api_keys_provided:
444
+ api_key_response: ApiKeyWithSecret = await self.set_apikey()
445
+
446
+ # Retrieve system status to validate connection
447
+ try:
436
448
  await self.get_system_status()
437
- # If connection is successful, clean up temporary keys if requested (ephemeral)
449
+ except DiagralAPIError:
438
450
  if ephemeral and not api_keys_provided:
439
451
  await self.delete_apikey(apikey=self.__apikey)
440
- elif not ephemeral and not api_keys_provided:
441
- result.keys = api_key_response
442
- except DiagralAPIError as e:
443
- raise DiagralAPIError(f"Failed to connect to the system: {e}") from e
452
+ raise
453
+
454
+ # If connection is successful, clean up temporary keys if requested (ephemeral)
455
+ if ephemeral and not api_keys_provided:
456
+ await self.delete_apikey(apikey=self.__apikey)
457
+ elif not ephemeral and not api_keys_provided:
458
+ result.keys = api_key_response
459
+
444
460
  result.result = True
445
461
  return result
446
462
 
@@ -489,6 +505,28 @@ class DiagralAPI:
489
505
  )
490
506
  return self.alarm_configuration
491
507
 
508
+ async def get_alarm_name(self) -> str:
509
+ """Get the name of the alarm from the configuration.
510
+
511
+ Returns:
512
+ str: The name of the alarm from the configuration.
513
+
514
+ Raises:
515
+ ConfigurationError: If unable to retrieve the alarm configuration.
516
+
517
+ Note:
518
+ This method will attempt to fetch the configuration if it hasn't been loaded yet.
519
+
520
+ """
521
+
522
+ if not self.alarm_configuration:
523
+ await self.get_configuration()
524
+
525
+ if not self.alarm_configuration:
526
+ raise ConfigurationError("Failed to retrieve alarm configuration")
527
+
528
+ return self.alarm_configuration.alarm.name
529
+
492
530
  async def get_devices_info(self) -> DeviceList:
493
531
  """Asynchronously retrieves information about various device types from the alarm configuration.
494
532
 
@@ -580,17 +618,17 @@ class DiagralAPI:
580
618
  SystemStatus: An instance of SystemStatus containing the retrieved system status.
581
619
 
582
620
  Raises:
583
- AuthenticationError: If the API key, secret key, or PIN code is not provided.
621
+ ConfigurationError: If the API key, secret key, or PIN code is not provided.
584
622
 
585
623
  """
586
624
 
587
625
  if not self.__apikey or not self.__secret_key:
588
- raise AuthenticationError(
626
+ raise ConfigurationError(
589
627
  "API key and secret key required to get system details"
590
628
  )
591
629
 
592
630
  if not self.__pincode:
593
- raise AuthenticationError("PIN code required to get system details")
631
+ raise ConfigurationError("PIN code required to get system details")
594
632
 
595
633
  _TIMESTAMP = str(int(time.time()))
596
634
  _HMAC: str = generate_hmac_signature(
pydiagral/exceptions.py CHANGED
@@ -37,3 +37,11 @@ class ServerError(DiagralAPIError):
37
37
 
38
38
  class ClientError(DiagralAPIError):
39
39
  """Raised when client returns error."""
40
+
41
+
42
+ class APIKeyCreationError(DiagralAPIError):
43
+ """Raised when API key creation fails."""
44
+
45
+
46
+ class APIValidationError(DiagralAPIError):
47
+ """Raised when API validation fails."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydiagral
3
- Version: 1.5.0b2
3
+ Version: 1.5.0b5
4
4
  Summary: A Python library for interacting with Diagral systems
5
5
  Project-URL: Homepage, https://github.com/mguyard/pydiagral
6
6
  Project-URL: Documentation, https://github.com/mguyard/pydiagral
@@ -0,0 +1,10 @@
1
+ pydiagral/__init__.py,sha256=4uM-RD2GQ6JYJkxu-D6wj3XpqfY5gN2hP8NF6WvRI9k,576
2
+ pydiagral/api.py,sha256=hF-Ol5KHqRXfpKcnxV8SLf-vnYwCTYugQzuQ5_ZG_bs,50094
3
+ pydiagral/constants.py,sha256=2B0TdKxQHA3cpIBxojo43bMW44wN9xKYsHbBRHWsaBk,119
4
+ pydiagral/exceptions.py,sha256=vMhGDQW-AhYIH9gcKHK1-4SHV3eZUXeeqXPyznUAKnU,1211
5
+ pydiagral/models.py,sha256=vUjxuApjVaMtd7H6Iw5LarwABi30O4FfdObhZRbUNuc,54931
6
+ pydiagral/utils.py,sha256=-VxI-lNaC4bU1K4DSmWDhvbsS2bXv5FAGULGKBf1UMU,449
7
+ pydiagral-1.5.0b5.dist-info/METADATA,sha256=HhOvxestYlemcBg-V3rqY88zG0ltD5X7Az4-lUDWVw0,48531
8
+ pydiagral-1.5.0b5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ pydiagral-1.5.0b5.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
10
+ pydiagral-1.5.0b5.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- pydiagral/__init__.py,sha256=4uM-RD2GQ6JYJkxu-D6wj3XpqfY5gN2hP8NF6WvRI9k,576
2
- pydiagral/api.py,sha256=F00LgyVtayPtZOm9O5DNQ2gLCv36vpsK6gCJXxOZNRk,48736
3
- pydiagral/constants.py,sha256=2B0TdKxQHA3cpIBxojo43bMW44wN9xKYsHbBRHWsaBk,119
4
- pydiagral/exceptions.py,sha256=Q5wEpNtiykLs3Ck0W8r1IQAJek_omaQ3jpMOtiiwBUg,1030
5
- pydiagral/models.py,sha256=vUjxuApjVaMtd7H6Iw5LarwABi30O4FfdObhZRbUNuc,54931
6
- pydiagral/utils.py,sha256=-VxI-lNaC4bU1K4DSmWDhvbsS2bXv5FAGULGKBf1UMU,449
7
- pydiagral-1.5.0b2.dist-info/METADATA,sha256=ngDa7KSkaHBpcc5EEqf0CS5IlpmQwNF_dvmPDQClfxs,48531
8
- pydiagral-1.5.0b2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- pydiagral-1.5.0b2.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
10
- pydiagral-1.5.0b2.dist-info/RECORD,,