Enhance Dynalite Integration after review (#31760)
* fixes per Martin Hjelmare * pylint fix * final fixes per request * fixed unit tests for new config flow * Added unit-tests to increase coverage. at 97% now * Added unit tests for 100% coverage of component * removed configured_host function and updated config_flow unit tests * added a pylint directive since it tells me by mistake DOMAIN is not used * fixed path (removed __init__) * Update homeassistant/components/dynalite/light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/dynalite/light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * fixed the test as we moved from schedule_update_... to async_schedule * Update homeassistant/components/dynalite/bridge.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * removed context from config_flow changed test_init to use the core methods * moved test_light to also use the core interfaces * moved to config_entries.async_unload * additional fixes for the tests * pylint fix and removed unnecessary code * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * added break in loop * removed last mock_coro reference pylint fix * added coverage for try_connect * added check for a successful connection before bridge.async_setup succeeds also added a "nowait" config option (default False) that avoids this check * changed log level * fixed accidental chmod I did * fixed accidental change * not storing config in bridge * not patching asyncio * moved CONFIG_SCHEMA into component * moved all logs to start capitalized (and revised some of them) * moved test_config_flow to not patch the DynaliteBridge * also took DynaliteBridge patching out of test_init * removed NO_WAIT * fixes to SCHEMA * changed _ in multi-word CONF moved imports to component const.py * removed tries * removed redundant tests * fixed some small change i broke in the library. only version update * fixed rewuirements * Update tests/components/dynalite/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_light.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/dynalite/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * removed HIDDEN_ENTITY removed hass in test fixture * black fixes * removed final piece of hidden_entity from light fix in the library updated config flow so if the entry is already set but with a different config, calls async_update_entry * removed DATA_CONFIGS - no longer necessary * pylint fixes * added coverage * use abort in config_flow * test update * removed logs * test that update actually updates the entry Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
fbe7ec7b11
commit
a102728997
12 files changed
+442
-413
No files matched your search
@@ -1,136 +1,81 @@
|
||||
"""Test Dynalite bridge."""
|
||||
from unittest.mock import Mock, call, patch
|
||||
from unittest.mock import Mock, call
|
||||
|
||||
from asynctest import patch
|
||||
from dynalite_lib import CONF_ALL
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.dynalite import DATA_CONFIGS, DOMAIN
|
||||
from homeassistant.components.dynalite.bridge import BridgeError, DynaliteBridge
|
||||
|
||||
from tests.common import mock_coro
|
||||
from homeassistant.components import dynalite
|
||||
|
||||
|
||||
async def test_bridge_setup():
|
||||
@pytest.fixture
|
||||
def dyn_bridge():
|
||||
"""Define a basic mock bridge."""
|
||||
hass = Mock()
|
||||
host = "1.2.3.4"
|
||||
bridge = dynalite.DynaliteBridge(hass, {dynalite.CONF_HOST: host})
|
||||
return bridge
|
||||
|
||||
|
||||
async def test_update_device(dyn_bridge):
|
||||
"""Test a successful setup."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {host: {}}}}
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
async_dispatch = Mock()
|
||||
|
||||
with patch.object(
|
||||
dyn_bridge.dynalite_devices, "async_setup", return_value=mock_coro(True)
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.async_dispatcher_send", async_dispatch
|
||||
):
|
||||
assert await dyn_bridge.async_setup() is True
|
||||
|
||||
forward_entries = set(
|
||||
c[1][1] for c in hass.config_entries.async_forward_entry_setup.mock_calls
|
||||
)
|
||||
hass.config_entries.async_forward_entry_setup.assert_called_once()
|
||||
assert forward_entries == set(["light"])
|
||||
dyn_bridge.update_device(CONF_ALL)
|
||||
async_dispatch.assert_called_once()
|
||||
assert async_dispatch.mock_calls[0] == call(
|
||||
dyn_bridge.hass, f"dynalite-update-{dyn_bridge.host}"
|
||||
)
|
||||
async_dispatch.reset_mock()
|
||||
device = Mock
|
||||
device.unique_id = "abcdef"
|
||||
dyn_bridge.update_device(device)
|
||||
async_dispatch.assert_called_once()
|
||||
assert async_dispatch.mock_calls[0] == call(
|
||||
dyn_bridge.hass, f"dynalite-update-{dyn_bridge.host}-{device.unique_id}"
|
||||
)
|
||||
|
||||
|
||||
async def test_invalid_host():
|
||||
"""Test without host in hass.data."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {}}}
|
||||
|
||||
dyn_bridge = None
|
||||
with pytest.raises(BridgeError):
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
assert dyn_bridge is None
|
||||
|
||||
|
||||
async def test_add_devices_then_register():
|
||||
async def test_add_devices_then_register(dyn_bridge):
|
||||
"""Test that add_devices work."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {host: {}}}}
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
|
||||
# First test empty
|
||||
dyn_bridge.add_devices_when_registered([])
|
||||
assert not dyn_bridge.waiting_devices
|
||||
# Now with devices
|
||||
device1 = Mock()
|
||||
device1.category = "light"
|
||||
device2 = Mock()
|
||||
device2.category = "switch"
|
||||
dyn_bridge.add_devices([device1, device2])
|
||||
dyn_bridge.add_devices_when_registered([device1, device2])
|
||||
reg_func = Mock()
|
||||
dyn_bridge.register_add_entities(reg_func)
|
||||
dyn_bridge.register_add_devices(reg_func)
|
||||
reg_func.assert_called_once()
|
||||
assert reg_func.mock_calls[0][1][0][0].device is device1
|
||||
assert reg_func.mock_calls[0][1][0][0] is device1
|
||||
|
||||
|
||||
async def test_register_then_add_devices():
|
||||
async def test_register_then_add_devices(dyn_bridge):
|
||||
"""Test that add_devices work after register_add_entities."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {host: {}}}}
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
|
||||
device1 = Mock()
|
||||
device1.category = "light"
|
||||
device2 = Mock()
|
||||
device2.category = "switch"
|
||||
reg_func = Mock()
|
||||
dyn_bridge.register_add_entities(reg_func)
|
||||
dyn_bridge.add_devices([device1, device2])
|
||||
dyn_bridge.register_add_devices(reg_func)
|
||||
dyn_bridge.add_devices_when_registered([device1, device2])
|
||||
reg_func.assert_called_once()
|
||||
assert reg_func.mock_calls[0][1][0][0].device is device1
|
||||
assert reg_func.mock_calls[0][1][0][0] is device1
|
||||
|
||||
|
||||
async def test_update_device():
|
||||
"""Test the update_device callback."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {host: {}}}}
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
with patch.object(dyn_bridge, "dynalite_devices") as devices_mock:
|
||||
# Single device update
|
||||
device1 = Mock()
|
||||
device1.unique_id = "testing1"
|
||||
device2 = Mock()
|
||||
device2.unique_id = "testing2"
|
||||
dyn_bridge.all_entities = {
|
||||
device1.unique_id: device1,
|
||||
device2.unique_id: device2,
|
||||
}
|
||||
dyn_bridge.update_device(device1)
|
||||
device1.try_schedule_ha.assert_called_once()
|
||||
device2.try_schedule_ha.assert_not_called()
|
||||
# connected to network - all devices update
|
||||
devices_mock.available = True
|
||||
dyn_bridge.update_device(CONF_ALL)
|
||||
assert device1.try_schedule_ha.call_count == 2
|
||||
device2.try_schedule_ha.assert_called_once()
|
||||
# disconnected from network - all devices update
|
||||
devices_mock.available = False
|
||||
dyn_bridge.update_device(CONF_ALL)
|
||||
assert device1.try_schedule_ha.call_count == 3
|
||||
assert device2.try_schedule_ha.call_count == 2
|
||||
|
||||
|
||||
async def test_async_reset():
|
||||
"""Test async_reset."""
|
||||
hass = Mock()
|
||||
hass.config_entries.async_forward_entry_unload = Mock(
|
||||
return_value=mock_coro(Mock())
|
||||
)
|
||||
entry = Mock()
|
||||
host = "1.2.3.4"
|
||||
entry.data = {"host": host}
|
||||
hass.data = {DOMAIN: {DATA_CONFIGS: {host: {}}}}
|
||||
dyn_bridge = DynaliteBridge(hass, entry)
|
||||
await dyn_bridge.async_reset()
|
||||
hass.config_entries.async_forward_entry_unload.assert_called_once()
|
||||
assert hass.config_entries.async_forward_entry_unload.mock_calls[0] == call(
|
||||
entry, "light"
|
||||
)
|
||||
async def test_try_connection(dyn_bridge):
|
||||
"""Test that try connection works."""
|
||||
# successful
|
||||
with patch.object(dyn_bridge.dynalite_devices, "connected", True):
|
||||
assert await dyn_bridge.try_connection()
|
||||
# unsuccessful
|
||||
with patch.object(dyn_bridge.dynalite_devices, "connected", False), patch(
|
||||
"homeassistant.components.dynalite.bridge.CONNECT_INTERVAL", 0
|
||||
):
|
||||
assert not await dyn_bridge.try_connection()
|
||||
@@ -1,36 +1,90 @@
|
||||
"""Test Dynalite config flow."""
|
||||
from unittest.mock import Mock, call, patch
|
||||
from asynctest import patch
|
||||
|
||||
from homeassistant.components.dynalite import config_flow
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import dynalite
|
||||
|
||||
from tests.common import mock_coro
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_step_import():
|
||||
"""Test a successful setup."""
|
||||
flow_handler = config_flow.DynaliteFlowHandler()
|
||||
with patch.object(flow_handler, "context", create=True):
|
||||
with patch.object(flow_handler, "hass", create=True) as mock_hass:
|
||||
with patch.object(
|
||||
flow_handler, "async_create_entry", create=True
|
||||
) as mock_create:
|
||||
host = "1.2.3.4"
|
||||
entry1 = Mock()
|
||||
entry1.data = {"host": host}
|
||||
entry2 = Mock()
|
||||
entry2.data = {"host": "5.5"}
|
||||
mock_hass.config_entries.async_entries = Mock(
|
||||
return_value=[entry1, entry2]
|
||||
)
|
||||
mock_hass.config_entries.async_remove = Mock(
|
||||
return_value=mock_coro(Mock())
|
||||
)
|
||||
await flow_handler.async_step_import({"host": "1.2.3.4"})
|
||||
mock_hass.config_entries.async_remove.assert_called_once()
|
||||
assert mock_hass.config_entries.async_remove.mock_calls[0] == call(
|
||||
entry1.entry_id
|
||||
)
|
||||
mock_create.assert_called_once()
|
||||
assert mock_create.mock_calls[0] == call(
|
||||
title=host, data={"host": host}
|
||||
)
|
||||
async def run_flow(hass, setup, connection):
|
||||
"""Run a flow with or without errors and return result."""
|
||||
host = "1.2.3.4"
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
|
||||
return_value=setup,
|
||||
), patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.available", connection
|
||||
), patch(
|
||||
"homeassistant.components.dynalite.bridge.CONNECT_INTERVAL", 0
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
dynalite.DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={dynalite.CONF_HOST: host},
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
async def test_flow_works(hass):
|
||||
"""Test a successful config flow."""
|
||||
result = await run_flow(hass, True, True)
|
||||
assert result["type"] == "create_entry"
|
||||
|
||||
|
||||
async def test_flow_setup_fails(hass):
|
||||
"""Test a flow where async_setup fails."""
|
||||
result = await run_flow(hass, False, True)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "bridge_setup_failed"
|
||||
|
||||
|
||||
async def test_flow_no_connection(hass):
|
||||
"""Test a flow where connection times out."""
|
||||
result = await run_flow(hass, True, False)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "no_connection"
|
||||
|
||||
|
||||
async def test_existing(hass):
|
||||
"""Test when the entry exists with the same config."""
|
||||
host = "1.2.3.4"
|
||||
MockConfigEntry(
|
||||
domain=dynalite.DOMAIN, unique_id=host, data={dynalite.CONF_HOST: host}
|
||||
).add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.available", True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
dynalite.DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={dynalite.CONF_HOST: host},
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_existing_update(hass):
|
||||
"""Test when the entry exists with the same config."""
|
||||
host = "1.2.3.4"
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=dynalite.DOMAIN, unique_id=host, data={dynalite.CONF_HOST: host}
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.available", True
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
dynalite.DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={dynalite.CONF_HOST: host, "aaa": "bbb"},
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_entry.data.get("aaa") == "bbb"
|
||||
@@ -1,74 +1,62 @@
|
||||
"""Test Dynalite __init__."""
|
||||
from unittest.mock import Mock, call, patch
|
||||
|
||||
from homeassistant.components.dynalite import DATA_CONFIGS, DOMAIN, LOGGER
|
||||
from homeassistant.components.dynalite.__init__ import (
|
||||
async_setup,
|
||||
async_setup_entry,
|
||||
async_unload_entry,
|
||||
)
|
||||
from asynctest import patch
|
||||
|
||||
from tests.common import mock_coro
|
||||
from homeassistant.components import dynalite
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_async_setup():
|
||||
async def test_empty_config(hass):
|
||||
"""Test with an empty config."""
|
||||
assert await async_setup_component(hass, dynalite.DOMAIN, {}) is True
|
||||
assert len(hass.config_entries.flow.async_progress()) == 0
|
||||
assert hass.data[dynalite.DOMAIN] == {}
|
||||
|
||||
|
||||
async def test_async_setup(hass):
|
||||
"""Test a successful setup."""
|
||||
new_host = "1.2.3.4"
|
||||
old_host = "5.6.7.8"
|
||||
hass = Mock()
|
||||
hass.data = {}
|
||||
config = {DOMAIN: {"bridges": [{"host": old_host}, {"host": new_host}]}}
|
||||
mock_conf_host = Mock(return_value=[old_host])
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.__init__.configured_hosts", mock_conf_host
|
||||
):
|
||||
await async_setup(hass, config)
|
||||
mock_conf_host.assert_called_once()
|
||||
assert mock_conf_host.mock_calls[0] == call(hass)
|
||||
assert hass.data[DOMAIN][DATA_CONFIGS] == {
|
||||
new_host: {"host": new_host},
|
||||
old_host: {"host": old_host},
|
||||
}
|
||||
hass.async_create_task.assert_called_once()
|
||||
|
||||
|
||||
async def test_async_setup_entry():
|
||||
"""Test setup of an entry."""
|
||||
|
||||
def async_mock(mock):
|
||||
"""Return the return value of a mock from async."""
|
||||
|
||||
async def async_func(*args, **kwargs):
|
||||
return mock()
|
||||
|
||||
return async_func
|
||||
|
||||
host = "1.2.3.4"
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
entry.data = {"host": host}
|
||||
hass.data = {}
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][DATA_CONFIGS] = {host: {}}
|
||||
mock_async_setup = Mock(return_value=True)
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.__init__.DynaliteBridge.async_setup",
|
||||
async_mock(mock_async_setup),
|
||||
):
|
||||
assert await async_setup_entry(hass, entry)
|
||||
mock_async_setup.assert_called_once()
|
||||
"dynalite_devices_lib.DynaliteDevices.async_setup", return_value=True
|
||||
), patch("dynalite_devices_lib.DynaliteDevices.available", True):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
dynalite.DOMAIN,
|
||||
{dynalite.DOMAIN: {dynalite.CONF_BRIDGES: [{dynalite.CONF_HOST: host}]}},
|
||||
)
|
||||
|
||||
assert len(hass.data[dynalite.DOMAIN]) == 1
|
||||
|
||||
|
||||
async def test_async_unload_entry():
|
||||
"""Test unloading of an entry."""
|
||||
hass = Mock()
|
||||
mock_bridge = Mock()
|
||||
mock_bridge.async_reset.return_value = mock_coro(True)
|
||||
entry = Mock()
|
||||
hass.data = {}
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][entry.entry_id] = mock_bridge
|
||||
await async_unload_entry(hass, entry)
|
||||
LOGGER.error("XXX calls=%s", mock_bridge.mock_calls)
|
||||
mock_bridge.async_reset.assert_called_once()
|
||||
assert mock_bridge.mock_calls[0] == call.async_reset()
|
||||
async def test_async_setup_failed(hass):
|
||||
"""Test a setup when DynaliteBridge.async_setup fails."""
|
||||
host = "1.2.3.4"
|
||||
with patch("dynalite_devices_lib.DynaliteDevices.async_setup", return_value=False):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
dynalite.DOMAIN,
|
||||
{dynalite.DOMAIN: {dynalite.CONF_BRIDGES: [{dynalite.CONF_HOST: host}]}},
|
||||
)
|
||||
assert hass.data[dynalite.DOMAIN] == {}
|
||||
|
||||
|
||||
async def test_unload_entry(hass):
|
||||
"""Test being able to unload an entry."""
|
||||
host = "1.2.3.4"
|
||||
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={"host": host})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"dynalite_devices_lib.DynaliteDevices.async_setup", return_value=True
|
||||
), patch("dynalite_devices_lib.DynaliteDevices.available", True):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
dynalite.DOMAIN,
|
||||
{dynalite.DOMAIN: {dynalite.CONF_BRIDGES: [{dynalite.CONF_HOST: host}]}},
|
||||
)
|
||||
assert hass.data[dynalite.DOMAIN].get(entry.entry_id)
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
assert not hass.data[dynalite.DOMAIN].get(entry.entry_id)
|
||||
@@ -1,44 +1,78 @@
|
||||
"""Test Dynalite light."""
|
||||
from unittest.mock import Mock, call, patch
|
||||
from unittest.mock import Mock
|
||||
|
||||
from homeassistant.components.dynalite import DOMAIN
|
||||
from homeassistant.components.dynalite.light import DynaliteLight, async_setup_entry
|
||||
from asynctest import CoroutineMock, patch
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_coro
|
||||
from homeassistant.components import dynalite
|
||||
from homeassistant.components.light import SUPPORT_BRIGHTNESS
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_light_setup():
|
||||
"""Test a successful setup."""
|
||||
hass = Mock()
|
||||
entry = Mock()
|
||||
async_add = Mock()
|
||||
bridge = Mock()
|
||||
hass.data = {DOMAIN: {entry.entry_id: bridge}}
|
||||
await async_setup_entry(hass, entry, async_add)
|
||||
bridge.register_add_entities.assert_called_once()
|
||||
assert bridge.register_add_entities.mock_calls[0] == call(async_add)
|
||||
|
||||
|
||||
async def test_light():
|
||||
"""Test the light entity."""
|
||||
@pytest.fixture
|
||||
def mock_device():
|
||||
"""Mock a Dynalite device."""
|
||||
device = Mock()
|
||||
device.async_turn_on = Mock(return_value=mock_coro(Mock()))
|
||||
device.async_turn_off = Mock(return_value=mock_coro(Mock()))
|
||||
bridge = Mock()
|
||||
dyn_light = DynaliteLight(device, bridge)
|
||||
assert dyn_light.name is device.name
|
||||
assert dyn_light.unique_id is device.unique_id
|
||||
assert dyn_light.available is device.available
|
||||
assert dyn_light.hidden is device.hidden
|
||||
await dyn_light.async_update() # does nothing
|
||||
assert dyn_light.device_info is device.device_info
|
||||
assert dyn_light.brightness is device.brightness
|
||||
assert dyn_light.is_on is device.is_on
|
||||
await dyn_light.async_turn_on(aaa="bbb")
|
||||
assert device.async_turn_on.mock_calls[0] == call(aaa="bbb")
|
||||
await dyn_light.async_turn_off(ccc="ddd")
|
||||
assert device.async_turn_off.mock_calls[0] == call(ccc="ddd")
|
||||
with patch.object(dyn_light, "hass"):
|
||||
with patch.object(dyn_light, "schedule_update_ha_state") as update_ha:
|
||||
dyn_light.try_schedule_ha()
|
||||
update_ha.assert_called_once()
|
||||
device.category = "light"
|
||||
device.unique_id = "UNIQUE"
|
||||
device.name = "NAME"
|
||||
device.device_info = {
|
||||
"identifiers": {(dynalite.DOMAIN, device.unique_id)},
|
||||
"name": device.name,
|
||||
"manufacturer": "Dynalite",
|
||||
}
|
||||
return device
|
||||
|
||||
|
||||
async def create_light_from_device(hass, device):
|
||||
"""Set up the component and platform and create a light based on the device provided."""
|
||||
host = "1.2.3.4"
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.async_setup",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices.available", True
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
dynalite.DOMAIN,
|
||||
{dynalite.DOMAIN: {dynalite.CONF_BRIDGES: [{dynalite.CONF_HOST: host}]}},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
# Find the bridge
|
||||
bridge = None
|
||||
assert len(hass.data[dynalite.DOMAIN]) == 1
|
||||
key = next(iter(hass.data[dynalite.DOMAIN]))
|
||||
bridge = hass.data[dynalite.DOMAIN][key]
|
||||
bridge.dynalite_devices.newDeviceFunc([device])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_light_setup(hass, mock_device):
|
||||
"""Test a successful setup."""
|
||||
await create_light_from_device(hass, mock_device)
|
||||
entity_state = hass.states.get("light.name")
|
||||
assert entity_state.attributes["brightness"] == mock_device.brightness
|
||||
assert entity_state.attributes["supported_features"] == SUPPORT_BRIGHTNESS
|
||||
|
||||
|
||||
async def test_turn_on(hass, mock_device):
|
||||
"""Test turning a light on."""
|
||||
mock_device.async_turn_on = CoroutineMock(return_value=True)
|
||||
await create_light_from_device(hass, mock_device)
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": "light.name"}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_device.async_turn_on.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_turn_off(hass, mock_device):
|
||||
"""Test turning a light off."""
|
||||
mock_device.async_turn_off = CoroutineMock(return_value=True)
|
||||
await create_light_from_device(hass, mock_device)
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": "light.name"}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_device.async_turn_off.assert_awaited_once()
|
||||
Reference in new issue
Block a user