Skip to content

Commit

Permalink
Modified Sequence and added cancel to the async_listen to avoid dupli…
Browse files Browse the repository at this point in the history
…cated events

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
sca075 committed Dec 8, 2024
1 parent 144716b commit 7a6ea03
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions custom_components/mqtt_vacuum_camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
async_load_file,
is_auth_updated,
)
from .utils.image_handler_utils import resize_to_aspect_ratio
# from .utils.image_handler_utils import resize_to_aspect_ratio

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)

Expand Down Expand Up @@ -82,6 +82,7 @@ class MQTTCamera(CoordinatorEntity, Camera):
_attr_has_entity_name = True
_unrecorded_attributes = frozenset({MATCH_ALL})

# noinspection PyUnusedLocal
def __init__(self, coordinator, device_info):
super().__init__(coordinator)
Camera.__init__(self)
Expand Down Expand Up @@ -123,10 +124,10 @@ def __init__(self, coordinator, device_info):
self.processor = CameraProcessor(self.hass, self._shared)

# Listen to the vacuum.start event
self.hass.bus.async_listen("event_vacuum_start", self.handle_vacuum_start)
self.hass.bus.async_listen(
"mqtt_vacuum_camera_obstacle_coordinates", self.handle_obstacle_view
)
cancel = self.hass.bus.async_listen("event_vacuum_start", self.handle_vacuum_start)
cancel = self.hass.bus.async_listen(
"mqtt_vacuum_camera_obstacle_coordinates", self.handle_obstacle_view
)

@staticmethod
def _start_up_logs():
Expand Down Expand Up @@ -253,7 +254,6 @@ def should_poll(self) -> bool:
CameraModes.MAP_VIEW,
]:
self._should_poll = True
_LOGGER.debug(f"{self._file_name}: Camera Polling: {self._should_poll}")
return self._should_poll

@property
Expand Down Expand Up @@ -565,7 +565,9 @@ async def _set_map_view_mode(reason: str = None):
async def _async_find_nearest_obstacle(x, y, all_obstacles):
"""Find the nearest obstacle to the given coordinates."""
nearest_obstacles = None
min_distance = 50 # Start 50 pixels distance
width = self._shared.image_ref_width
height = self._shared.image_ref_height
min_distance = round(60 * (width / height)) # (60 * aspect ratio) pixels distance
_LOGGER.debug(
f"Finding in the nearest {min_distance} pixels obstacle to coordinates: {x}, {y}"
)
Expand Down Expand Up @@ -636,41 +638,38 @@ async def _async_find_nearest_obstacle(x, y, all_obstacles):
)
return # Return to Camera Mode
if temp_image is not None:
self._shared.camera_mode = CameraModes.OBSTACLE_VIEW
_LOGGER.debug(
f"Camera Mode Change to {self._shared.camera_mode}"
)
try:
# Open the downloaded image with PIL
pil_img = await self.processor.async_open_image(
temp_image
pil_img = await self.hass.async_create_task(
self.processor.async_open_image(temp_image)
)

# Resize the image if resize_to is provided
width = self._shared.image_ref_width
height = self._shared.image_ref_height
(resized_image, _) = await resize_to_aspect_ratio(
pil_img,
width,
height,
self._shared.image_aspect_ratio,
)
_LOGGER.debug(
f"{self._file_name}: Image resized to: {width}, {height}"
# width = self._shared.image_ref_width
# height = self._shared.image_ref_height
# (resized_image, _) = await resize_to_aspect_ratio(
# pil_img,
# width,
# height,
# self._shared.image_aspect_ratio,
# None,
# )
# _LOGGER.debug(
# f"{self._file_name}: Image resized to: {width}, {height}"
# )
self.Image = await self.hass.async_create_task(
self.run_async_pil_to_bytes(pil_img)
)
return
except Exception as e:
_LOGGER.warning(
f"{self._file_name}: Unexpected Error processing image: {e}"
)
self._shared.camera_mode = CameraModes.MAP_VIEW
_LOGGER.debug(
f"Camera Mode Change to {self._shared.camera_mode}"
)
return # Return to Camera Mode

self.Image = await self.hass.async_create_task(
self.run_async_pil_to_bytes(resized_image)
)
self._shared.camera_mode = CameraModes.OBSTACLE_VIEW
_LOGGER.debug(
f"Camera Mode Change to {self._shared.camera_mode}"
)
await _set_map_view_mode()
return
else:
await _set_map_view_mode("No image downloaded.")
else:
Expand Down

0 comments on commit 7a6ea03

Please sign in to comment.