Skip to content

Commit 4774dc0

Browse files
author
jalcaras
committed
refactor: code improvement, style
1 parent c413f0d commit 4774dc0

File tree

1 file changed

+47
-62
lines changed

1 file changed

+47
-62
lines changed

custom_components/weback_vacuum/vacmap.py

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def draw_robot_position(self, col=(0xDA, 0x36, 0x25, 0xFF), radius=10):
3939
def draw_room(self, room):
4040
self.draw.polygon(
4141
self.vac_map._virtual_to_pixel_list(room.get_room_bounds()),
42-
tuple(random.choices(range(256), k=4)), # nosec B311
42+
tuple(random.choices(range(256), k=4)), # noqa: S311
4343
(255, 255, 0, 128),
4444
)
4545

@@ -90,16 +90,14 @@ def get_room_name(self):
9090
return self.data["room_name"]
9191
return None
9292

93-
def get_room_bounds(self, tuple=True):
94-
r = list()
95-
for i in range(0, len(self.data["room_point_x"])):
96-
if tuple:
97-
r.append((self.data["room_point_x"][i], self.data["room_point_y"][i]))
93+
def get_room_bounds(self, use_tuple=True):
94+
bounds = []
95+
for x, y in zip(self.data["room_point_x"], self.data["room_point_y"]):
96+
if use_tuple:
97+
bounds.append((x, y))
9898
else:
99-
r.append(
100-
list([self.data["room_point_x"][i], self.data["room_point_y"][i]])
101-
)
102-
return r
99+
bounds.append([x, y])
100+
return bounds
103101

104102
def get_room_label_offset(self):
105103
bounds = self.get_room_bounds()
@@ -113,7 +111,7 @@ def get_room_label_offset(self):
113111

114112
def get_xaiomi_vacuum_map_card_rooms(self):
115113
label_offset = self.get_room_label_offset()
116-
ret = {
114+
return {
117115
"id": self.get_room_id(),
118116
"outline": self.get_room_bounds(False),
119117
"label": {
@@ -123,8 +121,6 @@ def get_xaiomi_vacuum_map_card_rooms(self):
123121
},
124122
}
125123

126-
return ret
127-
128124

129125
class VacMap:
130126
MAP_FORMAT_YW_LASER = "yw_ls"
@@ -138,24 +134,22 @@ class VacMap:
138134
PATH_RELOCATING = 0x40
139135
PATH_VACUUMING = 0x0
140136

141-
ALLOW_MAP_FORMATS = {MAP_FORMAT_YW_LASER, MAP_FORMAT_BV_LASER}
137+
def __init__(self, data_input):
138+
self.load_data(data_input)
142139

143-
def __init__(self, input):
144-
self.load_data(input)
145-
146-
def load_data(self, input):
147-
self.data = json.loads(zlib.decompress(base64.b64decode(input)))
140+
def load_data(self, data_input):
141+
self.data = json.loads(zlib.decompress(base64.b64decode(data_input)))
148142
self.map_data = bytearray(base64.b64decode(self.data["MapData"]))
149143
self.map_bitmap = False
150144
self.map_scale = 4
151145
if "PointData" in self.data:
152146
self.data["PointData"] = base64.b64decode(self.data["PointData"])
153147
self.data["PointType"] = base64.b64decode(self.data["PointType"])
154148

155-
def wss_update(self, input):
149+
def wss_update(self, data_input):
156150
existing_room_data = self.data["room_zone_info"]
157151

158-
self.load_data(input)
152+
self.load_data(data_input)
159153

160154
for i, room in enumerate(self.data["room_zone_info"]):
161155
existing_room = next(
@@ -171,7 +165,7 @@ def wss_update(self, input):
171165
def get_map_bitmap(self):
172166
"""Parse MapData into 8-Bit lightness (grayscale) bitmap, return it as bytes"""
173167
self.map_bitmap = bytearray(b"")
174-
for i in range(0, len(self.map_data)):
168+
for i in range(len(self.map_data)):
175169
byte = self.map_data[i]
176170

177171
self.map_bitmap.append(((byte & 192) >> 6) * 85)
@@ -208,14 +202,13 @@ def get_map_image(self, black=(0x1C, 0x89, 0xE3), white=(0xFF, 0xFF, 0xFF)):
208202
img.putdata(new_img_data)
209203
del new_img_data
210204

211-
img = img.resize(
205+
return img.resize(
212206
(
213207
int((self.get_map_width()) * self.map_scale),
214208
int((self.get_map_height()) * self.map_scale),
215209
),
216210
Image.NEAREST,
217211
)
218-
return img
219212

220213
def get_map_width(self):
221214
return self.data["MapWidth"]
@@ -231,29 +224,28 @@ def get_room_id_by_name(self, name):
231224
room for room in self.data["room_zone_info"] if room["room_name"] == name
232225
)["room_id"]
233226

234-
def get_room_by_id(self, id):
227+
def get_room_by_id(self, room_id):
235228
return VacMapRoom(
236-
next(room for room in self.data["room_zone_info"] if room["room_id"] == id)
229+
next(
230+
room
231+
for room in self.data["room_zone_info"]
232+
if room["room_id"] == room_id
233+
),
237234
)
238235

239236
def get_rooms(self):
240-
rooms = list()
241-
242-
for room in self.data["room_zone_info"]:
243-
rooms.append(VacMapRoom(room))
244-
245-
return rooms
237+
return [VacMapRoom(room) for room in self.data.get("room_zone_info")]
246238

247239
def get_room_by_name(self, name):
248-
if id := self.get_room_id_by_name(name):
249-
return self.get_room_by_id(id)
240+
if r_id := self.get_room_id_by_name(name):
241+
return self.get_room_by_id(r_id)
250242
return None
251243

252244
def get_charger_point_pixel(self):
253245
return self._scale_up_pixel_coords(
254246
self._pixel_apply_offset(
255-
(self.data["ChargerPoint"][0], self.data["ChargerPoint"][1])
256-
)
247+
(self.data["ChargerPoint"][0], self.data["ChargerPoint"][1]),
248+
),
257249
)
258250

259251
def get_charger_point_virtual(self):
@@ -270,25 +262,24 @@ def get_robot_position_virtual(self):
270262

271263
def get_path(self):
272264
if "PointData" not in self.data:
273-
return list(), list()
265+
return [], []
274266

275267
point_data = io.BytesIO(self.data["PointData"])
276-
coords = list()
277-
point_types = list()
268+
point_types = []
278269

279-
coords.append(self.get_charger_point_pixel())
270+
coords = [self.get_charger_point_pixel()]
280271

281272
while x := point_data.read(2):
282273
coords.append(
283274
self._virtual_to_pixel(
284275
(
285276
struct.unpack("h", x)[0],
286277
struct.unpack("h", point_data.read(2))[0],
287-
)
288-
)
278+
),
279+
),
289280
)
290281

