Apply more suggestions from bond code review (#37592)

* apply more suggestions from bond code review

* Update cover.py

* Update test_cover.py

* Update test_cover.py

* Update test_cover.py

* Update cover.py

* Update utils.py

* Update test_cover.py

* Update test_utils.py

* Delete test_utils.py

* Update cover.py

* Update test_cover.py

* Update test_cover.py
This commit is contained in:
Chris Talkington 2020-07-08 16:28:53 -05:00 committed by GitHub
parent c1de781a23
commit fb420d5952
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 77 deletions

View File

@ -1,5 +1,4 @@
"""Support for Bond covers."""
import asyncio
import logging
from typing import Any, Callable, Dict, List, Optional
@ -23,19 +22,17 @@ async def async_setup_entry(
async_add_entities: Callable[[List[Entity]], None],
) -> None:
"""Set up Bond cover devices."""
bond: Bond = hass.data[DOMAIN][entry.entry_id]
async def discover():
devices = await get_bond_devices(hass, bond)
covers = [
BondCover(bond, device)
for device in devices
if device.type == BOND_DEVICE_TYPE_MOTORIZED_SHADES
]
async_add_entities(covers)
devices = await hass.async_add_executor_job(get_bond_devices, hass, bond)
asyncio.create_task(discover())
covers = [
BondCover(bond, device)
for device in devices
if device.type == BOND_DEVICE_TYPE_MOTORIZED_SHADES
]
async_add_entities(covers)
class BondCover(CoverEntity):

View File

@ -26,13 +26,10 @@ class BondDevice:
return self._attrs["type"]
async def get_bond_devices(hass: HomeAssistant, bond: Bond) -> List[BondDevice]:
def get_bond_devices(hass: HomeAssistant, bond: Bond) -> List[BondDevice]:
"""Fetch all available devices using Bond API."""
device_ids = await hass.async_add_executor_job(bond.getDeviceIds)
device_ids = bond.getDeviceIds()
devices = [
BondDevice(
device_id, await hass.async_add_executor_job(bond.getDevice, device_id)
)
for device_id in device_ids
BondDevice(device_id, bond.getDevice(device_id)) for device_id in device_ids
]
return devices

View File

@ -3,7 +3,6 @@ import logging
from bond import BOND_DEVICE_TYPE_MOTORIZED_SHADES
from homeassistant.components.bond.utils import BondDevice
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
@ -19,19 +18,16 @@ from tests.async_mock import patch
_LOGGER = logging.getLogger(__name__)
TEST_DEVICE_IDS = ["device-1"]
TEST_DEVICE = {"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES}
async def test_entity_registry(hass):
"""Tests that the devices are registered in the entity registry."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE):
await setup_platform(hass, COVER_DOMAIN)
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
@ -42,17 +38,11 @@ async def test_open_cover(hass):
"""Tests that open cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.open") as mock_open:
with patch("homeassistant.components.bond.Bond.open") as mock_open:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
@ -67,17 +57,11 @@ async def test_close_cover(hass):
"""Tests that close cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.close") as mock_close:
with patch("homeassistant.components.bond.Bond.close") as mock_close:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_CLOSE_COVER,
@ -92,17 +76,11 @@ async def test_stop_cover(hass):
"""Tests that stop cover command delegates to API."""
with patch(
"homeassistant.components.bond.utils.get_bond_devices",
return_value=[
BondDevice(
"device-1",
{"name": "name-1", "type": BOND_DEVICE_TYPE_MOTORIZED_SHADES},
)
],
):
"homeassistant.components.bond.Bond.getDeviceIds", return_value=TEST_DEVICE_IDS
), patch("homeassistant.components.bond.Bond.getDevice", return_value=TEST_DEVICE):
await setup_platform(hass, COVER_DOMAIN)
with patch("bond.Bond.hold") as mock_hold:
with patch("homeassistant.components.bond.Bond.hold") as mock_hold:
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_STOP_COVER,

View File

@ -1,24 +0,0 @@
"""Tests for the Bond cover device."""
import logging
from bond import Bond
from homeassistant.components.bond.utils import get_bond_devices
from homeassistant.core import HomeAssistant
from tests.async_mock import patch
_LOGGER = logging.getLogger(__name__)
async def test_get_bond_devices(hass: HomeAssistant):
"""Tests that the querying for devices delegates to API."""
bond: Bond = Bond("1.1.1.1", "test-token")
with patch(
"bond.Bond.getDeviceIds", return_value=["device-1", "device-2"],
) as mock_get_device_ids, patch("bond.Bond.getDevice") as mock_get_device:
await get_bond_devices(hass, bond)
assert len(mock_get_device_ids.mock_calls) == 1
assert len(mock_get_device.mock_calls) == 2