2020-07-06 01:17:53 +00:00
|
|
|
"""Tests for the Bond cover device."""
|
2020-07-09 23:25:18 +00:00
|
|
|
from datetime import timedelta
|
2020-07-06 01:17:53 +00:00
|
|
|
import logging
|
|
|
|
|
2020-07-12 01:04:07 +00:00
|
|
|
from bond import DeviceTypes
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
from homeassistant import core
|
2020-07-06 01:17:53 +00:00
|
|
|
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
SERVICE_CLOSE_COVER,
|
|
|
|
SERVICE_OPEN_COVER,
|
|
|
|
SERVICE_STOP_COVER,
|
|
|
|
)
|
|
|
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
2020-07-09 23:25:18 +00:00
|
|
|
from homeassistant.util import utcnow
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
from .common import setup_platform
|
|
|
|
|
|
|
|
from tests.async_mock import patch
|
2020-07-09 23:25:18 +00:00
|
|
|
from tests.common import async_fire_time_changed
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
def shades(name: str):
|
|
|
|
"""Create motorized shades with given name."""
|
2020-07-12 01:04:07 +00:00
|
|
|
return {"name": name, "type": DeviceTypes.MOTORIZED_SHADES}
|
2020-07-08 21:28:53 +00:00
|
|
|
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
async def test_entity_registry(hass: core.HomeAssistant):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Tests that the devices are registered in the entity registry."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-06 01:17:53 +00:00
|
|
|
|
|
|
|
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
assert [key for key in registry.entities.keys()] == ["cover.name_1"]
|
|
|
|
|
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
async def test_open_cover(hass: core.HomeAssistant):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Tests that open cover command delegates to API."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-08 21:28:53 +00:00
|
|
|
with patch("homeassistant.components.bond.Bond.open") as mock_open:
|
2020-07-06 01:17:53 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
COVER_DOMAIN,
|
|
|
|
SERVICE_OPEN_COVER,
|
|
|
|
{ATTR_ENTITY_ID: "cover.name_1"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_open.assert_called_once()
|
|
|
|
|
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
async def test_close_cover(hass: core.HomeAssistant):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Tests that close cover command delegates to API."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-08 21:28:53 +00:00
|
|
|
with patch("homeassistant.components.bond.Bond.close") as mock_close:
|
2020-07-06 01:17:53 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
COVER_DOMAIN,
|
|
|
|
SERVICE_CLOSE_COVER,
|
|
|
|
{ATTR_ENTITY_ID: "cover.name_1"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_close.assert_called_once()
|
|
|
|
|
|
|
|
|
2020-07-09 23:25:18 +00:00
|
|
|
async def test_stop_cover(hass: core.HomeAssistant):
|
2020-07-06 01:17:53 +00:00
|
|
|
"""Tests that stop cover command delegates to API."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-06 01:17:53 +00:00
|
|
|
|
2020-07-08 21:28:53 +00:00
|
|
|
with patch("homeassistant.components.bond.Bond.hold") as mock_hold:
|
2020-07-06 01:17:53 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
COVER_DOMAIN,
|
|
|
|
SERVICE_STOP_COVER,
|
|
|
|
{ATTR_ENTITY_ID: "cover.name_1"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_hold.assert_called_once()
|
2020-07-09 23:25:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_open_cover(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct state when Bond API reports cover is open."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-09 23:25:18 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 1}
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("cover.name_1").state == "open"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_closed_cover(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct state when Bond API reports cover is closed."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
2020-07-09 23:25:18 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState", return_value={"open": 0}
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("cover.name_1").state == "closed"
|