291-
for i, coord in enumerate(coords):
282+
for i, _ in enumerate(coords):
292283
byte = int(i * 2 / 8)
293284
bit = int((i * 2 / 8 % 1) * 8)
294285

@@ -312,20 +303,20 @@ def _scale_up_pixel_coords(self, coords):
312303
return x * self.map_scale, y * self.map_scale
313304

314305
def _virtual_to_pixel_list(self, coords):
315-
ret = list()
316-
for coord in coords:
317-
ret.append(self._virtual_to_pixel(coord))
318-
return ret
306+
return [self._virtual_to_pixel(coord) for coord in coords]
319307

320308
def _virtual_to_pixel(self, coords):
321-
"""Convert virtual (laser map coordinates) to pixel coords, taking origin into account"""
309+
"""
310+
Convert virtual (laser map coordinates) to pixel coords,
311+
taking origin into account
312+
"""
322313
x, y = coords
323314
x, y = round(
324315
(self.data["MapOrigin"][0] + (x * 2 * self.get_map_resolution()))
325-
* self.map_scale
316+
* self.map_scale,
326317
), round(
327318
(self.data["MapOrigin"][1] + (y * 2 * self.get_map_resolution()))
328-
* self.map_scale
319+
* self.map_scale,
329320
)
330321
return x, y
331322

@@ -345,42 +336,36 @@ def calibration_points(self):
345336
{
346337
"vacuum": {"x": 0, "y": 0},
347338
"map": {"x": int(map_point[0]), "y": int(map_point[1])},
348-
}
339+
},
349340
)
350341

351342
map_point = self._virtual_to_pixel(
352-
(self.get_map_width(), self.get_map_height())
343+
(self.get_map_width(), self.get_map_height()),
353344
)
354345
cal.append(
355346
{
356347
"vacuum": {"x": self.get_map_width(), "y": self.get_map_height()},
357348
"map": {"x": int(map_point[0]), "y": int(map_point[1])},
358-
}
349+
},
359350
)
360351

361352
map_point = self._virtual_to_pixel((0, self.get_map_height()))
362353
cal.append(
363354
{
364355
"vacuum": {"x": 0, "y": self.get_map_height()},
365356
"map": {"x": int(map_point[0]), "y": int(map_point[1])},
366-
}
357+
},
367358
)
368359

369360
map_point = self._virtual_to_pixel((self.get_map_width(), 0))
370361
cal.append(
371362
{
372363
"vacuum": {"x": self.get_map_width(), "y": 0},
373364
"map": {"x": int(map_point[0]), "y": int(map_point[1])},
374-
}
365+
},
375366
)
376367

377368
return cal
378369

379370
def get_predefined_selections(self):
380-
all_rooms = self.get_rooms()
381-
predefined_selections = list()
382-
383-
for room in all_rooms:
384-
predefined_selections.append(room.get_xaiomi_vacuum_map_card_rooms())
385-
386-
return predefined_selections
371+
return [room.get_xaiomi_vacuum_map_card_rooms() for room in self.get_rooms()]

0 commit comments

Comments
 (0)