From 323992e224be72b9bd4af33a659eb95edee519d5 Mon Sep 17 00:00:00 2001
From: ChristianKuehnel <christian.kuehnel@gmail.com>
Date: Sat, 20 Jan 2018 08:50:25 +0100
Subject: [PATCH] MiFlora - use bluepy on linux systems (#11284)

* Updated bluepy to version 1.1.4 as some issues with the native code were fixed there.

* Miflora - Added support for bluepy backend.

* miflora - now using bluepy backend on linux platforms

* fixed pylint findings

* miflora now using bluepy as default bluetooth backend and gatttool as fallback

* fixed hound complaints

* fixed pylint warning

* updated requirements

* Update miflora.py
---
 homeassistant/components/light/decora.py   |  2 +-
 homeassistant/components/sensor/miflora.py | 13 ++++++++++---
 requirements_all.txt                       |  2 +-
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/homeassistant/components/light/decora.py b/homeassistant/components/light/decora.py
index 17cc741c593..6d502e15d6f 100644
--- a/homeassistant/components/light/decora.py
+++ b/homeassistant/components/light/decora.py
@@ -16,7 +16,7 @@ from homeassistant.components.light import (
     PLATFORM_SCHEMA)
 import homeassistant.helpers.config_validation as cv
 
-REQUIREMENTS = ['decora==0.6', 'bluepy==1.1.1']
+REQUIREMENTS = ['decora==0.6', 'bluepy==1.1.4']
 
 _LOGGER = logging.getLogger(__name__)
 
diff --git a/homeassistant/components/sensor/miflora.py b/homeassistant/components/sensor/miflora.py
index 77d77949ebd..56f8c3cfe47 100644
--- a/homeassistant/components/sensor/miflora.py
+++ b/homeassistant/components/sensor/miflora.py
@@ -15,6 +15,7 @@ from homeassistant.const import (
     CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC
 )
 
+
 REQUIREMENTS = ['miflora==0.2.0']
 
 _LOGGER = logging.getLogger(__name__)
@@ -60,12 +61,19 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
 def setup_platform(hass, config, add_devices, discovery_info=None):
     """Set up the MiFlora sensor."""
     from miflora import miflora_poller
-    from miflora.backends.gatttool import GatttoolBackend
+    try:
+        import bluepy.btle  # noqa: F401 # pylint: disable=unused-variable
+        from miflora.backends.bluepy import BluepyBackend
+        backend = BluepyBackend
+    except ImportError:
+        from miflora.backends.gatttool import GatttoolBackend
+        backend = GatttoolBackend
+    _LOGGER.debug('Miflora is using %s backend.', backend.__name__)
 
     cache = config.get(CONF_CACHE)
     poller = miflora_poller.MiFloraPoller(
         config.get(CONF_MAC), cache_timeout=cache,
-        adapter=config.get(CONF_ADAPTER), backend=GatttoolBackend)
+        adapter=config.get(CONF_ADAPTER), backend=backend)
     force_update = config.get(CONF_FORCE_UPDATE)
     median = config.get(CONF_MEDIAN)
     poller.ble_timeout = config.get(CONF_TIMEOUT)
@@ -135,7 +143,6 @@ class MiFloraSensor(Entity):
             data = self.poller.parameter_value(self.parameter)
         except IOError as ioerr:
             _LOGGER.info("Polling error %s", ioerr)
-            data = None
             return
 
         if data is not None:
diff --git a/requirements_all.txt b/requirements_all.txt
index f7c7ce2d889..ffa7c4e1cbd 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -141,7 +141,7 @@ blinkstick==1.1.8
 blockchain==1.4.0
 
 # homeassistant.components.light.decora
-# bluepy==1.1.1
+# bluepy==1.1.4
 
 # homeassistant.components.notify.aws_lambda
 # homeassistant.components.notify.aws_sns
-- 
GitLab