From 393743c6455d5e8cb0a9c8b4e94916a94ccb052b Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 24 Mar 2024 08:35:23 +0300 Subject: [PATCH] Fix time pattern for giot.light.v5ssm #1142 --- .../xiaomi_gateway3/core/converters/mesh.py | 28 +++++++++++++++++++ .../xiaomi_gateway3/core/devices.py | 2 +- tests/test_conv_mesh.py | 10 +++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_gateway3/core/converters/mesh.py b/custom_components/xiaomi_gateway3/core/converters/mesh.py index d4f65fcc..031fd98c 100644 --- a/custom_components/xiaomi_gateway3/core/converters/mesh.py +++ b/custom_components/xiaomi_gateway3/core/converters/mesh.py @@ -24,3 +24,31 @@ def encode(self, device: "XDevice", payload: dict, value: str): if v in "+xv": mask |= 1 << i super().encode(device, payload, mask) + + +class GiotTimePatternConv(BaseConv): + """ + Period encoding: + 8-digit number: HHMMhhmm + HH = start hour + MM = start minute + hh = end hour + mm = end minute + Example: + Period: 23:59 - 10:44 + Encoded: 23591044 + """ + + pattern = "^[0-2][0-9]:[0-5][0-9]-[0-2][0-9]:[0-5][0-9]$" + + def decode(self, device: "XDevice", payload: dict, value: int): + value = str(value) + if len(value) != 8: + return + payload[self.attr] = f"{value[:2]}:{value[2:4]}-{value[4:6]}:{value[6:]}" + + def encode(self, device: "XDevice", payload: dict, value: str): + value = value.replace(":", "").replace("-", "") + if len(value) != 8: + return + super().encode(device, payload, int(value)) diff --git a/custom_components/xiaomi_gateway3/core/devices.py b/custom_components/xiaomi_gateway3/core/devices.py index 2ff11b7d..c0d455ab 100644 --- a/custom_components/xiaomi_gateway3/core/devices.py +++ b/custom_components/xiaomi_gateway3/core/devices.py @@ -2777,7 +2777,7 @@ MathConv("turn_off_transit_sec", "number", mi="3.p.18", multiply=0.001, min=100, max=30000, step=100, round=1, entity=ENTITY_CONFIG), MathConv("change_transit_sec", "number", mi="3.p.19", multiply=0.001, min=100, max=30000, step=100, round=1, entity=ENTITY_CONFIG), MathConv("min_brightness", "number", mi="3.p.23", multiply=0.1, min=1, max=500, step=1, round=1, entity=ENTITY_CONFIG), - # TODO GiotTimePatternConv("night_light_time", "text", mi="3.p.16") + GiotTimePatternConv("night_light_time", "text", mi="3.p.16", entity=ENTITY_CONFIG) # Converter("fill_light_detection", "sensor", mi="3.p.20"), # Converter("fill_light_switch", "switch", mi="3.p.21"), # MathConv("min_bri_factory", "number", mi="3.p.16", min=1, max=500), diff --git a/tests/test_conv_mesh.py b/tests/test_conv_mesh.py index 8dfc5780..b0f567e5 100644 --- a/tests/test_conv_mesh.py +++ b/tests/test_conv_mesh.py @@ -12,3 +12,13 @@ def test_es1(): p = device.encode({"induction_range": "0+0.8+1.5+2.3+3.0_3.8_4.5_5.3_6"}) assert p == {"mi_spec": [{"piid": 2, "siid": 3, "value": 15}]} + + +def test_11724(): + device = XDevice(11724) + + p = device.decode({"did": "123", "siid": 3, "piid": 16, "value": 23591044}) + assert p == {"night_light_time": "23:59-10:44"} + + p = device.encode({"night_light_time": "23:59-10:44"}) + assert p == {"mi_spec": [{"siid": 3, "piid": 16, "value": 23591044}]}