Finally remove all legacy test stubs from deCONZ tests (#121323)

This commit is contained in:
Robert Svensson 2024-07-05 20:47:43 +02:00 committed by GitHub
parent ad2d794fd6
commit 94db251aea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 105 deletions

View File

@ -33,7 +33,7 @@ from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType 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 from tests.test_util.aiohttp import AiohttpClientMocker

View File

@ -1,8 +1,6 @@
"""Test deCONZ gateway.""" """Test deCONZ gateway."""
from collections.abc import Callable from collections.abc import Callable
from copy import deepcopy
from typing import Any
from unittest.mock import patch from unittest.mock import patch
import pydeconz import pydeconz
@ -35,107 +33,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 ( from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, ConfigEntry
SOURCE_HASSIO, from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE
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.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
from .conftest import API_KEY, BRIDGEID, HOST, PORT from .conftest import 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
async def test_gateway_setup( async def test_gateway_setup(
@ -289,9 +192,8 @@ async def test_reset_after_successful_setup(
assert result is True 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.""" """Successful call."""
config_entry = MockConfigEntry(domain=DECONZ_DOMAIN, data=ENTRY_CONFIG)
with patch("pydeconz.DeconzSession.refresh_state", return_value=True): with patch("pydeconz.DeconzSession.refresh_state", return_value=True):
assert await get_deconz_api(hass, config_entry) 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( 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: ) -> None:
"""Failed call.""" """Failed call."""
config_entry = MockConfigEntry(domain=DECONZ_DOMAIN, data=ENTRY_CONFIG)
with ( with (
patch( patch(
"pydeconz.DeconzSession.refresh_state", "pydeconz.DeconzSession.refresh_state",