Add network support to flux_led discovery (#61132)
This commit is contained in:
parent
a40549c1b9
commit
a4c101b021
8 changed files with 185 additions and 87 deletions
|
@ -168,10 +168,10 @@ def _patch_discovery(device=None, no_device=False):
|
|||
@contextmanager
|
||||
def _patcher():
|
||||
with patch(
|
||||
"homeassistant.components.flux_led.AIOBulbScanner.async_scan",
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.async_scan",
|
||||
new=_discovery,
|
||||
), patch(
|
||||
"homeassistant.components.flux_led.AIOBulbScanner.getBulbInfo",
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.getBulbInfo",
|
||||
return_value=[] if no_device else [device or FLUX_DISCOVERY],
|
||||
):
|
||||
yield
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
"""Tests for the flux_led integration."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.common import mock_device_registry
|
||||
|
@ -9,3 +11,23 @@ from tests.common import mock_device_registry
|
|||
def device_reg_fixture(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_device_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_single_broadcast_address():
|
||||
"""Mock network's async_async_get_ipv4_broadcast_addresses."""
|
||||
with patch(
|
||||
"homeassistant.components.network.async_get_ipv4_broadcast_addresses",
|
||||
return_value={"10.255.255.255"},
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_multiple_broadcast_addresses():
|
||||
"""Mock network's async_async_get_ipv4_broadcast_addresses to return multiple addresses."""
|
||||
with patch(
|
||||
"homeassistant.components.network.async_get_ipv4_broadcast_addresses",
|
||||
return_value={"10.255.255.255", "192.168.0.255"},
|
||||
):
|
||||
yield
|
||||
|
|
|
@ -3,6 +3,8 @@ from __future__ import annotations
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
from flux_led.aioscanner import AIOBulbScanner
|
||||
from flux_led.scanner import FluxLEDDiscovery
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import flux_led
|
||||
|
@ -26,23 +28,50 @@ from . import (
|
|||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_single_broadcast_address")
|
||||
async def test_configuring_flux_led_causes_discovery(hass: HomeAssistant) -> None:
|
||||
"""Test that specifying empty config does discovery."""
|
||||
with patch(
|
||||
"homeassistant.components.flux_led.AIOBulbScanner.async_scan"
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.async_scan"
|
||||
) as scan, patch(
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.getBulbInfo"
|
||||
) as discover:
|
||||
discover.return_value = [FLUX_DISCOVERY]
|
||||
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(discover.mock_calls) == 1
|
||||
assert len(scan.mock_calls) == 1
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
assert len(discover.mock_calls) == 2
|
||||
assert len(scan.mock_calls) == 2
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
assert len(discover.mock_calls) == 3
|
||||
assert len(scan.mock_calls) == 3
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_multiple_broadcast_addresses")
|
||||
async def test_configuring_flux_led_causes_discovery_multiple_addresses(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test that specifying empty config does discovery."""
|
||||
with patch(
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.async_scan"
|
||||
) as scan, patch(
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner.getBulbInfo"
|
||||
) as discover:
|
||||
discover.return_value = [FLUX_DISCOVERY]
|
||||
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(scan.mock_calls) == 2
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 4
|
||||
|
||||
async_fire_time_changed(hass, utcnow() + flux_led.DISCOVERY_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
assert len(scan.mock_calls) == 6
|
||||
|
||||
|
||||
async def test_config_entry_reload(hass: HomeAssistant) -> None:
|
||||
|
@ -78,20 +107,32 @@ async def test_config_entry_retry(hass: HomeAssistant) -> None:
|
|||
],
|
||||
)
|
||||
async def test_config_entry_fills_unique_id_with_directed_discovery(
|
||||
hass: HomeAssistant, discovery: dict[str, str], title: str
|
||||
hass: HomeAssistant, discovery: FluxLEDDiscovery, title: str
|
||||
) -> None:
|
||||
"""Test that the unique id is added if its missing via directed (not broadcast) discovery."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={CONF_HOST: IP_ADDRESS}, unique_id=None
|
||||
domain=DOMAIN, data={CONF_NAME: "bogus", CONF_HOST: IP_ADDRESS}, unique_id=None
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert config_entry.unique_id is None
|
||||
|
||||
async def _discovery(self, *args, address=None, **kwargs):
|
||||
# Only return discovery results when doing directed discovery
|
||||
return [discovery] if address == IP_ADDRESS else []
|
||||
class MockBulbScanner(AIOBulbScanner):
|
||||
def __init__(self) -> None:
|
||||
self._last_address: str | None = None
|
||||
super().__init__()
|
||||
|
||||
async def async_scan(
|
||||
self, timeout: int = 10, address: str | None = None
|
||||
) -> list[FluxLEDDiscovery]:
|
||||
self._last_address = address
|
||||
return [discovery] if address == IP_ADDRESS else []
|
||||
|
||||
def getBulbInfo(self) -> FluxLEDDiscovery:
|
||||
return [discovery] if self._last_address == IP_ADDRESS else []
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.flux_led.AIOBulbScanner.async_scan", new=_discovery
|
||||
"homeassistant.components.flux_led.discovery.AIOBulbScanner",
|
||||
return_value=MockBulbScanner(),
|
||||
), _patch_wifibulb():
|
||||
await async_setup_component(hass, flux_led.DOMAIN, {flux_led.DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue