AlexxIT / XiaomiGateway3

Home Assistant custom component for control Xiaomi Multimode Gateway (aka Gateway 3), Xiaomi Multimode Gateway 2, Aqara Hub E1 on default firmwares over LAN
https://github.com/AlexxIT/Blog
MIT License
2.46k stars 345 forks source link

Request to Adapt xiaomi.lock.b03 Door Lock, Fix Related Bug and Merge Code #1483

Open 1269699120 opened 1 week ago

1269699120 commented 1 week ago

I have a xiaomi.lock.b03 door lock and I hope it can be adapted into the plugin. I have written relevant code in the xiaomi_gateway3.py file. After testing, other parts are normal, but the "Abnormal Condition" entity related to this door lock has no response and keeps reporting errors. I have tried various methods but haven't been able to solve it. Now I request you to help fix this bug and also hope that my code can be merged into the plugin so as to better support this door lock. Thank you!

xiaomi_gateway3.py code

from custom_components.xiaomi_gateway3.core.devices import *

DEVICES = [{
    17601: ["Xiaomi", "Xiaomi Smart Door Lock 2 (Finger Vein Unlock)", "xiaomi.lock.b03"],
    "spec": [
        # sensors 传感器
        BaseConv("action", "sensor"),
        # battery 电池
        BaseConv("battery", "sensor", mi="4.p.1003"),
        # lock sensors 门锁状态
        MapConv("door", "sensor", mi="3.p.1021", map={
            1: "Locked", 2: "Unlocked", 3: "Door Closed Properly After Left Ajar", 
            4: "Door Was Ajar", 5: "Leaving The Door Open Timed Out", 6: "Door Not Closed Well", 
            17: "Locked", 33: "Locked", 49: "Locked", 18: "Unlocked", 34: "Unlocked", 50: "Unlocked", 
            19: "Door Closed Properly After Left Ajar", 35: "Door Closed Properly After Left Ajar", 
            51: "Door Closed Properly After Left Ajar", 20: "Door Was Ajar", 36: "Door Was Ajar", 
            52: "Door Was Ajar", 21: "Leaving The Door Open Timed Out", 37: "Leaving The Door Open Timed Out", 
            53: "Leaving The Door Open Timed Out", 22: "Door Not Closed Well", 38: "Door Not Closed Well", 
            54: "Door Not Closed Well"}),
        # Operation Method 开锁方式
        MapConv("Operation Method", "sensor", mi="2.e.1020.p.2", map = {
            1: "Smart Device",          # Mobile Phone: 智能设备解锁
            2: "Biometric",             # Finger Print: 生物识别(指纹)
            3: "PIN/Password",          # PassWord: 密码认证
            4: "Contactless",           # NFC: 非接触式认证
            5: "Physical Key",          # Lock Key: 物理钥匙
            6: "OTP",                   # One Time Password: 一次性密码
            7: "Cyclic Code",           # Periodic Password: 周期性密码
            8: "Emergency Override",    # Coerce: 强制开锁机制(紧急情况下)
            9: "Interior Unlock",       # Button Unlock Inside: 内部按钮解锁
            10: "Vein Recognition"}),   # Finger Vein: 指静脉识别
        # Operation ID 开锁ID
        BaseConv("Operation ID", "sensor", mi="2.e.1020.p.3"),
        # Abnormal Condition 异常状态
        MapConv("Abnormal Condition", "sensor", mi="2.e.1020.p.1", map={
            1: "Frequent Unlocking Failed By Multiple Methods",
            2: "Door Lock Was Damaged",
            3: "Door Lock Was Reset",
            4: "Door Lock Batteries Are Low",
            5: "Door Lock Batteries Are Nearly Depleted",
            6: "Unlock From Inside After Leaving Home",
            7: "Door Was Ajar",
            9: "Leaving The Door Open Timed Out",
            10: "Door Closed Properly After Left Ajar",
            11: "The Door Is Not Closed Well",
            12: "Unlock Exception"}),
        # lock action 门锁执行动作
        MapConv("Lock Action", "sensor", mi="2.e.1020.p.6", map={1: "Lock", 2: "Unlock", 3: "Locked From Inside",
            4: "Released Lock From Inside",5: "Enabled Child Lock", 6: "Disabled Child Lock", 
            7: "Enable The Away From Home Mode", 8: "Disable The Away From Home Mode", 9: "Close The Door", 
            10: "Enabled Child Lock And From Inside", 11: "Disabled Child Lock And From Inside"}),
        # Operation Position 哪里开门
        MapConv("position", "sensor", mi="2.e.1020.p.5", map={1: "Indoor", 2: "OutDoor"}),
        # doorbell action 门铃
        ConstConv("action", mi="5.e.1006", value="doorbell"),
        BaseConv("timestamp", mi="5.e.1006.p.1"),
    ],
}] + DEVICES
1269699120 commented 1 week ago

The "Abnormal Condition" has been showing as "unknown" all the time. I don't know what to do. As shown in the picture. 屏幕截图 2024-11-05 223941

I've tested the other entities and they all work normally.

Related links of the door lock https://home.miot-spec.com/spec/xiaomi.lock.b03

1269699120 commented 1 week ago

code files xiaomi_gateway3.py.txt

AlexxIT commented 6 days ago

This is 4.p.1003 property. It has state. This is 2.e.1020.p.2 event. It hasn't state. It's bad idea to use sensor for it.

1269699120 commented 5 days ago

This is 4.p.1003 property. It has state. This is 2.e.1020.p.2 event. It hasn't state. It's bad idea to use sensor for it.

