2015-11-19 18:27:10 +00:00
|
|
|
"""
|
2016-02-24 09:38:06 +00:00
|
|
|
Demo camera platform that has a fake camera.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation
|
|
|
|
https://home-assistant.io/components/demo/
|
2015-11-19 18:27:10 +00:00
|
|
|
"""
|
2017-07-01 04:06:56 +00:00
|
|
|
import logging
|
2018-07-24 17:13:26 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from homeassistant.components.camera import Camera, SUPPORT_ON_OFF
|
2015-11-19 18:27:10 +00:00
|
|
|
|
2017-07-01 04:06:56 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-11-19 18:27:10 +00:00
|
|
|
|
2018-06-16 14:49:11 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_devices,
|
|
|
|
discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Demo camera platform."""
|
2018-06-16 14:49:11 +00:00
|
|
|
async_add_devices([
|
2018-07-24 17:13:26 +00:00
|
|
|
DemoCamera('Demo camera')
|
2015-11-19 18:27:10 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
class DemoCamera(Camera):
|
2016-07-12 14:46:29 +00:00
|
|
|
"""The representation of a Demo camera."""
|
2016-03-07 19:29:54 +00:00
|
|
|
|
2018-07-24 17:13:26 +00:00
|
|
|
def __init__(self, name):
|
2016-03-07 19:29:54 +00:00
|
|
|
"""Initialize demo camera component."""
|
2015-11-19 18:27:10 +00:00
|
|
|
super().__init__()
|
|
|
|
self._name = name
|
2017-07-01 04:06:56 +00:00
|
|
|
self._motion_status = False
|
2018-07-24 17:13:26 +00:00
|
|
|
self.is_streaming = True
|
|
|
|
self._images_index = 0
|
2015-11-19 18:27:10 +00:00
|
|
|
|
|
|
|
def camera_image(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return a faked still image response."""
|
2018-07-24 17:13:26 +00:00
|
|
|
self._images_index = (self._images_index + 1) % 4
|
2015-11-29 07:16:20 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
image_path = os.path.join(
|
2018-07-24 17:13:26 +00:00
|
|
|
os.path.dirname(__file__),
|
|
|
|
'demo_{}.jpg'.format(self._images_index))
|
|
|
|
_LOGGER.debug('Loading camera_image: %s', image_path)
|
2015-11-19 18:27:10 +00:00
|
|
|
with open(image_path, 'rb') as file:
|
2015-11-29 02:59:59 +00:00
|
|
|
return file.read()
|
2015-11-19 18:27:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the name of this camera."""
|
2015-11-19 18:27:10 +00:00
|
|
|
return self._name
|
2017-07-01 04:06:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2018-07-24 17:13:26 +00:00
|
|
|
"""Demo camera doesn't need poll.
|
|
|
|
|
|
|
|
Need explicitly call schedule_update_ha_state() after state changed.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Camera support turn on/off features."""
|
|
|
|
return SUPPORT_ON_OFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Whether camera is on (streaming)."""
|
|
|
|
return self.is_streaming
|
2017-07-01 04:06:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def motion_detection_enabled(self):
|
|
|
|
"""Camera Motion Detection Status."""
|
|
|
|
return self._motion_status
|
|
|
|
|
|
|
|
def enable_motion_detection(self):
|
|
|
|
"""Enable the Motion detection in base station (Arm)."""
|
|
|
|
self._motion_status = True
|
2018-07-24 17:13:26 +00:00
|
|
|
self.schedule_update_ha_state()
|
2017-07-01 04:06:56 +00:00
|
|
|
|
|
|
|
def disable_motion_detection(self):
|
|
|
|
"""Disable the motion detection in base station (Disarm)."""
|
|
|
|
self._motion_status = False
|
2018-07-24 17:13:26 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn off camera."""
|
|
|
|
self.is_streaming = False
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Turn on camera."""
|
|
|
|
self.is_streaming = True
|
|
|
|
self.schedule_update_ha_state()
|