async def test_get_device_info(mock_bind, mock_search): # DeviceInfo("192.168.1.29", 7000, "f4911e7aca59", "1e7aca59") mock_info = ( "1.1.1.0", "7000", "aabbcc001122", "MockDevice1", "MockBrand", "MockModel", "0.0.1-fake", ) mock_search.return_value = [mock_info] mock_bind.return_value = "St8Vw1Yz4Bc7Ef0H" """ The only way to get the key through binding is by scanning first """ devices = await Discovery.search_devices() device = Device(devices[0]) await device.bind() assert devices is not None assert len(devices) == 1 assert devices[0].ip == mock_info[0] assert devices[0].port == mock_info[1] assert devices[0].mac == mock_info[2] assert devices[0].name == mock_info[3] assert devices[0].brand == mock_info[4] assert devices[0].model == mock_info[5] assert devices[0].version == mock_info[6] assert device is not None assert device.device_key == "St8Vw1Yz4Bc7Ef0H"
async def test_device_late_bind(mock_push, mock_update, mock_bind): """Check that the device handles late binding sequences.""" fake_key = "abcdefgh12345678" mock_bind.return_value = fake_key mock_update.return_value = {} mock_push.return_value = [] info = DeviceInfo(*get_mock_info()) device = Device(info) assert device.device_info == info await device.update_state() assert device.device_key == fake_key device.device_key = None device.power = True await device.push_state_update() assert device.device_key == fake_key
async def try_bind_device(device_info: DeviceInfo) -> Device: """Try and bing with a discovered device. Note the you must bind with the device very quickly after it is discovered, or the process may not be completed correctly, raising a `CannotConnect` error. """ device = Device(device_info) try: await device.bind() except DeviceNotBoundError as exception: raise CannotConnect from exception return device
async def test_get_device_info(mock_bind): """Initialize device, check properties.""" info = DeviceInfo(*get_mock_info()) device = Device(info) assert device.device_info == info fake_key = "abcdefgh12345678" await device.bind(key=fake_key) assert device.device_key == fake_key
async def test_device_bind(mock_bind): """Check that the device returns a device key when binding.""" info = DeviceInfo(*get_mock_info()) device = Device(info) assert device.device_info == info fake_key = "abcdefgh12345678" mock_bind.return_value = fake_key await device.bind() assert device.device_key == fake_key
async def test_device_bind_none(mock_bind): """Check that the device handles bad binding sequences.""" info = DeviceInfo(*get_mock_info()) device = Device(info) assert device.device_info == info mock_bind.return_value = None with pytest.raises(DeviceNotBoundError): await device.bind() assert device.device_key is None
async def test_device_bind_timeout(mock_bind): """Check that the device handles timeout errors when binding.""" info = DeviceInfo(*get_mock_info()) device = Device(info) assert device.device_info == info mock_bind.side_effect = asyncio.TimeoutError with pytest.raises(DeviceTimeoutError): await device.bind() assert device.device_key is None
async def test_get_device_key_timeout(mock_bind, mock_search): mock_search.return_value = [("1.1.1.0", "7000", "aabbcc001122", "MockDevice1")] mock_bind.side_effect = asyncio.TimeoutError """ The only way to get the key through binding is by scanning first """ devices = await Discovery.search_devices() device = Device(devices[0]) with pytest.raises(DeviceNotBoundError): await device.bind() assert device is not None assert device.device_key is None
async def run_discovery(): devices = [] _LOGGER.debug("Scanning network for Gree devices") for device_info in await Discovery.search_devices(): device = Device(device_info) await device.bind() _LOGGER.debug( "Adding Gree device at %s:%i (%s)", device.device_info.ip, device.device_info.port, device.device_info.name, ) devices.append(device)
async def device_found(self, device_info: DeviceInfo) -> None: """Handle new device found on the network.""" device = Device(device_info) try: await device.bind() except DeviceNotBoundError: _LOGGER.error("Unable to bind to gree device: %s", device_info) except DeviceTimeoutError: _LOGGER.error("Timeout trying to bind to gree device: %s", device_info) _LOGGER.info( "Adding Gree device %s at %s:%i", device.device_info.name, device.device_info.ip, device.device_info.port, ) coordo = DeviceDataUpdateCoordinator(self.hass, device) self.hass.data[DOMAIN][COORDINATORS].append(coordo) await coordo.async_refresh() async_dispatcher_send(self.hass, DISPATCH_DEVICE_DISCOVERED, coordo)
async def generate_device_mock_async(): d = Device(("192.168.1.29", 7000, "f4911e7aca59", "1e7aca59")) await d.bind(key="St8Vw1Yz4Bc7Ef0H") return d
async def device_found(self, device_info: DeviceInfo) -> None: """A new device was found on the network.""" if self.bind: device = Device(device_info) await device.bind()