I have modified the content inside. And for 2.e.1020.p.2, I wrote it by referring to the lumi.lock.mcn002 device from lines 1738 to 1760 in the file at https://github.com/AlexxIT/XiaomiGateway3/blob/master/custom_components/xiaomi_gateway3/core/devices.py. Currently, it can already be displayed in the action entity. Could you please take a look and see if it can be adapted to the plug-in? Here is the complete modified code.

from custom_components.xiaomi_gateway3.core.devices import *

DEVICES = [{
    # 门锁参数资料 https://home.miot-spec.com/spec/xiaomi.lock.b03
    # 参考其它的门锁 6017: ["Xiaomi", "Face Recognition Smart Door Lock", "XMZNMS09LM", "lumi.lock.mcn002"] 
    # https://github.com/AlexxIT/XiaomiGateway3/blob/master/custom_components/xiaomi_gateway3/core/devices.py
    # https://home.miot-spec.com/spec/lumi.lock.mcn002

    17601: ["Xiaomi", "Xiaomi Smart Door Lock 2 (Finger Vein Unlock)", "xiaomi.lock.b03"],
    "spec": [

        # action sensors 传感器
        BaseConv("action", "sensor"),

        # battery 电池
        BaseConv("battery", "sensor", mi="4.p.1003"),

        # The door lock status 门开/关状态
        MapConv("lock", "binary_sensor", mi="3.p.1021", map={
            1: False,
            2: True, 
            17: False, 
            33: False, 
            49: False,
            18: True, 
            34: False, 
            50: False}),

        # Door status 门状态
        MapConv("door", "sensor", mi="3.p.1021", map={
            1: "Locked",
            2: "Unlocked",
            3: "Door_Closed_Properly_After_Left_Ajar",
            4: "Door_Was_Ajar",
            5: "Leaving_The_Door_Open_Timed_Out",
            6: "Door_Not_Closed_Well",
            17: "Locked",
            33: "Locked",
            49: "Locked",
            18: "Unlocked",
            34: "Unlocked",
            50: "Unlocked",
            19: "Door_Closed_Properly_After_Left_Ajar",
            35: "Door_Closely_Properly_After_Left_Ajar",
            51: "Door_Closed_Properly_After_Left_Ajar",
            20: "Door_Was_Ajar",
            36: "Door_Was_Ajar",
            52: "Door_Was_Ajar",
            21: "Leaving_The_Door_Open_Timed_Out",
            37: "Leaving_The_Door_Open_Timed_Out",
            53: "Leaving_The_Door_Open_Timed_Out",
            22: "Door_Not_Closed_Well",
            38: "Door_Not_Closed_Well",
            54: "Door_Not_Closed_Well"}),

        # Operation Method 开锁方式
        MapConv("Operation_Method", "sensor", mi="2.e.1020.p.2", map = {
            1: "Smart_Device",          # Mobile Phone: 智能设备解锁
            2: "Biometric",             # Finger Print: 生物识别(指纹)
            3: "PIN/Password",          # PassWord: 密码认证
            4: "Contactless",           # NFC: 非接触式认证
            5: "Physical_Key",          # Lock Key: 物理钥匙
            6: "OTP",                   # One Time Password: 一次性密码
            7: "Cyclic_Code",           # Periodic Password: 周期性密码
            8: "Emergency_Override",    # Coerce: 强制开锁机制(紧急情况下)
            9: "Interior_Unlock",       # Button Unlock Inside: 内部按钮解锁
            10: "Vein_Recognition"}),   # Finger Vein: 指静脉识别

        # Operation ID 开锁ID
        BaseConv("Operation_ID", "sensor", mi="2.e.1020.p.3"),

        # Abnormal Condition 异常状态
        ConstConv("action", mi="2.e.1007", value="error"),
        BaseConv("error_id", mi="2.e.1007.p.1"),
        MapConv("abnormal_condition", mi="2.e.1007.p.1", map={
            1: "Frequent_Unlocking_Failed_By_Multiple_Methods",
            2: "Door_Lock_Was_Damaged",
            3: "Door_Lock_Was_Reset",
            4: "Door_Lock_Batteries_Are_Low",
            5: "Door_Lock_Batteries_Are_Nearly_Depleted",
            6: "Unlock_From_Inside_After_Leaving_Home",
            7: "Door_Was_Ajar",
            9: "Leaving_The_Door_Open_Timed_Out",
            10: "Door_Closed_Properly_After_Left_Ajar",
            11: "The_Door_Is_Not_Closed_Well",
            12: "Unlock_Exception"}),
        BaseConv("timestamp", mi="2.e.1007.p.4"),

        # lock action 门锁执行动作
        MapConv("Lock Action", "sensor", mi="2.e.1020.p.6", map={
            1: "Lock",
            2: "Unlock",
            3: "Locked_From_Inside",
            4: "Released_Lock_From_Inside",
            5: "Enabled_Child_Lock",
            6: "Disabled_Child_Lock",
            7: "Enable_The_Away_From_Home_Mode",
            8: "Disable_The_Away_From_Home_Mode",
            9: "Close_The_Door",
            10: "Enabled_Child_Lock_And_From_Inside",
            11: "Disabled_Child_Lock_And_From_Inside"}),

        # Operation Position 哪里开门
        MapConv("position", "sensor", mi="2.e.1020.p.5", map={1: "Indoor", 2: "OutDoor"}),

        # doorbell action 门铃
        ConstConv("action", mi="5.e.1006", value="doorbell"),
        BaseConv("timestamp", mi="5.e.1006.p.4"),
    ],
}] + DEVICES
AlexxIT commented 2 days ago

Your converter is very different from those already in the component (locks). I can't accept it as it is

1269699120 commented 1 day ago

Your converter is very different from those already in the component (locks). I can't accept it as it is

Sorry, my coding level isn't very professional. This is the best I could do with my code. It's okay if you don't accept it. And I'm really grateful if you're still willing to make adjustments for this smart lock