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 <joostlek@outlook.com>
This commit is contained in:
Tamer Wahba
2025-05-22 06:18:56 -04:00
committed by GitHub
parent 9a8c29e05d
commit d870410413
7 changed files with 113 additions and 10 deletions

1
CODEOWNERS generated
View File

@ -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

View File

@ -0,0 +1,7 @@
"""Constants for Quantum Gateway."""
import logging
LOGGER = logging.getLogger(__package__)
DEFAULT_HOST = "myfiosgateway.com"

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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