『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 2289|回复: 23

[技术探讨] 【求助】【已解决】关于gateway3添加新旋钮设备

[复制链接]

2

主题

96

帖子

737

积分

高级会员

Rank: 4

积分
737
金钱
641
HASS币
0
发表于 2024-6-17 23:02:42 | 显示全部楼层 |阅读模式
本帖最后由 invincible 于 2024-6-19 11:42 编辑

今天到手了一个四键旋钮,接入米家并且能够获取旋转角度。 米家日志.jpg
参照@relliky大佬的帖子https://bbs.hassbian.com/thread-25298-1-1.html折腾了一晚上,实在是心有余力不足,特来请教各位大佬。
https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:remote-control:0000A021:lemesh-ts10:1
https://home.miot-spec.com/spec?type=urn:miot-spec-v2:device:remote-control:0000A021:lemesh-ts10:1
目前xiaomi_gateway3.py的内容如下:
from custom_components.xiaomi_gateway3.core.devices import *

DEVICES = [{
    17658: ["LeMesh", "Scenario knob remote control Alpha", "lemesh.remote.ts10"],
    "spec": [
        BaseConv("battery", mi="4.p.1003"),  # uint8
        BaseConv("action", "sensor"),
        MapConv("action", mi="5.e.1012.p.1", map={1: "button_up_single", 2: "button_right_single", 3: "button_down_single", 4: "button_left_single", 5: "right_rotation", 6: "left_rotation", 11: "press_up_right_rotation", 12: "press_up_left_rotation", 13: "press_right_right_rotation", 14: "press_right_left_rotation", 7: "press_down_right_rotation", 8: "press_down_left_rotation", 9: "press_left_right_rotation", 10: "press_left_left_rotation"}),
        MapConv("action", mi="5.e.1013.p.1", map={1: "button_up_double", 2: "button_right_double", 3: "button_down_double", 4: "button_left_double"}),
        MapConv("action", mi="5.e.1014.p.1", map={1: "button_up_hold", 2: "button_right_hold", 3: "button_down_hold", 4: "button_left_hold"}),
        ConstConv("action", mi="5.e.1036", value="rotate"),
        BaseConv("rotate", mi="5.e.1036.p.2"),
    ],
}] + DEVICES
xiaomi_gateway3插件log示例如下:
#这是上键单击
2024-06-17 14:18:29.820 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":1771553510,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22028,"edata":"01"}],"frmCnt":6,"gwts":1718633908}}'}
#这是上键双击
2024-06-17 14:18:32.370 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":551606511,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22029,"edata":"01"}],"frmCnt":7,"gwts":1718633910}}'}
#这是上键长按
2024-06-17 14:18:35.940 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":2090572513,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22030,"edata":"01"}],"frmCnt":8,"gwts":1718633914}}'}
#这是右旋,两条。eid:22028与单击一样,edata:05。eid:22052是旋转,edata:19是旋转角度(十六进制)
2024-06-17 14:18:40.020 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":1927828514,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22028,"edata":"05"}],"frmCnt":9,"gwts":1718633918}}'}
2024-06-17 14:18:41.550 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":884777515,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22052,"edata":"19"}],"frmCnt":10,"gwts":1718633920}}'}
#这是按住上键右旋,也是两条。
2024-06-17 14:18:46.650 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":2102658517,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22028,"edata":"0b"}],"frmCnt":11,"gwts":1718633925}}'}
2024-06-17 14:18:48.180 DEBUG (MainThread) [custom_components.xiaomi_gateway3.mqtt.192.168.2.133] {'topic': 'miio/report', 'data': b'{"id":540644518,"method":"_async.ble_event","params":{"dev":{"did":"blt.3.1i12kt5104k03","mac":"78:DA:07:F2:BC:2D","pdid":17658},"evt":[{"eid":22052,"edata":"1e"}],"frmCnt":12,"gwts":1718633926}}'}
然后现在action无数据,旋转和电量都是不可用。
截图1.png
截图2.png




回复

使用道具 举报

12

主题

434

帖子

2340

积分

金牌会员

Rank: 6Rank: 6

积分
2340
金钱
1906
HASS币
0
发表于 2024-6-18 09:33:54 | 显示全部楼层
这个垃圾多模,反正我的门锁,接入一会儿,过段时间又不行,已经放弃了,你的21分钟前正常,然后就没有接入多模了
回复

