Skip to content

Commit

Permalink
Merge branch 'main' into fix-email-obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam authored Jul 28, 2024
2 parents 57bf286 + 8f5ec79 commit 828dec7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
28 changes: 27 additions & 1 deletion midealocal/devices/x40/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Midea local x40 device."""

import json
import logging
import math
from enum import StrEnum
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(
protocol: int,
model: str,
subtype: int,
customize: str, # noqa: ARG002
customize: str,
) -> None:
"""Initialize Midea x40 Device."""
super().__init__(
Expand All @@ -67,6 +68,14 @@ def __init__(
},
)
self._fields: dict[str, Any] = {}
self._precision_halves: bool | None = None
self._default_precision_halves = False
self.set_customize(customize)

@property
def precision_halves(self) -> bool | None:
"""Midea 40 device precision halves."""
return self._precision_halves

@property
def directions(self) -> list[str]:
Expand Down Expand Up @@ -100,6 +109,11 @@ def process_message(self, msg: bytes) -> dict[str, Any]:
for status in self._attributes:
if hasattr(message, str(status)):
value = getattr(message, str(status))
if (
self._precision_halves
and status == DeviceAttributes.current_temperature
):
value /= 2
if status == DeviceAttributes.direction:
self._attributes[status] = self._directions[
self._convert_from_midea_direction(value)
Expand Down Expand Up @@ -139,6 +153,18 @@ def set_attribute(self, attr: str, value: int | str | bool) -> None:
setattr(message, str(attr), value)
self.build_send(message)

def set_customize(self, customize: str) -> None:
"""Midea 40 device set customize."""
self._precision_halves = self._default_precision_halves
if customize and len(customize) > 0:
try:
params = json.loads(customize)
if params and "precision_halves" in params:
self._precision_halves = params.get("precision_halves")
except Exception:
_LOGGER.exception("[%s] Set customize error", self.device_id)
self.update_all({"precision_halves": self._precision_halves})


class MideaAppliance(MideaX40Device):
"""Midea x40 appliance."""
1 change: 1 addition & 0 deletions tests/devices/x40/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Midea local 40 device tests."""
54 changes: 54 additions & 0 deletions tests/devices/x40/device_x40_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Test 40 Device."""

from unittest.mock import patch

import pytest

from midealocal.devices.x40 import DeviceAttributes, MideaX40Device


class TestMideaX40Device:
"""Test Midea 40 Device."""

device: MideaX40Device

@pytest.fixture(autouse=True)
def _setup_device(self) -> None:
"""Midea DA Device setup."""
self.device = MideaX40Device(
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="",
)

def test_customize(self) -> None:
"""Test precision halves."""
with patch(
"midealocal.devices.x40.MessageX40Response",
) as mock_message_response:
mock_message = mock_message_response.return_value
mock_message.light = True
mock_message.ventilation = True
mock_message.fan_speed = 1
mock_message.direction = 5
mock_message.smelly_sensor = True

mock_message.current_temperature = 53
new_status = self.device.process_message(b"")
assert new_status[DeviceAttributes.current_temperature] == 53

self.device.set_customize('{"precision_halves": true}')
assert self.device.precision_halves is True
mock_message.current_temperature = 53
new_status = self.device.process_message(b"")
assert new_status[DeviceAttributes.current_temperature] == 26.5

self.device.set_customize("{") # Test invalid json
assert self.device.precision_halves is False

0 comments on commit 828dec7

Please sign in to comment.