| 1 |
import asyncio |
| 2 |
import unittest |
| 3 |
from unittest.mock import Mock |
| 4 |
from govee_local_api.device import GoveeDevice |
| 5 |
from govee_local_api.controller import GoveeController |
| 6 |
from govee_local_api.protocol import GoveeControllerProtocol |
| 7 |
from govee_local_api.message import ScanResponse |
| 8 |
|
| 9 |
|
| 10 |
class TestGoveeDevice(unittest.TestCase): |
| 11 |
def setUp(self): |
| 12 |
self._mock_controller = Mock(spec=GoveeController) |
| 13 |
self.device = GoveeDevice( |
| 14 |
self._mock_controller, "192.168.1.100", "AA:BB:CC:DD:EE:FF", "H6001", None |
| 15 |
) |
| 16 |
|
| 17 |
def test_initial_ip(self): |
| 18 |
assert self.device.ip == "192.168.1.100" |
| 19 |
|
| 20 |
def test_update_ip(self): |
| 21 |
self.device.update_ip("192.168.1.200") |
| 22 |
assert self.device.ip == "192.168.1.200" |
| 23 |
|
| 24 |
def test_update_ip_multiple_times(self): |
| 25 |
self.device.update_ip("192.168.1.200") |
| 26 |
assert self.device.ip == "192.168.1.200" |
| 27 |
self.device.update_ip("10.0.0.50") |
| 28 |
assert self.device.ip == "10.0.0.50" |
| 29 |
|
| 30 |
def _backdate_lastseen(self, seconds): |
| 31 |
from datetime import datetime, timedelta, timezone |
| 32 |
|
| 33 |
self.device._lastseen = datetime.now(timezone.utc) - timedelta(seconds=seconds) |
| 34 |
|
| 35 |
def test_is_connected_uses_controller_evict_interval(self): |
| 36 |
self._mock_controller.evict_interval = 60 |
| 37 |
self._backdate_lastseen(45) |
| 38 |
assert self.device.is_connected |
| 39 |
|
| 40 |
self._mock_controller.evict_interval = 30 |
| 41 |
assert not self.device.is_connected |
| 42 |
|
| 43 |
def test_is_connected_fresh_device(self): |
| 44 |
self._mock_controller.evict_interval = 30 |
| 45 |
assert self.device.is_connected |
| 46 |
|
| 47 |
|
| 48 |
class TestControllerIpUpdate(unittest.TestCase): |
| 49 |
def setUp(self): |
| 50 |
self.controller = GoveeController.__new__(GoveeController) |
| 51 |
self.controller._registry = Mock() |
| 52 |
self.controller._device_discovered_callback = None |
| 53 |
self.controller._evict_enabled = False |
| 54 |
self.controller._logger = Mock() |
| 55 |
self.device = GoveeDevice( |
| 56 |
self.controller, "192.168.1.100", "AA:BB:CC:DD:EE:FF", "H6001", None |
| 57 |
) |
| 58 |
self.mock_protocol = Mock(spec=GoveeControllerProtocol) |
| 59 |
self.mock_protocol.transport = Mock() |
| 60 |
|
| 61 |
def test_scan_response_updates_ip_when_changed(self): |
| 62 |
self.controller._registry.get_device_by_fingerprint = Mock( |
| 63 |
return_value=self.device |
| 64 |
) |
| 65 |
|
| 66 |
scan_data = { |
| 67 |
"device": "AA:BB:CC:DD:EE:FF", |
| 68 |
"sku": "H6001", |
| 69 |
"ip": "192.168.1.200", |
| 70 |
} |
| 71 |
scan_response = ScanResponse(scan_data) |
| 72 |
|
| 73 |
asyncio.run( |
| 74 |
self.controller._handle_scan_response( |
| 75 |
scan_response, ("192.168.1.100", 4002), self.mock_protocol |
| 76 |
) |
| 77 |
) |
| 78 |
|
| 79 |
assert self.device.ip == "192.168.1.200" |
| 80 |
|
| 81 |
def test_scan_response_does_not_update_ip_when_same(self): |
| 82 |
self.controller._registry.get_device_by_fingerprint = Mock( |
| 83 |
return_value=self.device |
| 84 |
) |
| 85 |
|
| 86 |
scan_data = { |
| 87 |
"device": "AA:BB:CC:DD:EE:FF", |
| 88 |
"sku": "H6001", |
| 89 |
"ip": "192.168.1.100", |
| 90 |
} |
| 91 |
scan_response = ScanResponse(scan_data) |
| 92 |
|
| 93 |
asyncio.run( |
| 94 |
self.controller._handle_scan_response( |
| 95 |
scan_response, ("192.168.1.100", 4002), self.mock_protocol |
| 96 |
) |
| 97 |
) |
| 98 |
|
| 99 |
assert self.device.ip == "192.168.1.100" |
| 100 |
|
| 101 |
for call in self.controller._logger.debug.call_args_list: |
| 102 |
assert "IP changed" not in str(call) |
| 103 |
|
| 104 |
def test_scan_response_handles_missing_ip(self): |
| 105 |
self.controller._registry.get_device_by_fingerprint = Mock( |
| 106 |
return_value=self.device |
| 107 |
) |
| 108 |
|
| 109 |
scan_data = { |
| 110 |
"device": "AA:BB:CC:DD:EE:FF", |
| 111 |
"sku": "H6001", |
| 112 |
} |
| 113 |
scan_response = ScanResponse(scan_data) |
| 114 |
|
| 115 |
original_ip = self.device.ip |
| 116 |
asyncio.run( |
| 117 |
self.controller._handle_scan_response( |
| 118 |
scan_response, ("192.168.1.100", 4002), self.mock_protocol |
| 119 |
) |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
assert self.device.ip == original_ip |