Skip to content

Commit 639e3ff

Browse files
authored
Merge pull request #246 from mdeweerd/dev
Fix Async initialisation issues by making getVersion async
2 parents 2fe9507 + 6994333 commit 639e3ff

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

custom_components/zha_toolkit/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,12 @@ async def async_setup(hass, config):
657657
return True
658658

659659
LOGGER.debug("Setup services from async_setup")
660-
await hass.async_add_executor_job(register_services, hass)
660+
register_services(hass)
661661

662662
return True
663663

664664

665-
def register_services(hass): # noqa: C901
665+
async def register_services(hass): # noqa: C901
666666
global LOADED_VERSION # pylint: disable=global-statement
667667
hass_ref = hass
668668

@@ -865,7 +865,7 @@ async def toolkit_service(service):
865865
schema=value,
866866
)
867867

868-
LOADED_VERSION = u.getVersion()
868+
LOADED_VERSION = await u.getVersion()
869869

870870

871871
async def command_handler_default(

custom_components/zha_toolkit/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"documentation": "https://github.com/mdeweerd/zha-toolkit",
77
"iot_class": "local_polling",
88
"issue_tracker": "https://github.com/mdeweerd/zha-toolkit/issues",
9-
"requirements": ["pytz"],
9+
"requirements": ["aiofiles>=0.4.0", "pytz>=>2016.10"],
1010
"version": "1.0.0"
1111
}

custom_components/zha_toolkit/utils.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import typing
1010
from enum import Enum
1111

12+
import aiofiles
1213
import zigpy
1314

1415
try:
@@ -59,7 +60,7 @@ def getZigpyVersion() -> str:
5960
return ZIGPY_VERSION
6061

6162

62-
def getVersion() -> str:
63+
async def getVersion() -> str:
6364
# pylint: disable=global-variable-undefined,used-before-assignment
6465
# pylint: disable=global-statement
6566
global VERSION_TIME
@@ -90,9 +91,9 @@ def getVersion() -> str:
9091
# No version, or file change -> get version again
9192
LOGGER.debug(f"Read version from {fname} {ftime}<>{VERSION_TIME}")
9293

93-
with open(fname, encoding="utf_8") as infile:
94-
VERSION_TIME = ftime
95-
MANIFEST = json.load(infile)
94+
async with aiofiles.open(fname, mode="r", encoding="utf_8") as infile:
95+
json_raw = await infile.read()
96+
MANIFEST = json.loads(json_raw)
9697

9798
if MANIFEST is not None:
9899
if "version" in MANIFEST.keys():

0 commit comments

Comments
 (0)