Use fixtures in deCONZ gateway tests (#121305)

This commit is contained in:
Robert Svensson 2024-07-05 16:59:10 +02:00 committed by GitHub
parent 4b4b17e774
commit 8c459959ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
"""Test deCONZ gateway.""" """Test deCONZ gateway."""
from collections.abc import Callable
from copy import deepcopy from copy import deepcopy
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import patch
@ -34,7 +35,12 @@ from homeassistant.components.ssdp import (
ATTR_UPNP_UDN, ATTR_UPNP_UDN,
) )
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, SOURCE_USER from homeassistant.config_entries import (
SOURCE_HASSIO,
SOURCE_SSDP,
SOURCE_USER,
ConfigEntry,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_API_KEY, CONF_API_KEY,
CONF_HOST, CONF_HOST,
@ -134,8 +140,8 @@ async def setup_deconz_integration(
async def test_gateway_setup( async def test_gateway_setup(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
config_entry_factory: Callable[[], ConfigEntry],
) -> None: ) -> None:
"""Successful setup.""" """Successful setup."""
# Patching async_forward_entry_setup* is not advisable, and should be refactored # Patching async_forward_entry_setup* is not advisable, and should be refactored
@ -144,7 +150,7 @@ async def test_gateway_setup(
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setups", "homeassistant.config_entries.ConfigEntries.async_forward_entry_setups",
return_value=True, return_value=True,
) as forward_entry_setup: ) as forward_entry_setup:
config_entry = await setup_deconz_integration(hass, aioclient_mock) config_entry = await config_entry_factory()
gateway = DeconzHub.get_hub(hass, config_entry) gateway = DeconzHub.get_hub(hass, config_entry)
assert gateway.bridgeid == BRIDGEID assert gateway.bridgeid == BRIDGEID
assert gateway.master is True assert gateway.master is True
@ -183,10 +189,11 @@ async def test_gateway_setup(
assert gateway_entry.entry_type is dr.DeviceEntryType.SERVICE assert gateway_entry.entry_type is dr.DeviceEntryType.SERVICE
@pytest.mark.parametrize("config_entry_source", [SOURCE_HASSIO])
async def test_gateway_device_configuration_url_when_addon( async def test_gateway_device_configuration_url_when_addon(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
config_entry_factory: Callable[[], ConfigEntry],
) -> None: ) -> None:
"""Successful setup.""" """Successful setup."""
# Patching async_forward_entry_setup* is not advisable, and should be refactored # Patching async_forward_entry_setup* is not advisable, and should be refactored
@ -195,9 +202,7 @@ async def test_gateway_device_configuration_url_when_addon(
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setups", "homeassistant.config_entries.ConfigEntries.async_forward_entry_setups",
return_value=True, return_value=True,
): ):
config_entry = await setup_deconz_integration( config_entry = await config_entry_factory()
hass, aioclient_mock, source=SOURCE_HASSIO
)
gateway = DeconzHub.get_hub(hass, config_entry) gateway = DeconzHub.get_hub(hass, config_entry)
gateway_entry = device_registry.async_get_device( gateway_entry = device_registry.async_get_device(
@ -209,12 +214,10 @@ async def test_gateway_device_configuration_url_when_addon(
) )
async def test_connection_status_signalling( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket "sensor_payload",
) -> None: [
"""Make sure that connection status triggers a dispatcher send.""" {
data = {
"sensors": {
"1": { "1": {
"name": "presence", "name": "presence",
"type": "ZHAPresence", "type": "ZHAPresence",
@ -223,10 +226,13 @@ async def test_connection_status_signalling(
"uniqueid": "00:00:00:00:00:00:00:00-00", "uniqueid": "00:00:00:00:00:00:00:00-00",
} }
} }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
await setup_deconz_integration(hass, aioclient_mock) @pytest.mark.usefixtures("config_entry_setup")
async def test_connection_status_signalling(
hass: HomeAssistant, mock_deconz_websocket
) -> None:
"""Make sure that connection status triggers a dispatcher send."""
assert hass.states.get("binary_sensor.presence").state == STATE_OFF assert hass.states.get("binary_sensor.presence").state == STATE_OFF
await mock_deconz_websocket(state=State.RETRYING) await mock_deconz_websocket(state=State.RETRYING)
@ -241,11 +247,10 @@ async def test_connection_status_signalling(
async def test_update_address( async def test_update_address(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None: ) -> None:
"""Make sure that connection status triggers a dispatcher send.""" """Make sure that connection status triggers a dispatcher send."""
config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = DeconzHub.get_hub(hass, config_entry_setup)
gateway = DeconzHub.get_hub(hass, config_entry)
assert gateway.api.host == "1.2.3.4" assert gateway.api.host == "1.2.3.4"
with patch( with patch(
@ -273,11 +278,10 @@ async def test_update_address(
async def test_reset_after_successful_setup( async def test_reset_after_successful_setup(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None: ) -> None:
"""Make sure that connection status triggers a dispatcher send.""" """Make sure that connection status triggers a dispatcher send."""
config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = DeconzHub.get_hub(hass, config_entry_setup)
gateway = DeconzHub.get_hub(hass, config_entry)
result = await gateway.async_reset() result = await gateway.async_reset()
await hass.async_block_till_done() await hass.async_block_till_done()