Skip to content

Commit

Permalink
chore: typing dc
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed Jun 4, 2024
1 parent 75493c3 commit bbd7b7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
10 changes: 6 additions & 4 deletions midealocal/devices/dc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import sys
from typing import Any

from .message import MessageDCResponse, MessagePower, MessageQuery, MessageStart

Expand Down Expand Up @@ -34,7 +35,7 @@ def __init__(
model: str,
subtype: int,
customize: str,
):
) -> None:
super().__init__(
name=name,
device_id=device_id,
Expand All @@ -55,10 +56,10 @@ def __init__(
},
)

def build_query(self):
def build_query(self) -> list[MessageQuery]:
return [MessageQuery(self._protocol_version)]

def process_message(self, msg):
def process_message(self, msg: bytes) -> dict[str, Any]:
message = MessageDCResponse(msg)
_LOGGER.debug(f"[{self.device_id}] Received: {message}")
new_status = {}
Expand All @@ -81,7 +82,8 @@ def process_message(self, msg):
new_status[str(status)] = self._attributes[status]
return new_status

def set_attribute(self, attr, value):
def set_attribute(self, attr: str, value: Any) -> None:
message: MessagePower | MessageStart | None = None
if attr == DeviceAttributes.power:
message = MessagePower(self._protocol_version)
message.power = value
Expand Down
27 changes: 14 additions & 13 deletions midealocal/devices/dc/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


class MessageDCBase(MessageRequest):
def __init__(self, protocol_version, message_type, body_type):
def __init__(
self, protocol_version: int, message_type: int, body_type: int
) -> None:
super().__init__(
device_type=0xDC,
protocol_version=protocol_version,
Expand All @@ -16,25 +18,25 @@ def __init__(self, protocol_version, message_type, body_type):
)

@property
def _body(self):
def _body(self) -> bytearray:
raise NotImplementedError


class MessageQuery(MessageDCBase):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.query,
body_type=0x03,
)

@property
def _body(self):
def _body(self) -> bytearray:
return bytearray([])


class MessagePower(MessageDCBase):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
Expand All @@ -43,13 +45,13 @@ def __init__(self, protocol_version):
self.power = False

@property
def _body(self):
def _body(self) -> bytearray:
power = 0x01 if self.power else 0x00
return bytearray([power, 0xFF])


class MessageStart(MessageDCBase):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
Expand All @@ -59,7 +61,7 @@ def __init__(self, protocol_version):
self.washing_data = bytearray([])

@property
def _body(self):
def _body(self) -> bytearray:
if self.start:
return bytearray([0xFF, 0x01]) + self.washing_data
else:
Expand All @@ -68,25 +70,24 @@ def _body(self):


class DCGeneralMessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.power = body[1] > 0
self.start = True if body[2] in [2, 6] else False
self.washing_data = body[3:15]
self.progress = 0
self.time_remaining: float | None = None
for i in range(7):
if (body[16] & (1 << i)) > 0:
self.progress = i + 1
break
if self.power:
self.time_remaining = body[17] + body[18] * 60
else:
self.time_remaining = None


class MessageDCResponse(MessageResponse):
def __init__(self, message):
super().__init__(message)
def __init__(self, message: bytes) -> None:
super().__init__(bytearray(message))
if self.message_type in [MessageType.query, MessageType.set] or (
self.message_type == MessageType.notify1 and self.body_type == 0x04
):
Expand Down

0 comments on commit bbd7b7d

Please sign in to comment.