camera.py 3.1 KB

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