1818from homeassistant .helpers .event import async_track_utc_time_change
1919from homeassistant .helpers .storage import STORAGE_DIR
2020
21- from .coordinator import MY_KEY
21+ from .coordinator import MY_KEY , BatteryNotesDomainConfig
2222from .discovery import DiscoveryManager
2323
2424_LOGGER = logging .getLogger (__name__ )
@@ -38,7 +38,9 @@ def __init__(self, hass: HomeAssistant):
3838 """Initialize the library updater."""
3939 self .hass = hass
4040
41- domain_config = self .hass .data [MY_KEY ]
41+ domain_config = self .hass .data .get (MY_KEY )
42+ if not domain_config :
43+ domain_config = BatteryNotesDomainConfig ()
4244
4345 library_url = domain_config .library_url
4446 schema_url = domain_config .schema_url
@@ -61,7 +63,7 @@ async def timer_update(self, now: datetime):
6163
6264 domain_config = self .hass .data [MY_KEY ]
6365
64- if domain_config .enable_autodiscovery :
66+ if domain_config and domain_config .enable_autodiscovery :
6567 discovery_manager = DiscoveryManager (self .hass , domain_config )
6668 await discovery_manager .start_discovery ()
6769 else :
@@ -90,7 +92,9 @@ def _update_library_json(library_file: str, content: str) -> None:
9092 _update_library_json , json_path , content
9193 )
9294
93- self .hass .data [MY_KEY ].library_last_update = datetime .now ()
95+ domain_config = self .hass .data .get (MY_KEY )
96+ if domain_config :
97+ self .hass .data [MY_KEY ].library_last_update = datetime .now ()
9498
9599 _LOGGER .debug ("Updated library" )
96100 else :
@@ -117,6 +121,10 @@ async def copy_schema(self):
117121 async def time_to_update_library (self , hours : int ) -> bool :
118122 """Check when last updated and if OK to do a new library update."""
119123 try :
124+ domain_config = self .hass .data .get (MY_KEY )
125+ if not domain_config :
126+ return True
127+
120128 if library_last_update := self .hass .data [MY_KEY ].library_last_update :
121129 time_since_last_update = (
122130 datetime .now () - library_last_update
0 commit comments