From 6994333aa4e36581b831ae0b2cb28a28e84c0281 Mon Sep 17 00:00:00 2001 From: mdeweerd Date: Thu, 8 Aug 2024 00:20:02 +0200 Subject: [PATCH] Make getVersion async --- custom_components/zha_toolkit/__init__.py | 6 +++--- custom_components/zha_toolkit/manifest.json | 2 +- custom_components/zha_toolkit/utils.py | 9 +++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/custom_components/zha_toolkit/__init__.py b/custom_components/zha_toolkit/__init__.py index 0dc603c..ea99816 100644 --- a/custom_components/zha_toolkit/__init__.py +++ b/custom_components/zha_toolkit/__init__.py @@ -652,12 +652,12 @@ async def async_setup(hass, config): return True LOGGER.debug("Setup services from async_setup") - await hass.async_add_executor_job(register_services, hass) + register_services(hass) return True -def register_services(hass): # noqa: C901 +async def register_services(hass): # noqa: C901 global LOADED_VERSION # pylint: disable=global-statement hass_ref = hass @@ -860,7 +860,7 @@ async def toolkit_service(service): schema=value, ) - LOADED_VERSION = u.getVersion() + LOADED_VERSION = await u.getVersion() async def command_handler_default( diff --git a/custom_components/zha_toolkit/manifest.json b/custom_components/zha_toolkit/manifest.json index 7f3b0b2..369cce9 100644 --- a/custom_components/zha_toolkit/manifest.json +++ b/custom_components/zha_toolkit/manifest.json @@ -6,6 +6,6 @@ "documentation": "https://github.com/mdeweerd/zha-toolkit", "iot_class": "local_polling", "issue_tracker": "https://github.com/mdeweerd/zha-toolkit/issues", - "requirements": ["pytz"], + "requirements": ["aiofiles>=0.4.0", "pytz>=>2016.10"], "version": "1.0.0" } diff --git a/custom_components/zha_toolkit/utils.py b/custom_components/zha_toolkit/utils.py index e3d0307..0282296 100644 --- a/custom_components/zha_toolkit/utils.py +++ b/custom_components/zha_toolkit/utils.py @@ -9,6 +9,7 @@ import typing from enum import Enum +import aiofiles import zigpy from homeassistant.components.zha.core.gateway import ZHAGateway from homeassistant.util import dt as dt_util @@ -54,7 +55,7 @@ def getZigpyVersion() -> str: return ZIGPY_VERSION -def getVersion() -> str: +async def getVersion() -> str: # pylint: disable=global-variable-undefined,used-before-assignment # pylint: disable=global-statement global VERSION_TIME @@ -85,9 +86,9 @@ def getVersion() -> str: # No version, or file change -> get version again LOGGER.debug(f"Read version from {fname} {ftime}<>{VERSION_TIME}") - with open(fname, encoding="utf_8") as infile: - VERSION_TIME = ftime - MANIFEST = json.load(infile) + async with aiofiles.open(fname, mode="r", encoding="utf_8") as infile: + json_raw = await infile.read() + MANIFEST = json.loads(json_raw) if MANIFEST is not None: if "version" in MANIFEST.keys():