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,78 +1,49 @@
|
||||
"""Test Dynalite light."""
|
||||
from unittest.mock import Mock
|
||||
|
||||
from asynctest import CoroutineMock, patch
|
||||
from dynalite_devices_lib.light import DynaliteChannelLightDevice
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import dynalite
|
||||
from homeassistant.components.light import SUPPORT_BRIGHTNESS
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .common import (
|
||||
ATTR_METHOD,
|
||||
ATTR_SERVICE,
|
||||
create_entity_from_device,
|
||||
create_mock_device,
|
||||
get_entry_id_from_hass,
|
||||
run_service_tests,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device():
|
||||
"""Mock a Dynalite device."""
|
||||
device = Mock()
|
||||
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()
|
||||
return create_mock_device("light", DynaliteChannelLightDevice)
|
||||
|
||||
|
||||
async def test_light_setup(hass, mock_device):
|
||||
"""Test a successful setup."""
|
||||
await create_light_from_device(hass, mock_device)
|
||||
await create_entity_from_device(hass, mock_device)
|
||||
entity_state = hass.states.get("light.name")
|
||||
assert entity_state.attributes["friendly_name"] == mock_device.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 run_service_tests(
|
||||
hass,
|
||||
mock_device,
|
||||
"light",
|
||||
[
|
||||
{ATTR_SERVICE: "turn_on", ATTR_METHOD: "async_turn_on"},
|
||||
{ATTR_SERVICE: "turn_off", ATTR_METHOD: "async_turn_off"},
|
||||
],
|
||||
)
|
||||
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
|
||||
)
|
||||
async def test_remove_entity(hass, mock_device):
|
||||
"""Test when an entity is removed from HA."""
|
||||
await create_entity_from_device(hass, mock_device)
|
||||
assert hass.states.get("light.name")
|
||||
entry_id = await get_entry_id_from_hass(hass)
|
||||
assert await hass.config_entries.async_unload(entry_id)
|
||||
await hass.async_block_till_done()
|
||||
mock_device.async_turn_off.assert_awaited_once()
|
||||
assert not hass.states.get("light.name")
|
||||
Reference in new issue
Block a user