Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:typing cf #60

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions midealocal/devices/cf/__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 MessageCFResponse, MessageQuery, MessageSet

Expand Down Expand Up @@ -36,7 +37,7 @@ def __init__(
model: str,
subtype: int,
customize: str,
):
) -> None:
super().__init__(
name=name,
device_id=device_id,
Expand All @@ -59,10 +60,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 = MessageCFResponse(msg)
_LOGGER.debug(f"[{self.device_id}] Received: {message}")
new_status = {}
Expand All @@ -72,7 +73,7 @@ def process_message(self, msg):
new_status[str(status)] = getattr(message, str(status))
return new_status

def set_target_temperature(self, target_temperature, mode):
def set_target_temperature(self, target_temperature: int, mode: int) -> None:
message = MessageSet(self._protocol_version)
message.power = True
message.mode = self._attributes[DeviceAttributes.mode]
Expand All @@ -81,7 +82,7 @@ def set_target_temperature(self, target_temperature, mode):
message.mode = mode
self.build_send(message)

def set_attribute(self, attr, value):
def set_attribute(self, attr: str, value: Any) -> None:
message = MessageSet(self._protocol_version)
message.power = True
message.mode = self._attributes[DeviceAttributes.mode]
Expand Down
24 changes: 13 additions & 11 deletions midealocal/devices/cf/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@


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

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


class MessageQuery(MessageCFBase):
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(MessageCFBase):
def __init__(self, protocol_version):
def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
body_type=0x01,
)
self.power = False
self.mode = 0 # 1 自动 2 制冷 3 制热
self.target_temperature = None
self.aux_heating = None
self.target_temperature: int | None = None
self.aux_heating: bool | None = None

@property
def _body(self):
def _body(self) -> bytearray:
power = 0x01 if self.power else 0x00
mode = self.mode
target_temperature = (
Expand All @@ -61,7 +63,7 @@ def _body(self):


class CFMessageBody(MessageBody):
def __init__(self, body, data_offset=0):
def __init__(self, body: bytearray, data_offset: int = 0) -> None:
super().__init__(body)
self.power = (body[data_offset + 0] & 0x01) > 0
self.aux_heating = (body[data_offset + 0] & 0x02) > 0
Expand All @@ -81,8 +83,8 @@ def __init__(self, body, data_offset=0):


class MessageCFResponse(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]
and self.body_type == 0x01
Expand Down
Loading