6
6
import os
7
7
from typing import NamedTuple
8
8
9
+ import aiofiles .os
10
+
9
11
from homeassistant .core import HomeAssistant
10
12
11
13
from .const import (
@@ -27,25 +29,31 @@ class Library: # pylint: disable=too-few-public-methods
27
29
28
30
def __init__ (self , hass : HomeAssistant ) -> None :
29
31
"""Init."""
32
+ self .hass = hass
33
+
34
+ async def initialize (self ):
35
+ """Load the user and default libraries."""
30
36
31
37
# User Library
32
38
if (
33
- DOMAIN_CONFIG in hass .data [DOMAIN ]
34
- and CONF_USER_LIBRARY in hass .data [DOMAIN ][DOMAIN_CONFIG ]
39
+ DOMAIN_CONFIG in self . hass .data [DOMAIN ]
40
+ and CONF_USER_LIBRARY in self . hass .data [DOMAIN ][DOMAIN_CONFIG ]
35
41
):
36
- user_library_filename = hass .data [DOMAIN ][DOMAIN_CONFIG ].get (CONF_USER_LIBRARY )
37
- if user_library_filename != "" :
42
+ user_library_filename = self . hass .data [DOMAIN ][DOMAIN_CONFIG ].get (CONF_USER_LIBRARY )
43
+ if user_library_filename != "" :
38
44
json_user_path = os .path .join (
39
45
BUILT_IN_DATA_DIRECTORY ,
40
46
user_library_filename ,
41
47
)
42
48
_LOGGER .debug ("Using user library file at %s" , json_user_path )
43
49
44
50
try :
45
- with open (json_user_path , encoding = "utf-8" ) as user_file :
46
- user_json_data = json .load (user_file )
51
+ async with aiofiles .open (json_user_path , mode = "r" , encoding = "utf-8" ) as user_file :
52
+ content = await user_file .read ()
53
+ user_json_data = json .loads (content )
47
54
self ._devices = user_json_data ["devices" ]
48
- user_file .close ()
55
+ _LOGGER .debug ("Loaded %s user devices" , len (user_json_data ["devices" ]))
56
+ await user_file .close ()
49
57
50
58
except FileNotFoundError :
51
59
_LOGGER .error (
@@ -61,10 +69,12 @@ def __init__(self, hass: HomeAssistant) -> None:
61
69
_LOGGER .debug ("Using library file at %s" , json_default_path )
62
70
63
71
try :
64
- with open (json_default_path , encoding = "utf-8" ) as default_file :
65
- default_json_data = json .load (default_file )
72
+ async with aiofiles .open (json_default_path , mode = "r" , encoding = "utf-8" ) as default_file :
73
+ content = await default_file .read ()
74
+ default_json_data = json .loads (content )
66
75
self ._devices .extend (default_json_data ["devices" ])
67
- default_file .close ()
76
+ _LOGGER .debug ("Loaded %s default devices" , len (default_json_data ["devices" ]))
77
+ await default_file .close ()
68
78
69
79
except FileNotFoundError :
70
80
_LOGGER .error (
@@ -73,7 +83,7 @@ def __init__(self, hass: HomeAssistant) -> None:
73
83
)
74
84
75
85
@staticmethod
76
- def factory (hass : HomeAssistant ) -> Library :
86
+ async def factory (hass : HomeAssistant ) -> Library :
77
87
"""Return the library or create."""
78
88
79
89
if DOMAIN not in hass .data :
@@ -83,6 +93,7 @@ def factory(hass: HomeAssistant) -> Library:
83
93
return hass .data [DOMAIN ][DATA_LIBRARY ] # type: ignore
84
94
85
95
library = Library (hass )
96
+ await library .initialize ()
86
97
hass .data [DOMAIN ][DATA_LIBRARY ] = library
87
98
return library
88
99
0 commit comments