Skip to content

Commit 118715d

Browse files
committed
♻️ refactor misc.py to make record_read_data async
- Modified `record_read_data` to be an async function - Updated calls to `record_read_data` in `misc_settime` to use `await` - This change is part of a larger effort to improve the async support in the ZHA Toolkit
1 parent 5929d40 commit 118715d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

custom_components/zha_toolkit/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ async def misc_settime(
290290
u.dict_to_jsonable(read_resp[0]),
291291
read_resp[1],
292292
)
293-
u.record_read_data(read_resp, cluster, params, listener)
293+
await u.record_read_data(read_resp, cluster, params, listener)
294294

295295
EPOCH2000_TIMESTAMP = 946684800
296296
utctime_towrite = utcnow().timestamp() - EPOCH2000_TIMESTAMP
@@ -312,7 +312,7 @@ async def misc_settime(
312312
u.dict_to_jsonable(read_resp[0]),
313313
read_resp[1],
314314
)
315-
u.record_read_data(read_resp, cluster, params, listener)
315+
await u.record_read_data(read_resp, cluster, params, listener)
316316

317317
event_data["success"] = True
318318
except DeliveryError as e:

custom_components/zha_toolkit/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@
3939

4040
# pylint: disable=too-many-lines
4141

42+
4243
async def get_ha_version() -> str:
4344
"""Get HA Version"""
4445
return await asyncio.to_thread(version, "homeassistant")
4546

47+
4648
HA_VERSION = asyncio.run(get_ha_version())
4749

50+
4851
async def get_zigpy_version() -> str:
4952
"""Get zigpy Version"""
5053
return await asyncio.to_thread(version, "zigpy")
5154

55+
5256
ZIGPY_VERSION = asyncio.run(get_zigpy_version())
5357

5458
if parse_version(HA_VERSION) < parse_version("2023.4"):
@@ -634,7 +638,9 @@ async def append_to_csvfile(
634638

635639
import csv
636640

637-
async with aiofiles.open(file_name, "w" if overwrite else "a", encoding="utf_8") as out:
641+
async with aiofiles.open(
642+
file_name, "w" if overwrite else "a", encoding="utf_8"
643+
) as out:
638644
writer = csv.writer(out)
639645
await writer.writerow(fields)
640646

@@ -644,7 +650,7 @@ async def append_to_csvfile(
644650
LOGGER.debug(f"Appended {desc} to '{file_name}'")
645651

646652

647-
def record_read_data(
653+
async def record_read_data(
648654
read_resp, cluster: zigpy.zcl.Cluster, params, listener=None
649655
):
650656
"""Record result from attr_write to CSV file if configured"""

0 commit comments

Comments
 (0)