Activate tradfri in coverage and clean conftest for tradfri (#58058)

This commit is contained in:
jan iversen 2021-10-20 13:36:02 +02:00 committed by GitHub
parent 62b7453719
commit 45983b5edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 45 deletions

View File

@ -1101,7 +1101,13 @@ omit =
homeassistant/components/tractive/entity.py
homeassistant/components/tractive/sensor.py
homeassistant/components/tractive/switch.py
homeassistant/components/tradfri/*
homeassistant/components/tradfri/__init__.py
homeassistant/components/tradfri/base_class.py
homeassistant/components/tradfri/config_flow.py
homeassistant/components/tradfri/cover.py
homeassistant/components/tradfri/light.py
homeassistant/components/tradfri/sensor.py
homeassistant/components/tradfri/switch.py
homeassistant/components/trafikverket_train/sensor.py
homeassistant/components/trafikverket_weatherstation/sensor.py
homeassistant/components/transmission/sensor.py

View File

@ -1,2 +1,3 @@
"""Tests for the tradfri component."""
MOCK_GATEWAY_ID = "mock-gateway-id"
GATEWAY_ID = "mock-gateway-id"
TRADFRI_PATH = "homeassistant.components.tradfri"

View File

@ -3,9 +3,7 @@ from unittest.mock import Mock, patch
import pytest
from . import MOCK_GATEWAY_ID
from tests.components.light.conftest import mock_light_profiles # noqa: F401
from . import GATEWAY_ID, TRADFRI_PATH
# pylint: disable=protected-access
@ -13,28 +11,20 @@ from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture
def mock_gateway_info():
"""Mock get_gateway_info."""
with patch(
"homeassistant.components.tradfri.config_flow.get_gateway_info"
) as gateway_info:
with patch(f"{TRADFRI_PATH}.config_flow.get_gateway_info") as gateway_info:
yield gateway_info
@pytest.fixture
def mock_entry_setup():
"""Mock entry setup."""
with patch("homeassistant.components.tradfri.async_setup_entry") as mock_setup:
with patch(f"{TRADFRI_PATH}.async_setup_entry") as mock_setup:
mock_setup.return_value = True
yield mock_setup
@pytest.fixture(name="gateway_id")
def mock_gateway_id_fixture():
"""Return mock gateway_id."""
return MOCK_GATEWAY_ID
@pytest.fixture(name="mock_gateway")
def mock_gateway_fixture(gateway_id):
def mock_gateway_fixture():
"""Mock a Tradfri gateway."""
def get_devices():
@ -45,7 +35,7 @@ def mock_gateway_fixture(gateway_id):
"""Return mock groups."""
return gateway.mock_groups
gateway_info = Mock(id=gateway_id, firmware_version="1.2.1234")
gateway_info = Mock(id=GATEWAY_ID, firmware_version="1.2.1234")
def get_gateway_info():
"""Return mock gateway info."""
@ -59,8 +49,8 @@ def mock_gateway_fixture(gateway_id):
mock_groups=[],
mock_responses=[],
)
with patch("homeassistant.components.tradfri.Gateway", return_value=gateway), patch(
"homeassistant.components.tradfri.config_flow.Gateway", return_value=gateway
with patch(f"{TRADFRI_PATH}.Gateway", return_value=gateway), patch(
f"{TRADFRI_PATH}.config_flow.Gateway", return_value=gateway
):
yield gateway
@ -79,10 +69,10 @@ def mock_api_fixture(mock_gateway):
return api
@pytest.fixture(name="api_factory")
def mock_api_factory_fixture(mock_api):
@pytest.fixture
def mock_api_factory(mock_api):
"""Mock pytradfri api factory."""
with patch("homeassistant.components.tradfri.APIFactory", autospec=True) as factory:
with patch(f"{TRADFRI_PATH}.APIFactory", autospec=True) as factory:
factory.init.return_value = factory.return_value
factory.return_value.request = mock_api
yield factory.return_value

View File

@ -6,27 +6,27 @@ import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.tradfri import config_flow
from . import TRADFRI_PATH
from tests.common import MockConfigEntry
@pytest.fixture
def mock_auth():
@pytest.fixture(name="mock_auth")
def mock_auth_fixture():
"""Mock authenticate."""
with patch(
"homeassistant.components.tradfri.config_flow.authenticate"
) as mock_auth:
yield mock_auth
with patch(f"{TRADFRI_PATH}.config_flow.authenticate") as auth:
yield auth
async def test_already_paired(hass, mock_entry_setup):
"""Test Gateway already paired."""
with patch(
"homeassistant.components.tradfri.config_flow.APIFactory",
f"{TRADFRI_PATH}.config_flow.APIFactory",
autospec=True,
) as mock_lib:
mx = AsyncMock()
mx.generate_psk.return_value = None
mock_lib.init.return_value = mx
mock_it = AsyncMock()
mock_it.generate_psk.return_value = None
mock_lib.init.return_value = mock_it
result = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER}
)

View File

@ -4,10 +4,12 @@ from unittest.mock import patch
from homeassistant.components import tradfri
from homeassistant.helpers import device_registry as dr
from . import GATEWAY_ID
from tests.common import MockConfigEntry
async def test_entry_setup_unload(hass, api_factory, gateway_id):
async def test_entry_setup_unload(hass, mock_api_factory):
"""Test config entry setup and unload."""
entry = MockConfigEntry(
domain=tradfri.DOMAIN,
@ -16,7 +18,7 @@ async def test_entry_setup_unload(hass, api_factory, gateway_id):
tradfri.CONF_IDENTITY: "mock-identity",
tradfri.CONF_KEY: "mock-key",
tradfri.CONF_IMPORT_GROUPS: True,
tradfri.CONF_GATEWAY_ID: gateway_id,
tradfri.CONF_GATEWAY_ID: GATEWAY_ID,
},
)
@ -46,4 +48,4 @@ async def test_entry_setup_unload(hass, api_factory, gateway_id):
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert unload.call_count == len(tradfri.PLATFORMS)
assert api_factory.shutdown.call_count == 1
assert mock_api_factory.shutdown.call_count == 1

View File

@ -10,7 +10,7 @@ from pytradfri.device.light_control import LightControl
from homeassistant.components import tradfri
from . import MOCK_GATEWAY_ID
from . import GATEWAY_ID
from tests.common import MockConfigEntry
@ -109,7 +109,7 @@ async def setup_integration(hass):
"identity": "mock-identity",
"key": "mock-key",
"import_groups": True,
"gateway_id": MOCK_GATEWAY_ID,
"gateway_id": GATEWAY_ID,
},
)
@ -153,7 +153,7 @@ def mock_light(test_features=None, test_state=None, light_number=0):
return _mock_light
async def test_light(hass, mock_gateway, api_factory):
async def test_light(hass, mock_gateway, mock_api_factory):
"""Test that lights are correctly added."""
features = {"can_set_dimmer": True, "can_set_color": True, "can_set_temp": True}
@ -176,7 +176,7 @@ async def test_light(hass, mock_gateway, api_factory):
assert lamp_1.attributes["hs_color"] == (0.549, 0.153)
async def test_light_observed(hass, mock_gateway, api_factory):
async def test_light_observed(hass, mock_gateway, mock_api_factory):
"""Test that lights are correctly observed."""
light = mock_light()
mock_gateway.mock_devices.append(light)
@ -184,7 +184,7 @@ async def test_light_observed(hass, mock_gateway, api_factory):
assert len(light.observe.mock_calls) > 0
async def test_light_available(hass, mock_gateway, api_factory):
async def test_light_available(hass, mock_gateway, mock_api_factory):
"""Test light available property."""
light = mock_light({"state": True}, light_number=1)
light.reachable = True
@ -225,7 +225,7 @@ def create_all_turn_on_cases():
async def test_turn_on(
hass,
mock_gateway,
api_factory,
mock_api_factory,
test_features,
test_data,
expected_result,
@ -288,7 +288,7 @@ async def test_turn_on(
assert states.attributes[result] == pytest.approx(value, abs=0.01)
async def test_turn_off(hass, mock_gateway, api_factory):
async def test_turn_off(hass, mock_gateway, mock_api_factory):
"""Test turning off a light."""
state = {"state": True, "dimmer": 100}
@ -341,7 +341,7 @@ def mock_group(test_state=None, group_number=0):
return _mock_group
async def test_group(hass, mock_gateway, api_factory):
async def test_group(hass, mock_gateway, mock_api_factory):
"""Test that groups are correctly added."""
mock_gateway.mock_groups.append(mock_group())
state = {"state": True, "dimmer": 100}
@ -358,7 +358,7 @@ async def test_group(hass, mock_gateway, api_factory):
assert group.attributes["brightness"] == 100
async def test_group_turn_on(hass, mock_gateway, api_factory):
async def test_group_turn_on(hass, mock_gateway, mock_api_factory):
"""Test turning on a group."""
group = mock_group()
group2 = mock_group(group_number=1)
@ -391,7 +391,7 @@ async def test_group_turn_on(hass, mock_gateway, api_factory):
group3.set_dimmer.assert_called_with(100, transition_time=10)
async def test_group_turn_off(hass, mock_gateway, api_factory):
async def test_group_turn_off(hass, mock_gateway, mock_api_factory):
"""Test turning off a group."""
group = mock_group({"state": True})
mock_gateway.mock_groups.append(group)