2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Abode Security System cameras."""
|
2019-03-21 05:56:46 +00:00
|
|
|
from datetime import timedelta
|
2017-09-18 15:39:41 +00:00
|
|
|
|
2019-10-12 20:02:12 +00:00
|
|
|
import abodepy.helpers.constants as CONST
|
|
|
|
import abodepy.helpers.timeline as TIMELINE
|
2019-10-13 18:01:04 +00:00
|
|
|
import requests
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
from homeassistant.components.camera import Camera
|
2019-11-09 06:35:45 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-09-18 15:39:41 +00:00
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
from . import AbodeDevice
|
2020-03-04 22:54:28 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-09-18 15:39:41 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=90)
|
|
|
|
|
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2019-11-09 06:35:45 +00:00
|
|
|
"""Set up Abode camera devices."""
|
2019-10-13 18:01:04 +00:00
|
|
|
data = hass.data[DOMAIN]
|
2017-09-18 15:39:41 +00:00
|
|
|
|
2019-11-09 06:35:45 +00:00
|
|
|
entities = []
|
|
|
|
|
2017-09-18 15:39:41 +00:00
|
|
|
for device in data.abode.get_devices(generic_type=CONST.TYPE_CAMERA):
|
2019-11-09 06:35:45 +00:00
|
|
|
entities.append(AbodeCamera(data, device, TIMELINE.CAPTURE_IMAGE))
|
2017-09-18 15:39:41 +00:00
|
|
|
|
2019-11-09 06:35:45 +00:00
|
|
|
async_add_entities(entities)
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AbodeCamera(AbodeDevice, Camera):
|
|
|
|
"""Representation of an Abode camera."""
|
|
|
|
|
|
|
|
def __init__(self, data, device, event):
|
|
|
|
"""Initialize the Abode device."""
|
|
|
|
AbodeDevice.__init__(self, data, device)
|
|
|
|
Camera.__init__(self)
|
|
|
|
self._event = event
|
|
|
|
self._response = None
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-09-18 15:39:41 +00:00
|
|
|
"""Subscribe Abode events."""
|
2018-10-01 06:50:05 +00:00
|
|
|
await super().async_added_to_hass()
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
self.hass.async_add_job(
|
|
|
|
self._data.abode.events.add_timeline_callback,
|
2019-07-31 19:25:30 +00:00
|
|
|
self._event,
|
|
|
|
self._capture_callback,
|
2017-09-18 15:39:41 +00:00
|
|
|
)
|
|
|
|
|
2020-02-23 21:38:05 +00:00
|
|
|
signal = f"abode_camera_capture_{self.entity_id}"
|
2019-11-09 06:35:45 +00:00
|
|
|
async_dispatcher_connect(self.hass, signal, self.capture)
|
|
|
|
|
2017-09-18 15:39:41 +00:00
|
|
|
def capture(self):
|
|
|
|
"""Request a new image capture."""
|
|
|
|
return self._device.capture()
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def refresh_image(self):
|
|
|
|
"""Find a new image on the timeline."""
|
|
|
|
if self._device.refresh_image():
|
|
|
|
self.get_image()
|
|
|
|
|
|
|
|
def get_image(self):
|
|
|
|
"""Attempt to download the most recent capture."""
|
|
|
|
if self._device.image_url:
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._response = requests.get(self._device.image_url, stream=True)
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
self._response.raise_for_status()
|
|
|
|
except requests.HTTPError as err:
|
2020-03-04 22:54:28 +00:00
|
|
|
LOGGER.warning("Failed to get camera image: %s", err)
|
2017-09-18 15:39:41 +00:00
|
|
|
self._response = None
|
|
|
|
else:
|
|
|
|
self._response = None
|
|
|
|
|
|
|
|
def camera_image(self):
|
|
|
|
"""Get a camera image."""
|
|
|
|
self.refresh_image()
|
|
|
|
|
|
|
|
if self._response:
|
|
|
|
return self._response.content
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _capture_callback(self, capture):
|
|
|
|
"""Update the image with the device then refresh device."""
|
|
|
|
self._device.update_image_location(capture)
|
|
|
|
self.get_image()
|
|
|
|
self.schedule_update_ha_state()
|