Skip to content

Commit

Permalink
chore: typing ca (#54)
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 code clarity and type checking by adding type hints to
various functions and class constructors.
- Enhanced the maintainability and readability of the codebase with
explicit type annotations.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
chemelli74 authored Jun 3, 2024
1 parent 12e8179 commit fe251a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
9 changes: 5 additions & 4 deletions midealocal/devices/ca/__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 MessageCAResponse, MessageQuery

Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(
model: str,
subtype: int,
customize: str,
):
) -> None:
super().__init__(
name=name,
device_id=device_id,
Expand Down Expand Up @@ -81,10 +82,10 @@ def __init__(
)
self._modes = [""]

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 = MessageCAResponse(msg)
_LOGGER.debug(f"[{self.device_id}] Received: {message}")
new_status = {}
Expand All @@ -94,7 +95,7 @@ def process_message(self, msg):
new_status[str(status)] = getattr(message, str(status))
return new_status

def set_attribute(self, attr, value):
def set_attribute(self, attr: str, value: Any) -> None:
pass


Expand Down
22 changes: 12 additions & 10 deletions midealocal/devices/ca/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


class MessageCABase(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=0xCA,
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(MessageCABase):
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 CAGeneralMessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.refrigerator_setting_temp = body[2] & 0x0F
self.freezer_setting_temp = -12 - ((body[2] & 0xF0) >> 4)
Expand Down Expand Up @@ -62,7 +64,7 @@ def __init__(self, body):


class CAExceptionMessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.refrigerator_door_overtime = (body[1] & 0x01) > 0
self.freezer_door_overtime = (body[1] & 0x02) > 0
Expand All @@ -71,7 +73,7 @@ def __init__(self, body):


class CANotify00MessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.refrigerator_door = (body[1] & 0x01) > 0
self.freezer_door = (body[1] & 0x02) > 0
Expand All @@ -80,7 +82,7 @@ def __init__(self, body):


class CANotify01MessageBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.refrigerator_setting_temp = body[37]
self.freezer_setting_temp = -12 - body[38]
Expand All @@ -102,8 +104,8 @@ def __init__(self, body):


class MessageCAResponse(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]
Expand Down

0 comments on commit fe251a8

Please sign in to comment.