diff --git a/CHANGELOG.md b/CHANGELOG.md index a632249..e7a3d53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.33 + +- Fix "detected blocking call to open inside the event loop by custom integration" error + ## 3.0.32 - Fix warning - StrEnum is a deprecated alias which will be removed in HA Core 2025.5. Use enum.StrEnum instead diff --git a/custom_components/aqua_temp/managers/aqua_temp_config_manager.py b/custom_components/aqua_temp/managers/aqua_temp_config_manager.py index bd2b129..c175681 100644 --- a/custom_components/aqua_temp/managers/aqua_temp_config_manager.py +++ b/custom_components/aqua_temp/managers/aqua_temp_config_manager.py @@ -3,6 +3,8 @@ import logging import os +import aiofiles + from homeassistant.config_entries import STORAGE_VERSION, ConfigEntry from homeassistant.const import ( CONF_PASSWORD, @@ -118,7 +120,7 @@ async def initialize(self, entry_config: dict): self._config_data.update(entry_config) - self._load_api_config() + await self._load_api_config() self._translations = await translation.async_get_translations( self._hass, self._hass.config.language, "entity", {DOMAIN} @@ -292,11 +294,11 @@ async def _load(self): await self._save() - self._load_entity_descriptions("default") + await self._load_entity_descriptions("default") self._load_pc_mapping("default") for product_id in PRODUCT_IDS: - self._load_entity_descriptions(product_id) + await self._load_entity_descriptions(product_id) self._load_pc_mapping(product_id) log_messages = [ @@ -370,62 +372,58 @@ async def _save(self): await self._store.async_save(store_data) - def _load_entity_descriptions(self, product_id: str): + async def _load_entity_descriptions(self, product_id: str): entities = copy(DEFAULT_ENTITY_DESCRIPTIONS) file_path = self._get_product_file( ProductParameter.ENTITY_DESCRIPTION, product_id ) - if not os.path.exists(file_path): - return - - with open(file_path) as file: - json_str = file.read() - - json_data = json.loads(json_str) - - for data_item in json_data: - platform = data_item.get("platform") - key = data_item.get("key") - translation_key = f"{product_id}_{key}".replace("/", "").lower() - - if platform == Platform.SENSOR: - sensor_entity = AquaTempSensorEntityDescription( - key=key, - name=data_item.get("name"), - device_class=data_item.get("device_class"), - native_unit_of_measurement=data_item.get("unit_of_measurement"), - entity_category=EntityCategory.DIAGNOSTIC, - translation_key=translation_key, - ) + file = await aiofiles.open(file_path) + json_str = await file.read() + await file.close() + + json_data = json.loads(json_str) + + for data_item in json_data: + platform = data_item.get("platform") + key = data_item.get("key") + translation_key = f"{product_id}_{key}".replace("/", "").lower() + + if platform == Platform.SENSOR: + sensor_entity = AquaTempSensorEntityDescription( + key=key, + name=data_item.get("name"), + device_class=data_item.get("device_class"), + native_unit_of_measurement=data_item.get("unit_of_measurement"), + entity_category=EntityCategory.DIAGNOSTIC, + translation_key=translation_key, + ) + + entities.append(sensor_entity) + + elif platform == Platform.BINARY_SENSOR: + binary_sensor_entity = AquaTempBinarySensorEntityDescription( + key=key, + name=data_item.get("name"), + device_class=data_item.get("device_class"), + on_value=data_item.get("on_value"), + entity_category=EntityCategory.DIAGNOSTIC, + translation_key=translation_key, + ) + + entities.append(binary_sensor_entity) - entities.append(sensor_entity) - - elif platform == Platform.BINARY_SENSOR: - binary_sensor_entity = AquaTempBinarySensorEntityDescription( - key=key, - name=data_item.get("name"), - device_class=data_item.get("device_class"), - on_value=data_item.get("on_value"), - entity_category=EntityCategory.DIAGNOSTIC, - translation_key=translation_key, - ) - - entities.append(binary_sensor_entity) - - else: - entity = AquaTempEntityDescription( - key=key, name=data_item.get("name") - ) + else: + entity = AquaTempEntityDescription(key=key, name=data_item.get("name")) - entities.append(entity) + entities.append(entity) self._update_platforms(entities) self._update_protocol_codes(product_id, entities) self._entity_descriptions[product_id] = entities - def _load_pc_mapping(self, product_id: str): + async def _load_pc_mapping(self, product_id: str): file_path = self._get_product_file(ProductParameter.MAPPING, product_id) if not os.path.exists(file_path): @@ -437,50 +435,49 @@ def _load_pc_mapping(self, product_id: str): fan_mode_mapping = {} fan_mode_reverse_mapping = {} - with open(file_path) as file: - json_str = file.read() + file = await aiofiles.open(file_path) + json_str = await file.read() + await file.close() - json_data = json.loads(json_str) + json_data = json.loads(json_str) - self._protocol_codes_configuration[product_id] = json_data + self._protocol_codes_configuration[product_id] = json_data - hvac_modes = json_data.get(CONFIG_HVAC_MODES) + hvac_modes = json_data.get(CONFIG_HVAC_MODES) - for hvac_mode_ha_key in hvac_modes: - hvac_mode = hvac_modes[hvac_mode_ha_key] - hvac_mode_api = hvac_mode.get(CONFIG_HVAC_SET) + for hvac_mode_ha_key in hvac_modes: + hvac_mode = hvac_modes[hvac_mode_ha_key] + hvac_mode_api = hvac_mode.get(CONFIG_HVAC_SET) - hvac_mode_mapping[hvac_mode_ha_key] = hvac_mode_api - hvac_mode_reverse_mapping[hvac_mode_api] = hvac_mode_ha_key + hvac_mode_mapping[hvac_mode_ha_key] = hvac_mode_api + hvac_mode_reverse_mapping[hvac_mode_api] = hvac_mode_ha_key - fan_modes = json_data.get(CONFIG_FAN_MODES) + fan_modes = json_data.get(CONFIG_FAN_MODES) - for fan_mode_ha in fan_modes: - fan_mode = fan_modes[fan_mode_ha] + for fan_mode_ha in fan_modes: + fan_mode = fan_modes[fan_mode_ha] - fan_mode_mapping[fan_mode_ha] = fan_mode - fan_mode_reverse_mapping[fan_mode] = fan_mode_ha + fan_mode_mapping[fan_mode_ha] = fan_mode + fan_mode_reverse_mapping[fan_mode] = fan_mode_ha - self._hvac_modes[product_id] = hvac_mode_mapping - self._hvac_modes_reverse[product_id] = hvac_mode_reverse_mapping + self._hvac_modes[product_id] = hvac_mode_mapping + self._hvac_modes_reverse[product_id] = hvac_mode_reverse_mapping - self._fan_modes[product_id] = fan_mode_mapping - self._fan_modes_reverse[product_id] = fan_mode_reverse_mapping + self._fan_modes[product_id] = fan_mode_mapping + self._fan_modes_reverse[product_id] = fan_mode_reverse_mapping - def _load_api_config(self): + async def _load_api_config(self): api_type = self._config_data.api_type config_file = f"../parameters/api.{api_type}.json" file_path = os.path.join(os.path.dirname(__file__), config_file) - if not os.path.exists(file_path): - return - - with open(file_path) as file: - json_str = file.read() + file = await aiofiles.open(file_path) + json_str = await file.read() + await file.close() - json_data = json.loads(json_str) + json_data = json.loads(json_str) - self._api_config = json_data + self._api_config = json_data def _update_platforms(self, entity_descriptions): for entity_description in entity_descriptions: diff --git a/custom_components/aqua_temp/managers/password_manager.py b/custom_components/aqua_temp/managers/password_manager.py index 631fbdb..e78fe57 100644 --- a/custom_components/aqua_temp/managers/password_manager.py +++ b/custom_components/aqua_temp/managers/password_manager.py @@ -1,5 +1,4 @@ import logging -from os import path, remove import sys from cryptography.fernet import Fernet, InvalidToken @@ -14,7 +13,6 @@ CONFIGURATION_FILE, DOMAIN, INVALID_TOKEN_SECTION, - LEGACY_KEY_FILE, STORAGE_DATA_KEY, ) @@ -117,25 +115,14 @@ async def _import_encryption_key(self): """Load the retained data from store and return de-serialized data.""" key = None - legacy_key_path = self._hass.config.path(LEGACY_KEY_FILE) + store = Store(self._hass, STORAGE_VERSION, f".{DOMAIN}", encoder=JSONEncoder) - if path.exists(legacy_key_path): - with open(legacy_key_path, "rb") as file: - key = file.read().decode("utf-8") + data = await store.async_load() - remove(legacy_key_path) - - else: - store = Store( - self._hass, STORAGE_VERSION, f".{DOMAIN}", encoder=JSONEncoder - ) - - data = await store.async_load() - - if data is not None: - key = data.get("key") + if data is not None: + key = data.get("key") - await store.async_remove() + await store.async_remove() if key is not None: self._encryption_key = key diff --git a/custom_components/aqua_temp/manifest.json b/custom_components/aqua_temp/manifest.json index adcaf80..5c5b1df 100644 --- a/custom_components/aqua_temp/manifest.json +++ b/custom_components/aqua_temp/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/radical-squared/aquatemp/issues", "requirements": [], - "version": "3.0.32" + "version": "3.0.33" }