Refactor dynalite integration for multi-platform (#32335)
* refactoring for multi platform * adopted test_bridge to refactoring * refactoring tests for multi-platform additional coverage in config and init * comment for clarity * more specific imports from lib * library version bump * removed async_update * changed parameter order to start with hass * removed pylint disable * unsubscribe from signal dispatcher inherit from Entity * use device.unique_id * changed hass_obj to hass * added test for remove entity bug fix from the test * removed the polling try_connect. hate polling... it is now part of the async_setup() significantly makes the code clearer and simplifies the tests * removed leftover debug logs in the library * changed tests to get the entry_id from hass * changed place to assign hass.data only after success * fixes for test_init * removed assert * removed device_info * removed bridge internal from common * modified test_bridge to work without the bridge directly * removed bridge from test_existing_update * changed update to not use bridge internals * dyn_bridge fixture no longer used - removed
This commit is contained in:
14 files changed
+348
-280
No files matched your search
@@ -1,9 +1,64 @@
|
||||
"""Common functions for the Dynalite tests."""
|
||||
"""Common functions for tests."""
|
||||
from asynctest import CoroutineMock, Mock, call, patch
|
||||
|
||||
from homeassistant.components import dynalite
|
||||
from homeassistant.helpers import entity_registry
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ATTR_SERVICE = "service"
|
||||
ATTR_METHOD = "method"
|
||||
ATTR_ARGS = "args"
|
||||
|
||||
|
||||
def get_bridge_from_hass(hass_obj):
|
||||
"""Get the bridge from hass.data."""
|
||||
key = next(iter(hass_obj.data[dynalite.DOMAIN]))
|
||||
return hass_obj.data[dynalite.DOMAIN][key]
|
||||
def create_mock_device(platform, spec):
|
||||
"""Create a dynalite mock device for a platform according to a spec."""
|
||||
device = Mock(spec=spec)
|
||||
device.category = platform
|
||||
device.unique_id = "UNIQUE"
|
||||
device.name = "NAME"
|
||||
device.device_class = "Device Class"
|
||||
return device
|
||||
|
||||
|
||||
async def get_entry_id_from_hass(hass):
|
||||
"""Get the config entry id from hass."""
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
assert ent_reg
|
||||
conf_entries = hass.config_entries.async_entries(dynalite.DOMAIN)
|
||||
assert len(conf_entries) == 1
|
||||
return conf_entries[0].entry_id
|
||||
|
||||
|
||||
async def create_entity_from_device(hass, device):
|
||||
"""Set up the component and platform and create a light based on the device provided."""
|
||||
host = "1.2.3.4"
|
||||
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host})
|
||||
entry.add_to_hass(hass)
|
||||
with patch(
|
||||
"homeassistant.components.dynalite.bridge.DynaliteDevices"
|
||||
) as mock_dyn_dev:
|
||||
mock_dyn_dev().async_setup = CoroutineMock(return_value=True)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
new_device_func = mock_dyn_dev.mock_calls[1][2]["newDeviceFunc"]
|
||||
new_device_func([device])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def run_service_tests(hass, device, platform, services):
|
||||
"""Run a series of service calls and check that the entity and device behave correctly."""
|
||||
for cur_item in services:
|
||||
service = cur_item[ATTR_SERVICE]
|
||||
args = cur_item.get(ATTR_ARGS, {})
|
||||
service_data = {"entity_id": f"{platform}.name", **args}
|
||||
await hass.services.async_call(platform, service, service_data, blocking=True)
|
||||
await hass.async_block_till_done()
|
||||
for check_item in services:
|
||||
check_method = getattr(device, check_item[ATTR_METHOD])
|
||||
if check_item[ATTR_SERVICE] == service:
|
||||
check_method.assert_called_once()
|
||||
assert check_method.mock_calls == [call(**args)]
|
||||
check_method.reset_mock()
|
||||
else:
|
||||
check_method.assert_not_called()
|
||||
Reference in new issue
Block a user