mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Improve imports in deCONZ tests (#41976)
* Improve imports in deconz tests * Add spacing
This commit is contained in:
@@ -1,12 +1,34 @@
|
||||
"""Test deCONZ gateway."""
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
import pydeconz
|
||||
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.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
||||
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||
from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL
|
||||
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
|
||||
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
|
||||
from homeassistant.components.deconz.gateway import (
|
||||
get_gateway,
|
||||
get_gateway_from_config_entry,
|
||||
)
|
||||
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
||||
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.ssdp import (
|
||||
ATTR_SSDP_LOCATION,
|
||||
ATTR_UPNP_MANUFACTURER_URL,
|
||||
ATTR_UPNP_SERIAL,
|
||||
ATTR_UPNP_UDN,
|
||||
)
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.config_entries import CONN_CLASS_LOCAL_PUSH, SOURCE_SSDP
|
||||
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
@@ -15,11 +37,7 @@ from tests.common import MockConfigEntry
|
||||
API_KEY = "1234567890ABCDEF"
|
||||
BRIDGEID = "01234E56789A"
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.config_flow.CONF_API_KEY: API_KEY,
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80,
|
||||
}
|
||||
ENTRY_CONFIG = {CONF_API_KEY: API_KEY, CONF_HOST: "1.2.3.4", CONF_PORT: 80}
|
||||
|
||||
ENTRY_OPTIONS = {}
|
||||
|
||||
@@ -52,10 +70,10 @@ async def setup_deconz_integration(
|
||||
):
|
||||
"""Create the deCONZ gateway."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=deconz.DOMAIN,
|
||||
domain=DECONZ_DOMAIN,
|
||||
source=source,
|
||||
data=deepcopy(config),
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
||||
connection_class=CONN_CLASS_LOCAL_PUSH,
|
||||
options=deepcopy(options),
|
||||
entry_id=entry_id,
|
||||
)
|
||||
@@ -87,25 +105,28 @@ async def test_gateway_setup(hass):
|
||||
assert len(gateway.deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
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, "fan")
|
||||
assert forward_entry_setup.mock_calls[4][1] == (config_entry, "light")
|
||||
assert forward_entry_setup.mock_calls[5][1] == (config_entry, "lock")
|
||||
assert forward_entry_setup.mock_calls[6][1] == (config_entry, "scene")
|
||||
assert forward_entry_setup.mock_calls[7][1] == (config_entry, "sensor")
|
||||
assert forward_entry_setup.mock_calls[8][1] == (config_entry, "switch")
|
||||
assert forward_entry_setup.mock_calls[0][1] == (
|
||||
config_entry,
|
||||
BINARY_SENSOR_DOMAIN,
|
||||
)
|
||||
assert forward_entry_setup.mock_calls[1][1] == (config_entry, CLIMATE_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[2][1] == (config_entry, COVER_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[3][1] == (config_entry, FAN_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[4][1] == (config_entry, LIGHT_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[5][1] == (config_entry, LOCK_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[6][1] == (config_entry, SCENE_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[7][1] == (config_entry, SENSOR_DOMAIN)
|
||||
assert forward_entry_setup.mock_calls[8][1] == (config_entry, SWITCH_DOMAIN)
|
||||
|
||||
|
||||
async def test_gateway_retry(hass):
|
||||
"""Retry setup."""
|
||||
with patch(
|
||||
"homeassistant.components.deconz.gateway.get_gateway",
|
||||
side_effect=deconz.errors.CannotConnect,
|
||||
side_effect=CannotConnect,
|
||||
):
|
||||
await setup_deconz_integration(hass)
|
||||
assert not hass.data[deconz.DOMAIN]
|
||||
assert not hass.data[DECONZ_DOMAIN]
|
||||
|
||||
|
||||
async def test_gateway_setup_fails(hass):
|
||||
@@ -114,7 +135,7 @@ async def test_gateway_setup_fails(hass):
|
||||
"homeassistant.components.deconz.gateway.get_gateway", side_effect=Exception
|
||||
):
|
||||
await setup_deconz_integration(hass)
|
||||
assert not hass.data[deconz.DOMAIN]
|
||||
assert not hass.data[DECONZ_DOMAIN]
|
||||
|
||||
|
||||
async def test_connection_status_signalling(hass):
|
||||
@@ -145,14 +166,14 @@ async def test_update_address(hass):
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
await hass.config_entries.flow.async_init(
|
||||
deconz.config_flow.DOMAIN,
|
||||
DECONZ_DOMAIN,
|
||||
data={
|
||||
ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/",
|
||||
ssdp.ATTR_UPNP_MANUFACTURER_URL: deconz.config_flow.DECONZ_MANUFACTURERURL,
|
||||
ssdp.ATTR_UPNP_SERIAL: BRIDGEID,
|
||||
ssdp.ATTR_UPNP_UDN: "uuid:456DEF",
|
||||
ATTR_SSDP_LOCATION: "http://2.3.4.5:80/",
|
||||
ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL,
|
||||
ATTR_UPNP_SERIAL: BRIDGEID,
|
||||
ATTR_UPNP_UDN: "uuid:456DEF",
|
||||
},
|
||||
context={"source": "ssdp"},
|
||||
context={"source": SOURCE_SSDP},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -174,7 +195,7 @@ async def test_reset_after_successful_setup(hass):
|
||||
async def test_get_gateway(hass):
|
||||
"""Successful call."""
|
||||
with patch("pydeconz.DeconzSession.initialize", return_value=True):
|
||||
assert await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
||||
assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
||||
|
||||
|
||||
async def test_get_gateway_fails_unauthorized(hass):
|
||||
@@ -182,11 +203,8 @@ async def test_get_gateway_fails_unauthorized(hass):
|
||||
with patch(
|
||||
"pydeconz.DeconzSession.initialize",
|
||||
side_effect=pydeconz.errors.Unauthorized,
|
||||
), pytest.raises(deconz.errors.AuthenticationRequired):
|
||||
assert (
|
||||
await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
||||
is False
|
||||
)
|
||||
), pytest.raises(AuthenticationRequired):
|
||||
assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) is False
|
||||
|
||||
|
||||
async def test_get_gateway_fails_cannot_connect(hass):
|
||||
@@ -194,8 +212,5 @@ async def test_get_gateway_fails_cannot_connect(hass):
|
||||
with patch(
|
||||
"pydeconz.DeconzSession.initialize",
|
||||
side_effect=pydeconz.errors.RequestError,
|
||||
), pytest.raises(deconz.errors.CannotConnect):
|
||||
assert (
|
||||
await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
|
||||
is False
|
||||
)
|
||||
), pytest.raises(CannotConnect):
|
||||
assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) is False
|
||||
|
||||
Reference in New Issue
Block a user