使用道具 举报

2

主题

96

帖子

737

积分

高级会员

Rank: 4

积分
737
金钱
641
HASS币
0
 楼主| 发表于 2024-6-18 09:51:14 | 显示全部楼层
jjss520 发表于 2024-6-18 09:33
这个垃圾多模,反正我的门锁,接入一会儿,过段时间又不行,已经放弃了,你的21分钟前正常,然后就没有接入 ...

我这个不是21分钟前正常,是21分钟前BLE有数据
回复

使用道具 举报

12

主题

434

帖子

2340

积分

金牌会员

Rank: 6Rank: 6

积分
2340
金钱
1906
HASS币
0
发表于 2024-6-18 09:55:07 | 显示全部楼层

回帖奖励 +5 金钱

invincible 发表于 2024-6-18 09:51
我这个不是21分钟前正常,是21分钟前BLE有数据

是的,在这之后设备就没连接到网关,所以没数据
回复

使用道具 举报

32

主题

1065

帖子

4942

积分

论坛元老

Rank: 8Rank: 8

积分
4942
金钱
3862
HASS币
90
发表于 2024-6-18 19:13:23 | 显示全部楼层

回帖奖励 +5 金钱

本帖最后由 relliky 于 2024-6-18 20:50 编辑

看着没问题啊,service iid, event iid, property iid看着都是对的。你用的是最新的xiaomi gateway3吗?最近几个版本才改的这个新的语法。
我家全屋智能的HA设置 https://github.com/relliky/Tais_Home_Assistant_Config
回复

使用道具 举报

32

主题

1065

帖子

4942

积分

论坛元老

Rank: 8Rank: 8

积分
4942
金钱
3862
HASS币
90
发表于 2024-6-18 20:48:45 | 显示全部楼层
本帖最后由 relliky 于 2024-6-18 21:25 编辑

看你已经研究了一部分了,你要是看得懂python,也可去翻一下alexiit的源代码。。就算有些看不懂的地方,忽略掉它们,只在关键地方去打印点信息出来看看是哪步错了。
如果你想查错源代码,给你点思路,从这里查起 https://github.com/AlexxIT/Xiaom ... core/device.py#L340
查为什么这个eid最后没有生成你想要的
我家全屋智能的HA设置 https://github.com/relliky/Tais_Home_Assistant_Config
回复

使用道具 举报

2

主题

96

帖子

737

积分

高级会员

Rank: 4

积分
737
金钱
641
HASS币
0
 楼主| 发表于 2024-6-18 21:06:15 | 显示全部楼层
relliky 发表于 2024-6-18 19:13
看着没问题啊,service iid, event iid, property iid看着都是对的。你用的是最新的xiaomi gateway3吗?最 ...

唉,gw3已经是4.0.5了。大佬你的电量显示解决没,感觉是类似的问题
回复

使用道具 举报

2

主题

96

帖子

737

积分

高级会员

Rank: 4

积分
737
金钱
641
HASS币
0
 楼主| 发表于 2024-6-18 23:38:47 | 显示全部楼层
本帖最后由 invincible 于 2024-6-19 00:32 编辑
relliky 发表于 2024-6-18 20:48
看你已经研究了一部分了,你要是看得懂python,也可去翻一下alexiit的源代码。。就算有些看不懂的地方,忽 ...

大佬,目前歪打正着解决了按钮和旋转角度的问题。以下是当前的xiaomi_gateway3.py
from custom_components.xiaomi_gateway3.core.devices import *

