From 8b8a54b36773831cf3fc56fed98863fe9d995a71 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 18 Mar 2021 11:57:56 +0100 Subject: [PATCH] Improve deCONZ services and scenes tests (#47829) Use patch.dict rather than deep copy to change DECONZ_WEB_REQUEST --- tests/components/deconz/test_scene.py | 34 +++-- tests/components/deconz/test_services.py | 170 ++++++++++++----------- 2 files changed, 105 insertions(+), 99 deletions(-) diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index 229111bf9ae..189eb1e6eb7 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -1,6 +1,6 @@ """deCONZ scene platform tests.""" -from copy import deepcopy +from unittest.mock import patch from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON from homeassistant.const import ATTR_ENTITY_ID @@ -11,18 +11,6 @@ from .test_gateway import ( setup_deconz_integration, ) -GROUPS = { - "1": { - "id": "Light group id", - "name": "Light group", - "type": "LightGroup", - "state": {"all_on": False, "any_on": True}, - "action": {}, - "scenes": [{"id": "1", "name": "Scene"}], - "lights": [], - } -} - async def test_no_scenes(hass, aioclient_mock): """Test that scenes can be loaded without scenes being available.""" @@ -32,11 +20,21 @@ async def test_no_scenes(hass, aioclient_mock): async def test_scenes(hass, aioclient_mock): """Test that scenes works.""" - data = deepcopy(DECONZ_WEB_REQUEST) - data["groups"] = deepcopy(GROUPS) - config_entry = await setup_deconz_integration( - hass, aioclient_mock, get_state_response=data - ) + data = { + "groups": { + "1": { + "id": "Light group id", + "name": "Light group", + "type": "LightGroup", + "state": {"all_on": False, "any_on": True}, + "action": {}, + "scenes": [{"id": "1", "name": "Scene"}], + "lights": [], + } + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) assert len(hass.states.async_all()) == 1 assert hass.states.get("scene.light_group_scene") diff --git a/tests/components/deconz/test_services.py b/tests/components/deconz/test_services.py index 35279572113..a631327351f 100644 --- a/tests/components/deconz/test_services.py +++ b/tests/components/deconz/test_services.py @@ -1,5 +1,4 @@ """deCONZ service tests.""" -from copy import deepcopy from unittest.mock import Mock, patch import pytest @@ -9,7 +8,6 @@ from homeassistant.components.deconz.const import ( CONF_BRIDGE_ID, DOMAIN as DECONZ_DOMAIN, ) -from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.components.deconz.services import ( DECONZ_SERVICES, SERVICE_CONFIGURE_DEVICE, @@ -33,50 +31,6 @@ from .test_gateway import ( setup_deconz_integration, ) -GROUP = { - "1": { - "id": "Group 1 id", - "name": "Group 1 name", - "type": "LightGroup", - "state": {}, - "action": {}, - "scenes": [{"id": "1", "name": "Scene 1"}], - "lights": ["1"], - } -} - -LIGHT = { - "1": { - "id": "Light 1 id", - "name": "Light 1 name", - "state": {"reachable": True}, - "type": "Light", - "uniqueid": "00:00:00:00:00:00:00:01-00", - } -} - -SENSOR = { - "1": { - "id": "Sensor 1 id", - "name": "Sensor 1 name", - "type": "ZHALightLevel", - "state": {"lightlevel": 30000, "dark": False}, - "config": {"reachable": True}, - "uniqueid": "00:00:00:00:00:00:00:02-00", - } -} - -SWITCH = { - "1": { - "id": "Switch 1 id", - "name": "Switch 1", - "type": "ZHASwitch", - "state": {"buttonevent": 1000, "gesture": 1}, - "config": {"battery": 100}, - "uniqueid": "00:00:00:00:00:00:00:03-00", - }, -} - async def test_service_setup(hass): """Verify service setup works.""" @@ -140,10 +94,19 @@ async def test_configure_service_with_field(hass, aioclient_mock): async def test_configure_service_with_entity(hass, aioclient_mock): """Test that service invokes pydeconz with the correct path and data.""" - config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway = get_gateway_from_config_entry(hass, config_entry) + data = { + "lights": { + "1": { + "name": "Test", + "state": {"reachable": True}, + "type": "Light", + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway.deconz_ids["light.test"] = "/lights/1" data = { SERVICE_ENTITY: "light.test", SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, @@ -159,10 +122,19 @@ async def test_configure_service_with_entity(hass, aioclient_mock): async def test_configure_service_with_entity_and_field(hass, aioclient_mock): """Test that service invokes pydeconz with the correct path and data.""" - config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway = get_gateway_from_config_entry(hass, config_entry) + data = { + "lights": { + "1": { + "name": "Test", + "state": {"reachable": True}, + "type": "Light", + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway.deconz_ids["light.test"] = "/lights/1" data = { SERVICE_ENTITY: "light.test", SERVICE_FIELD: "/state", @@ -193,57 +165,93 @@ async def test_configure_service_with_faulty_field(hass, aioclient_mock): async def test_configure_service_with_faulty_entity(hass, aioclient_mock): """Test that service invokes pydeconz with the correct path and data.""" await setup_deconz_integration(hass, aioclient_mock) + aioclient_mock.clear_requests() data = { SERVICE_ENTITY: "light.nonexisting", SERVICE_DATA: {}, } - with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: - await hass.services.async_call( - DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data - ) - await hass.async_block_till_done() - put_state.assert_not_called() + await hass.services.async_call( + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data + ) + await hass.async_block_till_done() + + assert len(aioclient_mock.mock_calls) == 0 async def test_service_refresh_devices(hass, aioclient_mock): """Test that service can refresh devices.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway = get_gateway_from_config_entry(hass, config_entry) + + assert len(hass.states.async_all()) == 0 + aioclient_mock.clear_requests() - data = {CONF_BRIDGE_ID: BRIDGEID} + data = { + "groups": { + "1": { + "id": "Group 1 id", + "name": "Group 1 name", + "type": "LightGroup", + "state": {}, + "action": {}, + "scenes": [{"id": "1", "name": "Scene 1"}], + "lights": ["1"], + } + }, + "lights": { + "1": { + "name": "Light 1 name", + "state": {"reachable": True}, + "type": "Light", + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + }, + "sensors": { + "1": { + "name": "Sensor 1 name", + "type": "ZHALightLevel", + "state": {"lightlevel": 30000, "dark": False}, + "config": {"reachable": True}, + "uniqueid": "00:00:00:00:00:00:00:02-00", + } + }, + } - mock_deconz_request( - aioclient_mock, - config_entry.data, - {"groups": GROUP, "lights": LIGHT, "sensors": SENSOR}, - ) + mock_deconz_request(aioclient_mock, config_entry.data, data) await hass.services.async_call( - DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data=data + DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data={CONF_BRIDGE_ID: BRIDGEID} ) await hass.async_block_till_done() - assert gateway.deconz_ids == { - "light.group_1_name": "/groups/1", - "light.light_1_name": "/lights/1", - "scene.group_1_name_scene_1": "/groups/1/scenes/1", - "sensor.sensor_1_name": "/sensors/1", - } + assert len(hass.states.async_all()) == 4 async def test_remove_orphaned_entries_service(hass, aioclient_mock): """Test service works and also don't remove more than expected.""" - data = deepcopy(DECONZ_WEB_REQUEST) - data["lights"] = deepcopy(LIGHT) - data["sensors"] = deepcopy(SWITCH) - config_entry = await setup_deconz_integration( - hass, aioclient_mock, get_state_response=data - ) - - data = {CONF_BRIDGE_ID: BRIDGEID} + data = { + "lights": { + "1": { + "name": "Light 1 name", + "state": {"reachable": True}, + "type": "Light", + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + }, + "sensors": { + "1": { + "name": "Switch 1", + "type": "ZHASwitch", + "state": {"buttonevent": 1000, "gesture": 1}, + "config": {"battery": 100}, + "uniqueid": "00:00:00:00:00:00:00:03-00", + }, + }, + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) device_registry = dr.async_get(hass) device = device_registry.async_get_or_create( @@ -279,7 +287,7 @@ async def test_remove_orphaned_entries_service(hass, aioclient_mock): await hass.services.async_call( DECONZ_DOMAIN, SERVICE_REMOVE_ORPHANED_ENTRIES, - service_data=data, + service_data={CONF_BRIDGE_ID: BRIDGEID}, ) await hass.async_block_till_done()