Skip to content

Commit

Permalink
chore: typing bf (#51)
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 quality by adding type annotations to various methods
and classes, enhancing code readability and maintainability.

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

Expand All @@ -24,7 +25,7 @@ class DeviceAttributes(StrEnum):


class MideaBFDevice(MideaDevice):
_status = {
_status: dict[int, str] = {
0x01: "PowerSave",
0x02: "Standby",
0x03: "Working",
Expand All @@ -45,7 +46,7 @@ def __init__(
model: str,
subtype: int,
customize: str,
):
) -> None:
super().__init__(
name=name,
device_id=device_id,
Expand All @@ -68,10 +69,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 = MessageBFResponse(msg)
_LOGGER.debug(f"[{self.device_id}] Received: {message}")
new_status = {}
Expand All @@ -90,7 +91,7 @@ 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:
pass


Expand Down
20 changes: 11 additions & 9 deletions midealocal/devices/bf/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class MessageBFBase(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=0xBF,
protocol_version=protocol_version,
Expand All @@ -11,25 +13,25 @@ def __init__(self, protocol_version, message_type, body_type):
)

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


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

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


class MessageSet(MessageBFBase):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.query,
Expand All @@ -39,7 +41,7 @@ def __init__(self, protocol_version):
self.child_lock = None

@property
def _body(self):
def _body(self) -> bytearray:
power = 0xFF if self.power is None else 0x11 if self.power else 0x01
child_lock = (
0xFF if self.child_lock is None else 0x01 if self.child_lock else 0x00
Expand All @@ -48,7 +50,7 @@ def _body(self):


class MessageBFBody(MessageBody):
def __init__(self, body):
def __init__(self, body: bytearray) -> None:
super().__init__(body)
self.status = body[31]
self.time_remaining = (
Expand All @@ -68,8 +70,8 @@ def __init__(self, body):


class MessageBFResponse(MessageResponse):
def __init__(self, message):
super().__init__(message)
def __init__(self, message: bytes) -> None:
super().__init__(bytearray(message))
if (
self.message_type
in [MessageType.set, MessageType.notify1, MessageType.query]
Expand Down

0 comments on commit 739b1bc

Please sign in to comment.