|
| 1 | +"""Tests for the read-only Govee LAN discovery helper (issue #57).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from custom_components.govee.api import lan |
| 11 | + |
| 12 | + |
| 13 | +def _install_fake_endpoint(monkeypatch, responses, *, bind_error=None): |
| 14 | + """Patch socket + event loop so async_scan_lan_devices runs offline. |
| 15 | +
|
| 16 | + Returns the MagicMock transport so tests can assert on sendto. ``responses`` |
| 17 | + are fed to the real _ScanProtocol during the (stubbed) scan wait. |
| 18 | + """ |
| 19 | + sock = MagicMock() |
| 20 | + if bind_error is not None: |
| 21 | + sock.bind.side_effect = bind_error |
| 22 | + monkeypatch.setattr(lan.socket, "socket", lambda *a, **k: sock) |
| 23 | + |
| 24 | + proto = lan._ScanProtocol() |
| 25 | + transport = MagicMock() |
| 26 | + |
| 27 | + async def _create_endpoint(_factory, sock=None): |
| 28 | + return transport, proto |
| 29 | + |
| 30 | + mock_loop = MagicMock() |
| 31 | + mock_loop.create_datagram_endpoint = _create_endpoint |
| 32 | + monkeypatch.setattr(lan.asyncio, "get_running_loop", lambda: mock_loop) |
| 33 | + |
| 34 | + async def _sleep(_timeout): |
| 35 | + for resp in responses: |
| 36 | + proto.datagram_received( |
| 37 | + json.dumps(resp).encode("utf-8"), ("192.168.1.50", 4002) |
| 38 | + ) |
| 39 | + |
| 40 | + monkeypatch.setattr(lan.asyncio, "sleep", _sleep) |
| 41 | + return transport, sock |
| 42 | + |
| 43 | + |
| 44 | +def _scan_response(**data): |
| 45 | + return {"msg": {"cmd": "scan", "data": data}} |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +async def test_sends_scan_request_to_multicast(monkeypatch): |
| 50 | + transport, _ = _install_fake_endpoint(monkeypatch, []) |
| 51 | + |
| 52 | + await lan.async_scan_lan_devices(timeout=0.01) |
| 53 | + |
| 54 | + transport.sendto.assert_called_once() |
| 55 | + payload, addr = transport.sendto.call_args[0] |
| 56 | + assert addr == (lan.LAN_MULTICAST_GROUP, lan.LAN_DISCOVERY_PORT) |
| 57 | + sent = json.loads(payload.decode()) |
| 58 | + assert sent == {"msg": {"cmd": "scan", "data": {"account_topic": "reserve"}}} |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_parses_and_returns_devices(monkeypatch): |
| 63 | + responses = [ |
| 64 | + _scan_response( |
| 65 | + ip="192.168.1.23", |
| 66 | + device="1F:80:C5:32:32:36:72:4E", |
| 67 | + sku="H6072", |
| 68 | + wifiVersionSoft="1.02.03", |
| 69 | + ) |
| 70 | + ] |
| 71 | + _install_fake_endpoint(monkeypatch, responses) |
| 72 | + |
| 73 | + devices = await lan.async_scan_lan_devices(timeout=0.01) |
| 74 | + |
| 75 | + assert len(devices) == 1 |
| 76 | + assert devices[0]["sku"] == "H6072" |
| 77 | + assert devices[0]["ip"] == "192.168.1.23" |
| 78 | + assert devices[0]["device"] == "1F:80:C5:32:32:36:72:4E" |
| 79 | + assert devices[0]["wifiVersionSoft"] == "1.02.03" |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_dedupes_by_device_id(monkeypatch): |
| 84 | + dup = _scan_response(ip="192.168.1.23", device="AA:BB:CC:DD:EE:FF:00:11", sku="H6072") |
| 85 | + _install_fake_endpoint(monkeypatch, [dup, dup]) |
| 86 | + |
| 87 | + devices = await lan.async_scan_lan_devices(timeout=0.01) |
| 88 | + |
| 89 | + assert len(devices) == 1 |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_no_responses_returns_empty(monkeypatch): |
| 94 | + _install_fake_endpoint(monkeypatch, []) |
| 95 | + |
| 96 | + devices = await lan.async_scan_lan_devices(timeout=0.01) |
| 97 | + |
| 98 | + assert devices == [] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_ignores_malformed_and_non_scan(monkeypatch): |
| 103 | + proto = lan._ScanProtocol() |
| 104 | + # Garbage bytes. |
| 105 | + proto.datagram_received(b"not-json", ("192.168.1.9", 4002)) |
| 106 | + # Valid JSON but a different command. |
| 107 | + proto.datagram_received( |
| 108 | + json.dumps({"msg": {"cmd": "devStatus", "data": {"onOff": 1}}}).encode(), |
| 109 | + ("192.168.1.9", 4002), |
| 110 | + ) |
| 111 | + # scan but empty data -> no usable record. |
| 112 | + proto.datagram_received(json.dumps(_scan_response()).encode(), ("192.168.1.9", 4002)) |
| 113 | + |
| 114 | + assert proto.responses == {} |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.asyncio |
| 118 | +async def test_bind_failure_raises_oserror(monkeypatch): |
| 119 | + _install_fake_endpoint(monkeypatch, [], bind_error=OSError("port in use")) |
| 120 | + |
| 121 | + with pytest.raises(OSError): |
| 122 | + await lan.async_scan_lan_devices(timeout=0.01) |
0 commit comments