Skip to content

Commit 99b0c61

Browse files
Implements asynchronous device file loading
1 parent a6a3ee6 commit 99b0c61

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

custom_components/smartir/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
_LOGGER = logging.getLogger(__name__)
2020

2121
DOMAIN = 'smartir'
22-
VERSION = '1.17.10'
22+
VERSION = '1.17.11'
2323
MANIFEST_URL = (
2424
"https://raw.githubusercontent.com/"
2525
"smartHomeHub/SmartIR/{}/"

custom_components/smartir/climate.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import aiofiles
23
import json
34
import logging
45
import os.path
@@ -81,14 +82,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
8182
"place the file manually in the proper directory.")
8283
return
8384

84-
with open(device_json_path) as j:
85-
try:
85+
try:
86+
async with aiofiles.open(device_json_path, mode='r') as j:
8687
_LOGGER.debug(f"loading json file {device_json_path}")
87-
device_data = json.load(j)
88+
content = await j.read()
89+
device_data = json.loads(content)
8890
_LOGGER.debug(f"{device_json_path} file loaded")
89-
except Exception:
90-
_LOGGER.error("The device Json file is invalid")
91-
return
91+
except Exception:
92+
_LOGGER.error("The device JSON file is invalid")
93+
return
9294

9395
async_add_entities([SmartIRClimate(
9496
hass, config, device_data

custom_components/smartir/fan.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import aiofiles
23
import json
34
import logging
45
import os.path
@@ -74,12 +75,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
7475
"place the file manually in the proper directory.")
7576
return
7677

77-
with open(device_json_path) as j:
78-
try:
79-
device_data = json.load(j)
80-
except Exception:
81-
_LOGGER.error("The device JSON file is invalid")
82-
return
78+
try:
79+
async with aiofiles.open(device_json_path, mode='r') as j:
80+
_LOGGER.debug(f"loading json file {device_json_path}")
81+
content = await j.read()
82+
device_data = json.loads(content)
83+
_LOGGER.debug(f"{device_json_path} file loaded")
84+
except Exception:
85+
_LOGGER.error("The device JSON file is invalid")
86+
return
8387

8488
async_add_entities([SmartIRFan(
8589
hass, config, device_data

custom_components/smartir/manifest.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"codeowners": ["@smartHomeHub"],
77
"requirements": ["aiofiles>=0.6.0"],
88
"homeassistant": "2024.10.0",
9-
"version": "1.17.10",
9+
"version": "1.17.11",
1010
"updater": {
11-
"version": "1.17.10",
12-
"releaseNotes": "-- ClimateEntityFeature enum compatibility",
11+
"version": "1.17.11",
12+
"releaseNotes": "-- Implements asynchronous device file loading",
1313
"files": [
1414
"__init__.py",
1515
"climate.py",

custom_components/smartir/media_player.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import aiofiles
23
import json
34
import logging
45
import os.path
@@ -72,12 +73,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
7273
"place the file manually in the proper directory.")
7374
return
7475

75-
with open(device_json_path) as j:
76-
try:
77-
device_data = json.load(j)
78-
except Exception:
79-
_LOGGER.error("The device JSON file is invalid")
80-
return
76+
try:
77+
async with aiofiles.open(device_json_path, mode='r') as j:
78+
_LOGGER.debug(f"loading json file {device_json_path}")
79+
content = await j.read()
80+
device_data = json.loads(content)
81+
_LOGGER.debug(f"{device_json_path} file loaded")
82+
except Exception:
83+
_LOGGER.error("The device JSON file is invalid")
84+
return
8185

8286
async_add_entities([SmartIRMediaPlayer(
8387
hass, config, device_data

0 commit comments

Comments
 (0)