From 8f5ec79fe859ad88519360353cd3b6e159200625 Mon Sep 17 00:00:00 2001 From: Necroneco Date: Sun, 28 Jul 2024 20:44:54 +0800 Subject: [PATCH] feat(40): add `precision_halves` customization (#248) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit copied from `E3` https://github.com/georgezhao2010/midea_ac_lan/issues/152 ## Summary by CodeRabbit - **New Features** - Introduced a new method to customize precision for temperature settings. - Added a property to access the precision half setting easily. - **Enhancements** - Improved temperature data processing based on the new precision setting for better configurability. - Expanded test coverage with new unit tests for the `MideaX40Device` class to ensure reliability and functionality. --------- Co-authored-by: Lucas MindĂȘllo de Andrade --- midealocal/devices/x40/__init__.py | 28 ++++++++++++++- tests/devices/x40/__init__.py | 1 + tests/devices/x40/device_x40_test.py | 54 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/devices/x40/__init__.py create mode 100644 tests/devices/x40/device_x40_test.py 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.""" 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..cfb39a6a --- /dev/null +++ b/tests/devices/x40/device_x40_test.py @@ -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