Skip to content

Commit

Permalink
Merge pull request #9 from talss89/map-support
Browse files Browse the repository at this point in the history
Goto point, rectangle cleaning, performance improvements
  • Loading branch information
Jezza34000 authored Jan 23, 2023
2 parents 9338bab + 4eb3f8a commit a01c8fa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
31 changes: 31 additions & 0 deletions custom_components/weback_vacuum/VacDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, thing_name, thing_nickname, sub_type, thing_status,
self.sub_type = sub_type
self.map = None
self.map_image_buffer = None
self.map_camera = None

# First init status from HTTP API
if self.robot_status is None:
Expand Down Expand Up @@ -56,8 +57,19 @@ def render_map(self):
img.close()
self.map_image_buffer = img_byte_arr.getvalue()

if self.map_camera is not None:
self.should_poll = False
self.trigger_map_camera_update()

return True

def register_map_camera(self, camera):
self.map_camera = camera

def trigger_map_camera_update(self):
if self.map_camera is not None:
self.map_camera.schedule_update_ha_state(True)

# ==========================================================
# Vacuum Entity
# -> Properties
Expand Down Expand Up @@ -237,3 +249,22 @@ async def clean_room(self, room_ids: list):
room_data.append(dict(room_id = id))
working_payload = {self.ASK_STATUS: self.CLEAN_MODE_ROOMS, self.SELECTED_ZONE: room_data}
await self.send_command(self.name, self.sub_type, working_payload)

async def clean_zone(self, bounding):
box_x = []
box_y = []
num_boxes = len(bounding)

for box in bounding:
box_x.append(int(box[0] / 10))
box_x.append(int(box[0] / 10))
box_x.append(int(box[2] / 10))
box_x.append(int(box[2] / 10))
box_y.append(int(box[1] / 10))
box_y.append(int(box[3] / 10))
box_y.append(int(box[3] / 10))
box_y.append(int(box[1] / 10))

working_payload = {self.ASK_STATUS: self.ROBOT_PLANNING_RECT, self.PLANNING_RECT_POINT_NUM: num_boxes * 4, self.PLANNING_RECT_X: box_x, self.PLANNING_RECT_Y: box_y}
_LOGGER.debug(f"Vacuum : Planning rect sent {working_payload}")
await self.send_command(self.name, self.sub_type, working_payload)
6 changes: 3 additions & 3 deletions custom_components/weback_vacuum/VacMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def draw_rooms(self):
for room in rooms:
self.draw_room(room)

def draw_path(self, col = (0x1C, 0xE3, 0xDA, 0xFF)):
def draw_path(self, col = (0x1C, 0xE3, 0xDA, 0xFF), invisible_relocate = True):
path, point_types = self.vac_map.get_path()

last_coord = None
Expand All @@ -45,8 +45,8 @@ def draw_path(self, col = (0x1C, 0xE3, 0xDA, 0xFF)):
continue

point_type = point_types[i]

self.draw.line((last_coord, coord), col if point_type == VacMap.PATH_VACUUMING else (255,255,255,0), width=3)
if(point_type == VacMap.PATH_VACUUMING or invisible_relocate is False):
self.draw.line((last_coord, coord), col if point_type == VacMap.PATH_VACUUMING else (255,255,255,0), width=3)

last_coord = coord

Expand Down
28 changes: 18 additions & 10 deletions custom_components/weback_vacuum/WebackApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class WebackWssCtrl(WebackApi):
CLEANING_STATES = {
DIRECTION_CONTROL, ROBOT_PLANNING_RECT, RELOCATION, CLEAN_MODE_Z, CLEAN_MODE_AUTO,
CLEAN_MODE_EDGE, CLEAN_MODE_EDGE_DETECT, CLEAN_MODE_SPOT, CLEAN_MODE_SINGLE_ROOM,
CLEAN_MODE_ROOMS, CLEAN_MODE_MOP, CLEAN_MODE_SMART
CLEAN_MODE_ROOMS, CLEAN_MODE_MOP, CLEAN_MODE_SMART, CHARGE_MODE_RETURNING
}

