Move imports in tplink component (#27567)

* move imports in tplink component

* fix: order of imports

* fix: tplink tests

* fix: import order in tests

* fix: tests formatting
This commit is contained in:
Michał Mrozek 2019-10-13 17:05:04 +02:00 committed by Franck Nijhof
parent 5e4b33c740
commit d96cd4c4ea
5 changed files with 55 additions and 36 deletions

View File

@ -3,20 +3,20 @@ import logging
import voluptuous as vol
from homeassistant.const import CONF_HOST
from homeassistant import config_entries
from homeassistant.const import CONF_HOST
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from .common import (
async_discover_devices,
get_static_devices,
ATTR_CONFIG,
CONF_DIMMER,
CONF_DISCOVERY,
CONF_LIGHT,
CONF_SWITCH,
SmartDevices,
async_discover_devices,
get_static_devices,
)
_LOGGER = logging.getLogger(__name__)

View File

@ -1,10 +1,10 @@
"""Common code for tplink."""
import asyncio
import logging
from datetime import timedelta
import logging
from typing import Any, Callable, List
from pyHS100 import SmartBulb, SmartDevice, SmartPlug, SmartDeviceException
from pyHS100 import Discover, SmartBulb, SmartDevice, SmartDeviceException, SmartPlug
from homeassistant.helpers.typing import HomeAssistantType
@ -49,7 +49,6 @@ class SmartDevices:
async def async_get_discoverable_devices(hass):
"""Return if there are devices that can be discovered."""
from pyHS100 import Discover
def discover():
devs = Discover.discover()

View File

@ -1,9 +1,9 @@
"""Config flow for TP-Link."""
from homeassistant.helpers import config_entry_flow
from homeassistant import config_entries
from .const import DOMAIN
from .common import async_get_discoverable_devices
from homeassistant.helpers import config_entry_flow
from .common import async_get_discoverable_devices
from .const import DOMAIN
config_entry_flow.register_discovery_flow(
DOMAIN,

View File

@ -7,18 +7,19 @@ import re
from aiohttp.hdrs import (
ACCEPT,
COOKIE,
PRAGMA,
REFERER,
CONNECTION,
KEEP_ALIVE,
USER_AGENT,
CONTENT_TYPE,
CACHE_CONTROL,
ACCEPT_ENCODING,
ACCEPT_LANGUAGE,
CACHE_CONTROL,
CONNECTION,
CONTENT_TYPE,
COOKIE,
KEEP_ALIVE,
PRAGMA,
REFERER,
USER_AGENT,
)
import requests
from tplink.tplink import TpLinkClient
import voluptuous as vol
from homeassistant.components.device_tracker import (
@ -88,7 +89,6 @@ class TplinkDeviceScanner(DeviceScanner):
def __init__(self, config):
"""Initialize the scanner."""
from tplink.tplink import TpLinkClient
host = config[CONF_HOST]
password = config[CONF_PASSWORD]

View File

@ -1,21 +1,22 @@
"""Tests for the TP-Link component."""
from typing import Dict, Any
from typing import Any, Dict
from unittest.mock import MagicMock, patch
from pyHS100 import SmartBulb, SmartDevice, SmartDeviceException, SmartPlug
import pytest
from pyHS100 import SmartPlug, SmartBulb, SmartDevice, SmartDeviceException
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import tplink
from homeassistant.components.tplink.common import (
CONF_DISCOVERY,
CONF_DIMMER,
CONF_DISCOVERY,
CONF_LIGHT,
CONF_SWITCH,
)
from homeassistant.const import CONF_HOST
from homeassistant.setup import async_setup_component
from tests.common import MockDependency, MockConfigEntry, mock_coro
from tests.common import MockConfigEntry, MockDependency, mock_coro
MOCK_PYHS100 = MockDependency("pyHS100")
@ -25,7 +26,10 @@ async def test_creating_entry_tries_discover(hass):
with MOCK_PYHS100, patch(
"homeassistant.components.tplink.async_setup_entry",
return_value=mock_coro(True),
) as mock_setup, patch("pyHS100.Discover.discover", return_value={"host": 1234}):
) as mock_setup, patch(
"homeassistant.components.tplink.common.Discover.discover",
return_value={"host": 1234},
):
result = await hass.config_entries.flow.async_init(
tplink.DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@ -43,7 +47,9 @@ async def test_creating_entry_tries_discover(hass):
async def test_configuring_tplink_causes_discovery(hass):
"""Test that specifying empty config does discovery."""
with MOCK_PYHS100, patch("pyHS100.Discover.discover") as discover:
with MOCK_PYHS100, patch(
"homeassistant.components.tplink.common.Discover.discover"
) as discover:
discover.return_value = {"host": 1234}
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
await hass.async_block_till_done()
@ -61,8 +67,10 @@ async def test_configuring_tplink_causes_discovery(hass):
@pytest.mark.parametrize("count", [1, 2, 3])
async def test_configuring_device_types(hass, name, cls, platform, count):
"""Test that light or switch platform list is filled correctly."""
with patch("pyHS100.Discover.discover") as discover, patch(
"pyHS100.SmartDevice._query_helper"
with patch(
"homeassistant.components.tplink.common.Discover.discover"
) as discover, patch(
"homeassistant.components.tplink.common.SmartDevice._query_helper"
):
discovery_data = {
"123.123.123.{}".format(c): cls("123.123.123.123") for c in range(count)
@ -104,8 +112,10 @@ class UnknownSmartDevice(SmartDevice):
async def test_configuring_devices_from_multiple_sources(hass):
"""Test static and discover devices are not duplicated."""
with patch("pyHS100.Discover.discover") as discover, patch(
"pyHS100.SmartDevice._query_helper"
with patch(
"homeassistant.components.tplink.common.Discover.discover"
) as discover, patch(
"homeassistant.components.tplink.common.SmartDevice._query_helper"
):
discover_device_fail = SmartPlug("123.123.123.123")
discover_device_fail.get_sysinfo = MagicMock(side_effect=SmartDeviceException())
@ -139,11 +149,15 @@ async def test_configuring_devices_from_multiple_sources(hass):
async def test_is_dimmable(hass):
"""Test that is_dimmable switches are correctly added as lights."""
with patch("pyHS100.Discover.discover") as discover, patch(
with patch(
"homeassistant.components.tplink.common.Discover.discover"
) as discover, patch(
"homeassistant.components.tplink.light.async_setup_entry",
return_value=mock_coro(True),
) as setup, patch("pyHS100.SmartDevice._query_helper"), patch(
"pyHS100.SmartPlug.is_dimmable", True
) as setup, patch(
"homeassistant.components.tplink.common.SmartDevice._query_helper"
), patch(
"homeassistant.components.tplink.common.SmartPlug.is_dimmable", True
):
dimmable_switch = SmartPlug("123.123.123.123")
discover.return_value = {"host": dimmable_switch}
@ -162,7 +176,9 @@ async def test_configuring_discovery_disabled(hass):
with MOCK_PYHS100, patch(
"homeassistant.components.tplink.async_setup_entry",
return_value=mock_coro(True),
) as mock_setup, patch("pyHS100.Discover.discover", return_value=[]) as discover:
) as mock_setup, patch(
"homeassistant.components.tplink.common.Discover.discover", return_value=[]
) as discover:
await async_setup_component(
hass, tplink.DOMAIN, {tplink.DOMAIN: {tplink.CONF_DISCOVERY: False}}
)
@ -182,8 +198,10 @@ async def test_platforms_are_initialized(hass):
}
}
with patch("pyHS100.Discover.discover") as discover, patch(
"pyHS100.SmartDevice._query_helper"
with patch(
"homeassistant.components.tplink.common.Discover.discover"
) as discover, patch(
"homeassistant.components.tplink.common.SmartDevice._query_helper"
), patch(
"homeassistant.components.tplink.light.async_setup_entry",
return_value=mock_coro(True),
@ -191,7 +209,7 @@ async def test_platforms_are_initialized(hass):
"homeassistant.components.tplink.switch.async_setup_entry",
return_value=mock_coro(True),
) as switch_setup, patch(
"pyHS100.SmartPlug.is_dimmable", False
"homeassistant.components.tplink.common.SmartPlug.is_dimmable", False
):
# patching is_dimmable is necessray to avoid misdetection as light.
await async_setup_component(hass, tplink.DOMAIN, config)
@ -221,7 +239,9 @@ async def test_unload(hass, platform):
entry = MockConfigEntry(domain=tplink.DOMAIN)
entry.add_to_hass(hass)
with patch("pyHS100.SmartDevice._query_helper"), patch(
with patch(
"homeassistant.components.tplink.common.SmartDevice._query_helper"
), patch(
"homeassistant.components.tplink.{}" ".async_setup_entry".format(platform),
return_value=mock_coro(True),
) as light_setup: