Refactor bond integration to be completely async (#38066)

This commit is contained in:
Eugene Prystupa authored and GitHub committed 2020-07-22 20:22:25 -05:00
1 parent 15fe727f2c
commit 3480fb6996
16 files changed
+251 -259

No files matched your search

+47 -28
View File
@@ -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