Fix tests for Hue integration (#60683)

* fix tests

make sure the migration code is not called in all other tests

* only patch v2 check where needed
This commit is contained in:
Marcel van der Veldt 2021-12-01 14:53:30 +01:00 committed by GitHub
parent 6a8c732b37
commit 5d0cf4cb95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 37 deletions

View File

@ -22,8 +22,6 @@ from tests.common import (
mock_device_registry,
)
# from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture(autouse=True)
def no_request_delay():
@ -248,6 +246,9 @@ async def setup_component(hass):
async def setup_bridge(hass, mock_bridge, config_entry):
"""Load the Hue integration with the provided bridge."""
mock_bridge.config_entry = config_entry
with patch.object(
hue.migration, "is_v2_bridge", return_value=mock_bridge.api_version == 2
):
config_entry.add_to_hass(hass)
with patch("homeassistant.components.hue.HueBridge", return_value=mock_bridge):
await hass.config_entries.async_setup(config_entry.entry_id)
@ -283,22 +284,6 @@ async def setup_platform(
await hass.async_block_till_done()
@pytest.fixture
def mock_bridge_setup():
"""Mock bridge setup."""
with patch.object(hue, "HueBridge") as mock_bridge:
mock_bridge.return_value.async_initialize_bridge = AsyncMock(return_value=True)
mock_bridge.return_value.api_version = 1
mock_bridge.return_value.api.config = Mock(
bridge_id="mock-id",
mac_address="00:00:00:00:00:00",
software_version="1.0.0",
model_id="BSB002",
)
mock_bridge.return_value.api.config.name = "Mock Hue bridge"
yield mock_bridge.return_value
@pytest.fixture(name="device_reg")
def get_device_reg(hass):
"""Return an empty, loaded, registry."""

View File

@ -723,7 +723,7 @@ async def test_bridge_zeroconf(hass, aioclient_mock):
data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.217",
port=443,
hostname="Philips-hue.local.",
hostname="Philips-hue.local",
type="_hue._tcp.local.",
name="Philips Hue - ABCABC._hue._tcp.local.",
properties={
@ -740,7 +740,9 @@ async def test_bridge_zeroconf(hass, aioclient_mock):
async def test_bridge_zeroconf_already_exists(hass, aioclient_mock):
"""Test a bridge being discovered by zeroconf already exists."""
create_mock_api_discovery(aioclient_mock, [("192.168.1.217", "ecb5faabcabc")])
create_mock_api_discovery(
aioclient_mock, [("0.0.0.0", "ecb5faabcabc"), ("192.168.1.217", "ecb5faabcabc")]
)
entry = MockConfigEntry(
domain="hue",
source=config_entries.SOURCE_SSDP,
@ -754,7 +756,7 @@ async def test_bridge_zeroconf_already_exists(hass, aioclient_mock):
data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.217",
port=443,
hostname="Philips-hue.local.",
hostname="Philips-hue.local",
type="_hue._tcp.local.",
name="Philips Hue - ABCABC._hue._tcp.local.",
properties={

View File

@ -1,6 +1,7 @@
"""Test Hue setup process."""
from unittest.mock import AsyncMock, Mock, patch
import aiohue.v2 as aiohue_v2
import pytest
from homeassistant import config_entries
@ -10,17 +11,22 @@ from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.fixture(name="mock_bridge_setup")
def get_mock_bridge_setup():
@pytest.fixture
def mock_bridge_setup():
"""Mock bridge setup."""
with patch.object(hue, "HueBridge") as mock_bridge:
mock_bridge.return_value.api_version = 2
mock_bridge.return_value.async_initialize_bridge = AsyncMock(return_value=True)
mock_bridge.return_value.api_version = 1
mock_bridge.return_value.api.config = Mock(
bridge_id="mock-id",
mac_address="00:00:00:00:00:00",
software_version="1.0.0",
model_id="BSB002",
software_version="1.0.0",
bridge_device=Mock(
id="4a507550-8742-4087-8bf5-c2334f29891c",
product_data=Mock(manufacturer_name="Mock"),
),
spec=aiohue_v2.ConfigController,
)
mock_bridge.return_value.api.config.name = "Mock Hue bridge"
yield mock_bridge.return_value
@ -39,7 +45,9 @@ async def test_setup_with_no_config(hass):
async def test_unload_entry(hass, mock_bridge_setup):
"""Test being able to unload an entry."""
entry = MockConfigEntry(domain=hue.DOMAIN, data={"host": "0.0.0.0"})
entry = MockConfigEntry(
domain=hue.DOMAIN, data={"host": "0.0.0.0", "api_version": 2}
)
entry.add_to_hass(hass)
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
@ -58,7 +66,9 @@ async def test_unload_entry(hass, mock_bridge_setup):
async def test_setting_unique_id(hass, mock_bridge_setup):
"""Test we set unique ID if not set yet."""
entry = MockConfigEntry(domain=hue.DOMAIN, data={"host": "0.0.0.0"})
entry = MockConfigEntry(
domain=hue.DOMAIN, data={"host": "0.0.0.0", "api_version": 2}
)
entry.add_to_hass(hass)
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
assert entry.unique_id == "mock-id"
@ -67,7 +77,9 @@ async def test_setting_unique_id(hass, mock_bridge_setup):
async def test_fixing_unique_id_no_other(hass, mock_bridge_setup):
"""Test we set unique ID if not set yet."""
entry = MockConfigEntry(
domain=hue.DOMAIN, data={"host": "0.0.0.0"}, unique_id="invalid-id"
domain=hue.DOMAIN,
data={"host": "0.0.0.0", "api_version": 2},
unique_id="invalid-id",
)
entry.add_to_hass(hass)
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
@ -78,13 +90,13 @@ async def test_fixing_unique_id_other_ignored(hass, mock_bridge_setup):
"""Test we set unique ID if not set yet."""
MockConfigEntry(
domain=hue.DOMAIN,
data={"host": "0.0.0.0"},
data={"host": "0.0.0.0", "api_version": 2},
unique_id="mock-id",
source=config_entries.SOURCE_IGNORE,
).add_to_hass(hass)
entry = MockConfigEntry(
domain=hue.DOMAIN,
data={"host": "0.0.0.0"},
data={"host": "0.0.0.0", "api_version": 2},
unique_id="invalid-id",
)
entry.add_to_hass(hass)
@ -98,13 +110,13 @@ async def test_fixing_unique_id_other_correct(hass, mock_bridge_setup):
"""Test we remove config entry if another one has correct ID."""
correct_entry = MockConfigEntry(
domain=hue.DOMAIN,
data={"host": "0.0.0.0"},
data={"host": "0.0.0.0", "api_version": 2},
unique_id="mock-id",
)
correct_entry.add_to_hass(hass)
entry = MockConfigEntry(
domain=hue.DOMAIN,
data={"host": "0.0.0.0"},
data={"host": "0.0.0.0", "api_version": 2},
unique_id="invalid-id",
)
entry.add_to_hass(hass)
@ -121,11 +133,14 @@ async def test_security_vuln_check(hass):
entry.add_to_hass(hass)
config = Mock(
bridge_id="", mac_address="", model_id="BSB002", software_version="1935144020"
bridge_id="",
mac_address="",
model_id="BSB002",
software_version="1935144020",
)
config.name = "Hue"
with patch.object(
with patch.object(hue.migration, "is_v2_bridge", return_value=False), patch.object(
hue,
"HueBridge",
Mock(