Skip to content

Commit

Permalink
chore: ruff SIM102 (#142)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Improved readability of conditional statements in device info
retrieval and message routing methods.
  - Refined logic for setting message body in `MessageB4Response` class.
- Enhanced padding calculation condition in encoding method to ensure
alignment.



<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
rokam authored Jun 15, 2024
1 parent 6b67d03 commit 00a2ac1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
20 changes: 11 additions & 9 deletions midealocal/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ async def list_appliances(

async def get_device_info(self, device_id: int) -> dict[str, Any] | None:
"""Get device information."""
if response := await self.list_appliances(home_id=None):
if int(device_id) in response:
return cast(dict, response[device_id])
if (response := await self.list_appliances(home_id=None)) and (
int(device_id) in response
):
return cast(dict, response[device_id])
return None

async def download_lua(
Expand Down Expand Up @@ -486,12 +487,13 @@ async def _api_request(
async def _re_route(self) -> None:
data = self._make_general_data()
data.update({"userType": "0", "userName": f"{self._account}"})
if response := await self._api_request(
endpoint="/v1/multicloud/platform/user/route",
data=data,
):
if api_url := response.get("masUrl"):
self._api_url = api_url
if (
response := await self._api_request(
endpoint="/v1/multicloud/platform/user/route",
data=data,
)
) and (api_url := response.get("masUrl")):
self._api_url = api_url

async def login(self) -> bool:
"""Authenticate to MSmart Cloud."""
Expand Down
25 changes: 17 additions & 8 deletions midealocal/devices/b4/message.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""Midea local B4 message."""

from midealocal.const import MAX_BYTE_VALUE
from midealocal.message import MessageBody, MessageRequest, MessageResponse, MessageType
from midealocal.message import (
BodyType,
MessageBody,
MessageRequest,
MessageResponse,
MessageType,
)


class MessageB4Base(MessageRequest):
Expand Down Expand Up @@ -69,11 +75,14 @@ class MessageB4Response(MessageResponse):
def __init__(self, message: bytes) -> None:
"""Initialize B4 message response."""
super().__init__(bytearray(message))
if self.message_type in [
MessageType.notify1,
MessageType.query,
MessageType.set,
]:
if self.body_type == 0x01:
self.set_body(B4MessageBody(super().body))
if (
self.message_type
in [
MessageType.notify1,
MessageType.query,
MessageType.set,
]
and self.body_type == BodyType.X01
):
self.set_body(B4MessageBody(super().body))
self.set_attr()
11 changes: 6 additions & 5 deletions midealocal/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,12 @@ def encode_8370(self, data: bytes, msgtype: int) -> bytes:
"""Encode 8370 data."""
header = bytearray([0x83, 0x70])
size, padding = len(data), 0
if msgtype in (MSGTYPE_ENCRYPTED_RESPONSE, MSGTYPE_ENCRYPTED_REQUEST):
if (size + 2) % 16 != 0:
padding = 16 - (size + 2 & 0xF)
size += padding + 32
data += get_random_bytes(padding)
if (msgtype in (MSGTYPE_ENCRYPTED_RESPONSE, MSGTYPE_ENCRYPTED_REQUEST)) and (
(size + 2) % 16 != 0
):
padding = 16 - (size + 2 & 0xF)
size += padding + 32
data += get_random_bytes(padding)
header += size.to_bytes(2, "big")
header += bytearray([0x20, padding << 4 | msgtype])
data = self._request_count.to_bytes(2, "big") + data
Expand Down

0 comments on commit 00a2ac1

Please sign in to comment.