Skip to content

Commit

Permalink
chore:typing body_type/device_type/message_type/protocol_type (#274)
Browse files Browse the repository at this point in the history
fix the remain typing issue after #271 

Update the following to Enum classes:

1. body_type
2. device_type
3. message_type
4. protocol_type

please help to review and add your comments.

---------

Co-authored-by: Simone Chemelli <simone.chemelli@gmail.com>
  • Loading branch information
wuwentao and chemelli74 authored Sep 29, 2024
1 parent e0e0633 commit d23e5a9
Show file tree
Hide file tree
Showing 89 changed files with 790 additions and 566 deletions.
2 changes: 1 addition & 1 deletion midealocal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
get_midea_cloud,
get_preset_account_cloud,
)
from midealocal.const import ProtocolVersion
from midealocal.device import (
AuthException,
MideaDevice,
NoSupportedProtocol,
ProtocolVersion,
)
from midealocal.devices import device_selector
from midealocal.discover import discover
Expand Down
52 changes: 52 additions & 0 deletions midealocal/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
"""Midea local constants."""

from enum import IntEnum

MAX_BYTE_VALUE = 0xFF
MAX_DOUBLE_BYTE_VALUE = 0xFFFF


class DeviceType(IntEnum):
"""Device Type."""

A0 = 0xA0
A1 = 0xA1
AC = 0xAC
B0 = 0xB0
B1 = 0xB1
B3 = 0xB3
B4 = 0xB4
B6 = 0xB6
B8 = 0xB8
BF = 0xBF
C2 = 0xC2
C3 = 0xC3
CA = 0xCA
CC = 0xCC
CD = 0xCD
CE = 0xCE
CF = 0xCF
DA = 0xDA
DB = 0xDB
DC = 0xDC
E1 = 0xE1
E2 = 0xE2
E3 = 0xE3
E6 = 0xE6
E8 = 0xE8
EA = 0xEA
EC = 0xEC
ED = 0xED
FA = 0xFA
FB = 0xFB
FC = 0xFC
FD = 0xFD
X13 = 0x13
X26 = 0x26
X34 = 0x34
X40 = 0x40
X00 = 0x00


class ProtocolVersion(IntEnum):
"""Protocol version."""

V1 = 1
V2 = 2
V3 = 3
59 changes: 6 additions & 53 deletions midealocal/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from typing_extensions import deprecated

