camera.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. super().__init__()
  33. dps_map = self._init_begin(device, config)
  34. self._switch_dp = dps_map.pop("switch", None)
  35. self._snapshot_dp = dps_map.pop("snapshot", None)
  36. self._record_dp = dps_map.pop("record", None)
  37. self._motion_enable_dp = dps_map.pop("motion_enable", None)
  38. self._init_end(dps_map)
  39. if self._switch_dp:
  40. self._attr_supported_features |= CameraEntityFeature.ON_OFF
  41. @property
  42. def is_recording(self):
  43. """Return whether the camera is recording, if we know that."""
  44. if self._record_dp:
  45. return self._record_dp.get_value(self._device)
  46. @property
  47. def motion_detection_enabled(self):
  48. """Return whether motion detection is enabled if supported."""
  49. if self._motion_enable_dp:
  50. return self._motion_enable_dp.get_value(self._device)
  51. async def async_camera_image(self, width=None, height=None):
  52. if self._snapshot_dp:
  53. return self._snapshot_dp.decoded_value(self._device)
  54. @property
  55. def is_on(self):
  56. """Return the power state of the camera"""
  57. if self._switch_dp:
  58. return self._switch_dp.get_value(self._device)
  59. async def async_turn_off(self):
  60. """Turn off the camera"""
  61. if not self._switch_dp:
  62. raise NotImplementedError()
  63. await self._switch_dp.async_set_value(self._device, False)
  64. async def async_turn_on(self):
  65. """Turn on the camera"""
  66. if not self._switch_dp:
  67. raise NotImplementedError()
  68. await self._switch_dp.async_set_value(self._device, True)
  69. async def async_enable_motion_detection(self):
  70. """Enable motion detection on the camera"""
  71. if not self._motion_enable_dp:
  72. raise NotImplementedError()
  73. await self._motion_enable_dp.async_set_value(self._device, True)
  74. async def async_disable_motion_detection(self):
  75. """Disable motion detection on the camera"""
  76. if not self._motion_enable_dp:
  77. raise NotImplementedError()
  78. await self._motion_enable_dp.async_set_value(self._device, False)