Преглед изворни кода

optimize voltage and current update logic

zerzhang пре 1 месец
родитељ
комит
ee3a5ac442
2 измењених фајлова са 17 додато и 6 уклоњено
  1. 0 6
      switchbot/adv_parsers/relay_switch.py
  2. 17 0
      switchbot/devices/relay_switch.py

+ 0 - 6
switchbot/adv_parsers/relay_switch.py

@@ -28,8 +28,6 @@ def process_relay_switch_1pm(data: bytes | None, mfr_data: bytes | None) -> dict
         return {}
     common_data = process_relay_switch_common_data(data, mfr_data)
     common_data["power"] = parse_power_data(mfr_data, 10, 12)
-    common_data["voltage"] = 0
-    common_data["current"] = 0
     return common_data
 
 
@@ -51,16 +49,12 @@ def process_relay_switch_2pm(data: bytes | None, mfr_data: bytes | None) -> dict
         1: {
             **process_relay_switch_common_data(data, mfr_data),
             "power": parse_power_data(mfr_data, 10, 12),
-            "voltage": 0,
-            "current": 0,
         },
         2: {
             "switchMode": True,  # for compatibility, useless
             "sequence_number": mfr_data[6],
             "isOn": bool(mfr_data[7] & 0b01000000),
             "power": parse_power_data(mfr_data, 12, 14),
-            "voltage": 0,
-            "current": 0,
         },
     }
 

+ 17 - 0
switchbot/devices/relay_switch.py

@@ -5,6 +5,7 @@ from typing import Any
 from bleak.backends.device import BLEDevice
 
 from ..const import SwitchbotModel
+from ..models import SwitchBotAdvertisement
 from .device import (
     SwitchbotEncryptedDevice,
     SwitchbotSequenceDevice,
@@ -77,6 +78,22 @@ class SwitchbotRelaySwitch(SwitchbotSequenceDevice, SwitchbotEncryptedDevice):
             device, key_id, encryption_key, model, **kwargs
         )
 
+    def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> None:
+        """Update device data from advertisement."""
+        adv_data = advertisement.data["data"]
+        channel = self._channel if hasattr(self, "_channel") else None
+
+        if channel is None:
+            adv_data["voltage"] = self._get_adv_value("voltage") or 0
+            adv_data["current"] = self._get_adv_value("current") or 0
+        else:
+            for i in range(1, channel + 1):
+                adv_data[i] = adv_data.get(i, {})
+                adv_data[i]["voltage"] = self._get_adv_value("voltage", i) or 0
+                adv_data[i]["current"] = self._get_adv_value("current", i) or 0
+        super().update_from_advertisement(advertisement)
+
+
     def get_current_time_and_start_time(self) -> int:
         """Get current time in seconds since epoch."""
         current_time = int(time.time())