2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera."""
|
2017-03-07 22:26:53 +00:00
|
|
|
import logging
|
|
|
|
|
2020-06-03 00:25:12 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2020-06-03 00:25:12 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
|
|
|
from homeassistant.helpers import entity_platform
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-06-03 00:25:12 +00:00
|
|
|
from .const import DEFAULT_BRAND, DOMAIN, SERVICE_TRIGGER
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_VIDEO_CLIP = "video"
|
|
|
|
ATTR_IMAGE = "image"
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-06-03 00:25:12 +00:00
|
|
|
SERVICE_TRIGGER_SCHEMA = vol.Schema({vol.Optional(ATTR_ENTITY_ID): cv.comp_entity_ids})
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
async def async_setup_entry(hass, config, async_add_entities):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a Blink Camera."""
|
2020-05-13 13:50:29 +00:00
|
|
|
data = hass.data[DOMAIN][config.entry_id]
|
|
|
|
entities = []
|
2018-12-03 20:45:12 +00:00
|
|
|
for name, camera in data.cameras.items():
|
2020-05-13 13:50:29 +00:00
|
|
|
entities.append(BlinkCamera(data, name, camera))
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
async_add_entities(entities)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-06-03 00:25:12 +00:00
|
|
|
platform = entity_platform.current_platform.get()
|
|
|
|
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_TRIGGER, SERVICE_TRIGGER_SCHEMA, "trigger_camera"
|
|
|
|
)
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
class BlinkCamera(Camera):
|
|
|
|
"""An implementation of a Blink Camera."""
|
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
def __init__(self, data, name, camera):
|
2017-03-07 22:26:53 +00:00
|
|
|
"""Initialize a camera."""
|
|
|
|
super().__init__()
|
|
|
|
self.data = data
|
2020-05-13 13:50:29 +00:00
|
|
|
self._name = f"{DOMAIN} {name}"
|
2018-10-03 02:17:14 +00:00
|
|
|
self._camera = camera
|
2019-09-03 15:09:59 +00:00
|
|
|
self._unique_id = f"{camera.serial}-camera"
|
2017-03-07 22:26:53 +00:00
|
|
|
self.response = None
|
2018-10-03 02:17:14 +00:00
|
|
|
self.current_image = None
|
|
|
|
self.last_image = None
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER.debug("Initialized blink camera %s", self._name)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the camera name."""
|
2017-03-07 22:26:53 +00:00
|
|
|
return self._name
|
|
|
|
|
2018-10-17 06:38:03 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique camera id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the camera attributes."""
|
|
|
|
return self._camera.attributes
|
|
|
|
|
|
|
|
def enable_motion_detection(self):
|
|
|
|
"""Enable motion detection for the camera."""
|
|
|
|
self._camera.set_motion_detect(True)
|
|
|
|
|
|
|
|
def disable_motion_detection(self):
|
|
|
|
"""Disable motion detection for the camera."""
|
|
|
|
self._camera.set_motion_detect(False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def motion_detection_enabled(self):
|
|
|
|
"""Return the state of the camera."""
|
2018-10-17 06:38:03 +00:00
|
|
|
return self._camera.motion_enabled
|
2018-10-03 02:17:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brand(self):
|
|
|
|
"""Return the camera brand."""
|
|
|
|
return DEFAULT_BRAND
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-06-03 00:25:12 +00:00
|
|
|
def trigger_camera(self):
|
|
|
|
"""Trigger camera to take a snapshot."""
|
|
|
|
self._camera.snap_picture()
|
|
|
|
self.data.refresh()
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
def camera_image(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2018-10-03 02:17:14 +00:00
|
|
|
return self._camera.image_from_cache.content
|