From cbb4324c84cc20633d6cf7cc07992ffeb445a4f8 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 6 Oct 2020 23:25:57 +0200 Subject: [PATCH] Refactor deCONZ tests (#41362) * Use hass.config_entries.async_unload * Return config_entry from setup_deconz_integration --- tests/components/deconz/test_binary_sensor.py | 25 +++++++------ tests/components/deconz/test_climate.py | 18 ++++++---- tests/components/deconz/test_config_flow.py | 28 +++++++-------- tests/components/deconz/test_cover.py | 6 ++-- tests/components/deconz/test_deconz_event.py | 6 ++-- .../components/deconz/test_device_trigger.py | 7 ++-- tests/components/deconz/test_gateway.py | 36 ++++++++++--------- tests/components/deconz/test_init.py | 21 ++++++----- tests/components/deconz/test_light.py | 12 ++++--- tests/components/deconz/test_lock.py | 6 ++-- tests/components/deconz/test_scene.py | 6 ++-- tests/components/deconz/test_sensor.py | 18 ++++++---- tests/components/deconz/test_services.py | 32 ++++++++--------- tests/components/deconz/test_switch.py | 11 +++--- 14 files changed, 130 insertions(+), 102 deletions(-) diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index fdb0b62d5ea..f3a5cede08e 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -7,6 +7,7 @@ from homeassistant.components.binary_sensor import ( DEVICE_CLASS_MOTION, DEVICE_CLASS_VIBRATION, ) +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.helpers.entity_registry import async_entries_for_config_entry from homeassistant.setup import async_setup_component @@ -74,7 +75,8 @@ async def test_binary_sensors(hass): """Test successful creation of binary sensor entities.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 3 presence_sensor = hass.states.get("binary_sensor.presence_sensor") @@ -98,7 +100,7 @@ async def test_binary_sensors(hass): assert hass.states.get("binary_sensor.presence_sensor").state == "on" - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 @@ -107,7 +109,7 @@ async def test_allow_clip_sensor(hass): """Test that CLIP sensors can be allowed.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration( + config_entry = await setup_deconz_integration( hass, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, @@ -122,7 +124,7 @@ async def test_allow_clip_sensor(hass): # Disallow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -132,7 +134,7 @@ async def test_allow_clip_sensor(hass): # Allow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() @@ -142,7 +144,8 @@ async def test_allow_clip_sensor(hass): async def test_add_new_binary_sensor(hass): """Test that adding a new binary sensor works.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = deconz.gateway.get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { @@ -161,10 +164,11 @@ async def test_add_new_binary_sensor(hass): async def test_add_new_binary_sensor_ignored(hass): """Test that adding a new binary sensor is not allowed.""" - gateway = await setup_deconz_integration( + config_entry = await setup_deconz_integration( hass, options={deconz.gateway.CONF_ALLOW_NEW_DEVICES: False}, ) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { @@ -181,10 +185,5 @@ async def test_add_new_binary_sensor_ignored(hass): entity_registry = await hass.helpers.entity_registry.async_get_registry() assert ( - len( - async_entries_for_config_entry( - entity_registry, gateway.config_entry.entry_id - ) - ) - == 0 + len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 0 ) diff --git a/tests/components/deconz/test_climate.py b/tests/components/deconz/test_climate.py index bc6bc9415f3..3eb893d8a6e 100644 --- a/tests/components/deconz/test_climate.py +++ b/tests/components/deconz/test_climate.py @@ -3,6 +3,7 @@ from copy import deepcopy from homeassistant.components import deconz import homeassistant.components.climate as climate +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -56,7 +57,8 @@ async def test_climate_devices(hass): """Test successful creation of sensor entities.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 2 assert hass.states.get("climate.thermostat").state == "auto" @@ -166,7 +168,7 @@ async def test_climate_devices(hass): "put", "/sensors/1/config", json={"heatsetpoint": 2000.0} ) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 @@ -175,7 +177,7 @@ async def test_clip_climate_device(hass): """Test successful creation of sensor entities.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration( + config_entry = await setup_deconz_integration( hass, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, @@ -190,7 +192,7 @@ async def test_clip_climate_device(hass): # Disallow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -200,7 +202,7 @@ async def test_clip_climate_device(hass): # Allow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() @@ -212,7 +214,8 @@ async def test_verify_state_update(hass): """Test that state update properly.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert hass.states.get("climate.thermostat").state == "auto" @@ -232,7 +235,8 @@ async def test_verify_state_update(hass): async def test_add_new_climate_device(hass): """Test that adding a new climate device works.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 8e46308ccd7..70894e8926a 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -199,7 +199,7 @@ async def test_manual_configuration_after_discovery_ResponseError(hass, aioclien async def test_manual_configuration_update_configuration(hass, aioclient_mock): """Test that manual configuration can update existing config entry.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) aioclient_mock.get( pydeconz.utils.URL_DISCOVER, @@ -240,7 +240,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" + assert config_entry.data[CONF_HOST] == "2.3.4.5" async def test_manual_configuration_dont_update_configuration(hass, aioclient_mock): @@ -436,7 +436,7 @@ async def test_ssdp_discovery_not_deconz_bridge(hass): async def test_ssdp_discovery_update_configuration(hass): """Test if a discovered bridge is configured but updates with new attributes.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) with patch( "homeassistant.components.deconz.async_setup_entry", @@ -455,13 +455,13 @@ async def test_ssdp_discovery_update_configuration(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" + assert config_entry.data[CONF_HOST] == "2.3.4.5" assert len(mock_setup_entry.mock_calls) == 1 async def test_ssdp_discovery_dont_update_configuration(hass): """Test if a discovered bridge has already been configured.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) result = await hass.config_entries.flow.async_init( DOMAIN, @@ -475,12 +475,12 @@ async def test_ssdp_discovery_dont_update_configuration(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[CONF_HOST] == "1.2.3.4" + assert config_entry.data[CONF_HOST] == "1.2.3.4" async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass): """Test to ensure the SSDP discovery does not update an Hass.io entry.""" - gateway = await setup_deconz_integration(hass, source="hassio") + config_entry = await setup_deconz_integration(hass, source="hassio") result = await hass.config_entries.flow.async_init( DOMAIN, @@ -494,7 +494,7 @@ async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[CONF_HOST] == "1.2.3.4" + assert config_entry.data[CONF_HOST] == "1.2.3.4" async def test_flow_hassio_discovery(hass): @@ -537,7 +537,7 @@ async def test_flow_hassio_discovery(hass): async def test_hassio_discovery_update_configuration(hass): """Test we can update an existing config entry.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) with patch( "homeassistant.components.deconz.async_setup_entry", @@ -557,9 +557,9 @@ async def test_hassio_discovery_update_configuration(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - assert gateway.config_entry.data[CONF_HOST] == "2.3.4.5" - assert gateway.config_entry.data[CONF_PORT] == 8080 - assert gateway.config_entry.data[CONF_API_KEY] == "updated" + assert config_entry.data[CONF_HOST] == "2.3.4.5" + assert config_entry.data[CONF_PORT] == 8080 + assert config_entry.data[CONF_API_KEY] == "updated" assert len(mock_setup_entry.mock_calls) == 1 @@ -584,9 +584,9 @@ async def test_hassio_discovery_dont_update_configuration(hass): async def test_option_flow(hass): """Test config flow options.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) - result = await hass.config_entries.options.async_init(gateway.config_entry.entry_id) + result = await hass.config_entries.options.async_init(config_entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "deconz_devices" diff --git a/tests/components/deconz/test_cover.py b/tests/components/deconz/test_cover.py index 9e294c09cf2..799917b26fd 100644 --- a/tests/components/deconz/test_cover.py +++ b/tests/components/deconz/test_cover.py @@ -3,6 +3,7 @@ from copy import deepcopy from homeassistant.components import deconz import homeassistant.components.cover as cover +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -73,7 +74,8 @@ async def test_cover(hass): """Test that all supported cover entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(COVERS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 5 assert hass.states.get("cover.level_controllable_cover").state == "open" @@ -161,6 +163,6 @@ async def test_cover(hass): assert deconz_old_brightness_cover.state == "closed" assert deconz_old_brightness_cover.attributes["current_position"] == 0 - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_deconz_event.py b/tests/components/deconz/test_deconz_event.py index e7e584af679..252fd8c3062 100644 --- a/tests/components/deconz/test_deconz_event.py +++ b/tests/components/deconz/test_deconz_event.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -55,7 +56,8 @@ async def test_deconz_events(hass): """Test successful creation of deconz events.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 3 assert len(gateway.events) == 5 @@ -112,7 +114,7 @@ async def test_deconz_events(hass): "xy": [0.5982, 0.3897], } - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 assert len(gateway.events) == 0 diff --git a/tests/components/deconz/test_device_trigger.py b/tests/components/deconz/test_device_trigger.py index 61962b00fe6..5c59579fde9 100644 --- a/tests/components/deconz/test_device_trigger.py +++ b/tests/components/deconz/test_device_trigger.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components.deconz import device_trigger +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -34,7 +35,8 @@ async def test_get_triggers(hass): """Test triggers work.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) device_id = gateway.events[0].device_id triggers = await async_get_device_automations(hass, "trigger", device_id) @@ -97,7 +99,8 @@ async def test_helper_successful(hass): """Verify trigger helper.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) device_id = gateway.events[0].device_id deconz_event = device_trigger._get_deconz_event_from_device_id(hass, device_id) assert deconz_event == gateway.events[0] diff --git a/tests/components/deconz/test_gateway.py b/tests/components/deconz/test_gateway.py index e4dc0424f83..11516b36eca 100644 --- a/tests/components/deconz/test_gateway.py +++ b/tests/components/deconz/test_gateway.py @@ -6,6 +6,7 @@ import pytest from homeassistant import config_entries from homeassistant.components import deconz, ssdp +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.helpers.dispatcher import async_dispatcher_connect from tests.async_mock import Mock, patch @@ -66,8 +67,7 @@ async def setup_deconz_integration( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - bridgeid = get_state_response["config"]["bridgeid"] - return hass.data[deconz.DOMAIN].get(bridgeid) + return config_entry async def test_gateway_setup(hass): @@ -76,7 +76,8 @@ async def test_gateway_setup(hass): "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ) as forward_entry_setup: - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.bridgeid == BRIDGEID assert gateway.master is True assert gateway.option_allow_clip_sensor is False @@ -86,15 +87,14 @@ async def test_gateway_setup(hass): assert len(gateway.deconz_ids) == 0 assert len(hass.states.async_all()) == 0 - entry = gateway.config_entry - assert forward_entry_setup.mock_calls[0][1] == (entry, "binary_sensor") - assert forward_entry_setup.mock_calls[1][1] == (entry, "climate") - assert forward_entry_setup.mock_calls[2][1] == (entry, "cover") - assert forward_entry_setup.mock_calls[3][1] == (entry, "light") - assert forward_entry_setup.mock_calls[4][1] == (entry, "lock") - assert forward_entry_setup.mock_calls[5][1] == (entry, "scene") - assert forward_entry_setup.mock_calls[6][1] == (entry, "sensor") - assert forward_entry_setup.mock_calls[7][1] == (entry, "switch") + assert forward_entry_setup.mock_calls[0][1] == (config_entry, "binary_sensor") + assert forward_entry_setup.mock_calls[1][1] == (config_entry, "climate") + assert forward_entry_setup.mock_calls[2][1] == (config_entry, "cover") + assert forward_entry_setup.mock_calls[3][1] == (config_entry, "light") + assert forward_entry_setup.mock_calls[4][1] == (config_entry, "lock") + assert forward_entry_setup.mock_calls[5][1] == (config_entry, "scene") + assert forward_entry_setup.mock_calls[6][1] == (config_entry, "sensor") + assert forward_entry_setup.mock_calls[7][1] == (config_entry, "switch") async def test_gateway_retry(hass): @@ -112,13 +112,15 @@ async def test_gateway_setup_fails(hass): with patch( "homeassistant.components.deconz.gateway.get_gateway", side_effect=Exception ): - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway is None async def test_connection_status_signalling(hass): """Make sure that connection status triggers a dispatcher send.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) event_call = Mock() unsub = async_dispatcher_connect(hass, gateway.signal_reachable, event_call) @@ -134,7 +136,8 @@ async def test_connection_status_signalling(hass): async def test_update_address(hass): """Make sure that connection status triggers a dispatcher send.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.api.host == "1.2.3.4" with patch( @@ -159,7 +162,8 @@ async def test_update_address(hass): async def test_reset_after_successful_setup(hass): """Make sure that connection status triggers a dispatcher send.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) result = await gateway.async_reset() await hass.async_block_till_done() diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index 8d9d387c91c..8fd29ec2368 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -3,6 +3,7 @@ import asyncio from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -47,7 +48,8 @@ async def test_setup_entry_no_available_bridge(hass): async def test_setup_entry_successful(hass): """Test setup entry is successful.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert hass.data[deconz.DOMAIN] assert gateway.bridgeid in hass.data[deconz.DOMAIN] @@ -56,13 +58,15 @@ async def test_setup_entry_successful(hass): async def test_setup_entry_multiple_gateways(hass): """Test setup entry is successful with multiple gateways.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) data = deepcopy(DECONZ_WEB_REQUEST) data["config"]["bridgeid"] = "01234E56789B" - gateway2 = await setup_deconz_integration( + config_entry2 = await setup_deconz_integration( hass, get_state_response=data, entry_id="2" ) + gateway2 = get_gateway_from_config_entry(hass, config_entry2) assert len(hass.data[deconz.DOMAIN]) == 2 assert hass.data[deconz.DOMAIN][gateway.bridgeid].master @@ -71,26 +75,27 @@ async def test_setup_entry_multiple_gateways(hass): async def test_unload_entry(hass): """Test being able to unload an entry.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) assert hass.data[deconz.DOMAIN] - assert await deconz.async_unload_entry(hass, gateway.config_entry) + assert await deconz.async_unload_entry(hass, config_entry) assert not hass.data[deconz.DOMAIN] async def test_unload_entry_multiple_gateways(hass): """Test being able to unload an entry and master gateway gets moved.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) data = deepcopy(DECONZ_WEB_REQUEST) data["config"]["bridgeid"] = "01234E56789B" - gateway2 = await setup_deconz_integration( + config_entry2 = await setup_deconz_integration( hass, get_state_response=data, entry_id="2" ) + gateway2 = get_gateway_from_config_entry(hass, config_entry2) assert len(hass.data[deconz.DOMAIN]) == 2 - assert await deconz.async_unload_entry(hass, gateway.config_entry) + assert await deconz.async_unload_entry(hass, config_entry) assert len(hass.data[deconz.DOMAIN]) == 1 assert hass.data[deconz.DOMAIN][gateway2.bridgeid].master diff --git a/tests/components/deconz/test_light.py b/tests/components/deconz/test_light.py index e2adf45d9f2..00a3f49477e 100644 --- a/tests/components/deconz/test_light.py +++ b/tests/components/deconz/test_light.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry import homeassistant.components.light as light from homeassistant.setup import async_setup_component @@ -101,7 +102,8 @@ async def test_lights_and_groups(hass): data = deepcopy(DECONZ_WEB_REQUEST) data["groups"] = deepcopy(GROUPS) data["lights"] = deepcopy(LIGHTS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 6 @@ -258,7 +260,7 @@ async def test_lights_and_groups(hass): "put", "/lights/1/state", json={"alert": "lselect"} ) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 @@ -268,7 +270,7 @@ async def test_disable_light_groups(hass): data = deepcopy(DECONZ_WEB_REQUEST) data["groups"] = deepcopy(GROUPS) data["lights"] = deepcopy(LIGHTS) - gateway = await setup_deconz_integration( + config_entry = await setup_deconz_integration( hass, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False}, get_state_response=data, @@ -281,7 +283,7 @@ async def test_disable_light_groups(hass): assert hass.states.get("light.empty_group") is None hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: True} + config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: True} ) await hass.async_block_till_done() @@ -289,7 +291,7 @@ async def test_disable_light_groups(hass): assert hass.states.get("light.light_group") hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False} + config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False} ) await hass.async_block_till_done() diff --git a/tests/components/deconz/test_lock.py b/tests/components/deconz/test_lock.py index 3452dcaa0ff..cb7267aaada 100644 --- a/tests/components/deconz/test_lock.py +++ b/tests/components/deconz/test_lock.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry import homeassistant.components.lock as lock from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED from homeassistant.setup import async_setup_component @@ -48,7 +49,8 @@ async def test_locks(hass): """Test that all supported lock entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(LOCKS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 1 assert hass.states.get("lock.door_lock").state == STATE_UNLOCKED @@ -96,6 +98,6 @@ async def test_locks(hass): await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": False}) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index fa1d02a4ce6..4717ccb0ba8 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry import homeassistant.components.scene as scene from homeassistant.setup import async_setup_component @@ -43,7 +44,8 @@ async def test_scenes(hass): """Test that scenes works.""" data = deepcopy(DECONZ_WEB_REQUEST) data["groups"] = deepcopy(GROUPS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 1 assert hass.states.get("scene.light_group_scene") @@ -61,6 +63,6 @@ async def test_scenes(hass): await hass.async_block_till_done() set_callback.assert_called_with("put", "/groups/1/scenes/1/recall", json={}) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_sensor.py b/tests/components/deconz/test_sensor.py index 31799e2519a..5cfb01ddf9d 100644 --- a/tests/components/deconz/test_sensor.py +++ b/tests/components/deconz/test_sensor.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry import homeassistant.components.sensor as sensor from homeassistant.const import ( DEVICE_CLASS_BATTERY, @@ -101,7 +102,8 @@ async def test_sensors(hass): """Test successful creation of sensor entities.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 5 @@ -157,7 +159,7 @@ async def test_sensors(hass): assert hass.states.get("sensor.switch_2_battery_level").state == "75" - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 @@ -166,7 +168,7 @@ async def test_allow_clip_sensors(hass): """Test that CLIP sensors can be allowed.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) - gateway = await setup_deconz_integration( + config_entry = await setup_deconz_integration( hass, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, @@ -178,7 +180,7 @@ async def test_allow_clip_sensors(hass): # Disallow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -188,7 +190,7 @@ async def test_allow_clip_sensors(hass): # Allow clip sensors hass.config_entries.async_update_entry( - gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() @@ -198,7 +200,8 @@ async def test_allow_clip_sensors(hass): async def test_add_new_sensor(hass): """Test that adding a new sensor works.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { @@ -219,7 +222,8 @@ async def test_add_battery_later(hass): """Test that a sensor without an initial battery state creates a battery sensor once state exist.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = {"1": deepcopy(SENSORS["3"])} - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) remote = gateway.api.sensors["1"] assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_services.py b/tests/components/deconz/test_services.py index cfbc8cae56c..e8cd1c23dc9 100644 --- a/tests/components/deconz/test_services.py +++ b/tests/components/deconz/test_services.py @@ -7,6 +7,7 @@ import voluptuous as vol from homeassistant.components import deconz from homeassistant.components.deconz.const import CONF_BRIDGE_ID +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.helpers.entity_registry import async_entries_for_config_entry @@ -123,7 +124,8 @@ async def test_configure_service_with_field(hass): async def test_configure_service_with_entity(hass): """Test that service invokes pydeconz with the correct path and data.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) gateway.deconz_ids["light.test"] = "/light/1" data = { @@ -143,7 +145,8 @@ async def test_configure_service_with_entity(hass): async def test_configure_service_with_entity_and_field(hass): """Test that service invokes pydeconz with the correct path and data.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) gateway.deconz_ids["light.test"] = "/light/1" data = { @@ -194,7 +197,8 @@ async def test_configure_service_with_faulty_entity(hass): async def test_service_refresh_devices(hass): """Test that service can refresh devices.""" - gateway = await setup_deconz_integration(hass) + config_entry = await setup_deconz_integration(hass) + gateway = get_gateway_from_config_entry(hass, config_entry) data = {CONF_BRIDGE_ID: BRIDGEID} @@ -220,13 +224,13 @@ async def test_remove_orphaned_entries_service(hass): data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(LIGHT) data["sensors"] = deepcopy(SWITCH) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) data = {CONF_BRIDGE_ID: BRIDGEID} device_registry = await hass.helpers.device_registry.async_get_registry() device = device_registry.async_get_or_create( - config_entry_id=gateway.config_entry.entry_id, identifiers={("mac", "123")} + config_entry_id=config_entry.entry_id, identifiers={("mac", "123")} ) assert ( @@ -234,7 +238,7 @@ async def test_remove_orphaned_entries_service(hass): [ entry for entry in device_registry.devices.values() - if gateway.config_entry.entry_id in entry.config_entries + if config_entry.entry_id in entry.config_entries ] ) == 4 # Gateway, light, switch and orphan @@ -246,16 +250,12 @@ async def test_remove_orphaned_entries_service(hass): deconz.DOMAIN, "12345", suggested_object_id="Orphaned sensor", - config_entry=gateway.config_entry, + config_entry=config_entry, device_id=device.id, ) assert ( - len( - async_entries_for_config_entry( - entity_registry, gateway.config_entry.entry_id - ) - ) + len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 3 # Light, switch battery and orphan ) @@ -271,17 +271,13 @@ async def test_remove_orphaned_entries_service(hass): [ entry for entry in device_registry.devices.values() - if gateway.config_entry.entry_id in entry.config_entries + if config_entry.entry_id in entry.config_entries ] ) == 3 # Gateway, light and switch ) assert ( - len( - async_entries_for_config_entry( - entity_registry, gateway.config_entry.entry_id - ) - ) + len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 2 # Light and switch battery ) diff --git a/tests/components/deconz/test_switch.py b/tests/components/deconz/test_switch.py index f83a3273a0c..e4d5dcea590 100644 --- a/tests/components/deconz/test_switch.py +++ b/tests/components/deconz/test_switch.py @@ -2,6 +2,7 @@ from copy import deepcopy from homeassistant.components import deconz +from homeassistant.components.deconz.gateway import get_gateway_from_config_entry import homeassistant.components.switch as switch from homeassistant.setup import async_setup_component @@ -79,7 +80,8 @@ async def test_power_plugs(hass): """Test that all supported switch entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(POWER_PLUGS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 4 assert hass.states.get("switch.on_off_switch").state == "on" @@ -130,7 +132,7 @@ async def test_power_plugs(hass): await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": False}) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0 @@ -139,7 +141,8 @@ async def test_sirens(hass): """Test that siren entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(SIRENS) - gateway = await setup_deconz_integration(hass, get_state_response=data) + config_entry = await setup_deconz_integration(hass, get_state_response=data) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 2 assert hass.states.get("switch.warning_device").state == "on" @@ -192,6 +195,6 @@ async def test_sirens(hass): "put", "/lights/1/state", json={"alert": "none"} ) - await gateway.async_reset() + await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0