Refactor bond integration to be completely async (#38066)
This commit is contained in:
1 parent
15fe727f2c
commit
3480fb6996
16 files changed
+251
-259
No files matched your search
@@ -21,9 +21,7 @@ async def setup_bond_entity(
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.Bond.getVersion", return_value=hub_version
|
||||
):
|
||||
with patch("homeassistant.components.bond.Bond.version", return_value=hub_version):
|
||||
return await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
||||
|
||||
@@ -45,13 +43,15 @@ async def setup_platform(
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.bond.PLATFORMS", [platform]), patch(
|
||||
"homeassistant.components.bond.Bond.getVersion", return_value=MOCK_HUB_VERSION
|
||||
"homeassistant.components.bond.Bond.version", return_value=MOCK_HUB_VERSION
|
||||
), patch_bond_device_ids(return_value=[bond_device_id],), patch(
|
||||
"homeassistant.components.bond.Bond.getDevice", return_value=discovered_device
|
||||
"homeassistant.components.bond.Bond.device", return_value=discovered_device
|
||||
), patch_bond_device_state(
|
||||
return_value={}
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.getProperties", return_value=props
|
||||
"homeassistant.components.bond.Bond.device_properties", return_value=props
|
||||
), patch(
|
||||
"homeassistant.components.bond.Bond.device_state", return_value={}
|
||||
):
|
||||
assert await async_setup_component(hass, BOND_DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
@@ -60,70 +60,25 @@ async def setup_platform(
|
||||
|
||||
|
||||
def patch_bond_device_ids(return_value=None):
|
||||
"""Patch Bond API getDeviceIds command."""
|
||||
"""Patch Bond API devices command."""
|
||||
if return_value is None:
|
||||
return_value = []
|
||||
|
||||
return patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceIds", return_value=return_value,
|
||||
"homeassistant.components.bond.Bond.devices", return_value=return_value,
|
||||
)
|
||||
|
||||
|
||||
def patch_bond_turn_on():
|
||||
"""Patch Bond API turnOn command."""
|
||||
return patch("homeassistant.components.bond.Bond.turnOn")
|
||||
|
||||
|
||||
def patch_bond_turn_off():
|
||||
"""Patch Bond API turnOff command."""
|
||||
return patch("homeassistant.components.bond.Bond.turnOff")
|
||||
|
||||
|
||||
def patch_bond_set_speed():
|
||||
"""Patch Bond API setSpeed command."""
|
||||
return patch("homeassistant.components.bond.Bond.setSpeed")
|
||||
|
||||
|
||||
def patch_bond_set_flame():
|
||||
"""Patch Bond API setFlame command."""
|
||||
return patch("homeassistant.components.bond.Bond.setFlame")
|
||||
|
||||
|
||||
def patch_bond_open():
|
||||
"""Patch Bond API open command."""
|
||||
return patch("homeassistant.components.bond.Bond.open")
|
||||
|
||||
|
||||
def patch_bond_close():
|
||||
"""Patch Bond API close command."""
|
||||
return patch("homeassistant.components.bond.Bond.close")
|
||||
|
||||
|
||||
def patch_bond_hold():
|
||||
"""Patch Bond API hold command."""
|
||||
return patch("homeassistant.components.bond.Bond.hold")
|
||||
|
||||
|
||||
def patch_bond_set_direction():
|
||||
"""Patch Bond API setDirection command."""
|
||||
return patch("homeassistant.components.bond.Bond.setDirection")
|
||||
|
||||
|
||||
def patch_turn_light_on():
|
||||
"""Patch Bond API turnLightOn command."""
|
||||
return patch("homeassistant.components.bond.Bond.turnLightOn")
|
||||
|
||||
|
||||
def patch_turn_light_off():
|
||||
"""Patch Bond API turnLightOff command."""
|
||||
return patch("homeassistant.components.bond.Bond.turnLightOff")
|
||||
def patch_bond_action():
|
||||
"""Patch Bond API action command."""
|
||||
return patch("homeassistant.components.bond.Bond.action")
|
||||
|
||||
|
||||
def patch_bond_device_state(return_value=None):
|
||||
"""Patch Bond API getDeviceState command."""
|
||||
"""Patch Bond API device state endpoint."""
|
||||
if return_value is None:
|
||||
return_value = {}
|
||||
|
||||
return patch(
|
||||
"homeassistant.components.bond.Bond.getDeviceState", return_value=return_value
|
||||
"homeassistant.components.bond.Bond.device_state", return_value=return_value
|
||||
)
|
||||
@@ -1,13 +1,12 @@
|
||||
"""Test the Bond config flow."""
|
||||
from json import JSONDecodeError
|
||||
|
||||
from requests.exceptions import ConnectionError
|
||||
from aiohttp import ClientConnectionError, ClientResponseError
|
||||
|
||||
from homeassistant import config_entries, core, setup
|
||||
from homeassistant.components.bond.const import DOMAIN
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.async_mock import Mock, patch
|
||||
|
||||
|
||||
async def test_form(hass: core.HomeAssistant):
|
||||
@@ -20,7 +19,7 @@ async def test_form(hass: core.HomeAssistant):
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.config_flow.Bond.getDeviceIds", return_value=[],
|
||||
"homeassistant.components.bond.config_flow.Bond.devices", return_value=[],
|
||||
), patch(
|
||||
"homeassistant.components.bond.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
@@ -48,8 +47,8 @@ async def test_form_invalid_auth(hass: core.HomeAssistant):
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.config_flow.Bond.getDeviceIds",
|
||||
side_effect=JSONDecodeError("test-message", "test-doc", 0),
|
||||
"homeassistant.components.bond.config_flow.Bond.devices",
|
||||
side_effect=ClientResponseError(Mock(), Mock(), status=401),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
|
||||
@@ -66,8 +65,8 @@ async def test_form_cannot_connect(hass: core.HomeAssistant):
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.config_flow.Bond.getDeviceIds",
|
||||
side_effect=ConnectionError,
|
||||
"homeassistant.components.bond.config_flow.Bond.devices",
|
||||
side_effect=ClientConnectionError(),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
|
||||
@@ -84,8 +83,8 @@ async def test_form_unexpected_error(hass: core.HomeAssistant):
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.bond.config_flow.Bond.getDeviceIds",
|
||||
side_effect=Exception,
|
||||
"homeassistant.components.bond.config_flow.Bond.devices",
|
||||
side_effect=ClientResponseError(Mock(), Mock(), status=500),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from bond import DeviceTypes
|
||||
from bond_api import Action, DeviceType
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||
@@ -15,13 +15,7 @@ from homeassistant.const import (
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from .common import (
|
||||
patch_bond_close,
|
||||
patch_bond_device_state,
|
||||
patch_bond_hold,
|
||||
patch_bond_open,
|
||||
setup_platform,
|
||||
)
|
||||
from .common import patch_bond_action, patch_bond_device_state, setup_platform
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
@@ -30,7 +24,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def shades(name: str):
|
||||
"""Create motorized shades with given name."""
|
||||
return {"name": name, "type": DeviceTypes.MOTORIZED_SHADES}
|
||||
return {"name": name, "type": DeviceType.MOTORIZED_SHADES}
|
||||
|
||||
|
||||
async def test_entity_registry(hass: core.HomeAssistant):
|
||||
@@ -43,9 +37,11 @@ async def test_entity_registry(hass: core.HomeAssistant):
|
||||
|
||||
async def test_open_cover(hass: core.HomeAssistant):
|
||||
"""Tests that open cover command delegates to API."""
|
||||
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
||||
await setup_platform(
|
||||
hass, COVER_DOMAIN, shades("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_open() as mock_open, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_open, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_OPEN_COVER,
|
||||
@@ -53,14 +49,17 @@ async def test_open_cover(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_open.assert_called_once()
|
||||
|
||||
mock_open.assert_called_once_with("test-device-id", Action.open())
|
||||
|
||||
|
||||
async def test_close_cover(hass: core.HomeAssistant):
|
||||
"""Tests that close cover command delegates to API."""
|
||||
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
||||
await setup_platform(
|
||||
hass, COVER_DOMAIN, shades("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_close() as mock_close, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_close, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_CLOSE_COVER,
|
||||
@@ -68,14 +67,17 @@ async def test_close_cover(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_close.assert_called_once()
|
||||
|
||||
mock_close.assert_called_once_with("test-device-id", Action.close())
|
||||
|
||||
|
||||
async def test_stop_cover(hass: core.HomeAssistant):
|
||||
"""Tests that stop cover command delegates to API."""
|
||||
await setup_platform(hass, COVER_DOMAIN, shades("name-1"))
|
||||
await setup_platform(
|
||||
hass, COVER_DOMAIN, shades("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_hold() as mock_hold, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_hold, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
COVER_DOMAIN,
|
||||
SERVICE_STOP_COVER,
|
||||
@@ -83,7 +85,8 @@ async def test_stop_cover(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_hold.assert_called_once()
|
||||
|
||||
mock_hold.assert_called_once_with("test-device-id", Action.hold())
|
||||
|
||||
|
||||
async def test_update_reports_open_cover(hass: core.HomeAssistant):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Tests for the Bond fan device."""
|
||||
from datetime import timedelta
|
||||
from typing import Optional
|
||||
|
||||
from bond import DeviceTypes, Directions
|
||||
from bond_api import Action, DeviceType, Direction
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components import fan
|
||||
@@ -17,14 +18,7 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from .common import (
|
||||
patch_bond_device_state,
|
||||
patch_bond_set_direction,
|
||||
patch_bond_set_speed,
|
||||
patch_bond_turn_off,
|
||||
patch_bond_turn_on,
|
||||
setup_platform,
|
||||
)
|
||||
from .common import patch_bond_action, patch_bond_device_state, setup_platform
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
@@ -33,18 +27,20 @@ def ceiling_fan(name: str):
|
||||
"""Create a ceiling fan with given name."""
|
||||
return {
|
||||
"name": name,
|
||||
"type": DeviceTypes.CEILING_FAN,
|
||||
"type": DeviceType.CEILING_FAN,
|
||||
"actions": ["SetSpeed", "SetDirection"],
|
||||
}
|
||||
|
||||
|
||||
async def turn_fan_on(hass: core.HomeAssistant, fan_id: str, speed: str) -> None:
|
||||
async def turn_fan_on(
|
||||
hass: core.HomeAssistant, fan_id: str, speed: Optional[str] = None
|
||||
) -> None:
|
||||
"""Turn the fan on at the specified speed."""
|
||||
service_data = {ATTR_ENTITY_ID: fan_id}
|
||||
if speed:
|
||||
service_data[fan.ATTR_SPEED] = speed
|
||||
await hass.services.async_call(
|
||||
FAN_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: fan_id, fan.ATTR_SPEED: speed},
|
||||
blocking=True,
|
||||
FAN_DOMAIN, SERVICE_TURN_ON, service_data=service_data, blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -57,7 +53,7 @@ async def test_entity_registry(hass: core.HomeAssistant):
|
||||
assert [key for key in registry.entities] == ["fan.name_1"]
|
||||
|
||||
|
||||
async def test_entity_non_standard_speed_list(hass: core.HomeAssistant):
|
||||
async def test_non_standard_speed_list(hass: core.HomeAssistant):
|
||||
"""Tests that the device is registered with custom speed list if number of supported speeds differs form 3."""
|
||||
await setup_platform(
|
||||
hass,
|
||||
@@ -76,41 +72,62 @@ async def test_entity_non_standard_speed_list(hass: core.HomeAssistant):
|
||||
]
|
||||
|
||||
with patch_bond_device_state():
|
||||
with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_low:
|
||||
with patch_bond_action() as mock_set_speed_low:
|
||||
await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW)
|
||||
mock_set_speed_low.assert_called_once_with("test-device-id", speed=1)
|
||||
mock_set_speed_low.assert_called_once_with(
|
||||
"test-device-id", Action.set_speed(1)
|
||||
)
|
||||
|
||||
with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_medium:
|
||||
with patch_bond_action() as mock_set_speed_medium:
|
||||
await turn_fan_on(hass, "fan.name_1", fan.SPEED_MEDIUM)
|
||||
mock_set_speed_medium.assert_called_once_with("test-device-id", speed=3)
|
||||
mock_set_speed_medium.assert_called_once_with(
|
||||
"test-device-id", Action.set_speed(3)
|
||||
)
|
||||
|
||||
with patch_bond_turn_on(), patch_bond_set_speed() as mock_set_speed_high:
|
||||
with patch_bond_action() as mock_set_speed_high:
|
||||
await turn_fan_on(hass, "fan.name_1", fan.SPEED_HIGH)
|
||||
mock_set_speed_high.assert_called_once_with("test-device-id", speed=6)
|
||||
mock_set_speed_high.assert_called_once_with(
|
||||
"test-device-id", Action.set_speed(6)
|
||||
)
|
||||
|
||||
|
||||
async def test_turn_on_fan(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to API."""
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
async def test_turn_on_fan_with_speed(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to set speed API."""
|
||||
await setup_platform(
|
||||
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_on() as mock_turn_on, patch_bond_set_speed() as mock_set_speed, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_set_speed, patch_bond_device_state():
|
||||
await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW)
|
||||
|
||||
mock_set_speed.assert_called_once()
|
||||
mock_turn_on.assert_called_once()
|
||||
mock_set_speed.assert_called_with("test-device-id", Action.set_speed(1))
|
||||
|
||||
|
||||
async def test_turn_on_fan_without_speed(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to turn on API."""
|
||||
await setup_platform(
|
||||
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_action() as mock_turn_on, patch_bond_device_state():
|
||||
await turn_fan_on(hass, "fan.name_1")
|
||||
|
||||
mock_turn_on.assert_called_with("test-device-id", Action.turn_on())
|
||||
|
||||
|
||||
async def test_turn_off_fan(hass: core.HomeAssistant):
|
||||
"""Tests that turn off command delegates to API."""
|
||||
await setup_platform(hass, FAN_DOMAIN, ceiling_fan("name-1"))
|
||||
await setup_platform(
|
||||
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_off, patch_bond_device_state():
|
||||
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()
|
||||
mock_turn_off.assert_called_once_with("test-device-id", Action.turn_off())
|
||||
|
||||
|
||||
async def test_update_reports_fan_on(hass: core.HomeAssistant):
|
||||
@@ -139,7 +156,7 @@ 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_bond_device_state(return_value={"direction": Directions.FORWARD}):
|
||||
with patch_bond_device_state(return_value={"direction": Direction.FORWARD}):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -150,7 +167,7 @@ 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_bond_device_state(return_value={"direction": Directions.REVERSE}):
|
||||
with patch_bond_device_state(return_value={"direction": Direction.REVERSE}):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -159,9 +176,11 @@ async def test_update_reports_direction_reverse(hass: core.HomeAssistant):
|
||||
|
||||
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"))
|
||||
await setup_platform(
|
||||
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_set_direction() as mock_set_direction, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_set_direction, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
FAN_DOMAIN,
|
||||
SERVICE_SET_DIRECTION,
|
||||
@@ -169,4 +188,7 @@ async def test_set_fan_direction(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_set_direction.assert_called_once()
|
||||
|
||||
mock_set_direction.assert_called_once_with(
|
||||
"test-device-id", Action.set_direction(Direction.FORWARD)
|
||||
)
|
||||
@@ -2,7 +2,7 @@
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from bond import Actions, DeviceTypes
|
||||
from bond_api import Action, DeviceType
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN
|
||||
@@ -10,15 +10,7 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from .common import (
|
||||
patch_bond_device_state,
|
||||
patch_bond_set_flame,
|
||||
patch_bond_turn_off,
|
||||
patch_bond_turn_on,
|
||||
patch_turn_light_off,
|
||||
patch_turn_light_on,
|
||||
setup_platform,
|
||||
)
|
||||
from .common import patch_bond_action, patch_bond_device_state, setup_platform
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
@@ -29,14 +21,14 @@ def ceiling_fan(name: str):
|
||||
"""Create a ceiling fan (that has built-in light) with given name."""
|
||||
return {
|
||||
"name": name,
|
||||
"type": DeviceTypes.CEILING_FAN,
|
||||
"actions": [Actions.TOGGLE_LIGHT],
|
||||
"type": DeviceType.CEILING_FAN,
|
||||
"actions": [Action.TURN_LIGHT_ON, Action.TURN_LIGHT_OFF],
|
||||
}
|
||||
|
||||
|
||||
def fireplace(name: str):
|
||||
"""Create a fireplace with given name."""
|
||||
return {"name": name, "type": DeviceTypes.FIREPLACE}
|
||||
return {"name": name, "type": DeviceType.FIREPLACE}
|
||||
|
||||
|
||||
async def test_entity_registry(hass: core.HomeAssistant):
|
||||
@@ -49,9 +41,11 @@ async def test_entity_registry(hass: core.HomeAssistant):
|
||||
|
||||
async def test_turn_on_light(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to API."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1"))
|
||||
await setup_platform(
|
||||
hass, LIGHT_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_turn_light_on() as mock_turn_light_on, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_light_on, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
@@ -59,14 +53,17 @@ async def test_turn_on_light(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_turn_light_on.assert_called_once()
|
||||
|
||||
mock_turn_light_on.assert_called_once_with("test-device-id", Action.turn_light_on())
|
||||
|
||||
|
||||
async def test_turn_off_light(hass: core.HomeAssistant):
|
||||
"""Tests that turn off command delegates to API."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN, ceiling_fan("name-1"))
|
||||
await setup_platform(
|
||||
hass, LIGHT_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_turn_light_off() as mock_turn_light_off, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_light_off, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
@@ -74,7 +71,10 @@ async def test_turn_off_light(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_turn_light_off.assert_called_once()
|
||||
|
||||
mock_turn_light_off.assert_called_once_with(
|
||||
"test-device-id", Action.turn_light_off()
|
||||
)
|
||||
|
||||
|
||||
async def test_update_reports_light_is_on(hass: core.HomeAssistant):
|
||||
@@ -99,13 +99,13 @@ async def test_update_reports_light_is_off(hass: core.HomeAssistant):
|
||||
assert hass.states.get("light.name_1").state == "off"
|
||||
|
||||
|
||||
async def test_turn_on_fireplace(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to API."""
|
||||
async def test_turn_on_fireplace_with_brightness(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to set flame API."""
|
||||
await setup_platform(
|
||||
hass, LIGHT_DOMAIN, fireplace("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_on() as mock_turn_on, patch_bond_set_flame() as mock_set_flame, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_set_flame, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
@@ -114,15 +114,34 @@ async def test_turn_on_fireplace(hass: core.HomeAssistant):
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_turn_on.assert_called_once()
|
||||
mock_set_flame.assert_called_once_with("test-device-id", 50)
|
||||
mock_set_flame.assert_called_once_with("test-device-id", Action.set_flame(50))
|
||||
|
||||
|
||||
async def test_turn_on_fireplace_without_brightness(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to turn on API."""
|
||||
await setup_platform(
|
||||
hass, LIGHT_DOMAIN, fireplace("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_action() as mock_turn_on, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: "light.name_1"},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_turn_on.assert_called_once_with("test-device-id", Action.turn_on())
|
||||
|
||||
|
||||
async def test_turn_off_fireplace(hass: core.HomeAssistant):
|
||||
"""Tests that turn off command delegates to API."""
|
||||
await setup_platform(hass, LIGHT_DOMAIN, fireplace("name-1"))
|
||||
await setup_platform(
|
||||
hass, LIGHT_DOMAIN, fireplace("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_off, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
LIGHT_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
@@ -130,7 +149,8 @@ async def test_turn_off_fireplace(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_turn_off.assert_called_once()
|
||||
|
||||
mock_turn_off.assert_called_once_with("test-device-id", Action.turn_off())
|
||||
|
||||
|
||||
async def test_flame_converted_to_brightness(hass: core.HomeAssistant):
|
||||
@@ -141,5 +161,4 @@ async def test_flame_converted_to_brightness(hass: core.HomeAssistant):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
_LOGGER.warning(hass.states.get("light.name_1").attributes)
|
||||
assert hass.states.get("light.name_1").attributes[ATTR_BRIGHTNESS] == 128
|
||||
@@ -2,7 +2,7 @@
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from bond import DeviceTypes
|
||||
from bond_api import Action, DeviceType
|
||||
|
||||
from homeassistant import core
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
@@ -10,12 +10,7 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from .common import (
|
||||
patch_bond_device_state,
|
||||
patch_bond_turn_off,
|
||||
patch_bond_turn_on,
|
||||
setup_platform,
|
||||
)
|
||||
from .common import patch_bond_action, patch_bond_device_state, setup_platform
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
@@ -24,7 +19,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
def generic_device(name: str):
|
||||
"""Create a generic device with given name."""
|
||||
return {"name": name, "type": DeviceTypes.GENERIC_DEVICE}
|
||||
return {"name": name, "type": DeviceType.GENERIC_DEVICE}
|
||||
|
||||
|
||||
async def test_entity_registry(hass: core.HomeAssistant):
|
||||
@@ -37,9 +32,11 @@ async def test_entity_registry(hass: core.HomeAssistant):
|
||||
|
||||
async def test_turn_on_switch(hass: core.HomeAssistant):
|
||||
"""Tests that turn on command delegates to API."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1"))
|
||||
await setup_platform(
|
||||
hass, SWITCH_DOMAIN, generic_device("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_on() as mock_turn_on, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_on, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
@@ -47,14 +44,17 @@ async def test_turn_on_switch(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_turn_on.assert_called_once()
|
||||
|
||||
mock_turn_on.assert_called_once_with("test-device-id", Action.turn_on())
|
||||
|
||||
|
||||
async def test_turn_off_switch(hass: core.HomeAssistant):
|
||||
"""Tests that turn off command delegates to API."""
|
||||
await setup_platform(hass, SWITCH_DOMAIN, generic_device("name-1"))
|
||||
await setup_platform(
|
||||
hass, SWITCH_DOMAIN, generic_device("name-1"), bond_device_id="test-device-id"
|
||||
)
|
||||
|
||||
with patch_bond_turn_off() as mock_turn_off, patch_bond_device_state():
|
||||
with patch_bond_action() as mock_turn_off, patch_bond_device_state():
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
@@ -62,7 +62,8 @@ async def test_turn_off_switch(hass: core.HomeAssistant):
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_turn_off.assert_called_once()
|
||||
|
||||
mock_turn_off.assert_called_once_with("test-device-id", Action.turn_off())
|
||||
|
||||
|
||||
async def test_update_reports_switch_is_on(hass: core.HomeAssistant):
|
||||
|
||||
Reference in new issue
Block a user