From dd8fc4174779ee3010aaec8799595621ea5189bf Mon Sep 17 00:00:00 2001
From: javicalle <31999997+javicalle@users.noreply.github.com>
Date: Sun, 13 Oct 2019 22:19:11 +0200
Subject: [PATCH] Move imports in rflink component (#27367)

* Move imports in rflink component

* import order

* import order

* Update __init__.py

* Update __init__.py

I don't understand why tests are failing...

* Fix RFLink imports

* Fix monkeypatch for 'create_rflink_connection'

* isort for rflink classes
---
 homeassistant/components/rflink/__init__.py   | 15 +++++++--------
 homeassistant/components/rflink/sensor.py     |  3 +--
 tests/components/rflink/test_binary_sensor.py |  5 ++---
 tests/components/rflink/test_cover.py         |  8 ++++----
 tests/components/rflink/test_init.py          | 12 +++++++-----
 tests/components/rflink/test_light.py         |  4 ++--
 tests/components/rflink/test_sensor.py        |  3 ++-
 tests/components/rflink/test_switch.py        |  4 ++--
 8 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py
index c218bc271ce..b3e1d2b16b7 100644
--- a/homeassistant/components/rflink/__init__.py
+++ b/homeassistant/components/rflink/__init__.py
@@ -2,8 +2,10 @@
 import asyncio
 from collections import defaultdict
 import logging
-import async_timeout
 
+import async_timeout
+from rflink.protocol import create_rflink_connection
+from serial import SerialException
 import voluptuous as vol
 
 from homeassistant.const import (
@@ -11,18 +13,18 @@ from homeassistant.const import (
     CONF_COMMAND,
     CONF_HOST,
     CONF_PORT,
-    STATE_ON,
     EVENT_HOMEASSISTANT_STOP,
+    STATE_ON,
 )
 from homeassistant.core import CoreState, callback
 from homeassistant.exceptions import HomeAssistantError
 import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers.deprecation import get_deprecated
-from homeassistant.helpers.entity import Entity
 from homeassistant.helpers.dispatcher import (
-    async_dispatcher_send,
     async_dispatcher_connect,
+    async_dispatcher_send,
 )
+from homeassistant.helpers.entity import Entity
 from homeassistant.helpers.restore_state import RestoreEntity
 
 _LOGGER = logging.getLogger(__name__)
@@ -118,9 +120,6 @@ def identify_event_type(event):
 
 async def async_setup(hass, config):
     """Set up the Rflink component."""
-    from rflink.protocol import create_rflink_connection
-    import serial
-
     # Allow entities to register themselves by device_id to be looked up when
     # new rflink events arrive to be handled
     hass.data[DATA_ENTITY_LOOKUP] = {
@@ -239,7 +238,7 @@ async def async_setup(hass, config):
                 transport, protocol = await connection
 
         except (
-            serial.serialutil.SerialException,
+            SerialException,
             ConnectionRefusedError,
             TimeoutError,
             OSError,
diff --git a/homeassistant/components/rflink/sensor.py b/homeassistant/components/rflink/sensor.py
index 48484621c4d..aa0ef4f9c62 100644
--- a/homeassistant/components/rflink/sensor.py
+++ b/homeassistant/components/rflink/sensor.py
@@ -1,6 +1,7 @@
 """Support for Rflink sensors."""
 import logging
 
+from rflink.parser import PACKET_FIELDS, UNITS
 import voluptuous as vol
 
 from homeassistant.components.sensor import PLATFORM_SCHEMA
@@ -66,8 +67,6 @@ def lookup_unit_for_sensor_type(sensor_type):
 
     Async friendly.
     """
-    from rflink.parser import UNITS, PACKET_FIELDS
-
     field_abbrev = {v: k for k, v in PACKET_FIELDS.items()}
 
     return UNITS.get(field_abbrev.get(sensor_type))
diff --git a/tests/components/rflink/test_binary_sensor.py b/tests/components/rflink/test_binary_sensor.py
index 442ebebdffe..d1fdec579c9 100644
--- a/tests/components/rflink/test_binary_sensor.py
+++ b/tests/components/rflink/test_binary_sensor.py
@@ -8,14 +8,13 @@ from datetime import timedelta
 from unittest.mock import patch
 
 from homeassistant.components.rflink import CONF_RECONNECT_INTERVAL
-
-import homeassistant.core as ha
 from homeassistant.const import (
     EVENT_STATE_CHANGED,
-    STATE_ON,
     STATE_OFF,
+    STATE_ON,
     STATE_UNAVAILABLE,
 )
+import homeassistant.core as ha
 import homeassistant.util.dt as dt_util
 
 from tests.common import async_fire_time_changed
diff --git a/tests/components/rflink/test_cover.py b/tests/components/rflink/test_cover.py
index 858258e7efd..dc286502068 100644
--- a/tests/components/rflink/test_cover.py
+++ b/tests/components/rflink/test_cover.py
@@ -9,13 +9,13 @@ import logging
 
 from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
 from homeassistant.const import (
-    SERVICE_OPEN_COVER,
+    ATTR_ENTITY_ID,
     SERVICE_CLOSE_COVER,
-    STATE_OPEN,
+    SERVICE_OPEN_COVER,
     STATE_CLOSED,
-    ATTR_ENTITY_ID,
+    STATE_OPEN,
 )
-from homeassistant.core import callback, State, CoreState
+from homeassistant.core import CoreState, State, callback
 
 from tests.common import mock_restore_cache
 from tests.components.rflink.test_init import mock_rflink
diff --git a/tests/components/rflink/test_init.py b/tests/components/rflink/test_init.py
index 5e821fbdeb2..df96b0e87ae 100644
--- a/tests/components/rflink/test_init.py
+++ b/tests/components/rflink/test_init.py
@@ -5,14 +5,14 @@ from unittest.mock import Mock
 from homeassistant.bootstrap import async_setup_component
 from homeassistant.components.rflink import (
     CONF_RECONNECT_INTERVAL,
-    SERVICE_SEND_COMMAND,
-    RflinkCommand,
-    TMP_ENTITY,
     DATA_ENTITY_LOOKUP,
     EVENT_KEY_COMMAND,
     EVENT_KEY_SENSOR,
+    SERVICE_SEND_COMMAND,
+    TMP_ENTITY,
+    RflinkCommand,
 )
-from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_STOP_COVER
+from homeassistant.const import ATTR_ENTITY_ID, SERVICE_STOP_COVER, SERVICE_TURN_OFF
 
 
 async def mock_rflink(
@@ -46,7 +46,9 @@ async def mock_rflink(
             return transport, protocol
 
     mock_create = Mock(wraps=create_rflink_connection)
-    monkeypatch.setattr("rflink.protocol.create_rflink_connection", mock_create)
+    monkeypatch.setattr(
+        "homeassistant.components.rflink.create_rflink_connection", mock_create
+    )
 
     await async_setup_component(hass, "rflink", config)
     await async_setup_component(hass, domain, config)
diff --git a/tests/components/rflink/test_light.py b/tests/components/rflink/test_light.py
index a22e7680ac8..b22730a3310 100644
--- a/tests/components/rflink/test_light.py
+++ b/tests/components/rflink/test_light.py
@@ -11,10 +11,10 @@ from homeassistant.const import (
     ATTR_ENTITY_ID,
     SERVICE_TURN_OFF,
     SERVICE_TURN_ON,
-    STATE_ON,
     STATE_OFF,
+    STATE_ON,
 )
-from homeassistant.core import callback, State, CoreState
+from homeassistant.core import CoreState, State, callback
 
 from tests.common import mock_restore_cache
 from tests.components.rflink.test_init import mock_rflink
diff --git a/tests/components/rflink/test_sensor.py b/tests/components/rflink/test_sensor.py
index bf6c9e03fbc..3fea3ef6ef4 100644
--- a/tests/components/rflink/test_sensor.py
+++ b/tests/components/rflink/test_sensor.py
@@ -7,12 +7,13 @@ automatic sensor creation.
 
 from homeassistant.components.rflink import (
     CONF_RECONNECT_INTERVAL,
-    TMP_ENTITY,
     DATA_ENTITY_LOOKUP,
     EVENT_KEY_COMMAND,
     EVENT_KEY_SENSOR,
+    TMP_ENTITY,
 )
 from homeassistant.const import STATE_UNKNOWN
+
 from tests.components.rflink.test_init import mock_rflink
 
 DOMAIN = "sensor"
diff --git a/tests/components/rflink/test_switch.py b/tests/components/rflink/test_switch.py
index 4503f1a232f..d1fced33208 100644
--- a/tests/components/rflink/test_switch.py
+++ b/tests/components/rflink/test_switch.py
@@ -10,10 +10,10 @@ from homeassistant.const import (
     ATTR_ENTITY_ID,
     SERVICE_TURN_OFF,
     SERVICE_TURN_ON,
-    STATE_ON,
     STATE_OFF,
+    STATE_ON,
 )
-from homeassistant.core import callback, State, CoreState
+from homeassistant.core import CoreState, State, callback
 
 from tests.common import mock_restore_cache
 from tests.components.rflink.test_init import mock_rflink
-- 
GitLab