2020-07-11 00:23:35 +00:00
|
|
|
"""Tests for the Bond fan device."""
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2020-07-12 20:30:24 +00:00
|
|
|
from bond import DeviceTypes, Directions
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
from homeassistant import core
|
|
|
|
from homeassistant.components import fan
|
2020-07-12 20:30:24 +00:00
|
|
|
from homeassistant.components.fan import (
|
|
|
|
ATTR_DIRECTION,
|
|
|
|
DIRECTION_FORWARD,
|
|
|
|
DIRECTION_REVERSE,
|
|
|
|
DOMAIN as FAN_DOMAIN,
|
|
|
|
SERVICE_SET_DIRECTION,
|
|
|
|
)
|
2020-07-11 00:23:35 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
|
|
|
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
|
|
|
from homeassistant.util import utcnow
|
|
|
|
|
|
|
|
from ...common import async_fire_time_changed
|
|
|
|
from .common import setup_platform
|
|
|
|
|
|
|
|
from tests.async_mock import patch
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
|
|
|
|
def ceiling_fan(name: str):
|
|
|
|
"""Create a ceiling fan with given name."""
|
|
|
|
return {
|
|
|
|
"name": name,
|
2020-07-12 01:04:07 +00:00
|
|
|
"type": DeviceTypes.CEILING_FAN,
|
2020-07-12 20:30:24 +00:00
|
|
|
"actions": ["SetSpeed", "SetDirection"],
|
2020-07-11 01:20:50 +00:00
|
|
|
}
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_registry(hass: core.HomeAssistant):
|
|
|
|
"""Tests that the devices are registered in the entity registry."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry()
|
2020-07-13 21:07:35 +00:00
|
|
|
assert [key for key in registry.entities] == ["fan.name_1"]
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_turn_on_fan(hass: core.HomeAssistant):
|
|
|
|
"""Tests that turn on command delegates to API."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
with patch("homeassistant.components.bond.Bond.turnOn") as mock_turn_on, patch(
|
|
|
|
"homeassistant.components.bond.Bond.setSpeed"
|
|
|
|
) as mock_set_speed:
|
|
|
|
await hass.services.async_call(
|
|
|
|
FAN_DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: "fan.name_1", fan.ATTR_SPEED: fan.SPEED_LOW},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
mock_set_speed.assert_called_once()
|
|
|
|
mock_turn_on.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_turn_off_fan(hass: core.HomeAssistant):
|
|
|
|
"""Tests that turn off command delegates to API."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
with patch("homeassistant.components.bond.Bond.turnOff") as mock_turn_off:
|
|
|
|
await hass.services.async_call(
|
|
|
|
FAN_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "fan.name_1"}, blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_turn_off.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_fan_on(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct state when Bond API reports fan power is on."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState",
|
|
|
|
return_value={"power": 1, "speed": 1},
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("fan.name_1").state == "on"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_fan_off(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct state when Bond API reports fan power is off."""
|
2020-07-11 01:20:50 +00:00
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState",
|
|
|
|
return_value={"power": 0, "speed": 1},
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("fan.name_1").state == "off"
|
2020-07-12 20:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_direction_forward(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct direction when Bond API reports fan direction is forward."""
|
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState",
|
|
|
|
return_value={"direction": Directions.FORWARD},
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("fan.name_1").attributes[ATTR_DIRECTION] == DIRECTION_FORWARD
|
|
|
|
|
|
|
|
|
|
|
|
async def test_update_reports_direction_reverse(hass: core.HomeAssistant):
|
|
|
|
"""Tests that update command sets correct direction when Bond API reports fan direction is reverse."""
|
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.bond.Bond.getDeviceState",
|
|
|
|
return_value={"direction": Directions.REVERSE},
|
|
|
|
):
|
|
|
|
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("fan.name_1").attributes[ATTR_DIRECTION] == DIRECTION_REVERSE
|
|
|
|
|
|
|
|
|
|
|
|
async def test_set_fan_direction(hass: core.HomeAssistant):
|
|
|
|
"""Tests that set direction command delegates to API."""
|
|
|
|
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
|
|
|
|
|
|
|
with patch("homeassistant.components.bond.Bond.setDirection") as mock_set_direction:
|
|
|
|
await hass.services.async_call(
|
|
|
|
FAN_DOMAIN,
|
|
|
|
SERVICE_SET_DIRECTION,
|
|
|
|
{ATTR_ENTITY_ID: "fan.name_1", ATTR_DIRECTION: DIRECTION_FORWARD},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
mock_set_direction.assert_called_once()
|