Move imports to top for switcher_kis (#29530)

* Move imports to top for switcher_kis
* Disabled ungrouped imports if TYPE_CHECKING is true
This commit is contained in:
springstan 2019-12-06 00:20:07 +01:00 committed by Alexei Chetroi
parent 20fdcbadff
commit 173966f459
3 changed files with 48 additions and 25 deletions

View File

@ -5,6 +5,8 @@ from datetime import datetime, timedelta
from logging import getLogger from logging import getLogger
from typing import Dict, Optional from typing import Dict, Optional
from aioswitcher.api import SwitcherV2Api
from aioswitcher.bridge import SwitcherV2Bridge
import voluptuous as vol import voluptuous as vol
from homeassistant.auth.permissions.const import POLICY_EDIT from homeassistant.auth.permissions.const import POLICY_EDIT
@ -88,7 +90,6 @@ async def _validate_edit_permission(
async def async_setup(hass: HomeAssistantType, config: Dict) -> bool: async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
"""Set up the switcher component.""" """Set up the switcher component."""
from aioswitcher.bridge import SwitcherV2Bridge
phone_id = config[DOMAIN][CONF_PHONE_ID] phone_id = config[DOMAIN][CONF_PHONE_ID]
device_id = config[DOMAIN][CONF_DEVICE_ID] device_id = config[DOMAIN][CONF_DEVICE_ID]
@ -122,7 +123,6 @@ async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
async def async_set_auto_off_service(service: ServiceCallType) -> None: async def async_set_auto_off_service(service: ServiceCallType) -> None:
"""Use for handling setting device auto-off service calls.""" """Use for handling setting device auto-off service calls."""
from aioswitcher.api import SwitcherV2Api
await _validate_edit_permission( await _validate_edit_permission(
hass, service.context, service.data[CONF_ENTITY_ID] hass, service.context, service.data[CONF_ENTITY_ID]

View File

@ -1,7 +1,16 @@
"""Home Assistant Switcher Component Switch platform.""" """Home Assistant Switcher Component Switch platform."""
from logging import getLogger from logging import getLogger
from typing import Callable, Dict, TYPE_CHECKING from typing import TYPE_CHECKING, Callable, Dict
from aioswitcher.api import SwitcherV2Api
from aioswitcher.consts import (
COMMAND_OFF,
COMMAND_ON,
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON,
WAITING_TEXT,
)
from homeassistant.components.switch import ATTR_CURRENT_POWER_W, SwitchDevice from homeassistant.components.switch import ATTR_CURRENT_POWER_W, SwitchDevice
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -16,6 +25,7 @@ from . import (
SIGNAL_SWITCHER_DEVICE_UPDATE, SIGNAL_SWITCHER_DEVICE_UPDATE,
) )
# pylint: disable=ungrouped-imports
if TYPE_CHECKING: if TYPE_CHECKING:
from aioswitcher.devices import SwitcherV2Device from aioswitcher.devices import SwitcherV2Device
from aioswitcher.api.messages import SwitcherV2ControlResponseMSG from aioswitcher.api.messages import SwitcherV2ControlResponseMSG
@ -70,7 +80,6 @@ class SwitcherControl(SwitchDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return True if entity is on.""" """Return True if entity is on."""
from aioswitcher.consts import STATE_ON as SWITCHER_STATE_ON
return self._state == SWITCHER_STATE_ON return self._state == SWITCHER_STATE_ON
@ -82,7 +91,6 @@ class SwitcherControl(SwitchDevice):
@property @property
def device_state_attributes(self) -> Dict: def device_state_attributes(self) -> Dict:
"""Return the optional state attributes.""" """Return the optional state attributes."""
from aioswitcher.consts import WAITING_TEXT
attribs = {} attribs = {}
@ -96,10 +104,6 @@ class SwitcherControl(SwitchDevice):
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return True if entity is available.""" """Return True if entity is available."""
from aioswitcher.consts import (
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON,
)
return self._state in [SWITCHER_STATE_ON, SWITCHER_STATE_OFF] return self._state in [SWITCHER_STATE_ON, SWITCHER_STATE_OFF]
@ -135,13 +139,6 @@ class SwitcherControl(SwitchDevice):
async def _control_device(self, send_on: bool) -> None: async def _control_device(self, send_on: bool) -> None:
"""Turn the entity on or off.""" """Turn the entity on or off."""
from aioswitcher.api import SwitcherV2Api
from aioswitcher.consts import (
COMMAND_OFF,
COMMAND_ON,
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON,
)
response: "SwitcherV2ControlResponseMSG" = None response: "SwitcherV2ControlResponseMSG" = None
async with SwitcherV2Api( async with SwitcherV2Api(

View File

@ -103,10 +103,22 @@ def mock_bridge_fixture() -> Generator[None, Any, None]:
mock_bridge = CoroutineMock() mock_bridge = CoroutineMock()
patchers = [ patchers = [
patch("aioswitcher.bridge.SwitcherV2Bridge.start", new=mock_bridge), patch(
patch("aioswitcher.bridge.SwitcherV2Bridge.stop", new=mock_bridge), "homeassistant.components.switcher_kis.SwitcherV2Bridge.start",
patch("aioswitcher.bridge.SwitcherV2Bridge.queue", get=mock_queue), new=mock_bridge,
patch("aioswitcher.bridge.SwitcherV2Bridge.running", return_value=True), ),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Bridge.stop",
new=mock_bridge,
),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Bridge.queue",
get=mock_queue,
),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Bridge.running",
return_value=True,
),
] ]
for patcher in patchers: for patcher in patchers:
@ -127,9 +139,18 @@ def mock_failed_bridge_fixture() -> Generator[None, Any, None]:
raise RuntimeError raise RuntimeError
patchers = [ patchers = [
patch("aioswitcher.bridge.SwitcherV2Bridge.start", return_value=None), patch(
patch("aioswitcher.bridge.SwitcherV2Bridge.stop", return_value=None), "homeassistant.components.switcher_kis.SwitcherV2Bridge.start",
patch("aioswitcher.bridge.SwitcherV2Bridge.queue", get=mock_queue), return_value=None,
),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Bridge.stop",
return_value=None,
),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Bridge.queue",
get=mock_queue,
),
] ]
for patcher in patchers: for patcher in patchers:
@ -147,8 +168,13 @@ def mock_api_fixture() -> Generator[CoroutineMock, Any, None]:
mock_api = CoroutineMock() mock_api = CoroutineMock()
patchers = [ patchers = [
patch("aioswitcher.api.SwitcherV2Api.connect", new=mock_api), patch(
patch("aioswitcher.api.SwitcherV2Api.disconnect", new=mock_api), "homeassistant.components.switcher_kis.SwitcherV2Api.connect", new=mock_api
),
patch(
"homeassistant.components.switcher_kis.SwitcherV2Api.disconnect",
new=mock_api,
),
] ]
for patcher in patchers: for patcher in patchers: