From fa8bc0a36ccefdf64b78ab63be9ccd7d4c0f890e Mon Sep 17 00:00:00 2001
From: John Arild Berentsen <turbokongen@hotmail.com>
Date: Tue, 6 Dec 2016 02:51:58 +0100
Subject: [PATCH] Add Verisure smartcam capture service (#4559)

* Add verisure capture as service

* docstyle
---
 homeassistant/components/services.yaml |  9 +++++++++
 homeassistant/components/verisure.py   | 27 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/homeassistant/components/services.yaml b/homeassistant/components/services.yaml
index 46a3a46ced6..54c0e18a3ee 100644
--- a/homeassistant/components/services.yaml
+++ b/homeassistant/components/services.yaml
@@ -144,3 +144,12 @@ openalpr:
 
   restart:
     description: Restart ffmpeg process of device.
+
+verisure:
+  capture_smartcam:
+    description: Capture a new image from a smartcam.
+
+    fields:
+      device_serial:
+        description: The serial number of the smartcam you want to capture an image from.
+        example: '2DEU AT5Z'
diff --git a/homeassistant/components/verisure.py b/homeassistant/components/verisure.py
index c8241d8fae5..f2b091aa0f1 100644
--- a/homeassistant/components/verisure.py
+++ b/homeassistant/components/verisure.py
@@ -7,6 +7,7 @@ https://home-assistant.io/components/verisure/
 import logging
 import threading
 import time
+import os.path
 from datetime import timedelta
 
 import voluptuous as vol
@@ -14,12 +15,14 @@ import voluptuous as vol
 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
 from homeassistant.helpers import discovery
 from homeassistant.util import Throttle
+import homeassistant.config as conf_util
 import homeassistant.helpers.config_validation as cv
 
 REQUIREMENTS = ['vsure==0.11.1']
 
 _LOGGER = logging.getLogger(__name__)
 
+ATTR_DEVICE_SERIAL = 'device_serial'
 CONF_ALARM = 'alarm'
 CONF_CODE_DIGITS = 'code_digits'
 CONF_HYDROMETERS = 'hygrometers'
@@ -29,6 +32,7 @@ CONF_SMARTPLUGS = 'smartplugs'
 CONF_THERMOMETERS = 'thermometers'
 CONF_SMARTCAM = 'smartcam'
 DOMAIN = 'verisure'
+SERVICE_CAPTURE_SMARTCAM = 'capture_smartcam'
 
 HUB = None
 
@@ -47,6 +51,10 @@ CONFIG_SCHEMA = vol.Schema({
     }),
 }, extra=vol.ALLOW_EXTRA)
 
+CAPTURE_IMAGE_SCHEMA = vol.Schema({
+    vol.Required(ATTR_DEVICE_SERIAL): cv.string
+})
+
 
 def setup(hass, config):
     """Setup the Verisure component."""
@@ -60,6 +68,20 @@ def setup(hass, config):
                       'camera'):
         discovery.load_platform(hass, component, DOMAIN, {}, config)
 
+    descriptions = conf_util.load_yaml_config_file(
+        os.path.join(os.path.dirname(__file__), 'services.yaml'))
+
+    def capture_smartcam(service):
+        """Capture a new picture from a smartcam."""
+        device_id = service.data.get(ATTR_DEVICE_SERIAL)
+        HUB.smartcam_capture(device_id)
+        _LOGGER.debug('Capturing new image from %s', ATTR_DEVICE_SERIAL)
+
+    hass.services.register(DOMAIN, SERVICE_CAPTURE_SMARTCAM,
+                           capture_smartcam,
+                           descriptions[DOMAIN][SERVICE_CAPTURE_SMARTCAM],
+                           schema=CAPTURE_IMAGE_SCHEMA)
+
     return True
 
 
@@ -150,6 +172,11 @@ class VerisureHub(object):
         self.smartcam_dict = self.my_pages.smartcam.get_imagelist()
         _LOGGER.debug('New dict: %s', self.smartcam_dict)
 
+    @Throttle(timedelta(seconds=30))
+    def smartcam_capture(self, device_id):
+        """Capture a new image from a smartcam."""
+        self.my_pages.smartcam.capture(device_id)
+
     @property
     def available(self):
         """Return True if hub is available."""
-- 
GitLab