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/entity.py
homeassistant/components/tractive/sensor.py homeassistant/components/tractive/sensor.py
homeassistant/components/tractive/switch.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_train/sensor.py
homeassistant/components/trafikverket_weatherstation/sensor.py homeassistant/components/trafikverket_weatherstation/sensor.py
homeassistant/components/transmission/sensor.py homeassistant/components/transmission/sensor.py

View File

@ -1,2 +1,3 @@
"""Tests for the tradfri component.""" """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 import pytest
from . import MOCK_GATEWAY_ID from . import GATEWAY_ID, TRADFRI_PATH
from tests.components.light.conftest import mock_light_profiles # noqa: F401
# pylint: disable=protected-access # pylint: disable=protected-access
@ -13,28 +11,20 @@ from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture @pytest.fixture
def mock_gateway_info(): def mock_gateway_info():
"""Mock get_gateway_info.""" """Mock get_gateway_info."""
with patch( with patch(f"{TRADFRI_PATH}.config_flow.get_gateway_info") as gateway_info:
"homeassistant.components.tradfri.config_flow.get_gateway_info"
) as gateway_info:
yield gateway_info yield gateway_info
@pytest.fixture @pytest.fixture
def mock_entry_setup(): def mock_entry_setup():
"""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 mock_setup.return_value = True
yield mock_setup 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") @pytest.fixture(name="mock_gateway")
def mock_gateway_fixture(gateway_id): def mock_gateway_fixture():
"""Mock a Tradfri gateway.""" """Mock a Tradfri gateway."""
def get_devices(): def get_devices():
@ -45,7 +35,7 @@ def mock_gateway_fixture(gateway_id):
"""Return mock groups.""" """Return mock groups."""
return gateway.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(): def get_gateway_info():
"""Return mock gateway info.""" """Return mock gateway info."""
@ -59,8 +49,8 @@ def mock_gateway_fixture(gateway_id):
mock_groups=[], mock_groups=[],
mock_responses=[], mock_responses=[],
) )
with patch("homeassistant.components.tradfri.Gateway", return_value=gateway), patch( with patch(f"{TRADFRI_PATH}.Gateway", return_value=gateway), patch(
"homeassistant.components.tradfri.config_flow.Gateway", return_value=gateway f"{TRADFRI_PATH}.config_flow.Gateway", return_value=gateway
): ):
yield gateway yield gateway
@ -79,10 +69,10 @@ def mock_api_fixture(mock_gateway):
return api return api
@pytest.fixture(name="api_factory") @pytest.fixture
def mock_api_factory_fixture(mock_api): def mock_api_factory(mock_api):
"""Mock pytradfri api factory.""" """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.init.return_value = factory.return_value
factory.return_value.request = mock_api factory.return_value.request = mock_api
yield factory.return_value yield factory.return_value

View File

@ -6,27 +6,27 @@ import pytest
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components.tradfri import config_flow from homeassistant.components.tradfri import config_flow
from . import TRADFRI_PATH
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.fixture @pytest.fixture(name="mock_auth")
def mock_auth(): def mock_auth_fixture():
"""Mock authenticate.""" """Mock authenticate."""
with patch( with patch(f"{TRADFRI_PATH}.config_flow.authenticate") as auth:
"homeassistant.components.tradfri.config_flow.authenticate" yield auth
) as mock_auth:
yield mock_auth
async def test_already_paired(hass, mock_entry_setup): async def test_already_paired(hass, mock_entry_setup):
"""Test Gateway already paired.""" """Test Gateway already paired."""
with patch( with patch(
"homeassistant.components.tradfri.config_flow.APIFactory", f"{TRADFRI_PATH}.config_flow.APIFactory",
autospec=True, autospec=True,
) as mock_lib: ) as mock_lib:
mx = AsyncMock() mock_it = AsyncMock()
mx.generate_psk.return_value = None mock_it.generate_psk.return_value = None
mock_lib.init.return_value = mx mock_lib.init.return_value = mock_it
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER} "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.components import tradfri
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from . import GATEWAY_ID
from tests.common import MockConfigEntry 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.""" """Test config entry setup and unload."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=tradfri.DOMAIN, 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_IDENTITY: "mock-identity",
tradfri.CONF_KEY: "mock-key", tradfri.CONF_KEY: "mock-key",
tradfri.CONF_IMPORT_GROUPS: True, 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) assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert unload.call_count == len(tradfri.PLATFORMS) 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 homeassistant.components import tradfri
from . import MOCK_GATEWAY_ID from . import GATEWAY_ID
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -109,7 +109,7 @@ async def setup_integration(hass):
"identity": "mock-identity", "identity": "mock-identity",
"key": "mock-key", "key": "mock-key",
"import_groups": True, "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 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.""" """Test that lights are correctly added."""
features = {"can_set_dimmer": True, "can_set_color": True, "can_set_temp": True} 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) 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.""" """Test that lights are correctly observed."""
light = mock_light() light = mock_light()
mock_gateway.mock_devices.append(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 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.""" """Test light available property."""
light = mock_light({"state": True}, light_number=1) light = mock_light({"state": True}, light_number=1)
light.reachable = True light.reachable = True
@ -225,7 +225,7 @@ def create_all_turn_on_cases():
async def test_turn_on( async def test_turn_on(
hass, hass,
mock_gateway, mock_gateway,
api_factory, mock_api_factory,
test_features, test_features,
test_data, test_data,
expected_result, expected_result,
@ -288,7 +288,7 @@ async def test_turn_on(
assert states.attributes[result] == pytest.approx(value, abs=0.01) 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.""" """Test turning off a light."""
state = {"state": True, "dimmer": 100} state = {"state": True, "dimmer": 100}
@ -341,7 +341,7 @@ def mock_group(test_state=None, group_number=0):
return _mock_group 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.""" """Test that groups are correctly added."""
mock_gateway.mock_groups.append(mock_group()) mock_gateway.mock_groups.append(mock_group())
state = {"state": True, "dimmer": 100} state = {"state": True, "dimmer": 100}
@ -358,7 +358,7 @@ async def test_group(hass, mock_gateway, api_factory):
assert group.attributes["brightness"] == 100 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.""" """Test turning on a group."""
group = mock_group() group = mock_group()
group2 = mock_group(group_number=1) 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) 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.""" """Test turning off a group."""
group = mock_group({"state": True}) group = mock_group({"state": True})
mock_gateway.mock_groups.append(group) mock_gateway.mock_groups.append(group)