Move esphome imports at top-level (#29064)

pull/28253/head
Quentame 2019-11-26 03:00:58 +01:00 committed by Paulus Schoutsen
parent 87de5db535
commit 8a413b152d
6 changed files with 16 additions and 30 deletions

View File

@ -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."""

View File

@ -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,

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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(