general.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # general.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Mon Jul 31 17:04:07 UTC 2023
  3. """Other models that don't deserve their own file"""
  4. from __future__ import annotations
  5. import datetime
  6. import json
  7. import tempfile
  8. from base64 import b64encode
  9. from dataclasses import asdict, dataclass
  10. from functools import cached_property
  11. from pathlib import Path
  12. from pydantic import BaseModel, ConfigDict, Field
  13. from open_gopro.models.bases import CustomBaseModel
  14. from open_gopro.models.constants import SettingId
  15. class CameraInfo(CustomBaseModel):
  16. """General camera info"""
  17. model_config = ConfigDict(protected_namespaces=())
  18. model_number: int #: Camera model number
  19. model_name: str #: Camera model name as string
  20. firmware_version: str #: Complete firmware version
  21. serial_number: str #: Camera serial number
  22. ap_mac_addr: str #: Camera access point MAC address
  23. ap_ssid: str #: Camera access point SSID name
  24. class TzDstDateTime(CustomBaseModel):
  25. """DST aware datetime"""
  26. datetime: datetime.datetime
  27. tzone: int
  28. dst: bool
  29. class SupportedOption(CustomBaseModel):
  30. """A supported option in an invalid setting response"""
  31. display_name: str
  32. id: int
  33. class HttpInvalidSettingResponse(CustomBaseModel):
  34. """Invalid settings response with optional supported options"""
  35. error: int
  36. setting_id: SettingId
  37. option_id: int | None = Field(default=None)
  38. supported_options: list[SupportedOption] | None = Field(default=None)
  39. class CohnInfo(BaseModel):
  40. """Data model to store Camera on the Home Network connection info"""
  41. ip_address: str
  42. username: str
  43. password: str
  44. certificate: str
  45. @cached_property
  46. def auth_token(self) -> str:
  47. """Get the auth token from the username and password
  48. Returns:
  49. str: _description_
  50. """
  51. token = b64encode(f"{self.username}:{self.password}".encode("utf-8")).decode("ascii")
  52. return f"Basic {token}"
  53. @cached_property
  54. def certificate_as_path(self) -> Path:
  55. """Write the certificate to a tempfile and return the tempfile
  56. This is needed because requests will not take an in-memory certificate string
  57. Returns:
  58. Path: Path of temp certificate file
  59. """
  60. with tempfile.NamedTemporaryFile(delete=False) as cert_file:
  61. cert_file.write(self.certificate.encode("utf-8"))
  62. return Path(cert_file.name)
  63. def __add__(self, other: CohnInfo) -> CohnInfo:
  64. return CohnInfo(
  65. ip_address=other.ip_address or self.ip_address,
  66. username=other.username or self.username,
  67. password=other.password or self.password,
  68. certificate=other.certificate or self.certificate,
  69. )
  70. @property
  71. def is_complete(self) -> bool:
  72. """Are all of the fields non-empty?
  73. Returns:
  74. bool: True if complete, False otherwise
  75. """
  76. return bool(self.ip_address) and bool(self.username) and bool(self.password) and bool(self.certificate)
  77. @dataclass(frozen=True)
  78. class ScheduledCapture:
  79. """Scheduled capture request / status"""
  80. hour: int
  81. minute: int
  82. is_enabled: bool
  83. is_24_hour: bool
  84. @classmethod
  85. def from_datetime(cls, dt: datetime.datetime, is_enabled: bool) -> ScheduledCapture:
  86. """Helper method to build a ScheduledCapture object from Python standard datetime.datetime
  87. Args:
  88. dt (datetime.datetime): datetime to build from
  89. is_enabled (bool): is / should scheduled capture be enabled on the camera?
  90. Returns:
  91. ScheduledCapture: _description_
  92. """
  93. return cls(
  94. hour=dt.hour,
  95. minute=dt.minute,
  96. is_enabled=is_enabled,
  97. is_24_hour=True,
  98. )
  99. def __str__(self) -> str:
  100. return json.dumps(asdict(self), indent=8)
  101. @classmethod
  102. def off(cls) -> ScheduledCapture:
  103. """Helper method to return a pre-filled ScheduledCapture object that can be used to turn scheduled capture off
  104. Returns:
  105. ScheduledCapture: object with "is_enabled" set to False
  106. """
  107. return cls(0, 0, False, False)