diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index e9e452e6d731d..2d1f321fcd112 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -33,7 +33,7 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .test_gateway import API_KEY, BRIDGEID +from .conftest import API_KEY, BRIDGEID from tests.test_util.aiohttp import AiohttpClientMocker diff --git a/tests/components/deconz/test_gateway.py b/tests/components/deconz/test_gateway.py index 514c9b37b6aa7..1e3f09ecec505 100644 --- a/tests/components/deconz/test_gateway.py +++ b/tests/components/deconz/test_gateway.py @@ -1,8 +1,6 @@ """Test deCONZ gateway.""" from collections.abc import Callable -from copy import deepcopy -from typing import Any from unittest.mock import patch import pydeconz @@ -35,107 +33,12 @@ ATTR_UPNP_UDN, ) from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN -from homeassistant.config_entries import ( - SOURCE_HASSIO, - SOURCE_SSDP, - SOURCE_USER, - ConfigEntry, -) -from homeassistant.const import ( - CONF_API_KEY, - CONF_HOST, - CONF_PORT, - CONTENT_TYPE_JSON, - STATE_OFF, - STATE_UNAVAILABLE, -) +from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, ConfigEntry +from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr -from homeassistant.helpers.typing import UNDEFINED, UndefinedType - -from .conftest import API_KEY, BRIDGEID, HOST, PORT - -from tests.common import MockConfigEntry -from tests.test_util.aiohttp import AiohttpClientMocker - -DEFAULT_URL = f"http://{HOST}:{PORT}/api/{API_KEY}" - -ENTRY_CONFIG = {CONF_API_KEY: API_KEY, CONF_HOST: HOST, CONF_PORT: PORT} - -ENTRY_OPTIONS = {} - -DECONZ_CONFIG = { - "bridgeid": BRIDGEID, - "ipaddress": HOST, - "mac": "00:11:22:33:44:55", - "modelid": "deCONZ", - "name": "deCONZ mock gateway", - "sw_version": "2.05.69", - "uuid": "1234", - "websocketport": 1234, -} - -DECONZ_WEB_REQUEST = { - "config": DECONZ_CONFIG, - "groups": {}, - "lights": {}, - "sensors": {}, -} - - -def mock_deconz_request(aioclient_mock, config, data): - """Mock a deCONZ get request.""" - host = config[CONF_HOST] - port = config[CONF_PORT] - api_key = config[CONF_API_KEY] - - aioclient_mock.get( - f"http://{host}:{port}/api/{api_key}", - json=deepcopy(data), - headers={"content-type": CONTENT_TYPE_JSON}, - ) - -def mock_deconz_put_request(aioclient_mock, config, path): - """Mock a deCONZ put request.""" - host = config[CONF_HOST] - port = config[CONF_PORT] - api_key = config[CONF_API_KEY] - - aioclient_mock.put( - f"http://{host}:{port}/api/{api_key}{path}", - json={}, - headers={"content-type": CONTENT_TYPE_JSON}, - ) - - -async def setup_deconz_integration( - hass: HomeAssistant, - aioclient_mock: AiohttpClientMocker | None = None, - *, - options: dict[str, Any] | UndefinedType = UNDEFINED, - entry_id="1", - unique_id=BRIDGEID, - source=SOURCE_USER, -): - """Create the deCONZ gateway.""" - config_entry = MockConfigEntry( - domain=DECONZ_DOMAIN, - source=source, - data=deepcopy(ENTRY_CONFIG), - options=deepcopy(ENTRY_OPTIONS if options is UNDEFINED else options), - entry_id=entry_id, - unique_id=unique_id, - ) - config_entry.add_to_hass(hass) - - if aioclient_mock: - mock_deconz_request(aioclient_mock, ENTRY_CONFIG, DECONZ_WEB_REQUEST) - - await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - - return config_entry +from .conftest import BRIDGEID, HOST, PORT async def test_gateway_setup( @@ -289,9 +192,8 @@ async def test_reset_after_successful_setup( assert result is True -async def test_get_deconz_api(hass: HomeAssistant) -> None: +async def test_get_deconz_api(hass: HomeAssistant, config_entry: ConfigEntry) -> None: """Successful call.""" - config_entry = MockConfigEntry(domain=DECONZ_DOMAIN, data=ENTRY_CONFIG) with patch("pydeconz.DeconzSession.refresh_state", return_value=True): assert await get_deconz_api(hass, config_entry) @@ -306,10 +208,12 @@ async def test_get_deconz_api(hass: HomeAssistant) -> None: ], ) async def test_get_deconz_api_fails( - hass: HomeAssistant, side_effect, raised_exception + hass: HomeAssistant, + config_entry: ConfigEntry, + side_effect: Exception, + raised_exception: Exception, ) -> None: """Failed call.""" - config_entry = MockConfigEntry(domain=DECONZ_DOMAIN, data=ENTRY_CONFIG) with ( patch( "pydeconz.DeconzSession.refresh_state",