4
0

camera.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Platform for Tuya Cameras
  3. """
  4. import logging
  5. from homeassistant.components.camera import Camera as CameraEntity
  6. from homeassistant.components.camera import CameraEntityFeature
  7. from .device import TuyaLocalDevice
  8. from .entity import TuyaLocalEntity
  9. from .helpers.config import async_tuya_setup_platform
  10. from .helpers.device_config import TuyaEntityConfig
  11. _LOGGER = logging.getLogger(__name__)
  12. async def async_setup_entry(hass, config_entry, async_add_entities):
  13. config = {**config_entry.data, **config_entry.options}
  14. await async_tuya_setup_platform(
  15. hass,
  16. async_add_entities,
  17. config,
  18. "camera",
  19. TuyaLocalCamera,
  20. )
  21. class TuyaLocalCamera(TuyaLocalEntity, CameraEntity):
  22. """Representation of a Tuya Camera"""
  23. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  24. """
  25. Initialise the camera.
  26. Args:
  27. device (TuyaLocalDevice): the device API instance
  28. config (TuyaEntityConfig): the configuration for this entity
  29. """
  30. super().__init__()
  31. dps_map = self._init_begin(device, config)
  32. self._switch_dp = dps_map.pop("switch", None)
  33. self._snapshot_dp = dps_map.pop("snapshot", None)
  34. self._record_dp = dps_map.pop("record", None)
  35. self._motion_enable_dp = dps_map.pop("motion_enable", None)
  36. self._init_end(dps_map)
  37. if self._switch_dp:
  38. self._attr_supported_features |= CameraEntityFeature.ON_OFF
  39. @property
  40. def is_recording(self):
  41. """Return whether the camera is recording, if we know that."""
  42. if self._record_dp:
  43. return self._record_dp.get_value(self._device)
  44. @property
  45. def motion_detection_enabled(self):
  46. """Return whether motion detection is enabled if supported."""
  47. if self._motion_enable_dp:
  48. return self._motion_enable_dp.get_value(self._device)
  49. async def async_camera_image(self, width=None, height=None):
  50. if self._snapshot_dp:
  51. return self._snapshot_dp.decoded_value(self._device)
  52. @property
  53. def is_on(self):
  54. """Return the power state of the camera"""
  55. if self._switch_dp:
  56. return self._switch_dp.get_value(self._device)
  57. async def async_turn_off(self):
  58. """Turn off the camera"""
  59. if not self._switch_dp:
  60. raise NotImplementedError()
  61. await self._switch_dp.async_set_value(self._device, False)
  62. async def async_turn_on(self):
  63. """Turn on the camera"""
  64. if not self._switch_dp:
  65. raise NotImplementedError()
  66. await self._switch_dp.async_set_value(self._device, True)
  67. async def async_enable_motion_detection(self):
  68. """Enable motion detection on the camera"""
  69. if not self._motion_enable_dp:
  70. raise NotImplementedError()
  71. await self._motion_enable_dp.async_set_value(self._device, True)
  72. async def async_disable_motion_detection(self):
  73. """Disable motion detection on the camera"""
  74. if not self._motion_enable_dp:
  75. raise NotImplementedError()
  76. await self._motion_enable_dp.async_set_value(self._device, False)