Skip to content

Commit

Permalink
Implements asynchronous device file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
vassilis-panos committed Jan 3, 2025
1 parent a6a3ee6 commit 99b0c61
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion custom_components/smartir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
_LOGGER = logging.getLogger(__name__)

DOMAIN = 'smartir'
VERSION = '1.17.10'
VERSION = '1.17.11'
MANIFEST_URL = (
"https://raw.githubusercontent.com/"
"smartHomeHub/SmartIR/{}/"
Expand Down
14 changes: 8 additions & 6 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import aiofiles
import json
import logging
import os.path
Expand Down Expand Up @@ -81,14 +82,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"place the file manually in the proper directory.")
return

with open(device_json_path) as j:
try:
try:
async with aiofiles.open(device_json_path, mode='r') as j:
_LOGGER.debug(f"loading json file {device_json_path}")
device_data = json.load(j)
content = await j.read()
device_data = json.loads(content)
_LOGGER.debug(f"{device_json_path} file loaded")
except Exception:
_LOGGER.error("The device Json file is invalid")
return
except Exception:
_LOGGER.error("The device JSON file is invalid")
return

async_add_entities([SmartIRClimate(
hass, config, device_data
Expand Down
16 changes: 10 additions & 6 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import aiofiles
import json
import logging
import os.path
Expand Down Expand Up @@ -74,12 +75,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"place the file manually in the proper directory.")
return

with open(device_json_path) as j:
try:
device_data = json.load(j)
except Exception:
_LOGGER.error("The device JSON file is invalid")
return
try:
async with aiofiles.open(device_json_path, mode='r') as j:
_LOGGER.debug(f"loading json file {device_json_path}")
content = await j.read()
device_data = json.loads(content)
_LOGGER.debug(f"{device_json_path} file loaded")
except Exception:
_LOGGER.error("The device JSON file is invalid")
return

async_add_entities([SmartIRFan(
hass, config, device_data
Expand Down
6 changes: 3 additions & 3 deletions custom_components/smartir/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"codeowners": ["@smartHomeHub"],
"requirements": ["aiofiles>=0.6.0"],
"homeassistant": "2024.10.0",
"version": "1.17.10",
"version": "1.17.11",
"updater": {
"version": "1.17.10",
"releaseNotes": "-- ClimateEntityFeature enum compatibility",
"version": "1.17.11",
"releaseNotes": "-- Implements asynchronous device file loading",
"files": [
"__init__.py",
"climate.py",
Expand Down
16 changes: 10 additions & 6 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import aiofiles
import json
import logging
import os.path
Expand Down Expand Up @@ -72,12 +73,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"place the file manually in the proper directory.")
return

with open(device_json_path) as j:
try:
device_data = json.load(j)
except Exception:
_LOGGER.error("The device JSON file is invalid")
return
try:
async with aiofiles.open(device_json_path, mode='r') as j:
_LOGGER.debug(f"loading json file {device_json_path}")
content = await j.read()
device_data = json.loads(content)
_LOGGER.debug(f"{device_json_path} file loaded")
except Exception:
_LOGGER.error("The device JSON file is invalid")
return

async_add_entities([SmartIRMediaPlayer(
hass, config, device_data
Expand Down

0 comments on commit 99b0c61

Please sign in to comment.