11"""Support for Huawei LTE routers."""
2- # pylint: disable=hass-use-runtime-data # Uses legacy hass.data[DOMAIN] pattern
32
43from __future__ import annotations
54
98from dataclasses import dataclass , field
109from datetime import timedelta
1110import logging
12- from typing import Any , NamedTuple , cast
11+ from typing import Any , cast
1312from xml .parsers .expat import ExpatError
1413
1514from huawei_lte_api .Client import Client
6463 DEFAULT_MANUFACTURER ,
6564 DEFAULT_NOTIFY_SERVICE_NAME ,
6665 DOMAIN ,
66+ HUAWEI_LTE_CONFIG ,
6767 KEY_DEVICE_BASIC_INFORMATION ,
6868 KEY_DEVICE_INFORMATION ,
6969 KEY_DEVICE_SIGNAL ,
@@ -108,7 +108,7 @@ class Router:
108108 """Class for router state."""
109109
110110 hass : HomeAssistant
111- config_entry : ConfigEntry
111+ config_entry : HuaweiLteConfigEntry
112112 connection : Connection
113113 url : str
114114
@@ -278,14 +278,10 @@ def cleanup(self, *_: Any) -> None:
278278 self .connection .requests_session .close ()
279279
280280
281- class HuaweiLteData (NamedTuple ):
282- """Shared state."""
281+ type HuaweiLteConfigEntry = ConfigEntry [Router ]
283282
284- hass_config : ConfigType
285- routers : dict [str , Router ]
286283
287-
288- async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
284+ async def async_setup_entry (hass : HomeAssistant , entry : HuaweiLteConfigEntry ) -> bool :
289285 """Set up Huawei LTE component from config entry."""
290286 url = entry .data [CONF_URL ]
291287
@@ -352,7 +348,7 @@ def _connect() -> Connection:
352348 return False
353349
354350 # Store reference to router
355- hass . data [ DOMAIN ]. routers [ entry .entry_id ] = router
351+ entry .runtime_data = router
356352
357353 # Clear all subscriptions, enabled entities will push back theirs
358354 router .subscriptions .clear ()
@@ -417,7 +413,7 @@ def _connect() -> Connection:
417413 CONF_NAME : entry .options .get (CONF_NAME , DEFAULT_NOTIFY_SERVICE_NAME ),
418414 CONF_RECIPIENT : entry .options .get (CONF_RECIPIENT ),
419415 },
420- hass .data [DOMAIN ]. hass_config ,
416+ hass .data [HUAWEI_LTE_CONFIG ] ,
421417 )
422418
423419 def _update_router (* _ : Any ) -> None :
@@ -440,46 +436,47 @@ def _update_router(*_: Any) -> None:
440436 return True
441437
442438
443- async def async_unload_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
439+ async def async_unload_entry (
440+ hass : HomeAssistant , config_entry : HuaweiLteConfigEntry
441+ ) -> bool :
444442 """Unload config entry."""
445443
446444 # Forward config entry unload to platforms
447445 await hass .config_entries .async_unload_platforms (config_entry , PLATFORMS )
448446
449- # Forget about the router and invoke its cleanup
450- router = hass .data [DOMAIN ].routers .pop (config_entry .entry_id )
451- await hass .async_add_executor_job (router .cleanup )
447+ # Invoke router cleanup
448+ await hass .async_add_executor_job (config_entry .runtime_data .cleanup )
452449
453450 return True
454451
455452
456453async def async_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
457454 """Set up Huawei LTE component."""
458455
459- if DOMAIN not in hass .data :
460- hass .data [DOMAIN ] = HuaweiLteData (hass_config = config , routers = {})
456+ hass .data [HUAWEI_LTE_CONFIG ] = config
461457
462458 def service_handler (service : ServiceCall ) -> None :
463459 """Apply a service.
464460
465461 We key this using the router URL instead of its unique id / serial number,
466462 because the latter is not available anywhere in the UI.
467463 """
468- routers = hass .data [DOMAIN ].routers
464+ routers = [
465+ entry .runtime_data
466+ for entry in hass .config_entries .async_loaded_entries (DOMAIN )
467+ ]
469468 if url := service .data .get (CONF_URL ):
470- router = next (
471- (router for router in routers .values () if router .url == url ), None
472- )
469+ router = next ((router for router in routers if router .url == url ), None )
473470 elif not routers :
474471 _LOGGER .error ("%s: no routers configured" , service .service )
475472 return
476473 elif len (routers ) == 1 :
477- router = next ( iter ( routers . values ()))
474+ router = routers [ 0 ]
478475 else :
479476 _LOGGER .error (
480477 "%s: more than one router configured, must specify one of URLs %s" ,
481478 service .service ,
482- sorted (router .url for router in routers . values () ),
479+ sorted (router .url for router in routers ),
483480 )
484481 return
485482 if not router :
@@ -509,7 +506,9 @@ def service_handler(service: ServiceCall) -> None:
509506 return True
510507
511508
512- async def async_migrate_entry (hass : HomeAssistant , config_entry : ConfigEntry ) -> bool :
509+ async def async_migrate_entry (
510+ hass : HomeAssistant , config_entry : HuaweiLteConfigEntry
511+ ) -> bool :
513512 """Migrate config entry to new version."""
514513 if config_entry .version == 1 :
515514 options = dict (config_entry .options )
0 commit comments