Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix blocking call #87

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
143 changes: 70 additions & 73 deletions custom_components/aqua_temp/managers/aqua_temp_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import os

import aiofiles

from homeassistant.config_entries import STORAGE_VERSION, ConfigEntry
from homeassistant.const import (
CONF_PASSWORD,
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down
23 changes: 5 additions & 18 deletions custom_components/aqua_temp/managers/password_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from os import path, remove
import sys

from cryptography.fernet import Fernet, InvalidToken
Expand All @@ -14,7 +13,6 @@
CONFIGURATION_FILE,
DOMAIN,
INVALID_TOKEN_SECTION,
LEGACY_KEY_FILE,
STORAGE_DATA_KEY,
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/aqua_temp/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Loading