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

fix(capabilities): make capabilities optional #217

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 3 additions & 33 deletions midealocal/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def connect(
if refresh_status:
self.refresh_status(wait_response=True)
if get_capabilities:
self.get_capabilities(wait_response=True)
self.get_capabilities()
rokam marked this conversation as resolved.
Show resolved Hide resolved
connected = True
except TimeoutError:
_LOGGER.debug("[%s] Connection timed out", self._device_id)
Expand Down Expand Up @@ -292,41 +292,11 @@ def build_send(self, cmd: MessageRequest) -> None:
msg = PacketBuilder(self._device_id, data).finalize()
self.send_message(msg)

def get_capabilities(self, wait_response: bool = False) -> None:
def get_capabilities(self) -> None:
"""Get device capabilities."""
cmds: list = self.capabilities_query()
if self._appliance_query:
cmds = [MessageQueryAppliance(self.device_type), *cmds]
error_count = 0
for cmd in cmds:
if cmd.__class__.__name__ not in self._unsupported_protocol:
self.build_send(cmd)
if wait_response:
try:
while True:
if not self._socket:
raise SocketException
msg = self._socket.recv(512)
if len(msg) == 0:
raise OSError("Empty message received.")
result = self.parse_message(msg)
if result == ParseMessageResult.SUCCESS:
break
if result == ParseMessageResult.PADDING:
continue
error_count += 1
except TimeoutError:
error_count += 1
self._unsupported_protocol.append(cmd.__class__.__name__)
_LOGGER.debug(
"[%s] Does not supports the protocol %s, ignored",
self._device_id,
cmd.__class__.__name__,
)
else:
error_count += 1
if len(cmds) > 0 and error_count == len(cmds):
raise CapabilitiesFailed
self.build_send(cmd)

def refresh_status(self, wait_response: bool = False) -> None:
"""Refresh device status."""
Expand Down
41 changes: 4 additions & 37 deletions tests/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ProtocolVersion,
RefreshFailed,
)
from midealocal.devices.ac.message import MessageCapabilitiesQuery
from midealocal.exceptions import SocketException
from midealocal.message import MessageType

Expand Down Expand Up @@ -225,50 +226,16 @@ def test_send_message(self) -> None:

def test_get_capabilities(self) -> None:
"""Test get capabilities."""
self.device._appliance_query = False
self.device.get_capabilities() # Empty capabilities
self.device._appliance_query = True
socket_mock = MagicMock()
with (
patch.object(
socket_mock,
"recv",
side_effect=[
bytearray([]),
bytearray([0x0]),
bytearray([0x0]),
bytearray([0x0]),
TimeoutError(),
],
),
patch.object(self.device, "build_send", return_value=None),
patch.object(
self.device,
"parse_message",
side_effect=[
ParseMessageResult.SUCCESS,
ParseMessageResult.PADDING,
ParseMessageResult.ERROR,
],
"capabilities_query",
return_value=[MessageCapabilitiesQuery(ProtocolVersion.V2, False)],
),
):
self.device._socket = None
with pytest.raises(SocketException):
self.device.get_capabilities(True)

self.device._socket = socket_mock
with pytest.raises(OSError, match="Empty message received."):
self.device.get_capabilities(True)

self.device.get_capabilities(True) # SUCCESS
self.device.get_capabilities(True) # PADDING

with pytest.raises(CapabilitiesFailed):
self.device.get_capabilities(True) # ERROR
with pytest.raises(CapabilitiesFailed):
self.device.get_capabilities(True) # Timeout
with pytest.raises(CapabilitiesFailed):
self.device.get_capabilities(True) # Unsupported protocol
self.device.get_capabilities()

def test_refresh_status(self) -> None:
"""Test refresh status."""
Expand Down