mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
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:
parent
5e4b33c740
commit
d96cd4c4ea
@ -3,20 +3,20 @@ import logging
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import CONF_HOST
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.const import CONF_HOST
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
async_discover_devices,
|
|
||||||
get_static_devices,
|
|
||||||
ATTR_CONFIG,
|
ATTR_CONFIG,
|
||||||
CONF_DIMMER,
|
CONF_DIMMER,
|
||||||
CONF_DISCOVERY,
|
CONF_DISCOVERY,
|
||||||
CONF_LIGHT,
|
CONF_LIGHT,
|
||||||
CONF_SWITCH,
|
CONF_SWITCH,
|
||||||
SmartDevices,
|
SmartDevices,
|
||||||
|
async_discover_devices,
|
||||||
|
get_static_devices,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
"""Common code for tplink."""
|
"""Common code for tplink."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
from typing import Any, Callable, List
|
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
|
from homeassistant.helpers.typing import HomeAssistantType
|
||||||
|
|
||||||
@ -49,7 +49,6 @@ class SmartDevices:
|
|||||||
|
|
||||||
async def async_get_discoverable_devices(hass):
|
async def async_get_discoverable_devices(hass):
|
||||||
"""Return if there are devices that can be discovered."""
|
"""Return if there are devices that can be discovered."""
|
||||||
from pyHS100 import Discover
|
|
||||||
|
|
||||||
def discover():
|
def discover():
|
||||||
devs = Discover.discover()
|
devs = Discover.discover()
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"""Config flow for TP-Link."""
|
"""Config flow for TP-Link."""
|
||||||
from homeassistant.helpers import config_entry_flow
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from .const import DOMAIN
|
from homeassistant.helpers import config_entry_flow
|
||||||
from .common import async_get_discoverable_devices
|
|
||||||
|
|
||||||
|
from .common import async_get_discoverable_devices
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
config_entry_flow.register_discovery_flow(
|
config_entry_flow.register_discovery_flow(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
@ -7,18 +7,19 @@ import re
|
|||||||
|
|
||||||
from aiohttp.hdrs import (
|
from aiohttp.hdrs import (
|
||||||
ACCEPT,
|
ACCEPT,
|
||||||
COOKIE,
|
|
||||||
PRAGMA,
|
|
||||||
REFERER,
|
|
||||||
CONNECTION,
|
|
||||||
KEEP_ALIVE,
|
|
||||||
USER_AGENT,
|
|
||||||
CONTENT_TYPE,
|
|
||||||
CACHE_CONTROL,
|
|
||||||
ACCEPT_ENCODING,
|
ACCEPT_ENCODING,
|
||||||
ACCEPT_LANGUAGE,
|
ACCEPT_LANGUAGE,
|
||||||
|
CACHE_CONTROL,
|
||||||
|
CONNECTION,
|
||||||
|
CONTENT_TYPE,
|
||||||
|
COOKIE,
|
||||||
|
KEEP_ALIVE,
|
||||||
|
PRAGMA,
|
||||||
|
REFERER,
|
||||||
|
USER_AGENT,
|
||||||
)
|
)
|
||||||
import requests
|
import requests
|
||||||
|
from tplink.tplink import TpLinkClient
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.device_tracker import (
|
from homeassistant.components.device_tracker import (
|
||||||
@ -88,7 +89,6 @@ class TplinkDeviceScanner(DeviceScanner):
|
|||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
"""Initialize the scanner."""
|
"""Initialize the scanner."""
|
||||||
from tplink.tplink import TpLinkClient
|
|
||||||
|
|
||||||
host = config[CONF_HOST]
|
host = config[CONF_HOST]
|
||||||
password = config[CONF_PASSWORD]
|
password = config[CONF_PASSWORD]
|
||||||
|
@ -1,21 +1,22 @@
|
|||||||
"""Tests for the TP-Link component."""
|
"""Tests for the TP-Link component."""
|
||||||
from typing import Dict, Any
|
from typing import Any, Dict
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from pyHS100 import SmartBulb, SmartDevice, SmartDeviceException, SmartPlug
|
||||||
import pytest
|
import pytest
|
||||||
from pyHS100 import SmartPlug, SmartBulb, SmartDevice, SmartDeviceException
|
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow
|
from homeassistant import config_entries, data_entry_flow
|
||||||
from homeassistant.components import tplink
|
from homeassistant.components import tplink
|
||||||
from homeassistant.components.tplink.common import (
|
from homeassistant.components.tplink.common import (
|
||||||
CONF_DISCOVERY,
|
|
||||||
CONF_DIMMER,
|
CONF_DIMMER,
|
||||||
|
CONF_DISCOVERY,
|
||||||
CONF_LIGHT,
|
CONF_LIGHT,
|
||||||
CONF_SWITCH,
|
CONF_SWITCH,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.setup import async_setup_component
|
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")
|
MOCK_PYHS100 = MockDependency("pyHS100")
|
||||||
|
|
||||||
@ -25,7 +26,10 @@ async def test_creating_entry_tries_discover(hass):
|
|||||||
with MOCK_PYHS100, patch(
|
with MOCK_PYHS100, patch(
|
||||||
"homeassistant.components.tplink.async_setup_entry",
|
"homeassistant.components.tplink.async_setup_entry",
|
||||||
return_value=mock_coro(True),
|
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(
|
result = await hass.config_entries.flow.async_init(
|
||||||
tplink.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
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):
|
async def test_configuring_tplink_causes_discovery(hass):
|
||||||
"""Test that specifying empty config does discovery."""
|
"""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}
|
discover.return_value = {"host": 1234}
|
||||||
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
|
await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}})
|
||||||
await hass.async_block_till_done()
|
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])
|
@pytest.mark.parametrize("count", [1, 2, 3])
|
||||||
async def test_configuring_device_types(hass, name, cls, platform, count):
|
async def test_configuring_device_types(hass, name, cls, platform, count):
|
||||||
"""Test that light or switch platform list is filled correctly."""
|
"""Test that light or switch platform list is filled correctly."""
|
||||||
with patch("pyHS100.Discover.discover") as discover, patch(
|
with patch(
|
||||||
"pyHS100.SmartDevice._query_helper"
|
"homeassistant.components.tplink.common.Discover.discover"
|
||||||
|
) as discover, patch(
|
||||||
|
"homeassistant.components.tplink.common.SmartDevice._query_helper"
|
||||||
):
|
):
|
||||||
discovery_data = {
|
discovery_data = {
|
||||||
"123.123.123.{}".format(c): cls("123.123.123.123") for c in range(count)
|
"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):
|
async def test_configuring_devices_from_multiple_sources(hass):
|
||||||
"""Test static and discover devices are not duplicated."""
|
"""Test static and discover devices are not duplicated."""
|
||||||
with patch("pyHS100.Discover.discover") as discover, patch(
|
with patch(
|
||||||
"pyHS100.SmartDevice._query_helper"
|
"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 = SmartPlug("123.123.123.123")
|
||||||
discover_device_fail.get_sysinfo = MagicMock(side_effect=SmartDeviceException())
|
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):
|
async def test_is_dimmable(hass):
|
||||||
"""Test that is_dimmable switches are correctly added as lights."""
|
"""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",
|
"homeassistant.components.tplink.light.async_setup_entry",
|
||||||
return_value=mock_coro(True),
|
return_value=mock_coro(True),
|
||||||
) as setup, patch("pyHS100.SmartDevice._query_helper"), patch(
|
) as setup, patch(
|
||||||
"pyHS100.SmartPlug.is_dimmable", True
|
"homeassistant.components.tplink.common.SmartDevice._query_helper"
|
||||||
|
), patch(
|
||||||
|
"homeassistant.components.tplink.common.SmartPlug.is_dimmable", True
|
||||||
):
|
):
|
||||||
dimmable_switch = SmartPlug("123.123.123.123")
|
dimmable_switch = SmartPlug("123.123.123.123")
|
||||||
discover.return_value = {"host": dimmable_switch}
|
discover.return_value = {"host": dimmable_switch}
|
||||||
@ -162,7 +176,9 @@ async def test_configuring_discovery_disabled(hass):
|
|||||||
with MOCK_PYHS100, patch(
|
with MOCK_PYHS100, patch(
|
||||||
"homeassistant.components.tplink.async_setup_entry",
|
"homeassistant.components.tplink.async_setup_entry",
|
||||||
return_value=mock_coro(True),
|
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(
|
await async_setup_component(
|
||||||
hass, tplink.DOMAIN, {tplink.DOMAIN: {tplink.CONF_DISCOVERY: False}}
|
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(
|
with patch(
|
||||||
"pyHS100.SmartDevice._query_helper"
|
"homeassistant.components.tplink.common.Discover.discover"
|
||||||
|
) as discover, patch(
|
||||||
|
"homeassistant.components.tplink.common.SmartDevice._query_helper"
|
||||||
), patch(
|
), patch(
|
||||||
"homeassistant.components.tplink.light.async_setup_entry",
|
"homeassistant.components.tplink.light.async_setup_entry",
|
||||||
return_value=mock_coro(True),
|
return_value=mock_coro(True),
|
||||||
@ -191,7 +209,7 @@ async def test_platforms_are_initialized(hass):
|
|||||||
"homeassistant.components.tplink.switch.async_setup_entry",
|
"homeassistant.components.tplink.switch.async_setup_entry",
|
||||||
return_value=mock_coro(True),
|
return_value=mock_coro(True),
|
||||||
) as switch_setup, patch(
|
) 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.
|
# patching is_dimmable is necessray to avoid misdetection as light.
|
||||||
await async_setup_component(hass, tplink.DOMAIN, config)
|
await async_setup_component(hass, tplink.DOMAIN, config)
|
||||||
@ -221,7 +239,9 @@ async def test_unload(hass, platform):
|
|||||||
entry = MockConfigEntry(domain=tplink.DOMAIN)
|
entry = MockConfigEntry(domain=tplink.DOMAIN)
|
||||||
entry.add_to_hass(hass)
|
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),
|
"homeassistant.components.tplink.{}" ".async_setup_entry".format(platform),
|
||||||
return_value=mock_coro(True),
|
return_value=mock_coro(True),
|
||||||
) as light_setup:
|
) as light_setup:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user