Skip to content

Commit

Permalink
chore: typing e1 (#64)
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**
  - Added type annotations for improved code clarity and consistency.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
chemelli74 authored Jun 4, 2024
1 parent e3753db commit 605123a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
10 changes: 6 additions & 4 deletions midealocal/devices/e1/__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 (
MessageE1Response,
Expand Down Expand Up @@ -59,7 +60,7 @@ def __init__(
model: str,
subtype: int,
customize: str,
):
) -> None:
super().__init__(
name=name,
device_id=device_id,
Expand Down Expand Up @@ -125,10 +126,10 @@ def __init__(
self._status = ["Off", "Idle", "Delay", "Running", "Error"]
self._progress = ["Idle", "Pre-wash", "Wash", "Rinse", "Dry", "Complete"]

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 = MessageE1Response(msg)
_LOGGER.debug(f"[{self.device_id}] Received: {message}")
new_status = {}
Expand All @@ -154,7 +155,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 | MessageLock | MessageStorage | None = None
if attr == DeviceAttributes.power:
message = MessagePower(self._protocol_version)
message.power = value
Expand Down
28 changes: 15 additions & 13 deletions midealocal/devices/e1/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


class MessageE1Base(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=0xE1,
protocol_version=protocol_version,
Expand All @@ -16,12 +18,12 @@ def __init__(self, protocol_version, message_type, body_type):
)

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


class MessagePower(MessageE1Base):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
Expand All @@ -30,13 +32,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, 0x00, 0x00, 0x00])


class MessageLock(MessageE1Base):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
Expand All @@ -45,13 +47,13 @@ def __init__(self, protocol_version):
self.lock = False

@property
def _body(self):
def _body(self) -> bytearray:
lock = 0x03 if self.lock else 0x04
return bytearray([lock]) + bytearray([0x00] * 36)


class MessageStorage(MessageE1Base):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
Expand All @@ -60,7 +62,7 @@ def __init__(self, protocol_version):
self.storage = False

@property
def _body(self):
def _body(self) -> bytearray:
storage = 0x01 if self.storage else 0x00
return (
bytearray([0x00, 0x00, 0x00, storage])
Expand All @@ -70,20 +72,20 @@ def _body(self):


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

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


class E1GeneralMessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.power = body[1] > 0
self.status = body[1]
Expand Down Expand Up @@ -117,8 +119,8 @@ def __init__(self, body):


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

0 comments on commit 605123a

Please sign in to comment.