66import os
77from typing import NamedTuple
88
9+ import aiofiles .os
10+
911from homeassistant .core import HomeAssistant
1012
1113from .const import (
@@ -27,25 +29,31 @@ class Library: # pylint: disable=too-few-public-methods
2729
2830 def __init__ (self , hass : HomeAssistant ) -> None :
2931 """Init."""
32+ self .hass = hass
33+
34+ async def initialize (self ):
35+ """Load the user and default libraries."""
3036
3137 # User Library
3238 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 ]
3541 ):
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 != "" :
3844 json_user_path = os .path .join (
3945 BUILT_IN_DATA_DIRECTORY ,
4046 user_library_filename ,
4147 )
4248 _LOGGER .debug ("Using user library file at %s" , json_user_path )
4349
4450 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 )
4754 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 ()
4957
5058 except FileNotFoundError :
5159 _LOGGER .error (
@@ -61,10 +69,12 @@ def __init__(self, hass: HomeAssistant) -> None:
6169 _LOGGER .debug ("Using library file at %s" , json_default_path )
6270
6371 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 )
6675 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 ()
6878
6979 except FileNotFoundError :
7080 _LOGGER .error (
@@ -73,7 +83,7 @@ def __init__(self, hass: HomeAssistant) -> None:
7383 )
7484
7585 @staticmethod
76- def factory (hass : HomeAssistant ) -> Library :
86+ async def factory (hass : HomeAssistant ) -> Library :
7787 """Return the library or create."""
7888
7989 if DOMAIN not in hass .data :
@@ -83,6 +93,7 @@ def factory(hass: HomeAssistant) -> Library:
8393 return hass .data [DOMAIN ][DATA_LIBRARY ] # type: ignore
8494
8595 library = Library (hass )
96+ await library .initialize ()
8697 hass .data [DOMAIN ][DATA_LIBRARY ] = library
8798 return library
8899
0 commit comments