CHARGING_STATES = {
Expand All @@ -405,6 +405,10 @@ class WebackWssCtrl(WebackApi):
RECTANGLE_INFO = "virtual_rect_info"
SPEAKER_VOLUME = "volume"
SELECTED_ZONE = "selected_zone"
PLANNING_RECT_POINT_NUM = "planning_rect_point_num"
PLANNING_RECT_X = "planning_rect_x"
PLANNING_RECT_Y = "planning_rect_y"

# Payload switches
VOICE_SWITCH = "voice_switch"
UNDISTURB_MODE = "undisturb_mode"
Expand All @@ -425,6 +429,7 @@ def __init__(self, user, password, region, country, app, client_id, api_version)
self.wst = None
self.ws = None
self._refresh_time = 60
self._last_refresh = 0
self.sent_counter = 0

# Reloading cached creds
Expand Down Expand Up @@ -543,6 +548,7 @@ def on_message(self, ws, message):
_LOGGER.debug(f"WebackApi (WSS) Map data received")
self.map.wss_update(wss_data['map_data'])
self.render_map()
self.adapt_refresh_time(self.robot_status)
self._call_subscriber()
else:
_LOGGER.error(f"WebackApi (WSS) Received an unknown message from server : {wss_data}")
Expand Down Expand Up @@ -642,15 +648,17 @@ def adapt_refresh_time(self, status):
async def refresh_handler(self, thing_name, sub_type):
_LOGGER.debug("WebackApi (WSS) Start refresh_handler")
while True:
try:
if self.socket_state != SOCK_CONNECTED:
await self.connect_wss()

_LOGGER.debug(f"WebackApi (WSS) Refreshing...")
await self.update_status(thing_name, sub_type)
await asyncio.sleep(self._refresh_time)
except Exception as e:
_LOGGER.error(f"WebackApi (WSS) Error during refresh_handler (details={e})")
if time.time() - self._last_refresh >= self._refresh_time:
try:
if self.socket_state != SOCK_CONNECTED:
await self.connect_wss()

_LOGGER.debug(f"WebackApi (WSS) Refreshing...")
await self.update_status(thing_name, sub_type)
except Exception as e:
_LOGGER.error(f"WebackApi (WSS) Error during refresh_handler (details={e})")
self._last_refresh = time.time()
await asyncio.sleep(5)

def subscribe(self, subscriber):
_LOGGER.debug("WebackApi (WSS): adding a new subscriber")
Expand Down
3 changes: 2 additions & 1 deletion custom_components/weback_vacuum/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
for device in hass.data[DOMAIN]:
entity_id = generate_entity_id(ENTITY_ID_FORMAT, device.name, hass=hass)
vacuums.append(WebackVacuumCamera(device, entity_id))
hass.loop.create_task(device.watch_state())
# hass.loop.create_task(device.watch_state())

_LOGGER.debug("Adding Weback Vacuums Maps to Home Assistant: %s", vacuums)

Expand All @@ -43,6 +43,7 @@ def __init__(self, device: VacDevice, entity_id):
"""Initialize the Weback Vacuum Map"""
super().__init__()
self._vacdevice = device
self._vacdevice.register_map_camera(self)
# self.entity_id = entity_id
self.content_type = "image/png"

Expand Down
4 changes: 4 additions & 0 deletions custom_components/weback_vacuum/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,9 @@ async def async_send_command(self, command, params=None, **kwargs):
_LOGGER.debug(f"Vacuum: send_command (command={command} / params={params} / kwargs={kwargs})")
if(command == 'app_segment_clean'):
await self.device.clean_room(params)
elif(command == 'app_zoned_clean'):
await self.device.clean_zone(params)
elif(command == 'app_goto_target'):
await self.device.goto([int(params[0] / 10), int(params[1] / 10)])
else:
await self.device.send_command(self.name, self.sub, params)

0 comments on commit a01c8fa

Please sign in to comment.