1212 _async_setup_shared_data ,
1313 async_get_config_and_coordinator ,
1414 async_setup )
15- from custom_components .multiscrape .const import (COORDINATOR , DOMAIN ,
16- PLATFORM_IDX , SCRAPER ,
17- SCRAPER_DATA , SCRAPER_IDX )
15+ from custom_components .multiscrape .const import DOMAIN , ENTITY_KEY , SCRAPER_ID
16+ from custom_components .multiscrape .registry import ScraperRegistry
1817
1918
2019@pytest .fixture
@@ -47,18 +46,13 @@ def empty_config():
4746@pytest .mark .async_test
4847@pytest .mark .timeout (10 )
4948async def test_async_setup_shared_data (hass : HomeAssistant ):
50- """Test _async_setup_shared_data creates required data structures ."""
49+ """Test _async_setup_shared_data creates a ScraperRegistry ."""
5150 # Act
5251 _async_setup_shared_data (hass )
5352
5453 # Assert
5554 assert DOMAIN in hass .data
56- assert SCRAPER_DATA in hass .data [DOMAIN ]
57- assert Platform .SENSOR in hass .data [DOMAIN ]
58- assert Platform .BINARY_SENSOR in hass .data [DOMAIN ]
59- assert Platform .BUTTON in hass .data [DOMAIN ]
60- assert isinstance (hass .data [DOMAIN ][SCRAPER_DATA ], list )
61- assert isinstance (hass .data [DOMAIN ][Platform .SENSOR ], list )
55+ assert isinstance (hass .data [DOMAIN ], ScraperRegistry )
6256
6357
6458@pytest .mark .integration
@@ -119,10 +113,11 @@ async def test_async_process_config_creates_scraper_data(
119113
120114 # Assert
121115 assert result is True
122- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 1
123- scraper_data = hass .data [DOMAIN ][SCRAPER_DATA ][0 ]
124- assert SCRAPER in scraper_data
125- assert COORDINATOR in scraper_data
116+ registry : ScraperRegistry = hass .data [DOMAIN ]
117+ assert len (registry .get_all ()) == 1
118+ instance = registry .get ("test_scraper" )
119+ assert instance .scraper is not None
120+ assert instance .coordinator is not None
126121
127122
128123@pytest .mark .integration
@@ -154,8 +149,11 @@ async def test_async_process_config_generates_name_for_unnamed_scraper(
154149
155150 # Assert
156151 assert result is True
157- # Should have generated a name like "Scraper_noname_0"
158- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 1
152+ registry : ScraperRegistry = hass .data [DOMAIN ]
153+ assert len (registry .get_all ()) == 1
154+ # Should have generated an ID from the resource URL
155+ instance = registry .get_all ()[0 ]
156+ assert "example" in instance .scraper_id
159157
160158
161159@pytest .mark .integration
@@ -191,9 +189,11 @@ async def test_async_process_config_stores_platform_configs(
191189 await _async_process_config (hass , minimal_config )
192190
193191 # Assert
194- assert len (hass .data [DOMAIN ][Platform .SENSOR ]) == 1
195- sensor_config = hass .data [DOMAIN ][Platform .SENSOR ][0 ]
196- assert sensor_config [CONF_NAME ] == "test_sensor"
192+ registry : ScraperRegistry = hass .data [DOMAIN ]
193+ instance = registry .get ("test_scraper" )
194+ sensor_configs = instance .platform_configs [Platform .SENSOR ]
195+ assert len (sensor_configs ) == 1
196+ assert list (sensor_configs .values ())[0 ][CONF_NAME ] == "test_sensor"
197197
198198
199199@pytest .mark .integration
@@ -234,9 +234,10 @@ async def test_async_process_config_with_multiple_platforms(hass: HomeAssistant)
234234 await _async_process_config (hass , config )
235235
236236 # Assert
237- assert len (hass .data [DOMAIN ][Platform .SENSOR ]) == 1
238- assert len (hass .data [DOMAIN ][Platform .BINARY_SENSOR ]) == 1
239- assert len (hass .data [DOMAIN ][Platform .BUTTON ]) == 1
237+ instance = hass .data [DOMAIN ].get ("multi_platform_scraper" )
238+ assert len (instance .platform_configs [Platform .SENSOR ]) == 1
239+ assert len (instance .platform_configs [Platform .BINARY_SENSOR ]) == 1
240+ assert len (instance .platform_configs [Platform .BUTTON ]) == 1
240241
241242
242243@pytest .mark .integration
@@ -267,8 +268,10 @@ async def test_async_process_config_with_multiple_sensors_per_scraper(
267268 await _async_process_config (hass , config )
268269
269270 # Assert
270- assert len (hass .data [DOMAIN ][Platform .SENSOR ]) == 3
271- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 1 # Only one scraper
271+ registry : ScraperRegistry = hass .data [DOMAIN ]
272+ instance = registry .get ("multi_sensor_scraper" )
273+ assert len (instance .platform_configs [Platform .SENSOR ]) == 3
274+ assert len (registry .get_all ()) == 1 # Only one scraper
272275
273276
274277@pytest .mark .integration
@@ -298,8 +301,10 @@ async def test_async_process_config_with_multiple_scrapers(hass: HomeAssistant):
298301 await _async_process_config (hass , config )
299302
300303 # Assert
301- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 2
302- assert len (hass .data [DOMAIN ][Platform .SENSOR ]) == 2
304+ registry : ScraperRegistry = hass .data [DOMAIN ]
305+ assert len (registry .get_all ()) == 2
306+ assert registry .get ("scraper1" ).platform_configs [Platform .SENSOR ].get ("sensor1" ) is not None
307+ assert registry .get ("scraper2" ).platform_configs [Platform .SENSOR ].get ("sensor2" ) is not None
303308 # Each scraper should have its own trigger service
304309 assert hass .services .has_service (DOMAIN , "trigger_scraper1" )
305310 assert hass .services .has_service (DOMAIN , "trigger_scraper2" )
@@ -315,7 +320,7 @@ async def test_async_get_config_and_coordinator(hass: HomeAssistant, minimal_con
315320 _async_setup_shared_data (hass )
316321 await _async_process_config (hass , minimal_config )
317322
318- discovery_info = {SCRAPER_IDX : 0 , PLATFORM_IDX : 0 }
323+ discovery_info = {SCRAPER_ID : "test_scraper" , ENTITY_KEY : "test_sensor" }
319324
320325 # Act
321326 conf , coordinator , scraper = await async_get_config_and_coordinator (
@@ -362,7 +367,8 @@ async def test_async_process_config_with_form_submit(hass: HomeAssistant):
362367
363368 # Assert
364369 assert result is True
365- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 1
370+ registry : ScraperRegistry = hass .data [DOMAIN ]
371+ assert len (registry .get_all ()) == 1
366372
367373
368374@pytest .mark .integration
@@ -395,7 +401,8 @@ async def test_async_process_config_with_resource_template(hass: HomeAssistant):
395401
396402 # Assert
397403 assert result is True
398- assert len (hass .data [DOMAIN ][SCRAPER_DATA ]) == 1
404+ registry : ScraperRegistry = hass .data [DOMAIN ]
405+ assert len (registry .get_all ()) == 1
399406
400407
401408@pytest .mark .integration
@@ -426,6 +433,7 @@ async def test_async_process_config_skips_platforms_not_in_config(hass: HomeAssi
426433 await _async_process_config (hass , config )
427434
428435 # Assert
429- assert len (hass .data [DOMAIN ][Platform .SENSOR ]) == 1
430- assert len (hass .data [DOMAIN ][Platform .BINARY_SENSOR ]) == 0
431- assert len (hass .data [DOMAIN ][Platform .BUTTON ]) == 0
436+ instance = hass .data [DOMAIN ].get ("sensor_only_scraper" )
437+ assert len (instance .platform_configs .get (Platform .SENSOR , {})) == 1
438+ assert len (instance .platform_configs .get (Platform .BINARY_SENSOR , {})) == 0
439+ assert len (instance .platform_configs .get (Platform .BUTTON , {})) == 0
0 commit comments