@@ -227,6 +227,11 @@ async def test_discover_home_empty_cache(
227227 assert current_map_data .map_flag == 0
228228 assert current_map_data .name == "Ground Floor"
229229
230+ # Verify the persistent cache has been updated
231+ cache_data = await home_trait ._cache .get ()
232+ assert len (cache_data .home_map_info ) == 2
233+ assert len (cache_data .home_map_content ) == 2
234+
230235
231236async def test_discover_home_with_existing_cache (
232237 home_trait : HomeTrait ,
@@ -372,17 +377,6 @@ async def test_refresh_updates_current_map_cache(
372377 assert home_trait .home_map_content [0 ].image_content == TEST_IMAGE_CONTENT_1
373378
374379
375- async def test_refresh_no_cache_no_update (
376- home_trait : HomeTrait ,
377- mock_rpc_channel : AsyncMock ,
378- mock_mqtt_rpc_channel : AsyncMock ,
379- ) -> None :
380- """Test that refresh doesn't update when no cache exists."""
381- # Perform refresh without existing cache
382- with pytest .raises (Exception , match = "Cannot refresh home data without home cache, did you call discover_home()?" ):
383- await home_trait .refresh ()
384-
385-
386380async def test_current_map_data_property (
387381 home_trait : HomeTrait ,
388382 mock_rpc_channel : AsyncMock ,
@@ -425,8 +419,14 @@ async def test_discover_home_device_busy_cleaning(
425419 home_trait : HomeTrait ,
426420 mock_rpc_channel : AsyncMock ,
427421 mock_mqtt_rpc_channel : AsyncMock ,
422+ mock_map_rpc_channel : AsyncMock ,
428423) -> None :
429- """Test that discovery raises RoborockDeviceBusy when device is cleaning."""
424+ """Test that discovery raises RoborockDeviceBusy when device is cleaning.
425+
426+ This tests the initial failure scenario during discovery where the device
427+ is busy, then a retry attepmt where the device is still busy, then finally
428+ a successful attempt to run discovery when the device is idle.
429+ """
430430 # Set the status trait state to cleaning
431431 status_trait .state = RoborockStateCode .cleaning
432432
@@ -438,6 +438,80 @@ async def test_discover_home_device_busy_cleaning(
438438 assert mock_rpc_channel .send_command .call_count == 0
439439 assert mock_mqtt_rpc_channel .send_command .call_count == 0
440440
441+ # Setup mocks for refresh
442+ mock_rpc_channel .send_command .side_effect = [
443+ ROOM_MAPPING_DATA_MAP_0 , # Room mapping refresh
444+ ]
445+ mock_mqtt_rpc_channel .send_command .side_effect = [
446+ MULTI_MAP_LIST_DATA , # Maps refresh
447+ ]
448+ mock_map_rpc_channel .send_command .side_effect = [
449+ MAP_BYTES_RESPONSE_1 , # Map bytes refresh
450+ ]
451+
452+ # Now attempt to refresh the device while cleaning. This should still fail
453+ # home discovery but allow refresh to update the current map.
454+ await home_trait .refresh ()
455+
456+ # Verify the home information is now populated
457+ current_data = home_trait .current_map_data
458+ assert current_data is not None
459+ assert current_data .map_flag == 0
460+ assert current_data .name == "Ground Floor"
461+ assert home_trait .home_map_info is not None
462+ assert home_trait .home_map_info .keys () == {0 }
463+ assert home_trait .home_map_content is not None
464+ assert home_trait .home_map_content .keys () == {0 }
465+ map_0_content = home_trait .home_map_content [0 ]
466+ assert map_0_content is not None
467+ assert map_0_content .image_content == TEST_IMAGE_CONTENT_1
468+ assert map_0_content .map_data is not None
469+
470+ # Verify the persistent cache has not been updated since discovery
471+ # has not fully completed.
472+ cache_data = await home_trait ._cache .get ()
473+ assert not cache_data .home_map_info
474+ assert not cache_data .home_map_content
475+
476+ # Set the status trait state to idle which will mean we can attempt discovery
477+ # on the next refresh. This should have the result of updating the
478+ # persistent cache which is verified below.
479+ status_trait .state = RoborockStateCode .idle
480+
481+ # Setup mocks for the discovery process
482+ mock_rpc_channel .send_command .side_effect = [
483+ UPDATED_STATUS_MAP_123 , # Status after switching to map 123
484+ ROOM_MAPPING_DATA_MAP_123 , # Rooms for map 123
485+ UPDATED_STATUS_MAP_0 , # Status after switching back to map 0
486+ ROOM_MAPPING_DATA_MAP_0 , # Rooms for map 0
487+ ]
488+ mock_mqtt_rpc_channel .send_command .side_effect = [
489+ MULTI_MAP_LIST_DATA , # Multi maps list
490+ {}, # LOAD_MULTI_MAP response for map 123
491+ {}, # LOAD_MULTI_MAP response back to map 0
492+ ]
493+ mock_map_rpc_channel .send_command .side_effect = [
494+ MAP_BYTES_RESPONSE_2 , # Map bytes for 123
495+ MAP_BYTES_RESPONSE_1 , # Map bytes for 0
496+ ]
497+
498+ # Refreshing should now perform discovery successfully
499+ await home_trait .refresh ()
500+
501+ # Verify we now have all of the information populated from discovery
502+ assert home_trait .home_map_info is not None
503+ assert len (home_trait .home_map_info ) == 2
504+
505+ assert home_trait .home_map_info is not None
506+ assert home_trait .home_map_info .keys () == {0 , 123 }
507+ assert home_trait .home_map_content is not None
508+ assert home_trait .home_map_content .keys () == {0 , 123 }
509+
510+ # Verify the persistent cache has been updated
511+ cache_data = await home_trait ._cache .get ()
512+ assert len (cache_data .home_map_info ) == 2
513+ assert len (cache_data .home_map_content ) == 2
514+
441515
442516async def test_single_map_no_switching (
443517 home_trait : HomeTrait ,
0 commit comments