From 525c4ed86622da2f4799e6e5df54f4d229f4aa8a Mon Sep 17 00:00:00 2001 From: Necroneco Date: Fri, 26 Jul 2024 22:55:58 +0800 Subject: [PATCH 1/3] feat(40): add `precision_halves` customization --- midealocal/devices/x40/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/midealocal/devices/x40/__init__.py b/midealocal/devices/x40/__init__.py index 94c573f4..3a30fff4 100644 --- a/midealocal/devices/x40/__init__.py +++ b/midealocal/devices/x40/__init__.py @@ -1,5 +1,6 @@ """Midea local x40 device.""" +import json import logging import math from enum import StrEnum @@ -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__( @@ -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]: @@ -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) @@ -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.""" From e2e5704695601e46762ffe70ee964316b7ab6d69 Mon Sep 17 00:00:00 2001 From: Necroneco Date: Sat, 27 Jul 2024 22:23:08 +0800 Subject: [PATCH 2/3] feat(40): add test for `precision_halves` customization --- tests/devices/x40/__init__.py | 1 + tests/devices/x40/device_x40_test.py | 51 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/devices/x40/__init__.py create mode 100644 tests/devices/x40/device_x40_test.py diff --git a/tests/devices/x40/__init__.py b/tests/devices/x40/__init__.py new file mode 100644 index 00000000..3ca61f53 --- /dev/null +++ b/tests/devices/x40/__init__.py @@ -0,0 +1 @@ +"""Midea local 40 device tests.""" diff --git a/tests/devices/x40/device_x40_test.py b/tests/devices/x40/device_x40_test.py new file mode 100644 index 00000000..adff189d --- /dev/null +++ b/tests/devices/x40/device_x40_test.py @@ -0,0 +1,51 @@ +"""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 From ff7f2bb6b8057a5641b65cbd5f1917c8572ffc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mind=C3=AAllo=20de=20Andrade?= Date: Sun, 28 Jul 2024 12:41:07 +0000 Subject: [PATCH 3/3] test(x40): invalid customize --- tests/devices/x40/device_x40_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/devices/x40/device_x40_test.py b/tests/devices/x40/device_x40_test.py index adff189d..cfb39a6a 100644 --- a/tests/devices/x40/device_x40_test.py +++ b/tests/devices/x40/device_x40_test.py @@ -49,3 +49,6 @@ def test_customize(self) -> None: 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