Skip to content

Commit

Permalink
Fixed Detected blocking call to open inside the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sdahl1234 committed Jun 6, 2024
1 parent bf3d342 commit d8f4a6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
28 changes: 18 additions & 10 deletions custom_components/seven_segments_alt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The seven_segments component."""

# import asyncio
from datetime import datetime, timedelta
import json
Expand Down Expand Up @@ -48,8 +49,7 @@

async def async_setup(hass: HomeAssistant, config: ConfigType): # noqa: D103
hass.async_create_task(
async_load_platform(hass, Platform.IMAGE_PROCESSING, DOMAIN, {}, config
)
async_load_platform(hass, Platform.IMAGE_PROCESSING, DOMAIN, {}, config)
)
return True

Expand Down Expand Up @@ -178,7 +178,9 @@ async def set_default_data(self):
async def file_exits(self):
"""Do file exists."""
try:
f = open(self.filepath, encoding="utf-8")
f = await self.hass.async_add_executor_job(
open, self.filepath, "r", -1, "utf-8"
)
f.close()
except FileNotFoundError:
# save a new file
Expand All @@ -189,27 +191,33 @@ async def save_data(self, append: bool):
"""Save data."""
try:
if append:
cfile = open(self.filepath, "w", encoding="utf-8")
cfile = await self.hass.async_add_executor_job(
open, self.filepath, "w", -1, "utf-8"
)
else:
cfile = open(self.filepath, "a", encoding="utf-8")
cfile = await self.hass.async_add_executor_job(
open, self.filepath, "a", -1, "utf-8"
)
ocrdata = json.dumps(self.jdata)
cfile.write(ocrdata)
cfile.close()
except Exception as ex: # pylint: disable=broad-except
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(f"Save data failed: {ex}") # noqa: G004

async def load_data(self):
"""Load data."""
try:
cfile = open(self.filepath, encoding="utf-8")
cfile = await self.hass.async_add_executor_job(
open, self.filepath, "r", -1, "utf-8"
)
ocrdata = cfile.read()
cfile.close()
_LOGGER.debug(f"ocrdata: {ocrdata}") # noqa: G004
_LOGGER.debug(f"jsonload: {json.loads(ocrdata)}") # noqa: G004

self.jdata = json.loads(ocrdata)
self.data_loaded = True
except Exception as ex: # pylint: disable=broad-except
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(f"load data failed: {ex}") # noqa: G004

async def _async_update_data(self):
Expand All @@ -221,6 +229,6 @@ async def _async_update_data(self):
self.image_entity.image_last_updated = datetime.now()
self.image_entity_2.image_last_updated = datetime.now()
self.image_entity_3.image_last_updated = datetime.now()
return None
except Exception as ex: # pylint: disable=broad-except
return None # noqa: TRY300
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(f"update failed: {ex}") # noqa: G004
13 changes: 8 additions & 5 deletions custom_components/seven_segments_alt/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def async_image(self) -> bytes | None:
roi_img.save(img_byte_arr, format="PNG")
img_byte_arr = img_byte_arr.getvalue()

except Exception as ex: # pylint: disable=broad-except
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(ex)
return None
return img_byte_arr
Expand Down Expand Up @@ -107,13 +107,16 @@ async def async_image(self) -> bytes | None:
"""Return bytes of image."""
try: # noqa: SIM105
_LOGGER.debug(self.data_coordinator.processed_name)
img = Image.open(self.data_coordinator.processed_name, mode="r")
img = await self.hass.async_add_executor_job(
Image.open, self.data_coordinator.processed_name, "r"
)
# img = Image.open(self.data_coordinator.processed_name, mode="r")
roi_img = img.convert("RGB")
img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format="PNG")
img_byte_arr = img_byte_arr.getvalue()

except Exception as ex: # pylint: disable=broad-except
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(ex)
return None
return img_byte_arr
Expand Down Expand Up @@ -147,7 +150,7 @@ async def async_image(self) -> bytes | None:
"""Return bytes of image."""
try:
_LOGGER.debug("New ss image")
return self.data_coordinator.ocr_image.content
except Exception as ex: # pylint: disable=broad-except
return self.data_coordinator.ocr_image.content # noqa: TRY300
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(ex)
return None
2 changes: 1 addition & 1 deletion custom_components/seven_segments_alt/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "local_polling",
"requirements": ["Pillow==10.3.0"],
"config_flow": true,
"version": "1.0.4"
"version": "1.0.5"
}

0 comments on commit d8f4a6c

Please sign in to comment.