DEVICES = [{
    17658: ["LeMesh", "Scenario knob remote control Alpha", "lemesh.remote.ts10"],
    "spec": [
        # mibeacon2 spec
        BaseConv("battery", mi="4.p.1003"),  # uint8
        BaseConv("action", "sensor"),
        MapConv("action", mi=22028, map={"01": "button_up_single", "02": "button_right_single", "03": "button_down_single", "04": "button_left_single", "05": "right_rotation", "06": "left_rotation", "0b": "up_press_right_rotation", "0c": "up_press_left_rotation", "0d": "right_press_right_rotation", "0e": "right_press_left_rotation", "07": "down_press_right_rotation", "08": "down_press_left_rotation", "09": "left_press_right_rotation", "0a": "left_press_left_rotation"}),
        MapConv("action", mi=22029, map={"01": "button_up_double", "02": "button_right_double", "03": "button_down_double", "04": "button_left_double"}),
        MapConv("action", mi=22030, map={"01": "button_up_hold", "02": "button_right_hold", "03": "button_down_hold", "04": "button_left_hold"}),
        BLENegativeConv("rotate", "sensor", mi=22052),
        # miot spec
        #BaseConv("battery", mi="4.p.1003"),  # uint8
        #BaseConv("action", "sensor"),
        #MapConv("action", mi="5.e.1012.p.1", map={1: "button_up_single", 2: "button_right_single", 3: "button_down_single", 4: "button_left_single", 5: "right_rotation", 6: "left_rotation", 11: "press_up_right_rotation", 12: "press_up_left_rotation", 13: "press_right_right_rotation", 14: "press_right_left_rotation", 7: "press_down_right_rotation", 8: "press_down_left_rotation", 9: "press_left_right_rotation", 10: "press_left_left_rotation"}),
        #MapConv("action", mi="5.e.1013.p.1", map={1: "button_up_double", 2: "button_right_double", 3: "button_down_double", 4: "button_left_double"}),
        #MapConv("action", mi="5.e.1014.p.1", map={1: "button_up_hold", 2: "button_right_hold", 3: "button_down_hold", 4: "button_left_hold"}),
        #ConstConv("action", mi="5.e.1036", value="rotate"),
        #BaseConv("rotate", mi="5.e.1036.p.2"),
    ],
}] + DEVICES

1、抱着试试的心态,抄了下mibeacon协议下的写法,然后就好了。。。;
2、rotate在edata里是int8类型的十六进制数据,本来打算用作者mibeacon.py里的BLEByteConv,然后发现,右旋是正数,没问题;左旋是负数,显示出来是很离谱的数字,后来查了查,负数要单独处理;
3、然后想动mibeacon.py里的BLEByteConv,怎么改都不大行,干脆就复制了个BLEByteConv,改名BLENegativeConv,加上了处理负数。
class BLEByteConv(BaseConv):
    def decode(self, device: "XDevice", payload: dict, data: str):
        payload[self.attr] = int(data[:2], 16)  # uint8

class BLENegativeConv(BaseConv):
    def decode(self, device: "XDevice", payload: dict, data: str):
        value = int(data[:2], 16)
        if value > 0x7F: # 处理int8的16进制负数
            value = -(0xFF - value + 1)
        payload[self.attr] = value
电池电量就先不管他了,其他目前看下来还行,还在测试
屏幕截图 2024-06-18 233623.png


评分

参与人数 1金钱 +6 收起 理由
relliky + 6 大神666!

查看全部评分

回复

使用道具 举报

32

主题

1065

帖子

4942

积分

论坛元老

Rank: 8Rank: 8

积分
4942
金钱
3862
HASS币
90
发表于 2024-6-19 00:47:51 | 显示全部楼层
本帖最后由 relliky 于 2024-6-19 00:56 编辑
invincible 发表于 2024-6-18 23:38
大佬,目前歪打正着解决了按钮和旋转角度的问题。以下是当前的xiaomi_gateway3.py
1、抱着试试的心态,抄 ...

嗯嗯,这个值是sint8,我猜大概是在python里面要用把这个sint8用sign extension来转换成sint64,python应该才会正确显示。直接把sint8转换到sint64,会把sint8当成uint8来使用。

负数都是最高位是sign bit,所以表示的意义不同
我家全屋智能的HA设置 https://github.com/relliky/Tais_Home_Assistant_Config
回复

使用道具 举报

2

主题

96

帖子

737

积分

高级会员

Rank: 4

积分
737
金钱
641
HASS币
0
 楼主| 发表于 2024-6-19 07:45:15 | 显示全部楼层
relliky 发表于 2024-6-19 00:47
嗯嗯,这个值是sint8,我猜大概是在python里面要用把这个sint8用sign extension来转换成sint64,python应 ...

哈哈 吃了不懂技术的亏 都是一边gpt一边试
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-23 09:02 , Processed in 0.060332 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表