from .const import DeviceType, ProtocolVersion
from .exceptions import SocketException
from .message import (
MessageApplianceResponse,
Expand All @@ -35,46 +36,6 @@
_LOGGER = logging.getLogger(__name__)


class DeviceType(IntEnum):
"""Device Type."""

A0 = 0xA0
A1 = 0xA1
AC = 0xAC
B0 = 0xB0
B1 = 0xB1
B3 = 0xB3
B4 = 0xB4
B6 = 0xB6
BF = 0xBF
C2 = 0xC2
C3 = 0xC3
CA = 0xCA
CC = 0xCC
CD = 0xCD
CE = 0xCE
CF = 0xCF
DA = 0xDA
DB = 0xDB
DC = 0xDC
E1 = 0xE1
E2 = 0xE2
E3 = 0xE3
E6 = 0xE6
E8 = 0xE8
EA = 0xEA
EC = 0xEC
ED = 0xED
FA = 0xFA
FB = 0xFB
FC = 0xFC
FD = 0xFD
X13 = 0x13
X26 = 0x26
X34 = 0x34
X40 = 0x40


class AuthException(Exception):
"""Authentication exception."""

Expand All @@ -91,14 +52,6 @@ class DeviceAttributes(StrEnum):
"""Device attributes."""


class ProtocolVersion(IntEnum):
"""Protocol version."""

V1 = 1
V2 = 2
V3 = 3


class MessageResult(IntEnum):
"""Parse message result."""

Expand All @@ -117,12 +70,12 @@ def __init__(
self,
name: str,
device_id: int,
device_type: int,
device_type: DeviceType,
ip_address: str,
port: int,
token: str,
key: str,
protocol: int,
protocol: ProtocolVersion,
model: str,
subtype: int,
attributes: dict,
Expand All @@ -143,7 +96,7 @@ def __init__(
self._protocol = protocol
self._model = model
self._subtype = subtype
self._protocol_version = 0
self._protocol_version: ProtocolVersion = ProtocolVersion.V1
self._updates: list[Callable[[dict[str, Any]], None]] = []
self._unsupported_protocol: list[str] = []
self._is_run = False
Expand All @@ -167,7 +120,7 @@ def device_id(self) -> int:
return self._device_id

@property
def device_type(self) -> int:
def device_type(self) -> DeviceType:
"""Device type."""
return self._device_type

Expand Down Expand Up @@ -538,7 +491,7 @@ def process_message(self, msg: bytes) -> dict[str, Any]:
"""Process message."""
raise NotImplementedError

def send_command(self, cmd_type: int, cmd_body: bytearray) -> None:
def send_command(self, cmd_type: MessageType, cmd_body: bytearray) -> None:
"""Send command."""
cmd = MessageQuestCustom(
self._device_type,
Expand Down
3 changes: 2 additions & 1 deletion midealocal/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from importlib import import_module
from typing import cast

from midealocal.device import DeviceType, MideaDevice
from midealocal.const import DeviceType
from midealocal.device import MideaDevice


def device_selector(
Expand Down
5 changes: 3 additions & 2 deletions midealocal/devices/a1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from enum import StrEnum
from typing import Any, ClassVar

from midealocal.const import DeviceType, ProtocolVersion
from midealocal.device import MideaDevice

from .message import MessageA1Response, MessageQuery, MessageSet
Expand Down Expand Up @@ -57,7 +58,7 @@ def __init__(
port: int,
token: str,
key: str,
protocol: int,
protocol: ProtocolVersion,
model: str,
subtype: int,
customize: str, # noqa: ARG002
Expand All @@ -66,7 +67,7 @@ def __init__(
super().__init__(
name=name,
device_id=device_id,
device_type=0xA1,
device_type=DeviceType.A1,
ip_address=ip_address,
port=port,
token=token,
Expand Down
25 changes: 13 additions & 12 deletions midealocal/devices/a1/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from enum import IntEnum

from midealocal.const import DeviceType, ProtocolVersion
from midealocal.crc8 import calculate
from midealocal.message import (
BodyType,
Expand Down Expand Up @@ -30,13 +31,13 @@ class MessageA1Base(MessageRequest):

def __init__(
self,
protocol_version: int,
message_type: int,
body_type: int,
protocol_version: ProtocolVersion,
message_type: MessageType,
body_type: BodyType,
) -> None:
"""Initialize message A1 base."""
super().__init__(
device_type=0xA1,
device_type=DeviceType.A1,
protocol_version=protocol_version,
message_type=message_type,
body_type=body_type,
Expand All @@ -61,12 +62,12 @@ def body(self) -> bytearray:
class MessageQuery(MessageA1Base):
"""Message A1 query."""

def __init__(self, protocol_version: int) -> None:
def __init__(self, protocol_version: ProtocolVersion) -> None:
"""Initialize message A1 query."""
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.query,
body_type=0x41,
body_type=BodyType.X41,
)

@property
Expand Down Expand Up @@ -99,12 +100,12 @@ def _body(self) -> bytearray:
class MessageNewProtocolQuery(MessageA1Base):
"""Message A1 new protocol query."""

def __init__(self, protocol_version: int) -> None:
def __init__(self, protocol_version: ProtocolVersion) -> None:
"""Initialize message A1 new protocol query."""
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.query,
body_type=0xB1,
body_type=BodyType.B1,
)

@property
Expand All @@ -119,12 +120,12 @@ def _body(self) -> bytearray:
class MessageSet(MessageA1Base):
"""Message A1 set."""

def __init__(self, protocol_version: int) -> None:
def __init__(self, protocol_version: ProtocolVersion) -> None:
"""Initialize message A1 set."""
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
body_type=0x48,
body_type=BodyType.X48,
)
self.power = False
self.prompt_tone = False
Expand Down Expand Up @@ -184,12 +185,12 @@ def _body(self) -> bytearray:
class MessageNewProtocolSet(MessageA1Base):
"""Message A1 new protocol set."""

def __init__(self, protocol_version: int) -> None:
def __init__(self, protocol_version: ProtocolVersion) -> None:
"""Initialize message A1 new protocol set."""
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
body_type=0xB0,
body_type=BodyType.B0,
)
self.light: bool | None = None

Expand Down
5 changes: 3 additions & 2 deletions midealocal/devices/ac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from enum import StrEnum
from typing import Any, ClassVar

from midealocal.const import DeviceType, ProtocolVersion
from midealocal.device import MideaDevice

from .message import (
Expand Down Expand Up @@ -82,7 +83,7 @@ def __init__(
port: int,
token: str,
key: str,
protocol: int,
protocol: ProtocolVersion,
model: str,
subtype: int,
customize: str,
Expand All @@ -91,7 +92,7 @@ def __init__(
super().__init__(
name=name,
device_id=device_id,
device_type=0xAC,
device_type=DeviceType.AC,
ip_address=ip_address,
port=port,
token=token,
Expand Down
Loading

0 comments on commit d23e5a9

Please sign in to comment.