Skip to content

Commit

Permalink
Rand256 Image Handler Modifications #232 from sca075/220-addig-on-off…
Browse files Browse the repository at this point in the history
…-for-map-trims

Finalize Rand256 Image Handler Modifications.
  • Loading branch information
sca075 authored Aug 6, 2024
2 parents a315470 + 2a97440 commit d0f0641
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 138 deletions.
5 changes: 1 addition & 4 deletions custom_components/mqtt_vacuum_camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
Platform,
)
from homeassistant.core import ServiceCall
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryNotReady, ServiceValidationError
from homeassistant.helpers.reload import async_register_admin_service
from homeassistant.helpers.storage import STORAGE_DIR
from homeassistant.exceptions import (
ServiceValidationError,
)

from .common import (
get_device_info,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mqtt_vacuum_camera/camera_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CameraProcessor:
def __init__(self, hass, camera_shared):
self.hass = hass
self._map_handler = MapImageHandler(camera_shared, hass)
self._re_handler = ReImageHandler(camera_shared)
self._re_handler = ReImageHandler(camera_shared, hass)
self._shared = camera_shared
self._file_name = self._shared.file_name
self._translations_path = self.hass.config.path(
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mqtt_vacuum_camera/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"documentation": "https://github.com/sca075/mqtt_vacuum_camera",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/sca075/mqtt_vacuum_camera/issues",
"requirements": ["pillow", "numpy"],
"version": "2024.08.0b3"
"requirements": ["pillow>=10.3.0,<11.0.0", "numpy"],
"version": "2024.08.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
from typing import Any, Optional

from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.storage import STORAGE_DIR
from homeassistant.exceptions import (
ServiceValidationError,
)

from custom_components.mqtt_vacuum_camera.const import CAMERA_STORAGE
from custom_components.mqtt_vacuum_camera.types import RoomStore, UserLanguageStore
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mqtt_vacuum_camera/utils/img_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ async def async_get_rrm_segments(
)
if out_lines:
room_coords = await ImageData.async_get_rooms_coordinates(
pixels=segments[count_seg],
rand=True)
pixels=segments[count_seg], rand=True
)
outlines.append(room_coords)
count_seg += 1
if count_seg > 0:
Expand Down
52 changes: 30 additions & 22 deletions custom_components/mqtt_vacuum_camera/valetudo/MQTT/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ async def is_disconnect_vacuum(self) -> None:
Disconnect the vacuum detected.
Generate a Warning message if the vacuum is disconnected.
"""
if self._mqtt_vac_connect_state == "disconnected" or self._mqtt_vac_connect_state == "lost":
if (
self._mqtt_vac_connect_state == "disconnected"
or self._mqtt_vac_connect_state == "lost"
):
_LOGGER.debug(
f"{self._mqtt_topic}: Vacuum Disconnected from MQTT, waiting for connection."
)
Expand Down Expand Up @@ -248,41 +251,46 @@ async def rand256_handle_destinations(self, msg) -> None:
self._payload = msg.payload
tmp_data = await self.async_decode_mqtt_payload(msg)
self._rrm_destinations = tmp_data
if 'rooms' in tmp_data:
rooms_data = {str(room['id']): room['name'].strip('#') for room in tmp_data['rooms']}
if "rooms" in tmp_data:
rooms_data = {
str(room["id"]): room["name"].strip("#") for room in tmp_data["rooms"]
}
await RoomStore().async_set_rooms_data(self._file_name, rooms_data)
_LOGGER.info(
f"{self._file_name}: Received vacuum destinations: {self._rrm_destinations}"
)

async def rrm_handle_active_segments(self, msg) -> None:
"""
Handle new MQTT messages.
Handle new MQTT messages regarding active segments.
/active_segments is for Rand256.
@param msg: MQTT message
{ "command": "segmented_cleanup", "segment_ids": [2], "repeats": 1, "afterCleaning": "Base" }
"""
command_status = json.loads(msg.payload)
command_status = await self.async_decode_mqtt_payload(msg)
_LOGGER.debug(f"Command Status: {command_status}")
command = command_status.get("command", None)

if command == "segmented_cleanup" and self._rrm_destinations:
if command == "segmented_cleanup":
segment_ids = command_status.get("segment_ids", [])
# Parse rooms JSON from _rrm_destinations
rooms_json = json.loads(self._rrm_destinations)
rooms = rooms_json.get("rooms", [])
# Create a mapping of room IDs to their positions in rooms list
room_ids = {room["id"]: idx for idx, room in enumerate(rooms, start=1)}
# Initialize rrm_active_segments with zeros
self._rrm_active_segments = [0] * len(rooms)
_LOGGER.debug(f"Segment IDs: {segment_ids}")

# Retrieve room data from RoomStore
rooms_data = await RoomStore().async_get_rooms_data(self._file_name)
rrm_active_segments = [0] * len(
rooms_data
) # Initialize based on the number of rooms

for segment_id in segment_ids:
if segment_id in room_ids:
room_idx = room_ids[segment_id] - 1 # Index start from 0
self._rrm_active_segments[room_idx] = 1
self._shared.rand256_active_zone = self._rrm_active_segments
_LOGGER.debug(
f"Active Segments of {self._file_name}: {self._rrm_active_segments}"
)
room_name = rooms_data.get(str(segment_id))
if room_name:
# Convert room ID to index; since dict doesn't preserve order, find index manually
room_idx = list(rooms_data.keys()).index(str(segment_id))
rrm_active_segments[room_idx] = 1

self._shared.rand256_active_zone = rrm_active_segments
_LOGGER.debug(f"Updated Active Segments: {rrm_active_segments}")
else:
self._shared.rand256_active_zone = []
_LOGGER.debug("No valid command or room data; segments cleared.")

@callback
async def async_message_received(self, msg) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ async def async_extract_room_properties(self, json_data):
compressed_pixels = layer.get("compressedPixels", [])
pixels = self.data.sublist(compressed_pixels, 3)
# Calculate x and y min/max from compressed pixels
x_min, y_min, x_max, y_max = await self.data.async_get_rooms_coordinates(
pixels, pixel_size
x_min, y_min, x_max, y_max = (
await self.data.async_get_rooms_coordinates(pixels, pixel_size)
)
corners = [
(x_min, y_min),
Expand Down
Loading

0 comments on commit d0f0641

Please sign in to comment.