Skip to content

Commit

Permalink
Apply more suggestions from bond code review (home-assistant#37592)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ctalkington authored Jul 8, 2020
1 parent c1de781 commit fb420d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 77 deletions.
19 changes: 8 additions & 11 deletions homeassistant/components/bond/cover.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Support for Bond covers."""
import asyncio
import logging
from typing import Any, Callable, Dict, List, Optional

Expand All @@ -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)

covers = [
BondCover(bond, device)
for device in devices
if device.type == BOND_DEVICE_TYPE_MOTORIZED_SHADES
]

asyncio.create_task(discover())
async_add_entities(covers)


class BondCover(CoverEntity):
Expand Down
9 changes: 3 additions & 6 deletions homeassistant/components/bond/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ def type(self):
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
50 changes: 14 additions & 36 deletions tests/components/bond/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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,
Expand All @@ -19,19 +18,16 @@

_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()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
24 changes: 0 additions & 24 deletions tests/components/bond/test_utils.py

This file was deleted.

0 comments on commit fb420d5

Please sign in to comment.