From d87041041377dd5bd3c0c9bc6a9d44cff99bf0dc Mon Sep 17 00:00:00 2001 From: Tamer Wahba Date: Thu, 22 May 2025 06:18:56 -0400 Subject: [PATCH] Quantum Gateway device tracker tests (#145161) * move constants to central const file * add none return type to device scanner constructor * add quantum gateway device tracker tests * fix --------- Co-authored-by: Joostlek --- CODEOWNERS | 1 + .../components/quantum_gateway/const.py | 7 +++ .../quantum_gateway/device_tracker.py | 16 +++--- requirements_test_all.txt | 3 ++ tests/components/quantum_gateway/__init__.py | 22 ++++++++ tests/components/quantum_gateway/conftest.py | 23 +++++++++ .../quantum_gateway/test_device_tracker.py | 51 +++++++++++++++++++ 7 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 homeassistant/components/quantum_gateway/const.py create mode 100644 tests/components/quantum_gateway/__init__.py create mode 100644 tests/components/quantum_gateway/conftest.py create mode 100644 tests/components/quantum_gateway/test_device_tracker.py diff --git a/CODEOWNERS b/CODEOWNERS index a0324e329e1..b80b9bc6591 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1228,6 +1228,7 @@ build.json @home-assistant/supervisor /homeassistant/components/qnap_qsw/ @Noltari /tests/components/qnap_qsw/ @Noltari /homeassistant/components/quantum_gateway/ @cisasteelersfan +/tests/components/quantum_gateway/ @cisasteelersfan /homeassistant/components/qvr_pro/ @oblogic7 /homeassistant/components/qwikswitch/ @kellerza /tests/components/qwikswitch/ @kellerza diff --git a/homeassistant/components/quantum_gateway/const.py b/homeassistant/components/quantum_gateway/const.py new file mode 100644 index 00000000000..6e8bae10065 --- /dev/null +++ b/homeassistant/components/quantum_gateway/const.py @@ -0,0 +1,7 @@ +"""Constants for Quantum Gateway.""" + +import logging + +LOGGER = logging.getLogger(__package__) + +DEFAULT_HOST = "myfiosgateway.com" diff --git a/homeassistant/components/quantum_gateway/device_tracker.py b/homeassistant/components/quantum_gateway/device_tracker.py index 6491dca2e2c..c3eddc37f22 100644 --- a/homeassistant/components/quantum_gateway/device_tracker.py +++ b/homeassistant/components/quantum_gateway/device_tracker.py @@ -2,8 +2,6 @@ from __future__ import annotations -import logging - from quantum_gateway import QuantumGatewayScanner from requests.exceptions import RequestException import voluptuous as vol @@ -18,9 +16,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType -_LOGGER = logging.getLogger(__name__) - -DEFAULT_HOST = "myfiosgateway.com" +from .const import DEFAULT_HOST, LOGGER PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend( { @@ -43,13 +39,13 @@ def get_scanner( class QuantumGatewayDeviceScanner(DeviceScanner): """Class which queries a Quantum Gateway.""" - def __init__(self, config): + def __init__(self, config) -> None: """Initialize the scanner.""" self.host = config[CONF_HOST] self.password = config[CONF_PASSWORD] self.use_https = config[CONF_SSL] - _LOGGER.debug("Initializing") + LOGGER.debug("Initializing") try: self.quantum = QuantumGatewayScanner( @@ -58,10 +54,10 @@ class QuantumGatewayDeviceScanner(DeviceScanner): self.success_init = self.quantum.success_init except RequestException: self.success_init = False - _LOGGER.error("Unable to connect to gateway. Check host") + LOGGER.error("Unable to connect to gateway. Check host") if not self.success_init: - _LOGGER.error("Unable to login to gateway. Check password and host") + LOGGER.error("Unable to login to gateway. Check password and host") def scan_devices(self): """Scan for new devices and return a list of found MACs.""" @@ -69,7 +65,7 @@ class QuantumGatewayDeviceScanner(DeviceScanner): try: connected_devices = self.quantum.scan_devices() except RequestException: - _LOGGER.error("Unable to scan devices. Check connection to router") + LOGGER.error("Unable to scan devices. Check connection to router") return connected_devices def get_device_name(self, device): diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ffd0fd244d2..e99def6471e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2131,6 +2131,9 @@ qingping-ble==0.10.0 # homeassistant.components.qnap qnapstats==0.4.0 +# homeassistant.components.quantum_gateway +quantum-gateway==0.0.8 + # homeassistant.components.radio_browser radios==0.3.2 diff --git a/tests/components/quantum_gateway/__init__.py b/tests/components/quantum_gateway/__init__.py new file mode 100644 index 00000000000..73758f9081e --- /dev/null +++ b/tests/components/quantum_gateway/__init__.py @@ -0,0 +1,22 @@ +"""Tests for the quantum_gateway component.""" + +from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN +from homeassistant.const import CONF_PASSWORD, CONF_PLATFORM +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + + +async def setup_platform(hass: HomeAssistant) -> None: + """Set up the quantum_gateway integration.""" + result = await async_setup_component( + hass, + DEVICE_TRACKER_DOMAIN, + { + DEVICE_TRACKER_DOMAIN: { + CONF_PLATFORM: "quantum_gateway", + CONF_PASSWORD: "fake_password", + } + }, + ) + await hass.async_block_till_done() + assert result diff --git a/tests/components/quantum_gateway/conftest.py b/tests/components/quantum_gateway/conftest.py new file mode 100644 index 00000000000..b2445813023 --- /dev/null +++ b/tests/components/quantum_gateway/conftest.py @@ -0,0 +1,23 @@ +"""Fixtures for Quantum Gateway tests.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + + +@pytest.fixture +async def mock_scanner() -> Generator[AsyncMock]: + """Mock QuantumGatewayScanner instance.""" + with patch( + "homeassistant.components.quantum_gateway.device_tracker.QuantumGatewayScanner", + autospec=True, + ) as mock_scanner: + client = mock_scanner.return_value + client.success_init = True + client.scan_devices.return_value = ["ff:ff:ff:ff:ff:ff", "ff:ff:ff:ff:ff:fe"] + client.get_device_name.side_effect = { + "ff:ff:ff:ff:ff:ff": "", + "ff:ff:ff:ff:ff:fe": "desktop", + }.get + yield mock_scanner diff --git a/tests/components/quantum_gateway/test_device_tracker.py b/tests/components/quantum_gateway/test_device_tracker.py new file mode 100644 index 00000000000..df568d1f81a --- /dev/null +++ b/tests/components/quantum_gateway/test_device_tracker.py @@ -0,0 +1,51 @@ +"""Tests for the quantum_gateway device tracker.""" + +from unittest.mock import AsyncMock + +import pytest +from requests import RequestException + +from homeassistant.const import STATE_HOME +from homeassistant.core import HomeAssistant + +from . import setup_platform + +from tests.components.device_tracker.test_init import mock_yaml_devices # noqa: F401 + + +@pytest.mark.usefixtures("yaml_devices") +async def test_get_scanner(hass: HomeAssistant, mock_scanner: AsyncMock) -> None: + """Test creating a quantum gateway scanner.""" + await setup_platform(hass) + + device_1 = hass.states.get("device_tracker.desktop") + assert device_1 is not None + assert device_1.state == STATE_HOME + + device_2 = hass.states.get("device_tracker.ff_ff_ff_ff_ff_ff") + assert device_2 is not None + assert device_2.state == STATE_HOME + + +@pytest.mark.usefixtures("yaml_devices") +async def test_get_scanner_error(hass: HomeAssistant, mock_scanner: AsyncMock) -> None: + """Test failure when creating a quantum gateway scanner.""" + mock_scanner.side_effect = RequestException("Error") + await setup_platform(hass) + + assert "quantum_gateway.device_tracker" not in hass.config.components + + +@pytest.mark.usefixtures("yaml_devices") +async def test_scan_devices_error(hass: HomeAssistant, mock_scanner: AsyncMock) -> None: + """Test failure when scanning devices.""" + mock_scanner.return_value.scan_devices.side_effect = RequestException("Error") + await setup_platform(hass) + + assert "quantum_gateway.device_tracker" in hass.config.components + + device_1 = hass.states.get("device_tracker.desktop") + assert device_1 is None + + device_2 = hass.states.get("device_tracker.ff_ff_ff_ff_ff_ff") + assert device_2 is None