Skip to content

Commit

Permalink
fix: ed device power/lock return message set and body_type 0x15 parse (
Browse files Browse the repository at this point in the history
…#284)

fix #283 
origin from wuwentao/midea_ac_lan#299


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Introduced a new body type, `X15`, to enhance message categorization.
- Expanded response handling to accommodate a new message type,
improving flexibility in message processing.

- **Improvements**
- Updated initialization parameters for message handling classes to
enhance readability and maintainability.
- Established a comprehensive suite of unit tests for ED message
handling, ensuring robust functionality and correctness.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
wuwentao authored Sep 9, 2024
1 parent 4e5d61c commit d9d4fac
Show file tree
Hide file tree
Showing 5 changed files with 463 additions and 3 deletions.
10 changes: 7 additions & 3 deletions midealocal/devices/ed/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, protocol_version: int) -> None:
super().__init__(
protocol_version=protocol_version,
message_type=MessageType.set,
body_type=0x15,
body_type=BodyType.X15,
)
self.power: bool | None = None
self.lock: bool | None = None
Expand Down Expand Up @@ -244,9 +244,13 @@ class MessageEDResponse(MessageResponse):
def __init__(self, message: bytes) -> None:
"""Initialize ED message response."""
super().__init__(bytearray(message))
if self._message_type in [MessageType.query, MessageType.notify1]:
if self._message_type in [
MessageType.set,
MessageType.query,
MessageType.notify1,
]:
self.device_class = self._body_type
if self._body_type in [BodyType.X00, BodyType.FF]:
if self._body_type in [BodyType.X00, BodyType.X15, BodyType.FF]:
self.set_body(EDMessageBodyFF(super().body))
if self.body_type == BodyType.X01:
self.set_body(EDMessageBody01(super().body))
Expand Down
1 change: 1 addition & 0 deletions midealocal/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BodyType(IntEnum):
X07 = 0x07
X0A = 0x0A
X11 = 0x11
X15 = 0x15
X21 = 0x21
X22 = 0x22
X24 = 0x24
Expand Down
1 change: 1 addition & 0 deletions tests/devices/ed/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Midea local ED device tests."""
101 changes: 101 additions & 0 deletions tests/devices/ed/device_ed_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Test ED Device."""

from unittest.mock import patch

import pytest

from midealocal.devices.ed import DeviceAttributes, MideaEDDevice
from midealocal.devices.ed.message import MessageQuery


class TestMideaEDDevice:
"""Test Midea ED Device."""

device: MideaEDDevice

@pytest.fixture(autouse=True)
def _setup_device(self) -> None:
"""Midea ed Device setup."""
self.device = MideaEDDevice(
name="Test Device",
device_id=1,
ip_address="192.168.1.100",
port=6444,
token="AA",
key="BB",
protocol=3,
model="test_model",
subtype=1,
customize="test_customize",
)

def test_initial_attributes(self) -> None:
"""Test initial attributes."""
assert not self.device.attributes[DeviceAttributes.power]
assert self.device.attributes[DeviceAttributes.water_consumption] is None
assert self.device.attributes[DeviceAttributes.in_tds] is None
assert self.device.attributes[DeviceAttributes.out_tds] is None
assert self.device.attributes[DeviceAttributes.filter1] is None
assert self.device.attributes[DeviceAttributes.filter2] is None
assert self.device.attributes[DeviceAttributes.filter3] is None
assert self.device.attributes[DeviceAttributes.life1] is None
assert self.device.attributes[DeviceAttributes.life2] is None
assert self.device.attributes[DeviceAttributes.life3] is None
assert not self.device.attributes[DeviceAttributes.child_lock]

def test_process_message(self) -> None:
"""Test process message."""
with patch("midealocal.devices.ed.MessageEDResponse") as mock_message_response:
mock_message = mock_message_response.return_value
mock_message.protocol_version = 3
mock_message.power = True
mock_message.water_consumption = 123
mock_message.in_tds = 200
mock_message.out_tds = 5
mock_message.filter1 = 30
mock_message.filter2 = 20
mock_message.filter3 = 10
mock_message.life1 = 2
mock_message.life2 = 3
mock_message.life3 = 4
mock_message.child_lock = True
new_status = self.device.process_message(b"")
assert new_status[DeviceAttributes.power.value]
assert new_status[DeviceAttributes.water_consumption.value] == 123
assert new_status[DeviceAttributes.in_tds.value] == 200
assert new_status[DeviceAttributes.out_tds.value] == 5
assert new_status[DeviceAttributes.filter1.value] == 30
assert new_status[DeviceAttributes.filter2.value] == 20
assert new_status[DeviceAttributes.filter3.value] == 10
assert new_status[DeviceAttributes.life1.value] == 2
assert new_status[DeviceAttributes.life2.value] == 3
assert new_status[DeviceAttributes.life3.value] == 4

mock_message.child_lock = False
mock_message.water_consumption = 456
mock_message.in_tds = 300
mock_message.out_tds = 15
mock_message.filter1 = 15
mock_message.life3 = 15
new_status = self.device.process_message(b"")
assert not new_status[DeviceAttributes.child_lock.value]
assert new_status[DeviceAttributes.water_consumption.value] == 456
assert new_status[DeviceAttributes.in_tds.value] == 300
assert new_status[DeviceAttributes.out_tds.value] == 15
assert new_status[DeviceAttributes.filter1.value] == 15
assert new_status[DeviceAttributes.life3.value] == 15

def test_build_query(self) -> None:
"""Test build query."""
queries = self.device.build_query()
assert len(queries) == 1
assert isinstance(queries[0], MessageQuery)

def test_set_attribute(self) -> None:
"""Test set attribute."""
with patch.object(self.device, "build_send") as mock_build_send:
self.device.set_attribute(DeviceAttributes.power, True)
mock_build_send.assert_called_once()

self.device.set_attribute(DeviceAttributes.child_lock, True)
mock_build_send.assert_called()
Loading

0 comments on commit d9d4fac

Please sign in to comment.