From 8a413b152d4ebc9acd699fcb678707be1dbcc172 Mon Sep 17 00:00:00 2001 From: Quentame Date: Tue, 26 Nov 2019 03:00:58 +0100 Subject: [PATCH] Move esphome imports at top-level (#29064) --- .../components/esphome/binary_sensor.py | 3 --- homeassistant/components/esphome/climate.py | 2 +- .../components/esphome/config_flow.py | 5 +---- homeassistant/components/esphome/cover.py | 6 +----- .../components/esphome/entry_data.py | 10 +++++----- tests/components/esphome/test_config_flow.py | 20 ++++++++----------- 6 files changed, 16 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/esphome/binary_sensor.py b/homeassistant/components/esphome/binary_sensor.py index 64506f69283..fe41bb2f7bb 100644 --- a/homeassistant/components/esphome/binary_sensor.py +++ b/homeassistant/components/esphome/binary_sensor.py @@ -1,5 +1,4 @@ """Support for ESPHome binary sensors.""" -import logging from typing import Optional from aioesphomeapi import BinarySensorInfo, BinarySensorState @@ -8,8 +7,6 @@ from homeassistant.components.binary_sensor import BinarySensorDevice from . import EsphomeEntity, platform_async_setup_entry -_LOGGER = logging.getLogger(__name__) - async def async_setup_entry(hass, entry, async_add_entities): """Set up ESPHome binary sensors based on a config entry.""" diff --git a/homeassistant/components/esphome/climate.py b/homeassistant/components/esphome/climate.py index 14f684f1e32..960366a8332 100644 --- a/homeassistant/components/esphome/climate.py +++ b/homeassistant/components/esphome/climate.py @@ -20,8 +20,8 @@ from homeassistant.components.climate.const import ( CURRENT_HVAC_DRY, CURRENT_HVAC_FAN, CURRENT_HVAC_HEAT, - CURRENT_HVAC_OFF, CURRENT_HVAC_IDLE, + CURRENT_HVAC_OFF, FAN_AUTO, FAN_DIFFUSE, FAN_FOCUS, diff --git a/homeassistant/components/esphome/config_flow.py b/homeassistant/components/esphome/config_flow.py index 47c00f43463..53289799b43 100644 --- a/homeassistant/components/esphome/config_flow.py +++ b/homeassistant/components/esphome/config_flow.py @@ -2,6 +2,7 @@ from collections import OrderedDict from typing import Optional +from aioesphomeapi import APIClient, APIConnectionError import voluptuous as vol from homeassistant import config_entries @@ -147,8 +148,6 @@ class EsphomeFlowHandler(config_entries.ConfigFlow): async def fetch_device_info(self): """Fetch device info from API and return any errors.""" - from aioesphomeapi import APIClient, APIConnectionError - cli = APIClient(self.hass.loop, self._host, self._port, "") try: @@ -165,8 +164,6 @@ class EsphomeFlowHandler(config_entries.ConfigFlow): async def try_login(self): """Try logging in to device and return any errors.""" - from aioesphomeapi import APIClient, APIConnectionError - cli = APIClient(self.hass.loop, self._host, self._port, self._password) try: diff --git a/homeassistant/components/esphome/cover.py b/homeassistant/components/esphome/cover.py index 980fc936940..53014991de8 100644 --- a/homeassistant/components/esphome/cover.py +++ b/homeassistant/components/esphome/cover.py @@ -2,7 +2,7 @@ import logging from typing import Optional -from aioesphomeapi import CoverInfo, CoverState +from aioesphomeapi import CoverInfo, CoverOperation, CoverState from homeassistant.components.cover import ( ATTR_POSITION, @@ -82,15 +82,11 @@ class EsphomeCover(EsphomeEntity, CoverDevice): @esphome_state_property def is_opening(self) -> bool: """Return if the cover is opening or not.""" - from aioesphomeapi import CoverOperation - return self._state.current_operation == CoverOperation.IS_OPENING @esphome_state_property def is_closing(self) -> bool: """Return if the cover is closing or not.""" - from aioesphomeapi import CoverOperation - return self._state.current_operation == CoverOperation.IS_CLOSING @esphome_state_property diff --git a/homeassistant/components/esphome/entry_data.py b/homeassistant/components/esphome/entry_data.py index d916e1a90c8..48f1aea2c2d 100644 --- a/homeassistant/components/esphome/entry_data.py +++ b/homeassistant/components/esphome/entry_data.py @@ -1,22 +1,22 @@ """Runtime entry data for ESPHome stored in hass.data.""" import asyncio -from typing import Any, Callable, Dict, List, Optional, Tuple, Set +from typing import Any, Callable, Dict, List, Optional, Set, Tuple from aioesphomeapi import ( COMPONENT_TYPE_TO_INFO, - DeviceInfo, - EntityInfo, - EntityState, - UserService, BinarySensorInfo, CameraInfo, ClimateInfo, CoverInfo, + DeviceInfo, + EntityInfo, + EntityState, FanInfo, LightInfo, SensorInfo, SwitchInfo, TextSensorInfo, + UserService, ) import attr diff --git a/tests/components/esphome/test_config_flow.py b/tests/components/esphome/test_config_flow.py index 8c05b274dd0..4b951f9a369 100644 --- a/tests/components/esphome/test_config_flow.py +++ b/tests/components/esphome/test_config_flow.py @@ -4,23 +4,17 @@ from unittest.mock import MagicMock, patch import pytest -from homeassistant.components.esphome import config_flow, DATA_KEY -from tests.common import mock_coro, MockConfigEntry +from homeassistant.components.esphome import DATA_KEY, config_flow + +from tests.common import MockConfigEntry, mock_coro MockDeviceInfo = namedtuple("DeviceInfo", ["uses_password", "name"]) -@pytest.fixture(autouse=True) -def aioesphomeapi_mock(): - """Mock aioesphomeapi.""" - with patch.dict("sys.modules", {"aioesphomeapi": MagicMock()}): - yield - - @pytest.fixture def mock_client(): """Mock APIClient.""" - with patch("aioesphomeapi.APIClient") as mock_client: + with patch("homeassistant.components.esphome.config_flow.APIClient") as mock_client: def mock_constructor(loop, host, port, password): """Fake the client constructor.""" @@ -40,7 +34,8 @@ def mock_client(): def mock_api_connection_error(): """Mock out the try login method.""" with patch( - "aioesphomeapi.APIConnectionError", new_callable=lambda: OSError + "homeassistant.components.esphome.config_flow.APIConnectionError", + new_callable=lambda: OSError, ) as mock_error: yield mock_error @@ -86,7 +81,8 @@ async def test_user_resolve_error(hass, mock_api_connection_error, mock_client): super().__init__("Error resolving IP address") with patch( - "aioesphomeapi.APIConnectionError", new_callable=lambda: MockResolveError + "homeassistant.components.esphome.config_flow.APIConnectionError", + new_callable=lambda: MockResolveError, ) as exc: mock_client.device_info.side_effect = exc result = await flow.async_step_user(