api.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """API Placeholder.
  2. You should create your api seperately and have it hosted on PYPI. This is included here for the sole purpose
  3. of making this example code executable.
  4. """
  5. from dataclasses import dataclass
  6. from enum import StrEnum
  7. import logging
  8. from random import choice, randrange
  9. _LOGGER = logging.getLogger(__name__)
  10. class DeviceType(StrEnum):
  11. """Device types."""
  12. TEMP_SENSOR = "temp_sensor"
  13. DOOR_SENSOR = "door_sensor"
  14. OTHER = "other"
  15. DEVICES = [
  16. {"id": 1, "type": DeviceType.TEMP_SENSOR},
  17. {"id": 2, "type": DeviceType.TEMP_SENSOR},
  18. {"id": 3, "type": DeviceType.TEMP_SENSOR},
  19. {"id": 4, "type": DeviceType.TEMP_SENSOR},
  20. {"id": 1, "type": DeviceType.DOOR_SENSOR},
  21. {"id": 2, "type": DeviceType.DOOR_SENSOR},
  22. {"id": 3, "type": DeviceType.DOOR_SENSOR},
  23. {"id": 4, "type": DeviceType.DOOR_SENSOR},
  24. ]
  25. @dataclass
  26. class Device:
  27. """API device."""
  28. device_id: int
  29. device_unique_id: str
  30. device_type: DeviceType
  31. name: str
  32. state: int | bool
  33. class API:
  34. """Class for example API."""
  35. def __init__(self, host: str, user: str, pwd: str) -> None:
  36. """Initialise."""
  37. self.host = host
  38. self.user = user
  39. self.pwd = pwd
  40. self.connected: bool = False
  41. @property
  42. def controller_name(self) -> str:
  43. """Return the name of the controller."""
  44. return self.host.replace(".", "_")
  45. def connect(self) -> bool:
  46. """Connect to api."""
  47. if self.user == "test" and self.pwd == "1234":
  48. self.connected = True
  49. return True
  50. raise APIAuthError("Error connecting to api. Invalid username or password.")
  51. def disconnect(self) -> bool:
  52. """Disconnect from api."""
  53. self.connected = False
  54. return True
  55. def get_devices(self) -> list[Device]:
  56. """Get devices on api."""
  57. return [
  58. Device(
  59. device_id=device.get("id"),
  60. device_unique_id=self.get_device_unique_id(
  61. device.get("id"), device.get("type")
  62. ),
  63. device_type=device.get("type"),
  64. name=self.get_device_name(device.get("id"), device.get("type")),
  65. state=self.get_device_value(device.get("id"), device.get("type")),
  66. )
  67. for device in DEVICES
  68. ]
  69. def get_device_unique_id(self, device_id: str, device_type: DeviceType) -> str:
  70. """Return a unique device id."""
  71. if device_type == DeviceType.DOOR_SENSOR:
  72. return f"{self.controller_name}_D{device_id}"
  73. if device_type == DeviceType.TEMP_SENSOR:
  74. return f"{self.controller_name}_T{device_id}"
  75. return f"{self.controller_name}_Z{device_id}"
  76. def get_device_name(self, device_id: str, device_type: DeviceType) -> str:
  77. """Return the device name."""
  78. if device_type == DeviceType.DOOR_SENSOR:
  79. return f"DoorSensor{device_id}"
  80. if device_type == DeviceType.TEMP_SENSOR:
  81. return f"TempSensor{device_id}"
  82. return f"OtherSensor{device_id}"
  83. def get_device_value(self, device_id: str, device_type: DeviceType) -> int | bool:
  84. """Get device random value."""
  85. if device_type == DeviceType.DOOR_SENSOR:
  86. return choice([True, False])
  87. if device_type == DeviceType.TEMP_SENSOR:
  88. return randrange(15, 28)
  89. return randrange(1, 10)
  90. class APIAuthError(Exception):
  91. """Exception class for auth error."""
  92. class APIConnectionError(Exception):
  93. """Exception class for connection error."""