From c584b6b28d4dd24df2de3d57bd732225f15fa930 Mon Sep 17 00:00:00 2001
From: John Arild Berentsen <turbokongen@hotmail.com>
Date: Fri, 12 Feb 2016 20:52:02 +0100
Subject: [PATCH] Support for Mousedetectors connected in Verisure systems

---
 homeassistant/components/sensor/verisure.py | 38 +++++++++++++++++++++
 homeassistant/components/verisure.py        | 17 +++++++--
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/homeassistant/components/sensor/verisure.py b/homeassistant/components/sensor/verisure.py
index dec678677b4..f7e80e27549 100644
--- a/homeassistant/components/sensor/verisure.py
+++ b/homeassistant/components/sensor/verisure.py
@@ -39,6 +39,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
         hasattr(value, 'humidity') and value.humidity
         ])
 
+    sensors.extend([
+        VerisureMouseDetection(value)
+        for value in verisure.MOUSEDETECTION_STATUS.values()
+        if verisure.SHOW_MOUSEDETECTION and
+        hasattr(value, 'amountText') and value.amountText
+        ])
+
     add_devices(sensors)
 
 
@@ -98,3 +105,34 @@ class VerisureHygrometer(Entity):
     def update(self):
         """ update sensor """
         verisure.update_climate()
+
+
+class VerisureMouseDetection(Entity):
+    """ represents a Verisure mousedetector within home assistant. """
+
+    def __init__(self, mousedetection_status):
+        self._id = mousedetection_status.deviceLabel
+
+    @property
+    def name(self):
+        """ Returns the name of the device. """
+        return '{} {}'.format(
+            verisure.MOUSEDETECTION_STATUS[self._id].location,
+            "Mouse")
+
+    @property
+    def state(self):
+        """ Returns the state of the device. """
+        return verisure.MOUSEDETECTION_STATUS[self._id].count
+
+    @property
+    def unit_of_measurement(self):
+        """ Unit of measurement of this entity """
+        if verisure.MOUSEDETECTION_STATUS[self._id].count >= 1:
+            return "Mice"
+        else:
+            return "Mouse"
+
+    def update(self):
+        """ update sensor """
+        verisure.update_mousedetection()
diff --git a/homeassistant/components/verisure.py b/homeassistant/components/verisure.py
index 98a2356954a..e85ee5d7850 100644
--- a/homeassistant/components/verisure.py
+++ b/homeassistant/components/verisure.py
@@ -38,6 +38,7 @@ ALARM_STATUS = {}
 SMARTPLUG_STATUS = {}
 CLIMATE_STATUS = {}
 LOCK_STATUS = {}
+MOUSEDETECTION_STATUS = {}
 
 VERISURE_LOGIN_ERROR = None
 VERISURE_ERROR = None
@@ -47,6 +48,7 @@ SHOW_HYGROMETERS = True
 SHOW_ALARM = True
 SHOW_SMARTPLUGS = True
 SHOW_LOCKS = True
+SHOW_MOUSEDETECTION = True
 CODE_DIGITS = 4
 
 # if wrong password was given don't try again
@@ -66,12 +68,14 @@ def setup(hass, config):
     from verisure import MyPages, LoginError, Error
 
     global SHOW_THERMOMETERS, SHOW_HYGROMETERS,\
-        SHOW_ALARM, SHOW_SMARTPLUGS, SHOW_LOCKS, CODE_DIGITS
+        SHOW_ALARM, SHOW_SMARTPLUGS, SHOW_LOCKS, SHOW_MOUSEDETECTION,\
+        CODE_DIGITS
     SHOW_THERMOMETERS = int(config[DOMAIN].get('thermometers', '1'))
     SHOW_HYGROMETERS = int(config[DOMAIN].get('hygrometers', '1'))
     SHOW_ALARM = int(config[DOMAIN].get('alarm', '1'))
     SHOW_SMARTPLUGS = int(config[DOMAIN].get('smartplugs', '1'))
     SHOW_LOCKS = int(config[DOMAIN].get('locks', '1'))
+    SHOW_MOUSEDETECTION = int(config[DOMAIN].get('mouse', '1'))
     CODE_DIGITS = int(config[DOMAIN].get('code_digits', '4'))
 
     global MY_PAGES
@@ -92,6 +96,7 @@ def setup(hass, config):
     update_climate()
     update_smartplug()
     update_lock()
+    update_mousedetection()
 
     # Load components for the devices in the ISY controller that we support
     for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
@@ -145,6 +150,11 @@ def update_lock():
     update_component(MY_PAGES.lock.get, LOCK_STATUS)
 
 
+def update_mousedetection():
+    """ Updates the status of mouse detectors. """
+    update_component(MY_PAGES.mousedetection.get, MOUSEDETECTION_STATUS)
+
+
 def update_component(get_function, status):
     """ Updates the status of verisure components. """
     if WRONG_PASSWORD_GIVEN:
@@ -152,7 +162,10 @@ def update_component(get_function, status):
         return
     try:
         for overview in get_function():
-            status[overview.id] = overview
+            try:
+                status[overview.id] = overview
+            except IndexError:
+                status[overview.deviceLabel] = overview
     except (ConnectionError, VERISURE_ERROR) as ex:
         _LOGGER.error('Caught connection error %s, tries to reconnect', ex)
         reconnect()
-- 
GitLab