Skip to content

Commit d6a669b

Browse files
Handle configuration domain missing (#3578)
1 parent 26e5ba7 commit d6a669b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

custom_components/battery_notes/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ async def async_step_device(
213213

214214
schema = DEVICE_SCHEMA
215215
# If show_all_devices = is specified and true, don't filter
216-
domain_config = self.hass.data[MY_KEY]
217-
if domain_config.show_all_devices:
218-
schema = DEVICE_SCHEMA_ALL
216+
domain_config = self.hass.data.get(MY_KEY)
217+
if domain_config and domain_config.show_all_devices:
218+
schema = DEVICE_SCHEMA_ALL
219219

220220
return self.async_show_form(
221221
step_id="device",

custom_components/battery_notes/library.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ def _load_library_json(library_file: str) -> dict[str, Any]:
4343
return cast(dict[str, Any], json.load(file))
4444

4545
# User Library
46-
domain_config = self.hass.data[MY_KEY]
47-
48-
if domain_config.user_library != "":
46+
domain_config = self.hass.data.get(MY_KEY)
47+
if domain_config and domain_config.user_library != "":
4948
json_user_path = self.hass.config.path(STORAGE_DIR, "battery_notes", domain_config.user_library)
5049
_LOGGER.debug("Using user library file at %s", json_user_path)
5150

custom_components/battery_notes/library_updater.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from homeassistant.helpers.event import async_track_utc_time_change
1919
from homeassistant.helpers.storage import STORAGE_DIR
2020

21-
from .coordinator import MY_KEY
21+
from .coordinator import MY_KEY, BatteryNotesDomainConfig
2222
from .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

Comments
 (0)