Dynalite Integration (#27841)

* Initial commit

* ran hassfest and gen_requirements_all scripts

* fixes per request from Paulus Schoutsen

* ran gen_requirements_all

* updated library version - removed some debug leftover

* get_requirements again...

* added documentation URL

* ran isort

* changed storage in hass.data[DOMAIN] to use entry_id instead of host

* adopted unit tests to latest fix

* Update const.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
ZivandPaulus Schoutsen authored and GitHub committed 2020-02-10 13:16:04 -08:00
1 parent b78d156f0e
commit 4467409e5c
15 files changed
+672

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the Dynalite component."""
+136
View File
@@ -0,0 +1,136 @@
"""Test Dynalite bridge."""
from unittest.mock import Mock, call, 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
async def test_bridge_setup():
"""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)
with patch.object(
dyn_bridge.dynalite_devices, "async_setup", return_value=mock_coro(True)
):
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"])
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():
"""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)
device1 = Mock()
device1.category = "light"
device2 = Mock()
device2.category = "switch"
dyn_bridge.add_devices([device1, device2])
reg_func = Mock()
dyn_bridge.register_add_entities(reg_func)
reg_func.assert_called_once()
assert reg_func.mock_calls[0][1][0][0].device is device1
async def test_register_then_add_devices():
"""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])
reg_func.assert_called_once()
assert reg_func.mock_calls[0][1][0][0].device 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"
)
+36
View File
@@ -0,0 +1,36 @@
"""Test Dynalite config flow."""
from unittest.mock import Mock, call, patch
from homeassistant.components.dynalite import config_flow
from tests.common import mock_coro
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}
)
+74
View File
@@ -0,0 +1,74 @@
"""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 tests.common import mock_coro
async def test_async_setup():
"""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()
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()
+44
View File
@@ -0,0 +1,44 @@
"""Test Dynalite light."""
from unittest.mock import Mock, call, patch
from homeassistant.components.dynalite import DOMAIN
from homeassistant.components.dynalite.light import DynaliteLight, async_setup_entry
from tests.common import mock_coro
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."""
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()