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 9e14336 commit 8d9c4ab
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 98 deletions.
66 changes: 35 additions & 31 deletions custom_components/sunseeker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ def __init__(
self.brand = brand
self.always_update = True
self.data_handler = data_handler
self._devicesn = devicesn
self.data_handler.get_device(devicesn)._dataupdated = self.dataupdated
self.devicesn = devicesn
self.data_handler.get_device(devicesn).dataupdated = self.dataupdated
self.filepath = os.path.join(
self.hass.config.config_dir,
"Schedule-{}.json".format(self._devicesn.replace(" ", "_")),
"Schedule-{}.json".format(self.devicesn.replace(" ", "_")),
)
_LOGGER.info(self.filepath)
self.jdata = self.data_default
Expand All @@ -144,28 +144,26 @@ async def set_schedule_data(self):
async def GetSchedule(self, daynumber: int) -> str:
"""Get schedule."""
b_trim = (
self.data_handler.get_device(self._devicesn).Schedule.GetDay(daynumber).trim
self.data_handler.get_device(self.devicesn).Schedule.GetDay(daynumber).trim
)
if b_trim:
s_trim = " Trim"
else:
s_trim = ""
retval = {
self.data_handler.get_device(self._devicesn)
.Schedule.GetDay(daynumber)
.start
self.data_handler.get_device(self.devicesn).Schedule.GetDay(daynumber).start
+ " - "
+ self.data_handler.get_device(self._devicesn)
.Schedule.GetDay(daynumber)
.end
+ self.data_handler.get_device(self.devicesn).Schedule.GetDay(daynumber).end
+ s_trim
}
return str(retval).replace("{", "").replace("}", "").replace("'", "")

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 @@ -175,29 +173,35 @@ 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)
self.data_handler.get_device(self._devicesn).Schedule.SavedData = self.jdata
self.data_handler.get_device(self.devicesn).Schedule.SavedData = 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_handler.get_device(self._devicesn).Schedule.SavedData = self.jdata
self.data_handler.get_device(self.devicesn).Schedule.SavedData = self.jdata
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 save_schedule_data(self):
Expand All @@ -207,19 +211,19 @@ async def save_schedule_data(self):

def dataupdated(self, devicesn: str, schedule: bool):
"""Func Callback when data is updated."""
_LOGGER.debug(f"callback - Sunseeker {self._devicesn} data updated") # noqa: G004
if self._devicesn == devicesn:
_LOGGER.debug(f"callback - Sunseeker {self.devicesn} data updated") # noqa: G004
if self.devicesn == devicesn:
self.hass.add_job(self.async_set_updated_data, None)
if (
schedule
and not self.data_handler.get_device(self._devicesn).Schedule.IsEmpty()
and not self.data_handler.get_device(self.devicesn).Schedule.IsEmpty()
):
self.hass.add_job(self.save_schedule_data)

@property
def dsn(self):
"""DeviceSerialNumber."""
return self._devicesn
return self.devicesn

@property
def device_info(self) -> DeviceInfo:
Expand All @@ -228,30 +232,30 @@ def device_info(self) -> DeviceInfo:
identifiers={
(DOMAIN, self.unique_id),
},
model=self.data_handler.get_device(self._devicesn).DeviceModel,
model=self.data_handler.get_device(self.devicesn).DeviceModel,
manufacturer=self.brand,
serial_number=self._devicesn,
name=self.data_handler.get_device(self._devicesn).DeviceName,
sw_version=self.data_handler.get_device(self._devicesn)
serial_number=self.devicesn,
name=self.data_handler.get_device(self.devicesn).DeviceName,
sw_version=self.data_handler.get_device(self.devicesn)
.devicedata["data"]
.get("bbSv"),
hw_version=self.data_handler.get_device(self._devicesn)
hw_version=self.data_handler.get_device(self.devicesn)
.devicedata["data"]
.get("bbHv"),
)

@property
def unique_id(self) -> str:
"""Return the system descriptor."""
return f"{DOMAIN}-{self._devicesn}"
return f"{DOMAIN}-{self.devicesn}"

def update_device(self):
"""Update device."""
self.data_handler.update_devices(self._devicesn)
self.data_handler.update_devices(self.devicesn)

async def _async_update_data(self):
try:
await self.hass.async_add_executor_job(self.data_handler.update)
return self.data_handler
except Exception as ex: # pylint: disable=broad-except
return self.data_handler # noqa: TRY300
except Exception as ex: # pylint: disable=broad-except # noqa: BLE001
_LOGGER.debug(f"update failed: {ex}") # noqa: G004
2 changes: 1 addition & 1 deletion custom_components/sunseeker/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn

# This property is important to let HA know if this entity is online or not.
# If an entity is offline (return False), the UI will reflect this.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sunseeker/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn

async def async_press(self) -> None:
"""Handle the button press."""
Expand Down
7 changes: 4 additions & 3 deletions custom_components/sunseeker/device_tracker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Device tracker Sunseeker robotic mower."""

from __future__ import annotations

import logging
Expand Down Expand Up @@ -41,20 +42,20 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn
self._icon = "mdi:map-marker-radius"

@property
def latitude(self) -> float | None:
"""Return latitude value of the device."""
val = self._data_handler.get_device(self._sn).devicedata["data"].get("lat")
return val
return val # noqa: RET504

@property
def longitude(self) -> float | None:
"""Return longitude value of the device."""
val = self._data_handler.get_device(self._sn).devicedata["data"].get("lng")
return val
return val # noqa: RET504

@property
def source_type(self) -> Literal["gps"]:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sunseeker/lawn_mower.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, coordinator: SunseekerDataCoordinator) -> None:
super().__init__(coordinator)
self.coordinator = coordinator
self._data_handler = self.coordinator.data_handler
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn
self._name = self._data_handler.get_device(self._sn).DeviceName

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sunseeker/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"integration_type": "device",
"iot_class": "cloud_push",
"config_flow": true,
"version": "1.0.13"
"version": "1.0.14"
}
6 changes: 3 additions & 3 deletions custom_components/sunseeker/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn
self.icon = "mdi:clock-time-three-outline"

async def async_set_native_value(self, value: float) -> None:
Expand Down Expand Up @@ -134,7 +134,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn
self.icon = "mdi:map"
self.zonenumber = zonenumber

Expand Down Expand Up @@ -236,7 +236,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn
self.icon = "mdi:map"
self.mulnumber = mulnumber

Expand Down
3 changes: 2 additions & 1 deletion custom_components/sunseeker/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sensor."""

# import logging
import time

Expand Down Expand Up @@ -279,7 +280,7 @@ def __init__(
self._attr_has_entity_name = True
self._attr_translation_key = translationkey
self._attr_unique_id = f"{self._name}_{self.data_coordinator.dsn}"
self._sn = self.coordinator._devicesn
self._sn = self.coordinator.devicesn

def AddAttributes(self, day: str, data: any, attributes: dict) -> None:
"""Add schedule."""
Expand Down
Loading

0 comments on commit 8d9c4ab

Please sign in to comment.